Linux使用wake_up_interruptible()喚醒註冊到等待隊列上的進程

來源:互聯網
上載者:User

標籤:

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)需要一個全域變數來記住中斷的發生?
在驅動中實現讀取操作時,使用了
  1. wait_event_interruptible(button_waitq, ev_press);
在中斷函數中使用了如下語句喚醒:
  1. ev_press =1;  //表示中斷髮生了
  2. wake_up_interruptible(&button_waitq);  //喚醒休眠的進程
這樣的話,中斷能正確讀取到。我分別嘗試了屏蔽ev_press =1;和wake_up_interruptible(&button_waitq);代碼,發現中斷不能正常產生。

尋找資料,閱讀原始碼。
  1. #define wait_event_interruptible(wq,condition)          \
  2. ({                                                      \
  3.     int__ret =0;                                      \
  4.     if(!(condition))                                    \
  5.         __wait_event_interruptible(wq,condition, __ret);\
  6.     __ret;                                              \
  7. })
  1. #define __wait_event_interruptible(wq, condition,ret)                        \
  2. do{                                                                        \
  3.         DEFINE_WAIT(__wait);                                                \
  4.                                                                         \
  5.         for(;;){                                                        \
  6.                 prepare_to_wait(&wq,&__wait,TASK_INTERRUPTIBLE);        \
  7.                 if(condition)                                                \
  8.                         break;                                                \
  9.                 if(!signal_pending(current)){                                \
  10.                         schedule();                                        \
  11.                         continue;                                        \
  12.                 }                                                        \
  13.                 ret=-ERESTARTSYS;                                        \
  14.                 break;                                                        \
  15.         }                                                                \
  16.         finish_wait(&wq,&__wait);                                        \
  17. } while (0)
__wait_event_interruptible()首先定義並初始化一個wait_queue_t變數__wait,其中資料為當前進程current,並把__wait入隊。
   在無限迴圈中,__wait_event_interruptible()將本進程置為可中斷的掛起狀態,反覆檢查condition是否成立,如果成立則退出,如果不成立則繼續休眠;條件滿足後,即把本進程運行狀態置為運行態(此時如果不執行下面的函數wake_up_interruptible,上面wait_event_interruptible還會繼續休眠),並將__wait從等待隊列中清除掉,從而進程能夠調度運行。如果進程當前有非同步訊號(POSIX的),則返回-ERESTARTSYS。
  1. //喚醒 q指定的註冊在等待隊列上的進程。該函數不能直接的立即喚醒進程,而是由發送器轉換上下文,調整為可運行狀態。

Linux使用wake_up_interruptible()喚醒註冊到等待隊列上的進程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.