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