参考:libubox [4] - uloop runqueue ustream

任务队列是通过uloop定时器实现,把定时器超时时间设置为1,通过uloop事件循环来处理定时器就会处理任务队列中的task。进程任务在任务队列基本上实现,加入子进程退出监控。

procd采用此机制。

1. 数据结构

struct runqueue {
struct safe_list tasks_active; /** 活动任务队列 */
struct safe_list tasks_inactive; /** 不活动任务队列 */
struct uloop_timeout timeout; int running_tasks; /** 当前活动任务数目 */
int max_running_tasks; /** 允许最大活动任务数目 */
bool stopped; /** 是否停止任务队列 */
bool empty; /** 任务队列(包括活动和不活动)是否为空 */ /* called when the runqueue is emptied */
void (*empty_cb)(struct runqueue *q);
}; struct runqueue_task_type {
const char *name; /*
* called when a task is requested to run
*
* The task is removed from the list before this callback is run. It
* can re-arm itself using runqueue_task_add.
*/
void (*run)(struct runqueue *q, struct runqueue_task *t); /*
* called to request cancelling a task
*
* int type is used as an optional hint for the method to be used when
* cancelling the task, e.g. a signal number for processes. Calls
* runqueue_task_complete when done.
*/
void (*cancel)(struct runqueue *q, struct runqueue_task *t, int type); /*
* called to kill a task. must not make any calls to runqueue_task_complete,
* it has already been removed from the list.
*/
void (*kill)(struct runqueue *q, struct runqueue_task *t);
}; struct runqueue_task {
struct safe_list list;
const struct runqueue_task_type *type;
struct runqueue *q; void (*complete)(struct runqueue *q, struct runqueue_task *t); struct uloop_timeout timeout;
int run_timeout; /** >0表示规定此任务执行只有run_timeout毫秒 */
int cancel_timeout; /** >0表示规则任务延取消操作执行只有run_timeout毫秒*/
int cancel_type; bool queued; /** 此任务是否已加入任务队列中 */
bool running; /** 此任务是否活动,即已在活动队列中 */
bool cancelled; /** 此任务是否已被取消 */
}; struct runqueue_process {
struct runqueue_task task;
struct uloop_process proc;
};

2. 函数

任务队列

/**
* 初始化任务队列
*/
void runqueue_init(struct runqueue *q) /**
* 取消所有任务队列
*/
void runqueue_cancel(struct runqueue *q); /**
* 取消活动中的任务
*/
void runqueue_cancel_active(struct runqueue *q); /**
* 取消不活动的任务
*/
void runqueue_cancel_pending(struct runqueue *q); /**
* 杀死所有任务
*/
void runqueue_kill(struct runqueue *q); /**
* 停止所有任务
*/
void runqueue_stop(struct runqueue *q); /**
* 重新开始任务
*/
void runqueue_resume(struct runqueue *q);

任务操作

/**
* 添加新任务到队列尾
*
* @running true-加入活动队列;false-加入不活动队列
*/
void runqueue_task_add(struct runqueue *q, struct runqueue_task *t, bool running); /**
* 添加新任务到队列头
*
* @running true-加入活动队列;false-加入不活动队列
*/
void runqueue_task_add_first(struct runqueue *q, struct runqueue_task *t,
bool running); /**
* 完全任务
*/
void runqueue_task_complete(struct runqueue_task *t); /**
* 取消任务
*/
void runqueue_task_cancel(struct runqueue_task *t, int type); /**
* 杀死任务
*/
void runqueue_task_kill(struct runqueue_task *t);

进程任务

void runqueue_process_add(struct runqueue *q, struct runqueue_process *p,
pid_t pid); /**
* to be used only from runqueue_process callbacks
*/
void runqueue_process_cancel_cb(struct runqueue *q, struct runqueue_task *t,
int type);
void runqueue_process_kill_cb(struct runqueue *q, struct runqueue_task *t);

libubox-runqueue的更多相关文章

  1. libubox

    lbubox是openwrt的一个核心库,封装了一系列基础实用功能,主要提供事件循环,二进制格式处理,linux链表实现和一些JSON辅助处理. 它的目的是以动态链接库方式来提供可重用的通用功能,给其 ...

  2. openWrt libubox组件之uloop原理分析

    1.    libubox概述 libubox是openwrt新版本中的一个基础库,有很多应用是基于libubox开发的,如uhttpd,netifd,ubusd等. libubox主要提供以下两种功 ...

  3. libubox组件(3)——uloop

    一:uloop概述 uloop有三个功能: 文件描述符触发事件的监控,  timeout定时器处理, 当前进程的子进程的维护 二: uloop的整体框架 1: /** 2: * 初始化事件循环 3: ...

  4. libubox组件(1)——usock

    一:相关API介绍 1.相关源码文件:usocket.h usocket.c 2.类型标志 1: #define USOCK_TCP 0 2: #define USOCK_UDP 1 3: #defi ...

  5. libubox组件(2)——blob/blobmsg (转载 https://segmentfault.com/a/1190000002391970)

    一:blob相关接口 1.数据结构 1: struct blob_attr { 2: uint32_t id_len; /** 高1位为extend标志,高7位存储id, 3: * 低24位存储dat ...

  6. tr069开源协议EasyCwmp移植

    1.平台MT7628 2.交叉编译器及版本信息mipsel-linux + buildroot-gcc463_32bits.tar.bz2 3.创建工作目录lancer@ubuntu:~$ mkdir ...

  7. openwrt procd启动流程和脚本分析

    Linux内核执行start_kernel函数时会调用kernel_init来启动init进程,流程如下图: graph LR A[start_kernel] -->B(rest_init) B ...

  8. libubox-ustream

    参考:libubox [4] - uloop runqueue ustream libubox提供了流缓冲管理,定义在文件ustream.h,ustream.c和ustream-fd.c. 1. 数据 ...

  9. openwrt procd 运行的一些log

    void procd_inittab(void) { #define LINE_LEN 128 FILE *fp = fopen(tab, "r"); struct init_ac ...

随机推荐

  1. springMVC配置静态资源访问的<mvc:resources>标签的使用

    在springmvc中,为了引用资源的访问不会类似Controller一样被拦截,区分出关注的资源的访问,一般我们在springMVC里面的拦截都会配置为"/",拦截所有的.但是这 ...

  2. 阿里云64位centos6.3系统上编译安装redis

    环境 系统:阿里云64位centos 6.3 [rao@AY~]$ cat /etc/issue CentOS release 6.3 (Final) Kernel \r on an \m [rao@ ...

  3. RocketMQ里的一个获取时间的工具类SystemClock

    看rocketmq源码的时候发现他们还给时钟封装里一下. /** * Licensed to the Apache Software Foundation (ASF) under one or mor ...

  4. 转: ios的关于autolayout的设计与实现

    http://www.taijicoder.com/2015/12/12/iOS-Layout-and-Masnory/

  5. android 步骤控件的使用

    有的时候我们做Android开发会用到表示步骤的需求.这时候github给我们提供了一个非常好地表示步骤的组件,使用她仅仅须要4步就能够完毕了. 项目地址https://github.com/anto ...

  6. Win7文件正在使用无法删除怎么办

    Windows 7下删除文件提示被占用?系统内置工具搞定 Windows7之家(www.win7china.com):Windows7下删除文件提示被占用?系统内置工具搞定 在Windows中,删除文 ...

  7. user-defined conversion

    http://en.cppreference.com/w/cpp/language/cast_operator

  8. Google 地图 API for Android

    原文:Introduction to Google Maps API for Android 作者:Eunice Obugyei 译者:kmyhy 从健康类 app Runkeeper 到游戏 app ...

  9. j2ee高并发时使用全局变量需要注意的问题

    原文:https://blog.csdn.net/jston_learn/article/details/21617311 开发中,全局变量的使用很频繁,但对于多线程的访问,使用全局变量需要注意的地方 ...

  10. nginx location静态文件配置

    进入nginx安装目录的conf目录下,修改nginx.conf文件,在一个server{}中添加 一个location 部分配置代码如下 root@ubuntu:/usr/local/nginx/c ...