摘至:http://www.makelinux.net/books/lkd2/ch07lev1sec4
Let's look at the heart of worker_thread(), simplified:
for (;;) { set_task_state(current, TASK_INTERRUPTIBLE); add_wait_queue(&cwq->more_work, &wait); if (list_empty(&cwq->worklist)) schedule(); else set_task_state(current, TASK_RUNNING); remove_wait_queue(&cwq->more_work, &wait); if (!list_empty(&cwq->worklist)) run_workqueue(cwq); }
This function performs the following functions, in an infinite loop:
-
The thread marks itself sleeping (the task's state is set to TASK_INTERRUPTIBLE) and adds itself to a wait queue.
-
If the linked list of work is empty, the thread calls schedule() and goes to sleep.
-
If the list is not empty, the thread does not go to sleep. Instead, it marks itself TASK_RUNNING and removes itself from the wait queue.
-
If the list is nonempty, the thread calls run_workqueue() to perform the deferred work.