Linux源码分析:completion的解读


  1. /** 
  2.  * wait_for_completion: - waits for completion of a task 
  3.  * @x:  holds the state of this particular completion 
  4.  * 
  5.  * This waits to be signaled for completion of a specific task. It is NOT 
  6.  * interruptible and there is no timeout. 
  7.  * 
  8.  * See also similar routines (i.e. wait_for_completion_timeout()) with timeout 
  9.  * and interrupt capability. Also see complete(). 
  10.  */  
  11. void __sched wait_for_completion(struct completion *x)  
  12. {  
  13.     wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);  
  14. }  
[cpp]
  1. static long __sched  
  2. wait_for_common(struct completion *x, long timeout, int state)  
  3. {  
  4.     might_sleep();  
  5.   
  6.     spin_lock_irq(&x->wait.lock);  
  7.     timeout = do_wait_for_common(x, timeout, state);  
  8.     spin_unlock_irq(&x->wait.lock);  
  9.     return timeout;  
  10. }  

注意:spin_lock_irq(&x->wait.lock)和spin_unlock_irq(&x->wait.lock)并非真正对应的一对自旋锁,因为在自旋锁保护中是不允许休眠和调度的。与他们相对应的解锁和上锁操作在do_wait_for_common(x, timeout, state)函数内部。

[cpp]
  1. static inline long __sched  
  2. do_wait_for_common(struct completion *x, long timeout, int state)  
  3. {  
  4.     if (!x->done) {  
  5.         DECLARE_WAITQUEUE(wait, current);  
  6.   
  7.         wait.flags |= WQ_FLAG_EXCLUSIVE;  
  8.         __add_wait_queue_tail(&x->wait, &wait);  
  9.         do {  
  10.             if (signal_pending_state(state, current)) {  
  11.                 timeout = -ERESTARTSYS;  
  12.                 break;  
  13.             }  
  14.             __set_current_state(state);  
  15.             spin_unlock_irq(&x->wait.lock);  
  16.             timeout = schedule_timeout(timeout);  
  17.             spin_lock_irq(&x->wait.lock);  
  18.         } while (!x->done && timeout);  
  19.         __remove_wait_queue(&x->wait, &wait);  
  20.         if (!x->done)  
  21.             return timeout;  
  22.     }  
  23.     x->done--;  
  24.     return timeout ?: 1;  
  25. }  
  • 1
  • 2
  • 下一页

相关内容