可延遲函數和工作隊列非常相似,但是他們的區別還是很大的。主要區別在於:可延遲函數運行在中斷上下文中,而工作隊列中的函數運行在進程上下文中。在中斷上下文中不可能發生進程切換。可延遲函數和工作隊列中的函數都不能訪問進程的用戶態地址空間。
涉及數據結構
- /*
- * The per-CPU workqueue (if single thread, we always use the first
- * possible cpu).
- */
- struct cpu_workqueue_struct {
-
- spinlock_t lock;/*保護該數據結構的自旋鎖*/
-
- struct list_head worklist;/*掛起鏈表的頭結點*/
- /*等待隊列,其中的工作者線程因等待跟多
- 的工作而處於睡眠狀態*/
- wait_queue_head_t more_work;
- /*等待隊列,其中的進程由於等待工作隊列
- 被刷新而處於睡眠狀態*/
- struct work_struct *current_work;
-
- struct workqueue_struct *wq;
- struct task_struct *thread;/*指向結構中工作者線程的進程描述符指針*/
- } ____cacheline_aligned;
-
- /*
- * The externally visible workqueue abstraction is an array of
- * per-CPU workqueues:
- */
- struct workqueue_struct {
- struct cpu_workqueue_struct *cpu_wq;
- struct list_head list;
- const char *name;
- int singlethread;
- int freezeable; /* Freeze threads during suspend */
- int rt;
- #ifdef CONFIG_LOCKDEP
- struct lockdep_map lockdep_map;
- #endif
- };
工作隊列操作
創建
最終都會調用如下函數執行
- struct workqueue_struct *__create_workqueue_key(const char *name,
- int singlethread,
- int freezeable,
- int rt,
- struct lock_class_key *key,
- const char *lock_name)
- {
- struct workqueue_struct *wq;
- struct cpu_workqueue_struct *cwq;
- int err = 0, cpu;
- /*分配wq結構*/
- wq = kzalloc(sizeof(*wq), GFP_KERNEL);
- if (!wq)
- return NULL;
- /*分配cwq結構*/
- wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
- if (!wq->cpu_wq) {
- kfree(wq);
- return NULL;
- }
-
- wq->name = name;
- lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
- wq->singlethread = singlethread;
- wq->freezeable = freezeable;
- wq->rt = rt;
- INIT_LIST_HEAD(&wq->list);
-
- if (singlethread) {/*如果設置了單線程,只創建一個*/
- /*初始化cwq*/
- cwq = init_cpu_workqueue(wq, singlethread_cpu);
- /*創建內核線程*/
- err = create_workqueue_thread(cwq, singlethread_cpu);
- /*喚醒剛創建的內核線程*/
- start_workqueue_thread(cwq, -1);
- } else {/*反之,每個cpu創建一個線程*/
- cpu_maps_update_begin();
- /*
- * We must place this wq on list even if the code below fails.
- * cpu_down(cpu) can remove cpu from cpu_populated_map before
- * destroy_workqueue() takes the lock, in that case we leak
- * cwq[cpu]->thread.
- */
- spin_lock(&workqueue_lock);
- list_add(&wq->list, &workqueues);
- spin_unlock(&workqueue_lock);
- /*
- * We must initialize cwqs for each possible cpu even if we
- * are going to call destroy_workqueue() finally. Otherwise
- * cpu_up() can hit the uninitialized cwq once we drop the
- * lock.
- */
- for_each_possible_cpu(cpu) {/*對每個cpu*/
- cwq = init_cpu_workqueue(wq, cpu);
- if (err || !cpu_online(cpu))
- continue;
- err = create_workqueue_thread(cwq, cpu);
- start_workqueue_thread(cwq, cpu);
- }
- cpu_maps_update_done();
- }
-
- if (err) {
- destroy_workqueue(wq);
- wq = NULL;
- }
- return wq;
- }
可見,工作隊列在創建時就喚醒創建的內核線程,下面我們看看他創建的內核線程
- static int worker_thread(void *__cwq)
- {
- struct cpu_workqueue_struct *cwq = __cwq;
- DEFINE_WAIT(wait);
-
- if (cwq->wq->freezeable)
- set_freezable();
-
- for (;;) {
- prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
- if (!freezing(current) &&
- !kthread_should_stop() &&
- list_empty(&cwq->worklist))
- schedule();
- finish_wait(&cwq->more_work, &wait);
-
- try_to_freeze();
-
- if (kthread_should_stop())
- break;
- /*執行工作隊列*/
- run_workqueue(cwq);
- }
-
- return 0;
- }