Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / include / linux / rwsem.h
1 /* rwsem.h: R/W semaphores, public interface
2  *
3  * Written by David Howells (dhowells@redhat.com).
4  * Derived from asm-i386/semaphore.h
5  */
6
7 #ifndef _LINUX_RWSEM_H
8 #define _LINUX_RWSEM_H
9
10 #include <linux/linkage.h>
11
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/list.h>
15 #include <linux/spinlock.h>
16 #include <linux/atomic.h>
17 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
18 #include <linux/osq_lock.h>
19 #endif
20
21 #ifdef CONFIG_PREEMPT_RT_FULL
22 #include <linux/rwsem_rt.h>
23 #else /* PREEMPT_RT_FULL */
24
25 struct rw_semaphore;
26
27 #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
28 #include <linux/rwsem-spinlock.h> /* use a generic implementation */
29 #else
30 /* All arch specific implementations share the same struct */
31 struct rw_semaphore {
32         long count;
33         struct list_head wait_list;
34         raw_spinlock_t wait_lock;
35 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
36         struct optimistic_spin_queue osq; /* spinner MCS lock */
37         /*
38          * Write owner. Used as a speculative check to see
39          * if the owner is running on the cpu.
40          */
41         struct task_struct *owner;
42 #endif
43 #ifdef CONFIG_DEBUG_LOCK_ALLOC
44         struct lockdep_map      dep_map;
45 #endif
46 };
47
48 extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
49 extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
50 extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
51 extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
52
53 /* Include the arch specific part */
54 #include <asm/rwsem.h>
55
56 /* In all implementations count != 0 means locked */
57 static inline int rwsem_is_locked(struct rw_semaphore *sem)
58 {
59         return sem->count != 0;
60 }
61
62 #endif
63
64 /* Common initializer macros and functions */
65
66 #ifdef CONFIG_DEBUG_LOCK_ALLOC
67 # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
68 #else
69 # define __RWSEM_DEP_MAP_INIT(lockname)
70 #endif
71
72 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
73 #define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL
74 #else
75 #define __RWSEM_OPT_INIT(lockname)
76 #endif
77
78 #define __RWSEM_INITIALIZER(name)                               \
79         { .count = RWSEM_UNLOCKED_VALUE,                        \
80           .wait_list = LIST_HEAD_INIT((name).wait_list),        \
81           .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \
82           __RWSEM_OPT_INIT(name)                                \
83           __RWSEM_DEP_MAP_INIT(name) }
84
85 #define DECLARE_RWSEM(name) \
86         struct rw_semaphore name = __RWSEM_INITIALIZER(name)
87
88 extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
89                          struct lock_class_key *key);
90
91 #define init_rwsem(sem)                                         \
92 do {                                                            \
93         static struct lock_class_key __key;                     \
94                                                                 \
95         __init_rwsem((sem), #sem, &__key);                      \
96 } while (0)
97
98 /*
99  * This is the same regardless of which rwsem implementation that is being used.
100  * It is just a heuristic meant to be called by somebody alreadying holding the
101  * rwsem to see if somebody from an incompatible type is wanting access to the
102  * lock.
103  */
104 static inline int rwsem_is_contended(struct rw_semaphore *sem)
105 {
106         return !list_empty(&sem->wait_list);
107 }
108
109 /*
110  * lock for reading
111  */
112 extern void down_read(struct rw_semaphore *sem);
113
114 /*
115  * trylock for reading -- returns 1 if successful, 0 if contention
116  */
117 extern int down_read_trylock(struct rw_semaphore *sem);
118
119 /*
120  * lock for writing
121  */
122 extern void down_write(struct rw_semaphore *sem);
123
124 /*
125  * trylock for writing -- returns 1 if successful, 0 if contention
126  */
127 extern int down_write_trylock(struct rw_semaphore *sem);
128
129 /*
130  * release a read lock
131  */
132 extern void up_read(struct rw_semaphore *sem);
133
134 /*
135  * release a write lock
136  */
137 extern void up_write(struct rw_semaphore *sem);
138
139 /*
140  * downgrade write lock to read lock
141  */
142 extern void downgrade_write(struct rw_semaphore *sem);
143
144 #ifdef CONFIG_DEBUG_LOCK_ALLOC
145 /*
146  * nested locking. NOTE: rwsems are not allowed to recurse
147  * (which occurs if the same task tries to acquire the same
148  * lock instance multiple times), but multiple locks of the
149  * same lock class might be taken, if the order of the locks
150  * is always the same. This ordering rule can be expressed
151  * to lockdep via the _nested() APIs, but enumerating the
152  * subclasses that are used. (If the nesting relationship is
153  * static then another method for expressing nested locking is
154  * the explicit definition of lock class keys and the use of
155  * lockdep_set_class() at lock initialization time.
156  * See Documentation/locking/lockdep-design.txt for more details.)
157  */
158 extern void down_read_nested(struct rw_semaphore *sem, int subclass);
159 extern void down_write_nested(struct rw_semaphore *sem, int subclass);
160 extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
161
162 # define down_write_nest_lock(sem, nest_lock)                   \
163 do {                                                            \
164         typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
165         _down_write_nest_lock(sem, &(nest_lock)->dep_map);      \
166 } while (0);
167
168 /*
169  * Take/release a lock when not the owner will release it.
170  *
171  * [ This API should be avoided as much as possible - the
172  *   proper abstraction for this case is completions. ]
173  */
174 extern void down_read_non_owner(struct rw_semaphore *sem);
175 extern void up_read_non_owner(struct rw_semaphore *sem);
176 #else
177 # define down_read_nested(sem, subclass)                down_read(sem)
178 # define down_write_nest_lock(sem, nest_lock)   down_write(sem)
179 # define down_write_nested(sem, subclass)       down_write(sem)
180 # define down_read_non_owner(sem)               down_read(sem)
181 # define up_read_non_owner(sem)                 up_read(sem)
182 #endif
183
184 #endif /* !PREEMPT_RT_FULL */
185
186 #endif /* _LINUX_RWSEM_H */