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

相关推荐
Justice link1 天前
K8S基本配置
运维·docker·容器
观熵1 天前
SaaS 系统的自动化部署结构设计实战指南:基于 K8s + Helm 的工程落地路径
运维·kubernetes·自动化·saas 架构
chinesegf1 天前
ubuntu中虚拟环境的简单创建和管理
linux·运维·ubuntu
若涵的理解1 天前
一文读懂K8S kubectl 命令,运维小白必看!
运维·docker·kubernetes
java_logo1 天前
2025 年 11 月最新 Docker 镜像源加速列表与使用指南
linux·运维·docker·容器·运维开发·kylin
一碗面4211 天前
Linux下的网络模型
linux·网络模型
峰顶听歌的鲸鱼1 天前
Kubernetes管理
运维·笔记·云原生·容器·kubernetes·云计算
霖霖总总1 天前
[小技巧42]InnoDB 索引与 MVCC 的协同工作原理
运维·数据库·mysql
CRMEB系统商城1 天前
CRMEB多商户系统(PHP)- 移动端二开之基本容器组件使用
运维·开发语言·小程序·php
小镇学者1 天前
【python】python项目是如何部署到服务器上的
服务器·python·github