内核同步机制之Mutex Exclusion

S3C2440在内核版本2.6.34下开发ADC驱动时,会发现驱动程序里用了互斥通讯来实现同步:数据结构

<!-- lang: cpp -->
DECLARE_MUTEX(ADC_LOCK);
static int OwnADC = 0;

在wikipedia中式这么定义Mutex Exclusion的:函数

In computer science, mutual exclusion refers to the requirement of ensuring that no two processes or threads (henceforth referred to only as processes) are in their critical section at the same time. Here, a critical section refers to a period of time when the process accesses a shared resource, such as shared memory.ui

专业的表达也能够是这样:线程

A data structure for mutual exclusion, also known as a binary semaphore. A mutex is basically just a multitasking-aware binary flag that can be used to synchronize the activities of multiple tasks. As such, it can be used to protect critical sections of the code from interruption and shared resources from simultaneous use.code

互斥体是表现互斥现象的数据结构,也被看成二元信号灯。一个互斥基本上是一个多任务敏感的二元信号,它能用做同步多任务的行为,它经常使用做保护从中断来的临界段代码而且在共享同步使用的资源。ip

##使用## Linux内核的同步机制里面: 辅助宏:ci

<!-- lang: cpp -->
DECLARE_MUTEX(name); /*声明一个互斥锁,把名为name的信号量变量初始化为1 */
DECLARE_MUTEX_LOCKED(name); /* 声明一个互斥锁,把名为name的信号量变量初始化为0 */

在Linux世界中, P函数被称为down, 指的是该函数减少了信号量的值, 它也许会将调用者置于休眠状态, 而后等待信号量变得可用, 以后再授予调用者对被保护资源的访问权限.资源

<!-- lang: cpp -->
void down(struct semaphore *sem);

当一个线程成功调用down函数后, 就称为该线程拥有了该信号量, 能够访问被该信号量保护的临界区. 当互斥操做完成后, 必须释放该信号量. Linux的V函数是up:开发

<!-- lang: cpp -->
void up(struct semaphore *sem)

调用up以后, 调用者再也不拥有该信号量rem

##Reference## [1].Linux Device Drivers.Thirth Edition.P112-P114

相关文章
相关标签/搜索