linux kernel中的wait_for_completion和complete总结
生活随笔
收集整理的這篇文章主要介紹了
linux kernel中的wait_for_completion和complete总结
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
快速鏈接:
.
👉👉👉 個人博客筆記導(dǎo)讀目錄(全部) 👈👈👈
文章目錄
- 結(jié)構(gòu)體
- init_completion
- complete
- wait_for_completion
- wait_for_completion的相關(guān)API
- 在wait_for_completion時,先將x->wait加入到等待隊(duì)列,在檢查done的值,如果為0 ,則死循環(huán)卡住。推出循環(huán)后,則done–;
- 在complete時,先done++,在wake_up(x->wait)等待隊(duì)列
注意done–的最小值和done++的最大值都是(~0U)
結(jié)構(gòu)體
struct completion {unsigned int done;wait_queue_head_t wait; };#define UINT_MAX (~0U)init_completion
#define init_completion(x) __init_completion(x)static inline void __init_completion(struct completion *x) {x->done = 0;init_waitqueue_head(&x->wait); }complete
void complete(struct completion *x) {unsigned long flags;spin_lock_irqsave(&x->wait.lock, flags);if (x->done != UINT_MAX)x->done++;__wake_up_locked(&x->wait, TASK_NORMAL, 1);spin_unlock_irqrestore(&x->wait.lock, flags); } EXPORT_SYMBOL(complete);wait_for_completion
wait_for_completion --> wait_for_common --> do_wait_for_common
void __sched wait_for_completion(struct completion *x) {wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE); } EXPORT_SYMBOL(wait_for_completion);static long __sched wait_for_common(struct completion *x, long timeout, int state) {return __wait_for_common(x, schedule_timeout, timeout, state); }static inline long __sched __wait_for_common(struct completion *x,long (*action)(long), long timeout, int state) {might_sleep();complete_acquire(x);spin_lock_irq(&x->wait.lock);timeout = do_wait_for_common(x, action, timeout, state);spin_unlock_irq(&x->wait.lock);complete_release(x);return timeout; }static inline long __sched do_wait_for_common(struct completion *x,long (*action)(long), long timeout, int state) {if (!x->done) {DECLARE_WAITQUEUE(wait, current);__add_wait_queue_entry_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);spin_lock_irq(&x->wait.lock);} while (!x->done && timeout);__remove_wait_queue(&x->wait, &wait);if (!x->done)return timeout;}if (x->done != UINT_MAX)x->done--;return timeout ?: 1; }wait_for_completion的相關(guān)API
wait_for_completion
wait_for_completion_timeout
wait_for_completion_interruptible
wait_for_completion_killable
wait_for_completion_killable_timeout
wait_for_completion_io
wait_for_common_io
TASK_UNINTERRUPTIBLE
TASK_INTERRUPTIBLE
TASK_KILLABLE
wait_for_common和wait_for_common_io的區(qū)別,區(qū)別在于schedule_timeout和io_schedule_timeout變量,這兩個變量是指向函數(shù)的指針
static long __sched wait_for_common(struct completion *x, long timeout, int state) {return __wait_for_common(x, schedule_timeout, timeout, state); }static long __sched wait_for_common_io(struct completion *x, long timeout, int state) {return __wait_for_common(x, io_schedule_timeout, timeout, state); }下方的action(timeout),其實(shí)就是在調(diào)用schedule_timeout(xxx)或io_schedule_timeout(xxx)
static inline long __sched do_wait_for_common(struct completion *x,long (*action)(long), long timeout, int state) {if (!x->done) {DECLARE_WAITQUEUE(wait, current);__add_wait_queue_entry_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);spin_lock_irq(&x->wait.lock);} while (!x->done && timeout);__remove_wait_queue(&x->wait, &wait);if (!x->done)return timeout;}if (x->done != UINT_MAX)x->done--;return timeout ?: 1; }總結(jié)
以上是生活随笔為你收集整理的linux kernel中的wait_for_completion和complete总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [ATF]-ATF makefile的导
- 下一篇: [答疑]-中断流程举例:在TEE侧时产生