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

相关推荐
AI+程序员在路上20 小时前
CAN 总线与 Linux SocketCAN C 语言测试程序
linux·c语言·网络
Predestination王瀞潞20 小时前
4.3.3 存储->微软文件系统标准(微软,自有技术标准):VFAT(Virtual File Allocation Table)虚拟文件分配表系统
linux·microsoft·vfat
HalvmånEver20 小时前
Linux:socket套接字编程的基础概念
linux·运维·服务器
二进制person21 小时前
JavaEE初阶 --网络初识
运维·服务器·网络
IMPYLH21 小时前
Linux 的 cp 命令
linux·运维·服务器
@syh.21 小时前
【linux】多线程
linux
贝锐21 小时前
立航货运携手贝锐向日葵,大型物流园区如何进行远程运维升级
运维·远程
RisunJan21 小时前
Linux命令-man(查看Linux中的指令帮助)
linux·运维·服务器
REDcker21 小时前
CentOS 与主流 Linux 发行版:版本与时间表(年表)
linux·运维·centos
bai_lan_ya21 小时前
使用linux的io文件操作综合实验_处理表格
linux·服务器·算法