- binary semaphore: locked or unlocked
- used for protecting critical paths
- restrictions:
- one task can hold mutex at a time
- only task which locks can unlock again
- no recursive/multiple locking/unlocking
- can't be used in interrupt context (hard or soft)
- Advantages (over semaphores):
- Plattform independant
- faster than semphores (smaller!)
- code may sleep
-
DEBUG_MUTEXES
allows effective debugging
- Disadvantages
- only binary (no counting)
API: Kernel Mutexes
initalize:
#include <linux/mutex.h>
DEFINE_MUTEX(name);
at runtime:
mutex_init(struct mutex *lock);
locking:
void mutex_lock(struct mutex *lock);
int mutex_lock_interruptible(struct mutex *lock);
int mutex_trylock(struct mutex *lock);
unlocking:
void mutex_unlock(struct mutex *lock);
testing state:
int mutex_is_locked(struct mutex *lock);