函数 mutex_init() / mutex_lock() / mutex_unlock()
1. 初始化互斥体 -- mutex_init();
2. 获得互斥体 -- mutex_lock();
3. 释放互斥体 -- mutex_unlock();
mutex不能使用在 中断的上下文 中。
1. mutex_init(), 注意mutex使用之前都需要先init
[html] view plain copy /** * mutex_init - initialize the mutex * @mutex: the mutex to be initialized * * Initialize the mutex to unlocked state. 初始化互斥锁到解锁状态。 * * It is not allowed to initialize an already locked mutex. 不允许初始化已锁定的互斥锁。 */ # define mutex_init(mutex) \ do { \ static struct lock_class_key __key; \ \ __mutex_init((mutex), #mutex, &__key); \ } while (0) void __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key) { atomic_set(&lock->count, 1); spin_lock_init(&lock->wait_lock); INIT_LIST_HEAD(&lock->wait_list); mutex_clear_owner(lock); #ifdef CONFIG_MUTEX_SPIN_ON_OWNER lock->spin_mlock = NULL; #endif debug_mutex_init(lock, name, key); }
2. mutex_lock(), 注意看注释说明,
a. 如果mutex已经被其他task获取,那么目前的task先sleep直到获取;
b. mutex不能被嵌套获取;上一个task释放mutex之后,才能被其他的task获取;
c. mutex要先被初始化才能使用;mutex正在使用过程中,其内存不能被释放;
[html] view plain copy /** * mutex_lock - acquire the mutex 获得互斥锁 * @lock: the mutex to be acquired * * Lock the mutex exclusively for this task. If the mutex is not * available right now, it will sleep until it can get it. 锁定这个任务专用的互斥锁。如果互斥对象不是可用的,它会一直睡到它可以得到它。
* The mutex must later on be released by the same task that * acquired it. Recursive locking is not allowed. The task * may not exit without first unlocking the mutex. Also, kernel * memory where the mutex resides mutex must not be freed with * the mutex still locked. The mutex must first be initialized * (or statically defined) before it can be locked. memset()-ing * the mutex to 0 is not allowed. * * ( The CONFIG_DEBUG_MUTEXES .config option turns on debugging * checks that will enforce the restrictions and will also do * deadlock debugging. ) * * This function is similar to (but not equivalent to) down(). */ void __sched mutex_lock(struct mutex *lock) { might_sleep(); /* * The locking fastpath is the 1->0 transition from * 'unlocked' into 'locked' state. */ __mutex_fastpath_lock(&lock->count, __mutex_lock_slowpath); mutex_set_owner(lock); }
3. mutex_unlock(), 释放互斥体
a. 释放之前获得的mutex;
b. mutex只有被获得,才能调用这个函数来释放;换言之,如果没有获得,就没有必要做释放的动作;
[html] view plain copy /** * mutex_unlock - release the mutex * @lock: the mutex to be released * * Unlock a mutex that has been locked by this task previously. * * This function must not be used in interrupt context. Unlocking * of a not locked mutex is not allowed. * * This function is similar to (but not equivalent to) up(). */ void __sched mutex_unlock(struct mutex *lock) { /* * The unlocking fastpath is the 0->1 transition from 'locked' * into 'unlocked' state: */ #ifndef CONFIG_DEBUG_MUTEXES /* * When debugging is enabled we must not clear the owner before time, * the slow path will always be taken, and that clears the owner field * after verifying that it was indeed current. */ mutex_clear_owner(lock); #endif __mutex_fastpath_unlock(&lock->count, __mutex_unlock_slowpath); }
————————————————
版权声明:本文为CSDN博主「Jarry_le」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u011677209/article/details/77600575