2 * Mutexes: blocking mutual exclusion locks
4 * started by Ingo Molnar:
6 * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8 * This file contains the main data structure and API definitions.
10 #ifndef __LINUX_MUTEX_H
11 #define __LINUX_MUTEX_H
13 #include <asm/current.h>
14 #include <linux/list.h>
15 #include <linux/spinlock_types.h>
16 #include <linux/linkage.h>
17 #include <linux/lockdep.h>
18 #include <linux/atomic.h>
19 #include <asm/processor.h>
20 #include <linux/osq_lock.h>
22 #ifdef CONFIG_DEBUG_LOCK_ALLOC
23 # define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
24 , .dep_map = { .name = #lockname }
26 # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
29 #ifdef CONFIG_PREEMPT_RT_FULL
30 # include <linux/mutex_rt.h>
34 * Simple, straightforward mutexes with strict semantics:
36 * - only one task can hold the mutex at a time
37 * - only the owner can unlock the mutex
38 * - multiple unlocks are not permitted
39 * - recursive locking is not permitted
40 * - a mutex object must be initialized via the API
41 * - a mutex object must not be initialized via memset or copying
42 * - task may not exit with mutex held
43 * - memory areas where held locks reside must not be freed
44 * - held mutexes must not be reinitialized
45 * - mutexes may not be used in hardware or software interrupt
46 * contexts such as tasklets and timers
48 * These semantics are fully enforced when DEBUG_MUTEXES is
49 * enabled. Furthermore, besides enforcing the above rules, the mutex
50 * debugging code also implements a number of additional features
51 * that make lock debugging easier and faster:
53 * - uses symbolic names of mutexes, whenever they are printed in debug output
54 * - point-of-acquire tracking, symbolic lookup of function names
55 * - list of all locks held in the system, printout of them
57 * - detects self-recursing locks and prints out all relevant info
58 * - detects multi-task circular deadlocks and prints out all affected
59 * locks and tasks (and only those tasks)
62 /* 1: unlocked, 0: locked, negative: locked, possible waiters */
65 struct list_head wait_list;
66 #if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER)
67 struct task_struct *owner;
69 #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
70 struct optimistic_spin_queue osq; /* Spinner MCS lock */
72 #ifdef CONFIG_DEBUG_MUTEXES
75 #ifdef CONFIG_DEBUG_LOCK_ALLOC
76 struct lockdep_map dep_map;
81 * This is the control structure for tasks blocked on mutex,
82 * which resides on the blocked task's kernel stack:
85 struct list_head list;
86 struct task_struct *task;
87 #ifdef CONFIG_DEBUG_MUTEXES
92 #ifdef CONFIG_DEBUG_MUTEXES
93 # include <linux/mutex-debug.h>
95 # define __DEBUG_MUTEX_INITIALIZER(lockname)
97 * mutex_init - initialize the mutex
98 * @mutex: the mutex to be initialized
100 * Initialize the mutex to unlocked state.
102 * It is not allowed to initialize an already locked mutex.
104 # define mutex_init(mutex) \
106 static struct lock_class_key __key; \
108 __mutex_init((mutex), #mutex, &__key); \
110 static inline void mutex_destroy(struct mutex *lock) {}
113 #define __MUTEX_INITIALIZER(lockname) \
114 { .count = ATOMIC_INIT(1) \
115 , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
116 , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
117 __DEBUG_MUTEX_INITIALIZER(lockname) \
118 __DEP_MAP_MUTEX_INITIALIZER(lockname) }
120 #define DEFINE_MUTEX(mutexname) \
121 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
123 extern void __mutex_init(struct mutex *lock, const char *name,
124 struct lock_class_key *key);
127 * mutex_is_locked - is the mutex locked
128 * @lock: the mutex to be queried
130 * Returns 1 if the mutex is locked, 0 if unlocked.
132 static inline int mutex_is_locked(struct mutex *lock)
134 return atomic_read(&lock->count) != 1;
138 * See kernel/locking/mutex.c for detailed documentation of these APIs.
139 * Also see Documentation/locking/mutex-design.txt.
141 #ifdef CONFIG_DEBUG_LOCK_ALLOC
142 extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
143 extern void _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
145 extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock,
146 unsigned int subclass);
147 extern int __must_check mutex_lock_killable_nested(struct mutex *lock,
148 unsigned int subclass);
150 #define mutex_lock(lock) mutex_lock_nested(lock, 0)
151 #define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
152 #define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0)
154 #define mutex_lock_nest_lock(lock, nest_lock) \
156 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
157 _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
161 extern void mutex_lock(struct mutex *lock);
162 extern int __must_check mutex_lock_interruptible(struct mutex *lock);
163 extern int __must_check mutex_lock_killable(struct mutex *lock);
165 # define mutex_lock_nested(lock, subclass) mutex_lock(lock)
166 # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
167 # define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock)
168 # define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
172 * NOTE: mutex_trylock() follows the spin_trylock() convention,
173 * not the down_trylock() convention!
175 * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
177 extern int mutex_trylock(struct mutex *lock);
178 extern void mutex_unlock(struct mutex *lock);
180 #endif /* !PREEMPT_RT_FULL */
182 extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
184 #endif /* __LINUX_MUTEX_H */