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

相关推荐
蒸蒸yyyyzwd5 分钟前
cpp选手秋招学习笔记day24-cs144项目总结
运维·服务器·网络
ang78410 分钟前
日常高频使用的 Ansible 命令,按使用频率和场景整理。
linux·运维·服务器
高亦真24 分钟前
今天是学习嵌入式的第35天
linux·学习·算法
翼龙云_cloud31 分钟前
腾讯云国际代理商:如何用云服务器快速部署WorkBuddy AI助手?
服务器·人工智能·云计算·腾讯云
mounter6251 小时前
eBPF 驱动的云原生网络变革:Cilium 的前世今生与全机套接字迭代技术演进
linux·ebpf·linux kernel·kernel
赵庆明老师1 小时前
用同一个用户登录宝塔面板的ftp与Linux的sftp
linux·运维·服务器
oushaojun21 小时前
深入理解 Linux I2C 框架:内核子系统分层解析(转)
linux
山君爱摸鱼1 小时前
堡垒机远程服务器故障收集
运维·服务器
不会就选b2 小时前
Linux之socket编程(六)----TCP-->模拟shell命令
linux·运维·tcp/ip
比兔代理2 小时前
住宅代理IP的出口节点调度:ASN、BGP 与 IP 段信誉机制解析
linux·服务器·网络