標籤:
http://blog.sina.com.cn/s/blog_4770ef020101h48l.html
功能:喚醒註冊到等待隊列上的進程
原型: #include
void wake_up_interruptible (wait_queue_head_t *q);
說明:
喚醒 q指定的註冊在等待隊列上的進程。該函數不能直接的立即喚醒進程,而是由發送器轉換上下文,調整為可運行狀態。
變數:
q : 等待隊列變數指標。
最近在學習驅動時有一個問題始終想不明白,為什麼wait_event_interruptible(button_waitq,ev_press)需要一個全域變數來記住中斷的發生?
在驅動中實現讀取操作時,使用了
- wait_event_interruptible(button_waitq, ev_press);
在中斷函數中使用了如下語句喚醒:
- ev_press =1; //表示中斷髮生了
- wake_up_interruptible(&button_waitq); //喚醒休眠的進程
這樣的話,中斷能正確讀取到。我分別嘗試了屏蔽ev_press =1;和wake_up_interruptible(&button_waitq);代碼,發現中斷不能正常產生。
尋找資料,閱讀原始碼。
- #define wait_event_interruptible(wq,condition) \
- ({ \
- int__ret =0; \
- if(!(condition)) \
- __wait_event_interruptible(wq,condition, __ret);\
- __ret; \
- })
- #define __wait_event_interruptible(wq, condition,ret) \
- do{ \
- DEFINE_WAIT(__wait); \
- \
- for(;;){ \
- prepare_to_wait(&wq,&__wait,TASK_INTERRUPTIBLE); \
- if(condition) \
- break; \
- if(!signal_pending(current)){ \
- schedule(); \
- continue; \
- } \
- ret=-ERESTARTSYS; \
- break; \
- } \
- finish_wait(&wq,&__wait); \
- } while (0)
__wait_event_interruptible()首先定義並初始化一個wait_queue_t變數__wait,其中資料為當前進程current,並把__wait入隊。
在無限迴圈中,__wait_event_interruptible()將本進程置為可中斷的掛起狀態,反覆檢查condition是否成立,如果成立則退出,如果不成立則繼續休眠;條件滿足後,即把本進程運行狀態置為運行態(此時如果不執行下面的函數wake_up_interruptible,上面wait_event_interruptible還會繼續休眠),並將__wait從等待隊列中清除掉,從而進程能夠調度運行。如果進程當前有非同步訊號(POSIX的),則返回-ERESTARTSYS。
- //喚醒 q指定的註冊在等待隊列上的進程。該函數不能直接的立即喚醒進程,而是由發送器轉換上下文,調整為可運行狀態。
Linux使用wake_up_interruptible()喚醒註冊到等待隊列上的進程