Linux 内核里面有一个函数wait_for_completion,这是一个内核同步机制的函数,同步机制如果是早期的读者应该看过我发的文章,如果没有看过的可以看看Linux 专辑文章里面找找。
既然是同步机制,主要的工作就是调用了这个函数,程序就会等另外的事件完成之后再继续下面的工作。
wait_for_completion 结构体
/* * struct completion - structure used to maintain state for a 'completion' * * This is the opaque structure used to maintain the state for a 'completion'. * Completions currently use a FIFO to queue threads that have to waitfor * the 'completion' event. * * See also: complete(), wait_for_completion() (and friends _timeout, * _interruptible, _interruptible_timeout, and _killable), init_completion(), * reinit_completion(), and macros DECLARE_COMPLETION(), * DECLARE_COMPLETION_ONSTACK(). */ struct completion { unsigned int done; wait_queue_head_t wait; };
static inline long __sched do_wait_for_common(struct completion *x, long (*action)(long), long timeout, int state) { /*如果done是0就跑到if里面去,也就说明我们等的事件还没完成*/ if (!x->done) { DECLARE_WAITQUEUE(wait, current);
__add_wait_queue_tail_exclusive(&x->wait, &wait); do { if (signal_pending_state(state, current)) { timeout = -ERESTARTSYS; break; } __set_current_state(state); spin_unlock_irq(&x->wait.lock); timeout = action(timeout);/*休眠timeout时间等待*/ spin_lock_irq(&x->wait.lock); } while (!x->done && timeout);/*如果事件完成了就提出*/ __remove_wait_queue(&x->wait, &wait); if (!x->done) return timeout; } x->done--; return timeout ?: 1; }
-- To unsubscribe from this list: send the line 'unsubscribe linux-kernel'in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger./majordomo-info.html Please read the FAQ at http://www./lkml/