Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
1da177e4 LT |
12 | #include <linux/types.h> |
13 | #include <linux/kernel.h> | |
c16a87ce TG |
14 | #include <linux/list.h> |
15 | #include <linux/spinlock.h> | |
60063497 | 16 | #include <linux/atomic.h> |
d4799608 | 17 | #include <linux/err.h> |
5db6c6fe | 18 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
90631822 | 19 | #include <linux/osq_lock.h> |
5db6c6fe | 20 | #endif |
1da177e4 LT |
21 | |
22 | struct rw_semaphore; | |
23 | ||
24 | #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK | |
25 | #include <linux/rwsem-spinlock.h> /* use a generic implementation */ | |
26 | #else | |
1c8ed640 TG |
27 | /* All arch specific implementations share the same struct */ |
28 | struct rw_semaphore { | |
4fc828e2 | 29 | long count; |
4fc828e2 | 30 | struct list_head wait_list; |
ce069fc9 | 31 | raw_spinlock_t wait_lock; |
5db6c6fe | 32 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
ce069fc9 | 33 | struct optimistic_spin_queue osq; /* spinner MCS lock */ |
4fc828e2 DB |
34 | /* |
35 | * Write owner. Used as a speculative check to see | |
36 | * if the owner is running on the cpu. | |
37 | */ | |
38 | struct task_struct *owner; | |
4fc828e2 | 39 | #endif |
1c8ed640 TG |
40 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
41 | struct lockdep_map dep_map; | |
42 | #endif | |
43 | }; | |
44 | ||
d1233754 TG |
45 | extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem); |
46 | extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem); | |
d4799608 | 47 | extern struct rw_semaphore *rwsem_down_write_failed_killable(struct rw_semaphore *sem); |
d1233754 TG |
48 | extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *); |
49 | extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem); | |
aac72277 | 50 | |
1c8ed640 TG |
51 | /* Include the arch specific part */ |
52 | #include <asm/rwsem.h> | |
41e5887f TG |
53 | |
54 | /* In all implementations count != 0 means locked */ | |
55 | static inline int rwsem_is_locked(struct rw_semaphore *sem) | |
56 | { | |
57 | return sem->count != 0; | |
58 | } | |
59 | ||
1da177e4 LT |
60 | #endif |
61 | ||
12249b34 TG |
62 | /* Common initializer macros and functions */ |
63 | ||
64 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
65 | # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname } | |
66 | #else | |
67 | # define __RWSEM_DEP_MAP_INIT(lockname) | |
68 | #endif | |
69 | ||
5db6c6fe | 70 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
ce069fc9 | 71 | #define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL |
4fc828e2 | 72 | #else |
ce069fc9 | 73 | #define __RWSEM_OPT_INIT(lockname) |
4fc828e2 | 74 | #endif |
12249b34 | 75 | |
ce069fc9 JL |
76 | #define __RWSEM_INITIALIZER(name) \ |
77 | { .count = RWSEM_UNLOCKED_VALUE, \ | |
78 | .wait_list = LIST_HEAD_INIT((name).wait_list), \ | |
79 | .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \ | |
80 | __RWSEM_OPT_INIT(name) \ | |
81 | __RWSEM_DEP_MAP_INIT(name) } | |
82 | ||
12249b34 TG |
83 | #define DECLARE_RWSEM(name) \ |
84 | struct rw_semaphore name = __RWSEM_INITIALIZER(name) | |
85 | ||
86 | extern void __init_rwsem(struct rw_semaphore *sem, const char *name, | |
87 | struct lock_class_key *key); | |
88 | ||
89 | #define init_rwsem(sem) \ | |
90 | do { \ | |
91 | static struct lock_class_key __key; \ | |
92 | \ | |
93 | __init_rwsem((sem), #sem, &__key); \ | |
94 | } while (0) | |
95 | ||
4a444b1f JB |
96 | /* |
97 | * This is the same regardless of which rwsem implementation that is being used. | |
98 | * It is just a heuristic meant to be called by somebody alreadying holding the | |
99 | * rwsem to see if somebody from an incompatible type is wanting access to the | |
100 | * lock. | |
101 | */ | |
102 | static inline int rwsem_is_contended(struct rw_semaphore *sem) | |
103 | { | |
104 | return !list_empty(&sem->wait_list); | |
105 | } | |
106 | ||
1da177e4 LT |
107 | /* |
108 | * lock for reading | |
109 | */ | |
4ea2176d | 110 | extern void down_read(struct rw_semaphore *sem); |
1da177e4 LT |
111 | |
112 | /* | |
113 | * trylock for reading -- returns 1 if successful, 0 if contention | |
114 | */ | |
4ea2176d | 115 | extern int down_read_trylock(struct rw_semaphore *sem); |
1da177e4 LT |
116 | |
117 | /* | |
118 | * lock for writing | |
119 | */ | |
4ea2176d | 120 | extern void down_write(struct rw_semaphore *sem); |
916633a4 | 121 | extern int __must_check down_write_killable(struct rw_semaphore *sem); |
1da177e4 LT |
122 | |
123 | /* | |
124 | * trylock for writing -- returns 1 if successful, 0 if contention | |
125 | */ | |
4ea2176d | 126 | extern int down_write_trylock(struct rw_semaphore *sem); |
1da177e4 LT |
127 | |
128 | /* | |
129 | * release a read lock | |
130 | */ | |
4ea2176d | 131 | extern void up_read(struct rw_semaphore *sem); |
1da177e4 LT |
132 | |
133 | /* | |
134 | * release a write lock | |
135 | */ | |
4ea2176d | 136 | extern void up_write(struct rw_semaphore *sem); |
1da177e4 LT |
137 | |
138 | /* | |
139 | * downgrade write lock to read lock | |
140 | */ | |
4ea2176d IM |
141 | extern void downgrade_write(struct rw_semaphore *sem); |
142 | ||
143 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
144 | /* | |
5fca80e8 IM |
145 | * nested locking. NOTE: rwsems are not allowed to recurse |
146 | * (which occurs if the same task tries to acquire the same | |
147 | * lock instance multiple times), but multiple locks of the | |
148 | * same lock class might be taken, if the order of the locks | |
149 | * is always the same. This ordering rule can be expressed | |
150 | * to lockdep via the _nested() APIs, but enumerating the | |
151 | * subclasses that are used. (If the nesting relationship is | |
152 | * static then another method for expressing nested locking is | |
153 | * the explicit definition of lock class keys and the use of | |
154 | * lockdep_set_class() at lock initialization time. | |
214e0aed | 155 | * See Documentation/locking/lockdep-design.txt for more details.) |
4ea2176d IM |
156 | */ |
157 | extern void down_read_nested(struct rw_semaphore *sem, int subclass); | |
158 | extern void down_write_nested(struct rw_semaphore *sem, int subclass); | |
887bddfa | 159 | extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass); |
1b963c81 JK |
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 | ||
84759c6d KO |
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); | |
4ea2176d IM |
176 | #else |
177 | # define down_read_nested(sem, subclass) down_read(sem) | |
e65b9ad2 | 178 | # define down_write_nest_lock(sem, nest_lock) down_write(sem) |
4ea2176d | 179 | # define down_write_nested(sem, subclass) down_write(sem) |
887bddfa | 180 | # define down_write_killable_nested(sem, subclass) down_write_killable(sem) |
84759c6d KO |
181 | # define down_read_non_owner(sem) down_read(sem) |
182 | # define up_read_non_owner(sem) up_read(sem) | |
4ea2176d | 183 | #endif |
1da177e4 | 184 | |
1da177e4 | 185 | #endif /* _LINUX_RWSEM_H */ |