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
35struct 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
57static 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_COUNT_INIT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
64
65
66
67#ifdef CONFIG_DEBUG_LOCK_ALLOC
68# define __RWSEM_DEP_MAP_INIT(lockname) \
69 .dep_map = { \
70 .name = #lockname, \
71 .wait_type_inner = LD_WAIT_SLEEP, \
72 },
73#else
74# define __RWSEM_DEP_MAP_INIT(lockname)
75#endif
76
77#ifdef CONFIG_DEBUG_RWSEMS
78# define __RWSEM_DEBUG_INIT(lockname) .magic = &lockname,
79#else
80# define __RWSEM_DEBUG_INIT(lockname)
81#endif
82
83#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
84#define __RWSEM_OPT_INIT(lockname) .osq = OSQ_LOCK_UNLOCKED,
85#else
86#define __RWSEM_OPT_INIT(lockname)
87#endif
88
89#define __RWSEM_INITIALIZER(name) \
90 { __RWSEM_COUNT_INIT(name), \
91 .owner = ATOMIC_LONG_INIT(0), \
92 __RWSEM_OPT_INIT(name) \
93 .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock),\
94 .wait_list = LIST_HEAD_INIT((name).wait_list), \
95 __RWSEM_DEBUG_INIT(name) \
96 __RWSEM_DEP_MAP_INIT(name) }
97
98#define DECLARE_RWSEM(name) \
99 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
100
101extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
102 struct lock_class_key *key);
103
104#define init_rwsem(sem) \
105do { \
106 static struct lock_class_key __key; \
107 \
108 __init_rwsem((sem), #sem, &__key); \
109} while (0)
110
111
112
113
114
115
116
117static inline int rwsem_is_contended(struct rw_semaphore *sem)
118{
119 return !list_empty(&sem->wait_list);
120}
121
122
123
124
125extern void down_read(struct rw_semaphore *sem);
126extern int __must_check down_read_killable(struct rw_semaphore *sem);
127
128
129
130
131extern int down_read_trylock(struct rw_semaphore *sem);
132
133
134
135
136extern void down_write(struct rw_semaphore *sem);
137extern int __must_check down_write_killable(struct rw_semaphore *sem);
138
139
140
141
142extern int down_write_trylock(struct rw_semaphore *sem);
143
144
145
146
147extern void up_read(struct rw_semaphore *sem);
148
149
150
151
152extern void up_write(struct rw_semaphore *sem);
153
154
155
156
157extern void downgrade_write(struct rw_semaphore *sem);
158
159#ifdef CONFIG_DEBUG_LOCK_ALLOC
160
161
162
163
164
165
166
167
168
169
170
171
172
173extern void down_read_nested(struct rw_semaphore *sem, int subclass);
174extern void down_write_nested(struct rw_semaphore *sem, int subclass);
175extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
176extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
177
178# define down_write_nest_lock(sem, nest_lock) \
179do { \
180 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
181 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
182} while (0);
183
184
185
186
187
188
189
190extern void down_read_non_owner(struct rw_semaphore *sem);
191extern void up_read_non_owner(struct rw_semaphore *sem);
192#else
193# define down_read_nested(sem, subclass) down_read(sem)
194# define down_write_nest_lock(sem, nest_lock) down_write(sem)
195# define down_write_nested(sem, subclass) down_write(sem)
196# define down_write_killable_nested(sem, subclass) down_write_killable(sem)
197# define down_read_non_owner(sem) down_read(sem)
198# define up_read_non_owner(sem) up_read(sem)
199#endif
200
201#endif
202