1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Mutexes: blocking mutual exclusion locks
5 * started by Ingo Molnar:
7 * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9 * This file contains the main data structure and API definitions.
11 #ifndef __LINUX_MUTEX_H
12 #define __LINUX_MUTEX_H
14 #include <asm/current.h>
15 #include <linux/list.h>
16 #include <linux/spinlock_types.h>
17 #include <linux/lockdep.h>
18 #include <linux/atomic.h>
19 #include <asm/processor.h>
20 #include <linux/osq_lock.h>
21 #include <linux/debug_locks.h>
22 #include <linux/cleanup.h>
24 #ifdef CONFIG_DEBUG_LOCK_ALLOC
25 # define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
28 .wait_type_inner = LD_WAIT_SLEEP, \
31 # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
34 #ifndef CONFIG_PREEMPT_RT
37 * Simple, straightforward mutexes with strict semantics:
39 * - only one task can hold the mutex at a time
40 * - only the owner can unlock the mutex
41 * - multiple unlocks are not permitted
42 * - recursive locking is not permitted
43 * - a mutex object must be initialized via the API
44 * - a mutex object must not be initialized via memset or copying
45 * - task may not exit with mutex held
46 * - memory areas where held locks reside must not be freed
47 * - held mutexes must not be reinitialized
48 * - mutexes may not be used in hardware or software interrupt
49 * contexts such as tasklets and timers
51 * These semantics are fully enforced when DEBUG_MUTEXES is
52 * enabled. Furthermore, besides enforcing the above rules, the mutex
53 * debugging code also implements a number of additional features
54 * that make lock debugging easier and faster:
56 * - uses symbolic names of mutexes, whenever they are printed in debug output
57 * - point-of-acquire tracking, symbolic lookup of function names
58 * - list of all locks held in the system, printout of them
60 * - detects self-recursing locks and prints out all relevant info
61 * - detects multi-task circular deadlocks and prints out all affected
62 * locks and tasks (and only those tasks)
66 raw_spinlock_t wait_lock;
67 #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
68 struct optimistic_spin_queue osq; /* Spinner MCS lock */
70 struct list_head wait_list;
71 #ifdef CONFIG_DEBUG_MUTEXES
74 #ifdef CONFIG_DEBUG_LOCK_ALLOC
75 struct lockdep_map dep_map;
79 #ifdef CONFIG_DEBUG_MUTEXES
81 #define __DEBUG_MUTEX_INITIALIZER(lockname) \
84 extern void mutex_destroy(struct mutex *lock);
88 # define __DEBUG_MUTEX_INITIALIZER(lockname)
90 static inline void mutex_destroy(struct mutex *lock) {}
95 * mutex_init - initialize the mutex
96 * @mutex: the mutex to be initialized
98 * Initialize the mutex to unlocked state.
100 * It is not allowed to initialize an already locked mutex.
102 #define mutex_init(mutex) \
104 static struct lock_class_key __key; \
106 __mutex_init((mutex), #mutex, &__key); \
109 #define __MUTEX_INITIALIZER(lockname) \
110 { .owner = ATOMIC_LONG_INIT(0) \
111 , .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
112 , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
113 __DEBUG_MUTEX_INITIALIZER(lockname) \
114 __DEP_MAP_MUTEX_INITIALIZER(lockname) }
116 #define DEFINE_MUTEX(mutexname) \
117 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
119 extern void __mutex_init(struct mutex *lock, const char *name,
120 struct lock_class_key *key);
123 * mutex_is_locked - is the mutex locked
124 * @lock: the mutex to be queried
126 * Returns true if the mutex is locked, false if unlocked.
128 extern bool mutex_is_locked(struct mutex *lock);
130 #else /* !CONFIG_PREEMPT_RT */
132 * Preempt-RT variant based on rtmutexes.
134 #include <linux/rtmutex.h>
137 struct rt_mutex_base rtmutex;
138 #ifdef CONFIG_DEBUG_LOCK_ALLOC
139 struct lockdep_map dep_map;
143 #define __MUTEX_INITIALIZER(mutexname) \
145 .rtmutex = __RT_MUTEX_BASE_INITIALIZER(mutexname.rtmutex) \
146 __DEP_MAP_MUTEX_INITIALIZER(mutexname) \
149 #define DEFINE_MUTEX(mutexname) \
150 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
152 extern void __mutex_rt_init(struct mutex *lock, const char *name,
153 struct lock_class_key *key);
154 extern int mutex_trylock(struct mutex *lock);
156 static inline void mutex_destroy(struct mutex *lock) { }
158 #define mutex_is_locked(l) rt_mutex_base_is_locked(&(l)->rtmutex)
160 #define __mutex_init(mutex, name, key) \
162 rt_mutex_base_init(&(mutex)->rtmutex); \
163 __mutex_rt_init((mutex), name, key); \
166 #define mutex_init(mutex) \
168 static struct lock_class_key __key; \
170 __mutex_init((mutex), #mutex, &__key); \
172 #endif /* CONFIG_PREEMPT_RT */
175 * See kernel/locking/mutex.c for detailed documentation of these APIs.
176 * Also see Documentation/locking/mutex-design.rst.
178 #ifdef CONFIG_DEBUG_LOCK_ALLOC
179 extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
180 extern void _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
182 extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock,
183 unsigned int subclass);
184 extern int __must_check mutex_lock_killable_nested(struct mutex *lock,
185 unsigned int subclass);
186 extern void mutex_lock_io_nested(struct mutex *lock, unsigned int subclass);
188 #define mutex_lock(lock) mutex_lock_nested(lock, 0)
189 #define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
190 #define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0)
191 #define mutex_lock_io(lock) mutex_lock_io_nested(lock, 0)
193 #define mutex_lock_nest_lock(lock, nest_lock) \
195 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
196 _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
200 extern void mutex_lock(struct mutex *lock);
201 extern int __must_check mutex_lock_interruptible(struct mutex *lock);
202 extern int __must_check mutex_lock_killable(struct mutex *lock);
203 extern void mutex_lock_io(struct mutex *lock);
205 # define mutex_lock_nested(lock, subclass) mutex_lock(lock)
206 # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
207 # define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock)
208 # define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
209 # define mutex_lock_io_nested(lock, subclass) mutex_lock_io(lock)
213 * NOTE: mutex_trylock() follows the spin_trylock() convention,
214 * not the down_trylock() convention!
216 * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
218 extern int mutex_trylock(struct mutex *lock);
219 extern void mutex_unlock(struct mutex *lock);
221 extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
223 DEFINE_GUARD(mutex, struct mutex *, mutex_lock(_T), mutex_unlock(_T))
224 DEFINE_FREE(mutex, struct mutex *, if (_T) mutex_unlock(_T))
226 #endif /* __LINUX_MUTEX_H */