This source file includes following definitions.
- rwsem_is_locked
- rwsem_is_contended
1
2
3
4
5
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
24
25
26
27
28
29
30
31
32
33
34
35 struct rw_semaphore {
36 atomic_long_t count;
37
38
39
40
41
42 atomic_long_t owner;
43 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
44 struct optimistic_spin_queue osq;
45 #endif
46 raw_spinlock_t wait_lock;
47 struct list_head wait_list;
48 #ifdef CONFIG_DEBUG_RWSEMS
49 void *magic;
50 #endif
51 #ifdef CONFIG_DEBUG_LOCK_ALLOC
52 struct lockdep_map dep_map;
53 #endif
54 };
55
56
57
58
59
60 #define RWSEM_OWNER_UNKNOWN (-2L)
61
62
63 static inline int rwsem_is_locked(struct rw_semaphore *sem)
64 {
65 return atomic_long_read(&sem->count) != 0;
66 }
67
68 #define RWSEM_UNLOCKED_VALUE 0L
69 #define __RWSEM_INIT_COUNT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
70
71
72
73 #ifdef CONFIG_DEBUG_LOCK_ALLOC
74 # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
75 #else
76 # define __RWSEM_DEP_MAP_INIT(lockname)
77 #endif
78
79 #ifdef CONFIG_DEBUG_RWSEMS
80 # define __DEBUG_RWSEM_INITIALIZER(lockname) , .magic = &lockname
81 #else
82 # define __DEBUG_RWSEM_INITIALIZER(lockname)
83 #endif
84
85 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
86 #define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED
87 #else
88 #define __RWSEM_OPT_INIT(lockname)
89 #endif
90
91 #define __RWSEM_INITIALIZER(name) \
92 { __RWSEM_INIT_COUNT(name), \
93 .owner = ATOMIC_LONG_INIT(0), \
94 .wait_list = LIST_HEAD_INIT((name).wait_list), \
95 .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \
96 __RWSEM_OPT_INIT(name) \
97 __DEBUG_RWSEM_INITIALIZER(name) \
98 __RWSEM_DEP_MAP_INIT(name) }
99
100 #define DECLARE_RWSEM(name) \
101 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
102
103 extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
104 struct lock_class_key *key);
105
106 #define init_rwsem(sem) \
107 do { \
108 static struct lock_class_key __key; \
109 \
110 __init_rwsem((sem), #sem, &__key); \
111 } while (0)
112
113
114
115
116
117
118
119 static inline int rwsem_is_contended(struct rw_semaphore *sem)
120 {
121 return !list_empty(&sem->wait_list);
122 }
123
124
125
126
127 extern void down_read(struct rw_semaphore *sem);
128 extern int __must_check down_read_killable(struct rw_semaphore *sem);
129
130
131
132
133 extern int down_read_trylock(struct rw_semaphore *sem);
134
135
136
137
138 extern void down_write(struct rw_semaphore *sem);
139 extern int __must_check down_write_killable(struct rw_semaphore *sem);
140
141
142
143
144 extern int down_write_trylock(struct rw_semaphore *sem);
145
146
147
148
149 extern void up_read(struct rw_semaphore *sem);
150
151
152
153
154 extern void up_write(struct rw_semaphore *sem);
155
156
157
158
159 extern void downgrade_write(struct rw_semaphore *sem);
160
161 #ifdef CONFIG_DEBUG_LOCK_ALLOC
162
163
164
165
166
167
168
169
170
171
172
173
174
175 extern void down_read_nested(struct rw_semaphore *sem, int subclass);
176 extern void down_write_nested(struct rw_semaphore *sem, int subclass);
177 extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
178 extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
179
180 # define down_write_nest_lock(sem, nest_lock) \
181 do { \
182 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
183 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
184 } while (0);
185
186
187
188
189
190
191
192 extern void down_read_non_owner(struct rw_semaphore *sem);
193 extern void up_read_non_owner(struct rw_semaphore *sem);
194 #else
195 # define down_read_nested(sem, subclass) down_read(sem)
196 # define down_write_nest_lock(sem, nest_lock) down_write(sem)
197 # define down_write_nested(sem, subclass) down_write(sem)
198 # define down_write_killable_nested(sem, subclass) down_write_killable(sem)
199 # define down_read_non_owner(sem) down_read(sem)
200 # define up_read_non_owner(sem) up_read(sem)
201 #endif
202
203 #endif