Mutexes in the Linux kernel

Mutex means mutual Exclusion. The mutex provides a mechanism to protect critical data. The mutex allows a process/thread to go to sleep state in case the lock cannot be acquired immediately. This is in contrast to a spinlock wherein the calling process/thread keeps spinning at obtaining the lock and does not go down to sleep.

1

Mutex is only held by one process/thread at a time

2

The owner who holds the Mutex lock can only release the lock

3

A process/thread needs to release the Mutex before it can exit

4

Mutex cannot be used in the interrupt context (Hardware/software) or Tasklet as it is difficult for a mutex to be released in the same context as is necessary for a Mutex. Mutex can be used in a workqueues

5

Recursive locking and unlocking of Mutexes are not allowed. Multiple Unlocking of a Mutex is also disallowed. It is not allowed to initialize a Mutex which is already locked

In the next section, we will look at a Mutex API and how to initialize the same and use the API.

  • References:
    • Linux Device Drivers development – John Madieu
    • Linux Kernel Development – Raghu Bharadwaj 
    • https://www.kernel.org/doc/html/latest/locking/mutex-design.html

Mutex API

Comments

  1. Pingback: Spin lock Initialization and Use | Hitch Hiker's Guide to Learning

Leave a Reply

Your email address will not be published. Required fields are marked *