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

相关推荐
小小前端--可笑可笑4 小时前
Vue / React 单页应用刷新 /login 无法访问问题分析
运维·前端·javascript·vue.js·nginx·react.js
IP搭子来一个5 小时前
2026年动态IP代理怎么选:共享好还是独享好?
服务器·网络协议·tcp/ip
比奇堡派星星5 小时前
awk命令
linux·运维·服务器
WW、forever5 小时前
【服务器】上传百度网盘数据至服务器
运维·服务器
清水白石0085 小时前
Python 柯里化完全指南:从函数式思想到工程实践
linux·服务器·python
m0_694845576 小时前
netcut 是什么?简单安全的在线剪贴板搭建与使用教程
运维·服务器·安全·开源·云计算·github
女王大人万岁6 小时前
Golang标准库 CGO 介绍与使用指南
服务器·开发语言·后端·golang
宸迪6 小时前
【python】使用uv管理项目包依赖
linux·python·uv
网云工程师手记7 小时前
DDNS-Go部署与使用体验:动态公网IP远程访问不再断
运维·服务器·网络·网络协议·网络安全
HalvmånEver7 小时前
Linux:基于信号量的环形队列与生产者消费者模型(一)
linux·运维·服务器·信号量