The Mutex API can be defined statically using the “DEFINE_MUTEX” MACRO. It can be dynamically defined during process/thread run using the “mutex_init” MACRO. Both methods are shown below
DEFINE_MUTEX(mutex_name); /*statically assign mutex by name- mutex_name*/
mutex_init(mutex); /* dynamically assign the mutex */
Both these Macros are placed in the header file – mutex.h. The code snippet from the header file is provided below.
The Mutex can be invoked by the kernel code in three distinct ways based on the current state of the Mutex Lock. They are
- Fast Path
- Mid Path
- Slow Path
Mid Path was a recent introduction to the Linux Kernel and it allows opportunistic spinning for the lock. We will discuss the above three mechanisms in the following article.
- References:
- https://elixir.bootlin.com/linux/latest/source/include/linux/mutex.h#L139
- https://www.kernel.org/doc/html/latest/locking/mutex-design.html
Pingback: Mutexes in the Linux kernel | Hitch Hiker's Guide to Learning