参考: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. centos7 keepalived以及防火墙配置

    安装和配置keepalived,网上有很多资料,但是都没有提到防火墙这块,而且很多都是互相抄袭的,就算配置对了也会遇到很多问题 在查阅资料的时候配置好了keepalived,但是出现了裂脑,即两台服务 ...

  2. HTTP代理神器Fiddler

    HTTP代理神器Fiddler Fiddler是一款强大Web调试工具,它能记录所有客户端和服务器的HTTP请求. Fiddler启动的时候,默认IE的代理设为了127.0.0.1:8888,而其他浏 ...

  3. vmware虚拟机 C硬盘空间 无损扩容 新测

    摘自: http://hi.baidu.com/y276827893/item/78a351f427726549932af214 其实上面一步的话, 虚拟机设置 里选择磁盘,实用工具里也有这个功能的. ...

  4. Java反转单链表

    class Node { private int data; private Node nextNode; public Node(int data) { this.data = data; } pu ...

  5. Kudu – 在快数据上的进行快分析的存储

    转自: http://www.tuicool.com/articles/nmYf2uf Cloudera Impala Kudu – 在快数据上的进行快分析的存储     Kudu,对应中文的含义应该 ...

  6. Android 如何添加一个新的时区

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  7. Remove all your local git branches but keep master

    Sometimes after a sprint, all the remaining branches are just taking up space. Here's a small snippe ...

  8. Unity AssetServer小笔记

    普及一下Asset Server知识: Unity的Asset Server,其实就是一个纯PostgreSQL数据库.Unity操作Asset Server,就是一个数据库客户端操作,但是Unity ...

  9. CosmosEngine - Unity3D /2D 轻量级游戏开发框架

    CosmosEngine https://github.com/mr-kelly/CosmosEngine 快速入门 简介 特性 约定 整体架构图 使用经验 工作流 未来功能 快速入门 1.将NGUI ...

  10. 【java】对数据库操作的那些事(包含数据库中的预处理)

    一.连接问题 前面刚介绍了怎么连接数据库,也写了对应的模板.可是它的可维护性很差.那么怎么解决问题呢? 首先写一个配置文件jdbc.properties <span style="fo ...