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

相关推荐
AC赳赳老秦1 分钟前
用 OpenClaw 整理学习笔记:自动提取视频课程内容、生成文字笔记、分类归档
大数据·运维·数据库·人工智能·学习·deepseek·openclaw
无足鸟ICT5 分钟前
【RHCA+】移动光标快捷键
linux·编辑器·vim
石小千10 分钟前
DELL安装PERCCLI工具(ESXI)
服务器
七夜zippoe11 分钟前
OpenClaw 节点通知:推送消息到设备
运维·服务器·网络·ai·openclaw·nodes
网络与设备以及操作系统学习使用者15 分钟前
三层交换机实现PC互通方案
运维·网络·学习·华为
Zhu75817 分钟前
Docker环境部署Apache Hadoop3.1定制版
运维·docker·容器
齐齐大魔王17 分钟前
Linux-UDP广播机制
linux·udp·智能路由器
小此方1 小时前
Re:Linux系统篇(二十九)文件篇·二:深度解析Linux文件描述符、dup2指针覆盖与内建命令重定向完全解析
linux·运维·驱动开发
wuminyu1 小时前
Java锁机制之park与futex系统级协同机制解析
java·linux·c语言·jvm·c++