[linux kernel]semaphore信号量的用法

struct semaphore {

raw_spinlock_t lock; --->锁

unsigned int count;--->信号量计数

struct list_head wait_list;--->信号量等待链表

};

struct semaphore_waiter {

struct list_head list;

struct task_struct *task;

bool up;

};

两个关键函数:

/**

* up - release the semaphore

* @sem: the semaphore to release

*

* Release the semaphore. Unlike mutexes, up() may be called from any

* context and even by tasks which have never called down().

*/

void up(struct semaphore *sem)

{

unsigned long flags;

raw_spin_lock_irqsave(&sem->lock, flags);

if (likely(list_empty(&sem->wait_list)))///< 如果链表为空,表示没有人等待信号量,直接++

sem->count++;///< 信号量增加

else///< 否则

__up(sem);

raw_spin_unlock_irqrestore(&sem->lock, flags);

}

EXPORT_SYMBOL(up);

/**

* down - acquire the semaphore

* @sem: the semaphore to be acquired

*

* Acquires the semaphore. If no more tasks are allowed to acquire the

* semaphore, calling this function will put the task to sleep until the

* semaphore is released.

*

* Use of this function is deprecated, please use down_interruptible() or

* down_killable() instead.

*/

void down(struct semaphore *sem)

{

unsigned long flags;

raw_spin_lock_irqsave(&sem->lock, flags);

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

sem->count--;

else///< 小于等于0

__down(sem);

raw_spin_unlock_irqrestore(&sem->lock, flags);

}

EXPORT_SYMBOL(down);

相关推荐
电商API_180079052477 小时前
京东商品详情API技术文章
大数据·运维·人工智能·网络爬虫
三言老师8 小时前
秘诀-如何远程SSN访问家里的八台电脑(frp 实操方案)
linux·运维·ssh
躺不平的理查德9 小时前
ARCH与交叉编译器的关系
服务器·arm
举手10 小时前
Dispatcher模块剖析
linux·c++
BTU_YC10 小时前
Docker Compose 部署 DozerDB 完整教程
运维·docker·容器
谢慧琼11 小时前
2026零基础做企业网站,低成本搭建官方网站
服务器·前端·javascript
allforgood11 小时前
运行容器
linux
Light_It12 小时前
Linux 内核参数 pci-stub.ids=
linux·kernel
RisunJan12 小时前
Linux命令-scriptreplay(终端会话回放)
linux·运维·chrome
spencer_tseng13 小时前
[kylin & linux] install docker
linux·docker·kylin