【发布时间】:2015-08-18 06:52:57
【问题描述】:
我正在尝试了解 Linux 内核中互斥锁实现的内部结构。 在我看来,关于互斥锁实现的最基本的事情之一是
只有获得锁的线程才能释放锁 互斥体
但是,即使在完成互斥锁实现 (http://lxr.free-electrons.com/source/kernel/locking/mutex.c) 之后,我还是无法理解在 mutex_unlock 期间如何检查所有权?
根据实现:
void __sched mutex_unlock(struct mutex *lock){
.......
mutex_clear_owner(lock); // just clears the owner
__mutex_fastpath_unlock(&lock->count, __mutex_unlock_slowpath);
....
}
这里是 mutex_clear_owner 宏定义(http://lxr.free-electrons.com/source/kernel/locking/mutex.h#L25)。
static inline void mutex_clear_owner(struct mutex *lock)
{
lock->owner = NULL;
}
那么如何判断调用了unlock函数的线程是否真的是线程被锁住了呢?
我错过了什么?
提前致谢。
【问题讨论】:
-
据我所知,mutex_clear_owner 不会检查所有权,它会清除它。释放锁由不同的函数(mutex_unlock?)执行。
-
是的,你是对的。但是 __mutex_fastpath_unlock 也不执行任何与检查所有权相关的操作。由于 clear_owner 是第一个语句并清除所有者,因此任何所有权都应该发生在那里。由于那里没有发生,我无法理解所有权检查是如何执行的。
标签: multithreading linux-kernel mutex