21 同步与互斥(七)互斥量
1 简介
mutex相对于semaphore更加高效。
mutex在面对SMP时,如果mutex在别的CPU上运行,而“我”是唯一在等待这个mutex的进程。此时“我”是不会去休眠的,而是原地spin
2 mutex的结构和API
2.1 mutex结构
struct mutex my_mutex;
struct mutex {
/* 1: unlocked, 0: locked, negative: locked, possible waiters */
/* 1: unlock
* 0: lock
* -1:lock,有人wait
*/
atomic_t count; /* 1: unlocked, 0: locked, negative: locked, possible waiters,这里描述的是possible */
spinlock_t wait_lock; /* 借助spinlock */
struct list_head wait_list; /* 与spinlock一致,等待线程放于此 */
#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER)
struct task_struct *owner; /* 调试和性能优化 */
#endif
#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
struct optimistic_spin_queue osq; /* Spinner MCS lock */
#endif
#ifdef CONFIG_DEBUG_MUTEXES
void *magic;
#endif
#ifdef CONFIG_DEBUG_LOCK_ALLOC
struct lockdep_map dep_map;
#endif
};
2.2 mutex的API
-
mutex_init(mutex)
初始化mutex
-
DEFINE_MUTEX(mutexname)
定义并初始化一个mutexname
-
void mutex_lock(struct mutex *lock)
获得mutex,如果无法获得会休眠
-
int mutex_lock_interruptible(struct mutex *lock)
获得mutex,如果无法获得会休眠。此外还能被信号唤醒
return
- 0:成功获得mutex
- -EINTR:被信号唤醒
-
int mutex_lock_killable(struct mutex *lock)
获得mutex,如果无法获得会休眠。此外还能被
fatal signal
信号唤醒return
- 0:成功获得mutex
- -EINTR:被信号唤醒
-
int mutex_trylock(struct mutex *lock)
尝试获取mutex,如果无法获得立即返回
return
- 1:获得了mutex
- 0:没有获得mutex
-
void mutex_unlock(struct mutex *lock)
释放mutex,唤醒其他等待同一个mutex的线程
3 mutex实现机制
mutex中存在两条路径,一条fastpath,一条slowpath
3.1 mutex_lock
mutex_lock -> __mutex_fastpath_lock | | -> mutex_set_owner
| -> __mutex_lock_slowpath |
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);
}
EXPORT_SYMBOL(mutex_lock);
static inline void
__mutex_fastpath_lock(atomic_t *count, void (*fail_fn)(atomic_t *))
{
/* 原子操作减
* 如果之前count为1,即mutex未被占用。直接返回。
*/
if (unlikely(atomic_dec_return_acquire(count) < 0))
fail_fn(count); // 如果count值为0,即mutex被占用。则调用__mutex_lock_slowpath。走slow路径
}
__visible void __sched
__mutex_lock_slowpath(atomic_t *lock_count)
{
/* container_of 从成员推出指针head */
struct mutex *lock = container_of(lock_count, struct mutex, count);
__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0,
NULL, _RET_IP_, NULL, 0);
}
/*
* Lock a mutex (possibly interruptible), slowpath:
*/
static __always_inline int __sched
__mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
struct lockdep_map *nest_lock, unsigned long ip,
struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
{
struct task_struct *task = current;
struct mutex_waiter waiter;
unsigned long flags;
int ret;
/* 传入值为0 */
if (use_ww_ctx) {
struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
if (unlikely(ww_ctx == READ_ONCE(ww->ctx)))
return -EALREADY;
}
/* 禁止抢占 */
preempt_disable();
mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
/* 优化操作
* 如果是在另外一个CPU上的进程在使用,则不休眠,尝试等待一会
*/
if (mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx)) {
/* got the lock, yay! */
preempt_enable();
return 0;
}
/* 上锁 */
spin_lock_mutex(&lock->wait_lock, flags);
/*
* Once more, try to acquire the lock. Only try-lock the mutex if
* it is unlocked to reduce unnecessary xchg() operations.
*/
/* 再次判定如果mutex没有被锁,并且count为1,则跳过等待
*/
if (!mutex_is_locked(lock) &&
(atomic_xchg_acquire(&lock->count, 0) == 1))
goto skip_wait;
debug_mutex_lock_common(lock, &waiter);
debug_mutex_add_waiter(lock, &waiter, task);
/* add waiting tasks to the end of the waitqueue (FIFO): */
/* 将当前进程放入wait_list
* 这里是FIFO,即先等待的先获得mutex
*/
list_add_tail(&waiter.list, &lock->wait_list);
waiter.task = task;
lock_contended(&lock->dep_map, ip);
for (;;) {
/*
* Lets try to take the lock again - this is needed even if
* we get here for the first time (shortly after failing to
* acquire the lock), to make sure that we get a wakeup once
* it's unlocked. Later on, if we sleep, this is the
* operation that gives us the lock. We xchg it to -1, so
* that when we release the lock, we properly wake up the
* other waiters. We only attempt the xchg if the count is
* non-negative in order to avoid unnecessary xchg operations:
*/
/* 如果count为1,则意为着mutex可用,break */
if (atomic_read(&lock->count) >= 0 &&
(atomic_xchg_acquire(&lock->count, -1) == 1))
break;
/*
* got a signal? (This code gets eliminated in the
* TASK_UNINTERRUPTIBLE case.)
*/
/* 获得到信号,就退出 */
if (unlikely(signal_pending_state(state, task))) {
ret = -EINTR;
goto err;
}
if (use_ww_ctx && ww_ctx->acquired > 0) {
ret = __ww_mutex_lock_check_stamp(lock, ww_ctx);
if (ret)
goto err;
}
/* 把当前进程设为非RUNNING */
__set_task_state(task, state);
/* didn't get the lock, go to sleep: */
spin_unlock_mutex(&lock->wait_lock, flags); // 解锁
schedule_preempt_disabled(); // 开始调度
spin_lock_mutex(&lock->wait_lock, flags); // 被信号或者mutex_unlock唤醒,上锁
}
/* 设置当前进程为RUNNING */
__set_task_state(task, TASK_RUNNING);
/* 删除mutex中wait_list中的进程 */
mutex_remove_waiter(lock, &waiter, task);
/* set it to 0 if there are no waiters left: */
/* 如果wait_list为空,表示已经没人等待这个mutex了。将count设为0 */
if (likely(list_empty(&lock->wait_list)))
atomic_set(&lock->count, 0);
debug_mutex_free_waiter(&waiter);
skip_wait:
/* got the lock - cleanup and rejoice! */
lock_acquired(&lock->dep_map, ip);
mutex_set_owner(lock);
/* use_ww_ctx为0 */
if (use_ww_ctx) {
struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
ww_mutex_set_context_slowpath(ww, ww_ctx);
}
spin_unlock_mutex(&lock->wait_lock, flags);
preempt_enable();
return 0;
err:
mutex_remove_waiter(lock, &waiter, task);
spin_unlock_mutex(&lock->wait_lock, flags);
debug_mutex_free_waiter(&waiter);
mutex_release(&lock->dep_map, 1, ip);
preempt_enable();
return ret;
}
3.2 mutex_unlock
mutex_unlock -> __mutex_fastpath_unlock
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);
}
EXPORT_SYMBOL(mutex_unlock);
static inline void
__mutex_fastpath_unlock(atomic_t *count, void (*fail_fn)(atomic_t *))
{
/* count加1后如果count还是小于等于1,则表示设备当前有人等待。不能直接放回
* 需要调用slowpath
*/
if (unlikely(atomic_inc_return_release(count) <= 0))
fail_fn(count);
}
__visible void
__mutex_unlock_slowpath(atomic_t *lock_count)
{
struct mutex *lock = container_of(lock_count, struct mutex, count);
__mutex_unlock_common_slowpath(lock, 1);
}
static inline void
__mutex_unlock_common_slowpath(struct mutex *lock, int nested)
{
unsigned long flags;
WAKE_Q(wake_q);
/*
* As a performance measurement, release the lock before doing other
* wakeup related duties to follow. This allows other tasks to acquire
* the lock sooner, while still handling cleanups in past unlock calls.
* This can be done as we do not enforce strict equivalence between the
* mutex counter and wait_list.
*
*
* Some architectures leave the lock unlocked in the fastpath failure
* case, others need to leave it locked. In the later case we have to
* unlock it here - as the lock counter is currently 0 or negative.
*/
if (__mutex_slowpath_needs_to_unlock())
atomic_set(&lock->count, 1); // count被置为1
spin_lock_mutex(&lock->wait_lock, flags);
mutex_release(&lock->dep_map, nested, _RET_IP_);
debug_mutex_unlock(lock);
/* 从wait_list中取出第一个进程 */
if (!list_empty(&lock->wait_list)) {
/* get the first entry from the wait-list: */
struct mutex_waiter *waiter =
list_entry(lock->wait_list.next,
struct mutex_waiter, list);
debug_mutex_wake_waiter(lock, waiter);
wake_q_add(&wake_q, waiter->task);
}
spin_unlock_mutex(&lock->wait_lock, flags);
wake_up_q(&wake_q); // 唤醒wait进程
}