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

相关推荐
2301_7867566010 分钟前
不做分子仿真、不布局自动化实验室,垂直科研智能平台同样跑通 AI for Science 可持续变现
运维·人工智能·自动化
志栋智能18 分钟前
超自动化安全如何提升合规与审计效率?
运维·安全·自动化
March.s25 分钟前
深入理解 Linux 系统启动原理
linux·运维·服务器
zbyyd1 小时前
Linux 基础与 Shell 命令
linux·运维·arm开发
起名真的太难了1 小时前
Linux系统运维与远程管理核心命令速查指南
linux·运维·服务器
tg_xianheyun1 小时前
阿里云国际账号注册代充值怎么选服务商?
服务器·阿里云·云计算·全球访问优化
xiaoye-duck1 小时前
《Linux网络编程》吃透 Linux 网络传输全流程:完整梳理局域网通信、封装解包与跨网络传输原理
linux·网络
深念Y1 小时前
订阅管理及节点过滤实践(技术笔记)
linux·网络·arm·优化·日志·工具·soc
IT小盘1 小时前
17-AI应用防止越权访问知识库
服务器·网络·windows
酷可达拉斯1 小时前
自动化运维-Ansible Role详解
linux·运维·服务器·自动化·ansible