Merge branch 'devicetree/dtc' into devicetree/next
[deliverable/linux.git] / include / linux / rwsem.h
CommitLineData
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>
16
60063497 17#include <linux/atomic.h>
1da177e4
LT
18
19struct rw_semaphore;
20
21#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
22#include <linux/rwsem-spinlock.h> /* use a generic implementation */
23#else
1c8ed640
TG
24/* All arch specific implementations share the same struct */
25struct rw_semaphore {
26 long count;
ddb6c9b5 27 raw_spinlock_t wait_lock;
1c8ed640
TG
28 struct list_head wait_list;
29#ifdef CONFIG_DEBUG_LOCK_ALLOC
30 struct lockdep_map dep_map;
31#endif
32};
33
d1233754
TG
34extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
35extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
36extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
37extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
aac72277 38
1c8ed640
TG
39/* Include the arch specific part */
40#include <asm/rwsem.h>
41e5887f
TG
41
42/* In all implementations count != 0 means locked */
43static inline int rwsem_is_locked(struct rw_semaphore *sem)
44{
45 return sem->count != 0;
46}
47
1da177e4
LT
48#endif
49
12249b34
TG
50/* Common initializer macros and functions */
51
52#ifdef CONFIG_DEBUG_LOCK_ALLOC
53# define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
54#else
55# define __RWSEM_DEP_MAP_INIT(lockname)
56#endif
57
ddb6c9b5
TG
58#define __RWSEM_INITIALIZER(name) \
59 { RWSEM_UNLOCKED_VALUE, \
60 __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock), \
61 LIST_HEAD_INIT((name).wait_list) \
62 __RWSEM_DEP_MAP_INIT(name) }
12249b34
TG
63
64#define DECLARE_RWSEM(name) \
65 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
66
67extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
68 struct lock_class_key *key);
69
70#define init_rwsem(sem) \
71do { \
72 static struct lock_class_key __key; \
73 \
74 __init_rwsem((sem), #sem, &__key); \
75} while (0)
76
4a444b1f
JB
77/*
78 * This is the same regardless of which rwsem implementation that is being used.
79 * It is just a heuristic meant to be called by somebody alreadying holding the
80 * rwsem to see if somebody from an incompatible type is wanting access to the
81 * lock.
82 */
83static inline int rwsem_is_contended(struct rw_semaphore *sem)
84{
85 return !list_empty(&sem->wait_list);
86}
87
1da177e4
LT
88/*
89 * lock for reading
90 */
4ea2176d 91extern void down_read(struct rw_semaphore *sem);
1da177e4
LT
92
93/*
94 * trylock for reading -- returns 1 if successful, 0 if contention
95 */
4ea2176d 96extern int down_read_trylock(struct rw_semaphore *sem);
1da177e4
LT
97
98/*
99 * lock for writing
100 */
4ea2176d 101extern void down_write(struct rw_semaphore *sem);
1da177e4
LT
102
103/*
104 * trylock for writing -- returns 1 if successful, 0 if contention
105 */
4ea2176d 106extern int down_write_trylock(struct rw_semaphore *sem);
1da177e4
LT
107
108/*
109 * release a read lock
110 */
4ea2176d 111extern void up_read(struct rw_semaphore *sem);
1da177e4
LT
112
113/*
114 * release a write lock
115 */
4ea2176d 116extern void up_write(struct rw_semaphore *sem);
1da177e4
LT
117
118/*
119 * downgrade write lock to read lock
120 */
4ea2176d
IM
121extern void downgrade_write(struct rw_semaphore *sem);
122
123#ifdef CONFIG_DEBUG_LOCK_ALLOC
124/*
5fca80e8
IM
125 * nested locking. NOTE: rwsems are not allowed to recurse
126 * (which occurs if the same task tries to acquire the same
127 * lock instance multiple times), but multiple locks of the
128 * same lock class might be taken, if the order of the locks
129 * is always the same. This ordering rule can be expressed
130 * to lockdep via the _nested() APIs, but enumerating the
131 * subclasses that are used. (If the nesting relationship is
132 * static then another method for expressing nested locking is
133 * the explicit definition of lock class keys and the use of
134 * lockdep_set_class() at lock initialization time.
135 * See Documentation/lockdep-design.txt for more details.)
4ea2176d
IM
136 */
137extern void down_read_nested(struct rw_semaphore *sem, int subclass);
138extern void down_write_nested(struct rw_semaphore *sem, int subclass);
1b963c81
JK
139extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock);
140
141# define down_write_nest_lock(sem, nest_lock) \
142do { \
143 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
144 _down_write_nest_lock(sem, &(nest_lock)->dep_map); \
145} while (0);
146
84759c6d
KO
147/*
148 * Take/release a lock when not the owner will release it.
149 *
150 * [ This API should be avoided as much as possible - the
151 * proper abstraction for this case is completions. ]
152 */
153extern void down_read_non_owner(struct rw_semaphore *sem);
154extern void up_read_non_owner(struct rw_semaphore *sem);
4ea2176d
IM
155#else
156# define down_read_nested(sem, subclass) down_read(sem)
e65b9ad2 157# define down_write_nest_lock(sem, nest_lock) down_write(sem)
4ea2176d 158# define down_write_nested(sem, subclass) down_write(sem)
84759c6d
KO
159# define down_read_non_owner(sem) down_read(sem)
160# define up_read_non_owner(sem) up_read(sem)
4ea2176d 161#endif
1da177e4 162
1da177e4 163#endif /* _LINUX_RWSEM_H */
This page took 1.218317 seconds and 5 git commands to generate.