1.互斥体简介
信号量已经实现了互斥功能,如宏DECLARE_MUTEX(name)、DECLARE_MUTEX_LOCKED(name).但是在内核中,也存在专门实现互斥的机制.用mutex来表示.
2.内核API
2-1 定义并初始化
Struct mutex my_mutex;
Mutex_init(&my_mutex);
2-2 获取互斥体
Void fastcall mutex_lock(struct mutex *lock);
Int fastcall mutex_lock_interruptible(struct mutex *lock);
Int fastcall mutex_trylock(struct mutex *lock);
Mutext_lock()引起的睡眠不能被信号打断,mutex_lock_interruptible()引起的睡眠可以被信号打断.mutex_trylock()用于尝试获得mutex,获取不到mutex时不会引起进程睡眠.
2-3 释放互斥体
Void fastcall mutex_unlock(struct mutex *lock);
3.使用示意代码
Struct mutex my_mutex;
Mutex_init(&my_mutex);
Mutext_lock(&my_mutex);
...
Mutex_unlock(&my_mutex);