加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_泰州站长网 (http://www.0523zz.com/)- 视觉智能、AI应用、CDN、行业物联网、智能数字人!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

一篇文章告诉你Linux驱动7-内核互斥锁

发布时间:2021-05-23 17:21:22 所属栏目:Linux 来源:互联网
导读:副标题#e# 信号量是在并行处理环境中对多个处理器访问某个公共资源进行保护的机制,mutex用于互斥操作。信号量的count初始化为1,down()/up()也可以实现类似mutex的作用。 mutex的语义相对于信号量要简单轻便一些,在锁争用激烈的测试场景下,mutex比信号量
副标题[/!--empirenews.page--]

信号量是在并行处理环境中对多个处理器访问某个公共资源进行保护的机制,mutex用于互斥操作。信号量的count初始化为1,down()/up()也可以实现类似mutex的作用。

mutex的语义相对于信号量要简单轻便一些,在锁争用激烈的测试场景下,mutex比信号量执行速度更快,可扩展性更好,另外mutex数据结构的定义比信号量小。

mutex的优点

mutex和信号量相比要高效的多:

mutex最先实现自旋等待机制

mutex在睡眠之前尝试获取锁

mutex实现MCS所来避免多个CPU争用锁而导致CPU高速缓存颠簸现象。

mutex的使用注意事项:

同一时刻只有一个线程可以持有mutex。

只有锁持有者可以解锁。不能再一个进程中持有mutex,在另外一个进程中释放他。

不允许递归地加锁和解锁。

当进程持有mutex时,进程不可以退出。

mutex必须使用官方API来初始化。

mutex可以睡眠,所以不允许在中断处理程序或者中断下半部中使用,例如tasklet、定时器等。

目录:

/linux/include/linux/mutex.h 

 

/* 

 * Simple, straightforward mutexes with strict semantics: 

 * 

 * - only one task can hold the mutex at a time 

 * - only the owner can unlock the mutex 

 * - multiple unlocks are not permitted 

 * - recursive locking is not permitted 

 * - a mutex object must be initialized via the API 

 * - a mutex object must not be initialized via memset or copying 

 * - task may not exit with mutex held 

 * - memory areas where held locks reside must not be freed 

 * - held mutexes must not be reinitialized 

 * - mutexes may not be used in hardware or software interrupt 

 *   contexts such as tasklets and timers 

 * 

 * These semantics are fully enforced when DEBUG_MUTEXES is 

 * enabled. Furthermore, besides enforcing the above rules, the mutex 

 * debugging code also implements a number of additional features 

 * that make lock debugging easier and faster: 

 * 

 * - uses symbolic names of mutexes, whenever they are printed in debug output 

 * - point-of-acquire tracking, symbolic lookup of function names 

 * - list of all locks held in the system, printout of them 

 * - owner tracking 

 * - detects self-recursing locks and prints out all relevant info 

 * - detects multi-task circular deadlocks and prints out all affected 

(编辑:云计算网_泰州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读