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

相关推荐
冰封之寂3 小时前
Docker 资源管理终极指南:CPU、内存与 IO 的精细化控制
linux·运维·服务器·docker·云计算
xiaoye-duck4 小时前
《Linux系统编程》Linux 系统多线程(五):<线程同步与互斥>线程互斥(上):从条件变量到生产者消费者模型详解
linux·线程
小柯南敲键盘4 小时前
图片翻译API接入与自动化实现指南
运维·python·自动化
云计算磊哥@5 小时前
运维开发宝典058-大型网站nginx服务器管理全集4
服务器·nginx·运维开发
952365 小时前
Docker - 基础
运维·后端·docker·容器
Promise微笑5 小时前
智能激光清障仪选型指南:高效运维与安全保障的关键考量
运维·安全
Code_Ignis6 小时前
docker国内可用源(持续维护)
运维·docker·容器
一叶龙洲6 小时前
向日葵远程Ubuntu,支持隐私屏
linux·运维·ubuntu
酷可达拉斯6 小时前
Linux操作系统-shell编程之Grep与正则表达式
linux·运维·服务器·正则表达式·centos
寒水馨7 小时前
Linux下载、安装llama.cpp-b10068(附安装包llama-b10068-bin-ubuntu-vulkan-x64.tar.gz)
linux·ubuntu·llm·llama·本地部署·llama.cpp·推理引擎