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

相关推荐
wangjialelele11 分钟前
Linux mmap 机制:从 read/write 底层流程到手写 malloc 内存分配
linux·运维·服务器·mmap
草莓熊Lotso14 分钟前
【Linux网络】UDP Socket 编程全解析:从回显服务到通用字典服务,从零实现工业级代码
linux·运维·服务器·数据库·c++·单片机·udp
一只积极向上的小咸鱼2 小时前
Codex 在 VS Code + ModelArts 场景下的登录与配置总结
linux·运维·windows
Waay7 小时前
Linux Shell 知识点考评(一):grep 文本搜索(附答案)
linux·运维·服务器
jamon_tan7 小时前
Linux下串口RAW模式设置
linux
碧海银沙音频科技研究院7 小时前
基于VMware虚拟机ubuntu开发博通BK7258方法
linux·运维·ubuntu
云边有个稻草人10 小时前
【Linux系统】进程地址空间
linux·虚拟地址空间·进程地址空间·虚拟地址空间是怎么实现的?·为什么要有虚拟地址空间?·怎么理解虚拟地址空间?
weixin_5142531810 小时前
511-qwen3.5-patch
服务器
谁似人间西林客11 小时前
工厂大脑如何让汽车制造告别“救火式”运维?
运维·汽车·制造
飞飞传输12 小时前
数字化科研提速关键 构建安全可控一体化跨网数据传输体系
大数据·运维·安全