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

相关推荐
知星小度S9 分钟前
系统核心解析:深入文件系统底层机制——Ext系列探秘:从磁盘结构到挂载链接的全链路解析
linux
2401_8904430214 分钟前
Linux 基础IO
linux·c语言
字节数据平台1 小时前
刚刚,火山引擎多模态数据湖解决方案发布大数据运维Agent
大数据·运维·火山引擎
智慧地球(AI·Earth)1 小时前
在Linux上使用Claude Code 并使用本地VS Code SSH远程访问的完整指南
linux·ssh·ai编程
原神启动11 小时前
Docker 场景化作业:生产环境容器操作实训
运维·docker·容器
云老大TG:@yunlaoda3602 小时前
如何通过华为云国际站代理商CSBS进行备份策略设置?
运维·数据库·华为云
老王熬夜敲代码2 小时前
解决IP不够用的问题
linux·网络·笔记
zly35002 小时前
linux查看正在运行的nginx的当前工作目录(webroot)
linux·运维·nginx
来自于狂人3 小时前
华为云Stack服务实例创建失败通用排查对照表(备考+生产故障定位必备)
服务器·数据库·华为云
QT 小鲜肉3 小时前
【Linux命令大全】001.文件管理之file命令(实操篇)
linux·运维·前端·网络·chrome·笔记