mutex_lock 函数分析
path:kernel/locking/mutex.c
function source code:
void __sched mutex_lock(struct mutex *lock)
{
might_sleep();
if (!__mutex_trylock_fast(lock))
__mutex_lock_slowpath(lock);
}
EXPORT_SYMBOL(mutex_lock);
从上面代码可以看出 mutex_lock
函数有两条路径,首先尝试__mutex_trylock_fast(lock)
快速路径,如果尝试失败就进入 __mutex_lock_slowpath(lock)
慢速路径
__mutex_trylock_fast(lock)
atomic_long_try_cmpxchg_acquire(&lock->owner, &zero, curr) //判断 curr 的值是否 小于等于 zero
arch_atomic_long_try_cmpxchg_acquire(v, old, new);
arch_atomic_try_cmpxchg_acquire(v, (int *)old, new);
arch_atomic_try_cmpxchg_acquire(atomic_t *v, int *old, int new)
判断curr 的值是否小于等于 0 如果小于等于 0 则进入 __mutex_lock_slowpath(lock)
否则就直接返回
__mutex_lock_slowpath(lock)
__mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false);
__mutex_lock_slowpath(lock)
锁定互斥锁 ,可能会中断