down_interruptible()

来源:互联网 发布:信捷plc编程教学视频 编辑:程序博客网 时间:2024/06/07 06:05

最近研究了一下linux驱动,发现有很多同学对down_interruptible() 函数不是很理解。

 

现在就2.6.38.8 内核

 

down_interruptible()是处理信号量的函数。他的返回值有三种 1. “0” 2. “-ETIME”3.“-EINTR”

 

0 代表正常返回

 

-ETIME 等待超时

 

-EINTR 中断

 

 

 

函数的运作方式:

 

如果sem->count >0 (信号量允许访问)         注,关于信号量的详细原理参见操作系统进程同步相关的内容

 

返回0 (正常返回)

 

否则进行等待。

 

内核源码为

 

int down_interruptible(struct semaphore*sem)

 {

 unsigned long flags;

  intresult = 0;

 

 spin_lock_irqsave(&sem->lock, flags);//信号量自旋锁

  if(likely(sem->count > 0))

  sem->count--;

 else

  result = __down_interruptible(sem);//进入等待

 spin_unlock_irqrestore(&sem->lock, flags);

 

 return result;

 }

 

__down_interruptible()

 

调用__down_common()

 

__down_common()中有for循环

 

当有中断信号时返回 -EINTR ,当等待超时时返回 -ETIME.

 

信号量变为可访问状态时返回0

 

顺便提一下,信号的信号量是不同的,需要区分。

 

原创粉丝点击