mutex_lock() / mutex_unlock()¶
Purpose¶
Protects shared state in process context where sleeping is allowed.
Header¶
Prototype¶
Parameters¶
lock: pointer to an initializedstruct mutex.
Return Value¶
mutex_lock()returns no value and may sleep.mutex_unlock()releases the mutex.
Execution Context¶
Mutexes are sleepable synchronization primitives.
Typical usage includes:
- Process Context
- Workqueues
- Kernel threads
- Threaded IRQ handlers
Mutexes must never be used inside hard IRQ handlers.
Minimal Example¶
Common Pitfalls¶
- Do not use mutexes in hard IRQ context.
- Avoid holding mutexes while copying large data to/from user space.
- Use clear ownership rules to prevent deadlocks.