在内核中结构体struct mutex
的定义
// 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
* locks and tasks (and only those tasks)
*/
struct mutex {
atomic_long_t owner;
spinlock_t wait_lock;
#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
struct optimistic_spin_queue osq; /* Spinner MCS lock */
#endif
struct list_head wait_list;
#ifdef CONFIG_DEBUG_MUTEXES
void *magic;
#endif
#ifdef CONFIG_DEBUG_LOCK_ALLOC
struct lockdep_map dep_map;
#endif
};
竞争者从调度器的运行队列中删除,放入处于睡眠状态的等待链表(wait_list
)中,然后内核调度并执行其他任务。当锁被释放时,等待队列中的等待者被唤醒,从wait_list中溢出,然后被重新调度。
相关函数
// 静态声明
DEFINE_MUTEX(struct mutex);
// 动态声明
struct mutex test_mutex;
mutex_init(&test_mutex);
// 获取互斥锁
void mutex_lock(struct mutex *lock);
int mutex_lock_interruptible(struct mutex *lock);
int mutex_lock_killable(struct mutex *lock);
// 解锁
void mutex_unlock(struct mutex *lock);
// 检查互斥锁是否已经被持有
int mutex_is_locked(struct mutex *lock);
// 尝试获取互斥锁,如果没有被锁定,则获取互斥锁
int mutex_trylock(struct mutex *lock);
与等待队列的可终端函数一样,建议使用mutex_lock_interruptible()
,它使驱动可以被所有信号中断,mutex_lock_killable()
,只有杀死进程的信号才能中断驱动程序,也就是说mutex_lock()
不响应所有的信号,建议的使用组合为mutex_lock()+mutex_unlock_interruptible()
。
从内核函数说明来看,需要严格遵守一些规则。
API
初始化memset
或复制进行初始化Tasklet
和定时器