Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / include / linux / mutex.h
1 /*
2  * Mutexes: blocking mutual exclusion locks
3  *
4  * started by Ingo Molnar:
5  *
6  *  Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
7  *
8  * This file contains the main data structure and API definitions.
9  */
10 #ifndef __LINUX_MUTEX_H
11 #define __LINUX_MUTEX_H
12
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>
21
22 #ifdef CONFIG_DEBUG_LOCK_ALLOC
23 # define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
24         , .dep_map = { .name = #lockname }
25 #else
26 # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
27 #endif
28
29 #ifdef CONFIG_PREEMPT_RT_FULL
30 # include <linux/mutex_rt.h>
31 #else
32
33 /*
34  * Simple, straightforward mutexes with strict semantics:
35  *
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
47  *
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:
52  *
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
56  * - owner tracking
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)
60  */
61 struct mutex {
62         /* 1: unlocked, 0: locked, negative: locked, possible waiters */
63         atomic_t                count;
64         spinlock_t              wait_lock;
65         struct list_head        wait_list;
66 #if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER)
67         struct task_struct      *owner;
68 #endif
69 #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
70         struct optimistic_spin_queue osq; /* Spinner MCS lock */
71 #endif
72 #ifdef CONFIG_DEBUG_MUTEXES
73         void                    *magic;
74 #endif
75 #ifdef CONFIG_DEBUG_LOCK_ALLOC
76         struct lockdep_map      dep_map;
77 #endif
78 };
79
80 /*
81  * This is the control structure for tasks blocked on mutex,
82  * which resides on the blocked task's kernel stack:
83  */
84 struct mutex_waiter {
85         struct list_head        list;
86         struct task_struct      *task;
87 #ifdef CONFIG_DEBUG_MUTEXES
88         void                    *magic;
89 #endif
90 };
91
92 #ifdef CONFIG_DEBUG_MUTEXES
93 # include <linux/mutex-debug.h>
94 #else
95 # define __DEBUG_MUTEX_INITIALIZER(lockname)
96 /**
97  * mutex_init - initialize the mutex
98  * @mutex: the mutex to be initialized
99  *
100  * Initialize the mutex to unlocked state.
101  *
102  * It is not allowed to initialize an already locked mutex.
103  */
104 # define mutex_init(mutex) \
105 do {                                                    \
106         static struct lock_class_key __key;             \
107                                                         \
108         __mutex_init((mutex), #mutex, &__key);          \
109 } while (0)
110 static inline void mutex_destroy(struct mutex *lock) {}
111 #endif
112
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) }
119
120 #define DEFINE_MUTEX(mutexname) \
121         struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
122
123 extern void __mutex_init(struct mutex *lock, const char *name,
124                          struct lock_class_key *key);
125
126 /**
127  * mutex_is_locked - is the mutex locked
128  * @lock: the mutex to be queried
129  *
130  * Returns 1 if the mutex is locked, 0 if unlocked.
131  */
132 static inline int mutex_is_locked(struct mutex *lock)
133 {
134         return atomic_read(&lock->count) != 1;
135 }
136
137 /*
138  * See kernel/locking/mutex.c for detailed documentation of these APIs.
139  * Also see Documentation/locking/mutex-design.txt.
140  */
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);
144
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);
149
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)
153
154 #define mutex_lock_nest_lock(lock, nest_lock)                           \
155 do {                                                                    \
156         typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
157         _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map);             \
158 } while (0)
159
160 #else
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);
164
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)
169 #endif
170
171 /*
172  * NOTE: mutex_trylock() follows the spin_trylock() convention,
173  *       not the down_trylock() convention!
174  *
175  * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
176  */
177 extern int mutex_trylock(struct mutex *lock);
178 extern void mutex_unlock(struct mutex *lock);
179
180 #endif /* !PREEMPT_RT_FULL */
181
182 extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
183
184 #endif /* __LINUX_MUTEX_H */