printf: Remove unused 'bprintf'
[drm/drm-misc.git] / include / linux / mutex.h
blob2bf91b57591b49e4668752e773419ae945f124da
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
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>
23 #include <linux/mutex_types.h>
25 struct device;
27 #ifdef CONFIG_DEBUG_LOCK_ALLOC
28 # define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
29 , .dep_map = { \
30 .name = #lockname, \
31 .wait_type_inner = LD_WAIT_SLEEP, \
33 #else
34 # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
35 #endif
37 #ifdef CONFIG_DEBUG_MUTEXES
39 # define __DEBUG_MUTEX_INITIALIZER(lockname) \
40 , .magic = &lockname
42 extern void mutex_destroy(struct mutex *lock);
44 #else
46 # define __DEBUG_MUTEX_INITIALIZER(lockname)
48 static inline void mutex_destroy(struct mutex *lock) {}
50 #endif
52 /**
53 * mutex_init - initialize the mutex
54 * @mutex: the mutex to be initialized
56 * Initialize the mutex to unlocked state.
58 * It is not allowed to initialize an already locked mutex.
60 #define mutex_init(mutex) \
61 do { \
62 static struct lock_class_key __key; \
64 __mutex_init((mutex), #mutex, &__key); \
65 } while (0)
67 /**
68 * mutex_init_with_key - initialize a mutex with a given lockdep key
69 * @mutex: the mutex to be initialized
70 * @key: the lockdep key to be associated with the mutex
72 * Initialize the mutex to the unlocked state.
74 * It is not allowed to initialize an already locked mutex.
76 #define mutex_init_with_key(mutex, key) __mutex_init((mutex), #mutex, (key))
78 #ifndef CONFIG_PREEMPT_RT
79 #define __MUTEX_INITIALIZER(lockname) \
80 { .owner = ATOMIC_LONG_INIT(0) \
81 , .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
82 , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
83 __DEBUG_MUTEX_INITIALIZER(lockname) \
84 __DEP_MAP_MUTEX_INITIALIZER(lockname) }
86 #define DEFINE_MUTEX(mutexname) \
87 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
89 extern void __mutex_init(struct mutex *lock, const char *name,
90 struct lock_class_key *key);
92 /**
93 * mutex_is_locked - is the mutex locked
94 * @lock: the mutex to be queried
96 * Returns true if the mutex is locked, false if unlocked.
98 extern bool mutex_is_locked(struct mutex *lock);
100 #else /* !CONFIG_PREEMPT_RT */
102 * Preempt-RT variant based on rtmutexes.
105 #define __MUTEX_INITIALIZER(mutexname) \
107 .rtmutex = __RT_MUTEX_BASE_INITIALIZER(mutexname.rtmutex) \
108 __DEP_MAP_MUTEX_INITIALIZER(mutexname) \
111 #define DEFINE_MUTEX(mutexname) \
112 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
114 extern void __mutex_rt_init(struct mutex *lock, const char *name,
115 struct lock_class_key *key);
117 #define mutex_is_locked(l) rt_mutex_base_is_locked(&(l)->rtmutex)
119 #define __mutex_init(mutex, name, key) \
120 do { \
121 rt_mutex_base_init(&(mutex)->rtmutex); \
122 __mutex_rt_init((mutex), name, key); \
123 } while (0)
125 #endif /* CONFIG_PREEMPT_RT */
127 #ifdef CONFIG_DEBUG_MUTEXES
129 int __devm_mutex_init(struct device *dev, struct mutex *lock);
131 #else
133 static inline int __devm_mutex_init(struct device *dev, struct mutex *lock)
136 * When CONFIG_DEBUG_MUTEXES is off mutex_destroy() is just a nop so
137 * no really need to register it in the devm subsystem.
139 return 0;
142 #endif
144 #define devm_mutex_init(dev, mutex) \
145 ({ \
146 typeof(mutex) mutex_ = (mutex); \
148 mutex_init(mutex_); \
149 __devm_mutex_init(dev, mutex_); \
153 * See kernel/locking/mutex.c for detailed documentation of these APIs.
154 * Also see Documentation/locking/mutex-design.rst.
156 #ifdef CONFIG_DEBUG_LOCK_ALLOC
157 extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
158 extern void _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
160 extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock,
161 unsigned int subclass);
162 extern int __must_check mutex_lock_killable_nested(struct mutex *lock,
163 unsigned int subclass);
164 extern void mutex_lock_io_nested(struct mutex *lock, unsigned int subclass);
166 #define mutex_lock(lock) mutex_lock_nested(lock, 0)
167 #define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
168 #define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0)
169 #define mutex_lock_io(lock) mutex_lock_io_nested(lock, 0)
171 #define mutex_lock_nest_lock(lock, nest_lock) \
172 do { \
173 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
174 _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
175 } while (0)
177 #else
178 extern void mutex_lock(struct mutex *lock);
179 extern int __must_check mutex_lock_interruptible(struct mutex *lock);
180 extern int __must_check mutex_lock_killable(struct mutex *lock);
181 extern void mutex_lock_io(struct mutex *lock);
183 # define mutex_lock_nested(lock, subclass) mutex_lock(lock)
184 # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
185 # define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock)
186 # define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
187 # define mutex_lock_io_nested(lock, subclass) mutex_lock_io(lock)
188 #endif
191 * NOTE: mutex_trylock() follows the spin_trylock() convention,
192 * not the down_trylock() convention!
194 * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
196 extern int mutex_trylock(struct mutex *lock);
197 extern void mutex_unlock(struct mutex *lock);
199 extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
201 DEFINE_GUARD(mutex, struct mutex *, mutex_lock(_T), mutex_unlock(_T))
202 DEFINE_GUARD_COND(mutex, _try, mutex_trylock(_T))
203 DEFINE_GUARD_COND(mutex, _intr, mutex_lock_interruptible(_T) == 0)
205 #endif /* __LINUX_MUTEX_H */