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

相关推荐
可爱又迷人的反派角色“yang”14 分钟前
GitLab配置与git集成实践
linux·网络·git·docker·云计算·gitlab
斯普信云原生组14 分钟前
Linux 平台 Redis Insight 安装卸载与常见问题
linux·运维·redis
qq_4162764217 分钟前
linux bashrc写各种离线库路径并验证
linux·运维·服务器
Tipriest_23 分钟前
配置用户pip源与查看当前的pip的源的办法
linux·人工智能·python·pip
B2_Proxy25 分钟前
如何搭建高速稳定安全的网络环境?住宅代理是关键
服务器·网络·安全
wdfk_prog37 分钟前
[Linux]学习笔记系列 -- [fs]mbcache
linux·笔记·学习
航Hang*37 分钟前
第二章:网络系统建设与运维(中级)——华为设备基本命令
运维·计算机网络·华为·ensp·交换机
飞Link40 分钟前
【Hadoop】Linux(CentOS7)下安装Hadoop集群
大数据·linux·hadoop·分布式
不染尘.40 分钟前
TCP拥塞控制
服务器·网络·计算机网络·tcp
飞Link1 小时前
【Sqoop】Linux(CentOS7)下安装Sqoop教程
linux·hive·hadoop·sqoop