[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);

相关推荐
移远通信33 分钟前
配网-复杂场景
服务器·开发语言·php
liweiweili1261 小时前
Linux 中查询指定目录下查找包含特定文本的文件
linux·运维·chrome
YFLICKERH1 小时前
【Linux系统】安装包 deb | rpm
linux·运维·安装包
CodeByV1 小时前
【Linux】线程从内核到实战:本质、控制逻辑与封装指南
linux
Xの哲學1 小时前
Linux设备管理:从内核驱动到用户空间的完整架构解析
linux·服务器·算法·架构·边缘计算
繁华似锦respect2 小时前
C++ unordered_map 底层实现与详细使用指南
linux·开发语言·c++·网络协议·设计模式·哈希算法·散列表
大聪明-PLUS2 小时前
在 C++ 中开发接口类
linux·嵌入式·arm·smarc
IT 乔峰2 小时前
linux部署DHCP服务端
linux·运维·网络
__beginner__2 小时前
docker安装influxdb
运维·docker·容器
IDC02_FEIYA2 小时前
服务器带宽怎么计算最大并发?服务器带宽计算公式
运维·服务器