1. 程式人生 > >中斷下半部_工作佇列(work queue)

中斷下半部_工作佇列(work queue)

1>work_queue:<linux/workqueue.h> __3.0.4
2>description:
中斷下半部,在核心執行緒的程序上下文中執行推後的工作.
它是唯一能在程序上下文執行的中斷下半部實現機制,也只有它才可以睡眠.
3>建立推後的工作:
DECLARE_WORK(const char *name, void (*func)(struct work_struct *work));
INIT_WORK(struct work_struct *work, void (*func)(struct work_struct *work));

struct work_struct {
    atomic_long_t data;
    struct list_head entry;
    work_func_t func;                                                                                                                     
#ifdef CONFIG_LOCKDEP
    struct lockdep_map lockdep_map;
#endif
};
typedef void (*work_func_t)(struct work_struct *work);
4>工作佇列處理函式:
/* 除了不能訪問使用者空間,它的確比軟中斷和tasklet方便不少.
 * 只有系統呼叫陷入核心,使用者空間的記憶體才會被對映.
 */
void *func(struct work_struct *work);
5>排程(提交)工作:
/* 把建立好的工作提交到預設的worker執行緒(每個cpu一個,event/cpuno.).
 * 有時把這種工作佇列也叫共享佇列,因為很多驅動程式都將推後執行的工作提交到預設worker執行緒.
 */
int schedule_work(struct work_struct *work);
int schedule_delayed_work(struct delayed_work *work, unsigned long delay);
int schedule_on_each_cpu(work_func_t func);
6>重新整理工作佇列:
/* 實際上,它只是等待(睡眠),直到預設工作佇列上的工作被執行.
 * complete()實現.
 */
void flush_scheduled_work(void);
/*等待指定的工作被執行*/
bool flush_work(struct work_struct *work);
bool flush_delayed_work(struct delayed_work *dwork);
7>取消工作:
bool cancel_delayed_work(struct delayed_work *work);
bool cancel_work_sync(struct work_struct *work);
bool cancel_delayed_work_sync(struct delayed_work *dwork);

8>建立一個新的workqueue和對應的worker執行緒(每個cpu一個).

/* 如果一個任務是處理器密集型和效能要求嚴格的
 * 那麼可以選擇建立自己的worker(工作者)執行緒
 * 當然,每個處理器一個.
 */
struct workqueue_struct *create_workqueue(const char *name);

/* struct workqueue_struct *alloc_workqueue(const char *name, unsigned int flags, int max_active);
 * the maximum number of execution contexts per cpu, up to 512.
 */
9>排程(提交)佇列
int queue_work(struct workqueue_struct *wq, struct work_struct *work);
int queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *work, unsigned long delay);
10>重新整理一個工作佇列:
/* 等待(睡眠),直到指定佇列上的工作被執行.
 * 事實上除了指定工作佇列,它和flush_scheduled_work()沒有區別.
 */
void flush_workqueue(struct workqueue_struct *wq);
11>釋放一個工作佇列:
void destroy_workqueue(struct workqueue_struct *wq);