http://blog.csdn.net/yyttiao/article/details/7875871
上面講到添加和等待。這次主要講如何喚醒
#define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
#define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
#define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
#define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL)
#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
#define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
#define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
#define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
喚醒主要爲以上這些函數,其實都差不多,今天我們主要分析wake_up_interruptible 這個函數,因爲上一章節中對應。
這一章主要看流程及代碼註釋。講解會比較少
#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
/**
* __wake_up - wake up threads blocked on a waitqueue.
* @q: the waitqueue
* @mode: which threads
* @nr_exclusive: how many wake-one or wake-many threads to wake up
* @key: is directly passed to the wakeup function
*
* It may be assumed that this function implies a write memory barrier before
* changing the task state if and only if any tasks are woken up.
*/
void __wake_up(wait_queue_head_t *q, unsigned int mode,
int nr_exclusive, void *key)
{
unsigned long flags;
/* 鎖定wait_queue_head_t 的操作,並關閉中斷,保存中斷狀態 */
spin_lock_irqsave(&q->lock, flags);
__wake_up_common(q, mode, nr_exclusive, 0, key);
spin_unlock_irqrestore(&q->lock, flags);
}
/*
* The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
* wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
* number) then we wake all the non-exclusive tasks and one exclusive task.
*
* There are circumstances in which we can try to wake a task which has already
* started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
* zero in this (rare) case, and we handle it by continuing to scan the queue.
*/
static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
int nr_exclusive, int wake_flags, void *key)
{
wait_queue_t *curr, *next;
list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
unsigned flags = curr->flags;
/* 調用默認的default_wake_function函數接口,在初始化的時候指定 */
if (curr->func(curr, mode, wake_flags, key) &&
(flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
break;
}
}
int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags,
void *key)
{
return try_to_wake_up(curr->private, mode, wake_flags);
}
/**
* try_to_wake_up - wake up a thread
* @p: the thread to be awakened
* @state: the mask of task states that can be woken
* @wake_flags: wake modifier flags (WF_*)
*
* Put it on the run-queue if it's not already there. The "current"
* thread is always on the run-queue (except when the actual
* re-schedule is in progress), and as such you're allowed to do
* the simpler "current->state = TASK_RUNNING" to mark yourself
* runnable without the overhead of this.
*
* Returns %true if @p was woken up, %false if it was already running
* or @state didn't match @p's state.
*/
static int try_to_wake_up(struct task_struct *p, unsigned int state,
int wake_flags)
{
int cpu, orig_cpu, this_cpu, success = 0;
unsigned long flags;
unsigned long en_flags = ENQUEUE_WAKEUP;
struct rq *rq;
/* 關閉內核搶佔,獲得本地cpu編號 */
this_cpu = get_cpu();
/* 設置內存寫屏障 */
smp_wmb();
/* 獲取最後執行該進程的run_queue and lock it */
rq = task_rq_lock(p, &flags);
/* 狀態不一致,則直接退出 */
if (!(p->state & state))
goto out;
if (p->se.on_rq)
goto out_running;
/* 獲取最後執行該任務的CPU */
cpu = task_cpu(p);
/* save origin cpu */
orig_cpu = cpu;
/* support smp 在很多架構上還不支持smp可以忽略此處
* 判斷是否要將任務轉移到另外一個CPU的執行隊列上,消耗平衡
* 此處大部分代碼等以後深入詳解的時候探討。此次主要爲淺談
*/
#ifdef CONFIG_SMP
if (unlikely(task_running(rq, p)))
goto out_activate;
/*
* In order to handle concurrent wakeups and release the rq->lock
* we put the task in TASK_WAKING state.
*
* First fix up the nr_uninterruptible count:
*/
if (task_contributes_to_load(p)) {
if (likely(cpu_online(orig_cpu)))
rq->nr_uninterruptible--;
else
this_rq()->nr_uninterruptible--;
}
p->state = TASK_WAKING;
if (p->sched_class->task_waking) {
p->sched_class->task_waking(rq, p);
en_flags |= ENQUEUE_WAKING;
}
cpu = select_task_rq(rq, p, SD_BALANCE_WAKE, wake_flags);
if (cpu != orig_cpu)
set_task_cpu(p, cpu);
__task_rq_unlock(rq);
rq = cpu_rq(cpu);
raw_spin_lock(&rq->lock);
/*
* We migrated the task without holding either rq->lock, however
* since the task is not on the task list itself, nobody else
* will try and migrate the task, hence the rq should match the
* cpu we just moved it to.
*/
WARN_ON(task_cpu(p) != cpu);
WARN_ON(p->state != TASK_WAKING);
#ifdef CONFIG_SCHEDSTATS
schedstat_inc(rq, ttwu_count);
if (cpu == this_cpu)
schedstat_inc(rq, ttwu_local);
else {
struct sched_domain *sd;
for_each_domain(this_cpu, sd) {
if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
schedstat_inc(sd, ttwu_wake_remote);
break;
}
}
}
#endif /* CONFIG_SCHEDSTATS */
out_activate:
#endif /* CONFIG_SMP */
/* 將進程P送入目標運行隊列rq
* 內部調用activate_task 將任務q加入到rq裏
*/
ttwu_activate(p, rq, wake_flags & WF_SYNC, orig_cpu != cpu,
cpu == this_cpu, en_flags);
success = 1;
out_running:
/* 將任務狀態重新設置回TASK_RUNING,以便cpu可以重新調度該任務
下面的函數中有這麼一句話 p->state = TASK_RUNNING;
最終任務的狀態又回來了,這便喚醒了該任務
*/
ttwu_post_activation(p, rq, wake_flags, success);
out:
task_rq_unlock(rq, &flags);
put_cpu();
return success;
}
總結:對於等待隊列,其實最主要的就是對任務狀態的切換,使其是否能被schedule拉取被執行。如何通過設置讓它滿足這些條件,就是等待隊列機制的原理。
在wait_event中將任務設置爲非TASK_RUNING而在wake_up中將任務設置回TASK_RUNING
其實很多時候wait_event並不會直接去調用,而是會像我在第一篇文章中第一段代碼那樣去判斷條件,因爲有時候要考慮是否阻塞,在非阻塞方式下,就不需要
wait_event了。當然也有別的方法來完成,因人而異。。
等以後講解玩schedule之後,再詳細分析smp的情況是如何執行的,以及對消耗平衡的處理方式等謝謝
http://blog.csdn.net/yyttiao/article/details/7875871