- 论坛徽章:
- 0
|
内核中信号量(semaphore)中的__down函数,对这段函数感觉比较迷惑,有几个不太理解的地方:
1,for循环到底干什么啊? add_wait_queue_exclusive_locked 函数已经把当前的任务给suspend,并且插入了wait队列中了。
那么直接把sleepers设置为1,应该就可以了吧?
2,for循环中的schedule() 函数,会不会引起进程切换啊?也就是说,当调用schedule() 函数后,其后的代码要等到重新调度后,才会被执行?
- __attribute__((regparm(3))) void _ _down(struct semaphore * sem)
- {
- DECLARE_WAITQUEUE(wait, current);
- unsigned long flags;
- current->state = TASK_UNINTERRUPTIBLE;
- spin_lock_irqsave(&sem->wait.lock, flags);
- add_wait_queue_exclusive_locked(&sem->wait, &wait);
- sem->sleepers++;
- for (;;) {
- if (!atomic_add_negative(sem->sleepers-1, &sem->count)) {
- sem->sleepers = 0;
- break;
- }
- sem->sleepers = 1;
- spin_unlock_irqrestore(&sem->wait.lock, flags);
- schedule( );
- spin_lock_irqsave(&sem->wait.lock, flags);
- current->state = TASK_UNINTERRUPTIBLE;
- }
- remove_wait_queue_locked(&sem->wait, &wait);
- wake_up_locked(&sem->wait);
- spin_unlock_irqrestore(&sem->wait.lock, flags);
- current->state = TASK_RUNNING;
- }
复制代码 |
|