Mutex API List and Sample API Code

A few of the mutex APIs are provided below

#define mutex_init(mutex) Initialize the mutex to unlocked state. This Macro invokes the function __mutex_init
void __sched mutex_lock(struct mutex *lock) Acquire the lock
void __sched mutex_unlock(struct mutex *lock) Release the Mutex
int __sched mutex_lock_interruptible(struct mutex *lock) Acquire the Mutex, interruptible by signals. If a signal is delivered while the process is sleeping, this function will return without acquiring the Mutex
int __sched mutex_lock_killable(struct mutex *lock) Acquire the mutex, interruptible by fatal signals. If a signal which will be fatal to the current process is delivered while the process is sleeping, this function will return without acquiring the mutex.
int __sched mutex_trylock(struct mutex *lock) try to acquire the mutex, without waitingTry to acquire the mutex atomically. Returns 1 if the mutex has been acquired successfully, and 0 on contention.
int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock) return holding mutex if we dec to 0return true and hold lock if we dec to 0, return false otherwise

A sample code showing the usage of static and dynamic allocation mechanisms for the Mutex API is provided below

Static Mutex example code:

Dynamic Mutex example code:

Semaphores in the Linux Kernel

  • References
    • https://elixir.bootlin.com/linux/latest/source/kernel/locking/mutex.c#L279

Comments

  1. Pingback: Fast Path, Mid Path and Slow Path in Mutex | Hitch Hiker's Guide to Learning

Leave a Reply

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