1
2
3
4
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#include <linux/err.h>
18#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
19#include <linux/osq_lock.h>
20#endif
21
22struct rw_semaphore;
23
24#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
25#include <linux/rwsem-spinlock.h>
26#define __RWSEM_INIT_COUNT(name) .count = RWSEM_UNLOCKED_VALUE
27#else
28
29struct rw_semaphore {
30 atomic_long_t count;
31 struct list_head wait_list;
32 raw_spinlock_t wait_lock;
33#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
34 struct optimistic_spin_queue osq;
35
36
37
38
39 struct task_struct *owner;
40#endif
41#ifdef CONFIG_DEBUG_LOCK_ALLOC
42 struct lockdep_map dep_map;
43#endif
44};
45
46extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
47extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
48extern struct rw_semaphore *rwsem_down_write_failed_killable(struct rw_semaphore *sem);
49extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
50extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
51
52
53#include <asm/rwsem.h>
54
55
56static inline int rwsem_is_locked(struct rw_semaphore *sem)
57{
58 return atomic_long_read(&sem->count) != 0;
59}
60
61#define __RWSEM_INIT_COUNT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE)
62#endif
63
64
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 { __RWSEM_INIT_COUNT(name), \
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
88extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
89 struct lock_class_key *key);
90
91#define init_rwsem(sem) \
92do { \
93 static struct lock_class_key __key; \
94 \
95 __init_rwsem((sem), #sem, &__key); \
96} while (0)
97
98
99
100
101
102
103
104static inline int rwsem_is_contended(struct rw_semaphore *sem)
105{
106 return !list_empty(&sem->wait_list);
107}
108
109
110
111
112extern void down_read(struct rw_semaphore *sem);
113
114
115
116
117extern int down_read_trylock(struct rw_semaphore *sem);
118
119
120
121
122extern void down_write(struct rw_semaphore *sem);
123extern int __must_check down_write_killable(struct rw_semaphore *sem);
124
125
126
127
128extern int down_write_trylock(struct rw_semaphore *sem);
129
130
131
132
133extern void up_read(struct rw_semaphore *sem);
134
135
136
137
138extern void up_write(struct rw_semaphore *sem);
139
140
141
142
143extern void downgrade_write(struct rw_semaphore *sem);
144
145#ifdef CONFIG_DEBUG_LOCK_ALLOC
146
147
148
149
150
151
152
153
154
155
156
157
158
159extern void down_read_nested(struct rw_semaphore *sem, int subclass);
160extern void down_write_nested(struct rw_semaphore *sem, int subclass);
161extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass);
162extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
163
164# define down_write_nest_lock(sem, nest_lock) \
165do { \
166 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
167 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
168} while (0);
169
170
171
172
173
174
175
176extern void down_read_non_owner(struct rw_semaphore *sem);
177extern void up_read_non_owner(struct rw_semaphore *sem);
178#else
179# define down_read_nested(sem, subclass) down_read(sem)
180# define down_write_nest_lock(sem, nest_lock) down_write(sem)
181# define down_write_nested(sem, subclass) down_write(sem)
182# define down_write_killable_nested(sem, subclass) down_write_killable(sem)
183# define down_read_non_owner(sem) down_read(sem)
184# define up_read_non_owner(sem) up_read(sem)
185#endif
186
187#endif
188