]> Git Repo - linux.git/blob - include/linux/mutex.h
Merge tag 'v6.8-p4' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux.git] / include / linux / mutex.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Mutexes: blocking mutual exclusion locks
4  *
5  * started by Ingo Molnar:
6  *
7  *  Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <[email protected]>
8  *
9  * This file contains the main data structure and API definitions.
10  */
11 #ifndef __LINUX_MUTEX_H
12 #define __LINUX_MUTEX_H
13
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>
24
25 #ifdef CONFIG_DEBUG_LOCK_ALLOC
26 # define __DEP_MAP_MUTEX_INITIALIZER(lockname)                  \
27                 , .dep_map = {                                  \
28                         .name = #lockname,                      \
29                         .wait_type_inner = LD_WAIT_SLEEP,       \
30                 }
31 #else
32 # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
33 #endif
34
35 #ifndef CONFIG_PREEMPT_RT
36
37 #ifdef CONFIG_DEBUG_MUTEXES
38
39 #define __DEBUG_MUTEX_INITIALIZER(lockname)                             \
40         , .magic = &lockname
41
42 extern void mutex_destroy(struct mutex *lock);
43
44 #else
45
46 # define __DEBUG_MUTEX_INITIALIZER(lockname)
47
48 static inline void mutex_destroy(struct mutex *lock) {}
49
50 #endif
51
52 /**
53  * mutex_init - initialize the mutex
54  * @mutex: the mutex to be initialized
55  *
56  * Initialize the mutex to unlocked state.
57  *
58  * It is not allowed to initialize an already locked mutex.
59  */
60 #define mutex_init(mutex)                                               \
61 do {                                                                    \
62         static struct lock_class_key __key;                             \
63                                                                         \
64         __mutex_init((mutex), #mutex, &__key);                          \
65 } while (0)
66
67 #define __MUTEX_INITIALIZER(lockname) \
68                 { .owner = ATOMIC_LONG_INIT(0) \
69                 , .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
70                 , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
71                 __DEBUG_MUTEX_INITIALIZER(lockname) \
72                 __DEP_MAP_MUTEX_INITIALIZER(lockname) }
73
74 #define DEFINE_MUTEX(mutexname) \
75         struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
76
77 extern void __mutex_init(struct mutex *lock, const char *name,
78                          struct lock_class_key *key);
79
80 /**
81  * mutex_is_locked - is the mutex locked
82  * @lock: the mutex to be queried
83  *
84  * Returns true if the mutex is locked, false if unlocked.
85  */
86 extern bool mutex_is_locked(struct mutex *lock);
87
88 #else /* !CONFIG_PREEMPT_RT */
89 /*
90  * Preempt-RT variant based on rtmutexes.
91  */
92
93 #define __MUTEX_INITIALIZER(mutexname)                                  \
94 {                                                                       \
95         .rtmutex = __RT_MUTEX_BASE_INITIALIZER(mutexname.rtmutex)       \
96         __DEP_MAP_MUTEX_INITIALIZER(mutexname)                          \
97 }
98
99 #define DEFINE_MUTEX(mutexname)                                         \
100         struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
101
102 extern void __mutex_rt_init(struct mutex *lock, const char *name,
103                             struct lock_class_key *key);
104 extern int mutex_trylock(struct mutex *lock);
105
106 static inline void mutex_destroy(struct mutex *lock) { }
107
108 #define mutex_is_locked(l)      rt_mutex_base_is_locked(&(l)->rtmutex)
109
110 #define __mutex_init(mutex, name, key)                  \
111 do {                                                    \
112         rt_mutex_base_init(&(mutex)->rtmutex);          \
113         __mutex_rt_init((mutex), name, key);            \
114 } while (0)
115
116 #define mutex_init(mutex)                               \
117 do {                                                    \
118         static struct lock_class_key __key;             \
119                                                         \
120         __mutex_init((mutex), #mutex, &__key);          \
121 } while (0)
122 #endif /* CONFIG_PREEMPT_RT */
123
124 /*
125  * See kernel/locking/mutex.c for detailed documentation of these APIs.
126  * Also see Documentation/locking/mutex-design.rst.
127  */
128 #ifdef CONFIG_DEBUG_LOCK_ALLOC
129 extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
130 extern void _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
131
132 extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock,
133                                         unsigned int subclass);
134 extern int __must_check mutex_lock_killable_nested(struct mutex *lock,
135                                         unsigned int subclass);
136 extern void mutex_lock_io_nested(struct mutex *lock, unsigned int subclass);
137
138 #define mutex_lock(lock) mutex_lock_nested(lock, 0)
139 #define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
140 #define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0)
141 #define mutex_lock_io(lock) mutex_lock_io_nested(lock, 0)
142
143 #define mutex_lock_nest_lock(lock, nest_lock)                           \
144 do {                                                                    \
145         typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
146         _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map);             \
147 } while (0)
148
149 #else
150 extern void mutex_lock(struct mutex *lock);
151 extern int __must_check mutex_lock_interruptible(struct mutex *lock);
152 extern int __must_check mutex_lock_killable(struct mutex *lock);
153 extern void mutex_lock_io(struct mutex *lock);
154
155 # define mutex_lock_nested(lock, subclass) mutex_lock(lock)
156 # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
157 # define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock)
158 # define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
159 # define mutex_lock_io_nested(lock, subclass) mutex_lock_io(lock)
160 #endif
161
162 /*
163  * NOTE: mutex_trylock() follows the spin_trylock() convention,
164  *       not the down_trylock() convention!
165  *
166  * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
167  */
168 extern int mutex_trylock(struct mutex *lock);
169 extern void mutex_unlock(struct mutex *lock);
170
171 extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
172
173 DEFINE_GUARD(mutex, struct mutex *, mutex_lock(_T), mutex_unlock(_T))
174 DEFINE_GUARD_COND(mutex, _try, mutex_trylock(_T))
175 DEFINE_GUARD_COND(mutex, _intr, mutex_lock_interruptible(_T) == 0)
176
177 #endif /* __LINUX_MUTEX_H */
This page took 0.057126 seconds and 4 git commands to generate.