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

相关推荐
宴之敖者、5 小时前
Linux——yum和vim
linux·运维·服务器
人道领域5 小时前
JavaWeb从入门到进阶(Maven依赖管理)
linux·python·maven
开开心心_Every5 小时前
网络管理员IP配置工具:设置多台电脑地址
运维·服务器·网络·网络协议·学习·tcp/ip·edge
时空无限5 小时前
网络包匹配路由的原则是啥 从 01 与啥的运算讲起
运维·服务器·网络
一位不知名民工5 小时前
python3从入门到精通(五): pyhhon协程之asyncio模块(异步IO)(一)
运维·python
大柏怎么被偷了5 小时前
【Linux】信号
linux·运维·服务器
小安啃代码5 小时前
在ubuntu中使用wps无法使用宋体
linux·ubuntu·wps
Jia ming5 小时前
大小端模式:字节顺序的奥秘
linux·运维·服务器
Zach_yuan5 小时前
Linux 线程入门到理解:从 pthread 使用到线程库底层原理
linux·运维·服务器
不会kao代码的小王6 小时前
深信服超融合 HCI 核心技术解析:aSV、aSAN 与 aNET 的协同架构
运维·服务器·网络·数据库·github