October 2017
Intermediate to advanced
586 pages
14h 8m
English
Mutual exclusion (mutex) is the de facto, most-used locking mechanism. To understand how it works, let's see what its structure looks like in include/linux/mutex.h:
struct mutex {
/* 1: unlocked, 0: locked, negative: locked, possible waiters */
atomic_t count;
spinlock_t wait_lock;
struct list_head wait_list;
[...]
};
As we have seen in the wait queue section, there is also a list type field in the structure: wait_list. The principle of sleeping is the same.
Contenders are removed from the scheduler run queue and put onto the wait list (wait_list) in a sleep state. The kernel then schedules and executes other tasks. When the lock is released, a waiter in the wait queue is woken, moved off the wait_list, and scheduled back.