libubox-runqueue

参考: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);
posted @ 2017-05-01 21:00  yuxi_o  阅读(1716)  评论(0编辑  收藏  举报