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

相关推荐
PythonFun21 分钟前
自建RustDesk服务器:详细步骤与操作指南
运维·服务器
Android技术栈23 分钟前
鸿蒙开发(NEXT/API 12)【管理应用与Wear Engine服务的连接状态】手机侧应用开发
服务器·harmonyos·鸿蒙·鸿蒙系统·openharmony
可涵不会debug25 分钟前
【Linux】信号知识三把斧——信号的产生、保存和处理
linux·运维·信号
笑的像个child26 分钟前
使用树莓派搭建音乐服务器
服务器·树莓派·navidrome
facaixxx202427 分钟前
京东云主机怎么用?使用京东云服务器建网站(图文教程)
运维·服务器·京东云
jyan_敬言28 分钟前
【Linux】Linux命令与操作详解(一)文件管理(文件命令)、用户与用户组管理(创建、删除用户/组)
linux·运维·服务器·c语言·开发语言·汇编·c++
速盾cdn31 分钟前
速盾:网页游戏部署高防服务器有什么优势?
服务器·前端·web安全
一颗星星辰1 小时前
Python | 第九章 | 排序和查找
服务器·网络·python
unix2linux1 小时前
Parade Series - SHA256
linux·python·mysql·shell
学思之道2 小时前
给Linux操作系统命令取个别名
linux·运维·经验分享·学习方法