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

相关推荐
贵州诚鑫致达科技有限公司6 分钟前
接入汇聚都是环怎么防环路?华为SEP加RRPP混合环
服务器·华为·php
syagain_zsx9 分钟前
BRpc 搭建简单 rpc 服务器
服务器·c++·网络协议·rpc·protobuf·库
云飞云共享云桌面11 分钟前
一台SolidWorks设计服务器多人共享落地方案和配置
运维·服务器·3d·电脑·制造
Pointer Pursuit17 分钟前
进程优先级
linux·运维·服务器
skywalk816317 分钟前
FreeBSD仿真环境里的Ubuntu linux系统,安装软件的时候不支持snap怎么办?
linux·运维·ubuntu·系统·freebsd
2301_7807896621 分钟前
IPv6 洪水猛兽:DDoS 攻击面 3 倍扩张下的精准清洗新战法
运维·安全·web安全·ddos·devops
程序员-Benothing9 小时前
Linux安全加固指南:账号、权限、服务、审计基线
linux·运维·安全
Mininglamp_27189 小时前
接口调用 vs 屏幕理解:两种 GUI 自动化架构的技术取舍
运维·架构·自动化
Cry丶10 小时前
ubuntu安装vsftpd
服务器·网络·ubuntu
爱喝水的鱼丶12 小时前
SAP-ABAP:ECC→S/4HANA ABAP 迁移升级指南——程序改造、语法兼容与性能优化 导语
运维·开发语言·性能优化·sap·abap·经验交流·开发交流