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

相关推荐
雾岛听蓝30 分钟前
Linux用户权限管理基础
linux
遇见火星34 分钟前
Linux 命令篇:df、du、fdisk 磁盘管理
linux·运维·服务器·df·du·fdisk
凌~风36 分钟前
013-计算机操作系统实验报告之Linux命令!
linux·计算机操作系统·实验报告
牛奶咖啡1341 分钟前
shell脚本编程(七)
linux·shell脚本编程·列表for循环·列表for循环的多种示例·for循环的break用法·for循环continue用法·c语言型for循环
噎住佩奇1 小时前
正则表达式(Regex)入门
运维·正则表达式
运维行者_1 小时前
远程办公场景 NFA:从网络嗅探与局域网流量监控软件排查团队网络卡顿问题
运维·服务器·开发语言·网络·自动化·php
心丑姑娘1 小时前
怎么理解ClickHouse的向量化执行
java·服务器·clickhouse
成为你的宁宁1 小时前
【Zabbix运维监控实战(附图文教程):Nginx 服务可用性、连接请求状态、CPU 内存占用与 JVM(Jar 包 / Tomcat)全维度监控】
运维·jvm·nginx·zabbix
gaize12131 小时前
个人建站服务器完全指南:从基础认知到实操选型
运维·服务器