blob: a8afe9cd000c08bf06b394eab36e84d424307eb7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/types.h>
13#include <linux/kernel.h>
Thomas Gleixnerc16a87c2011-01-26 20:05:50 +000014#include <linux/list.h>
15#include <linux/spinlock.h>
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/system.h>
18#include <asm/atomic.h>
19
20struct rw_semaphore;
21
22#ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
23#include <linux/rwsem-spinlock.h> /* use a generic implementation */
24#else
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000025/* All arch specific implementations share the same struct */
26struct rw_semaphore {
27 long count;
28 spinlock_t wait_lock;
29 struct list_head wait_list;
30#ifdef CONFIG_DEBUG_LOCK_ALLOC
31 struct lockdep_map dep_map;
32#endif
33};
34
Thomas Gleixnerd1233752011-01-26 21:32:01 +010035extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
36extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
37extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
38extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
Thomas Gleixneraac72272011-01-26 20:06:06 +000039
Thomas Gleixner1c8ed642011-01-26 20:05:56 +000040/* Include the arch specific part */
41#include <asm/rwsem.h>
Thomas Gleixner41e58872011-01-26 20:06:03 +000042
43/* In all implementations count != 0 means locked */
44static inline int rwsem_is_locked(struct rw_semaphore *sem)
45{
46 return sem->count != 0;
47}
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#endif
50
Thomas Gleixner12249b32011-01-26 20:06:00 +000051/* Common initializer macros and functions */
52
53#ifdef CONFIG_DEBUG_LOCK_ALLOC
54# define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
55#else
56# define __RWSEM_DEP_MAP_INIT(lockname)
57#endif
58
59#define __RWSEM_INITIALIZER(name) \
60 { RWSEM_UNLOCKED_VALUE, __SPIN_LOCK_UNLOCKED(name.wait_lock), \
61 LIST_HEAD_INIT((name).wait_list) __RWSEM_DEP_MAP_INIT(name) }
62
63#define DECLARE_RWSEM(name) \
64 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
65
66extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
67 struct lock_class_key *key);
68
69#define init_rwsem(sem) \
70do { \
71 static struct lock_class_key __key; \
72 \
73 __init_rwsem((sem), #sem, &__key); \
74} while (0)
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/*
77 * lock for reading
78 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070079extern void down_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/*
82 * trylock for reading -- returns 1 if successful, 0 if contention
83 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070084extern int down_read_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*
87 * lock for writing
88 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070089extern void down_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
92 * trylock for writing -- returns 1 if successful, 0 if contention
93 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070094extern int down_write_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/*
97 * release a read lock
98 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070099extern void up_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/*
102 * release a write lock
103 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700104extern void up_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/*
107 * downgrade write lock to read lock
108 */
Ingo Molnar4ea21762006-07-03 00:24:53 -0700109extern void downgrade_write(struct rw_semaphore *sem);
110
111#ifdef CONFIG_DEBUG_LOCK_ALLOC
112/*
Ingo Molnar5fca80e2006-07-10 04:44:02 -0700113 * nested locking. NOTE: rwsems are not allowed to recurse
114 * (which occurs if the same task tries to acquire the same
115 * lock instance multiple times), but multiple locks of the
116 * same lock class might be taken, if the order of the locks
117 * is always the same. This ordering rule can be expressed
118 * to lockdep via the _nested() APIs, but enumerating the
119 * subclasses that are used. (If the nesting relationship is
120 * static then another method for expressing nested locking is
121 * the explicit definition of lock class keys and the use of
122 * lockdep_set_class() at lock initialization time.
123 * See Documentation/lockdep-design.txt for more details.)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700124 */
125extern void down_read_nested(struct rw_semaphore *sem, int subclass);
126extern void down_write_nested(struct rw_semaphore *sem, int subclass);
127/*
Ingo Molnar5fca80e2006-07-10 04:44:02 -0700128 * Take/release a lock when not the owner will release it.
129 *
130 * [ This API should be avoided as much as possible - the
131 * proper abstraction for this case is completions. ]
Ingo Molnar4ea21762006-07-03 00:24:53 -0700132 */
133extern void down_read_non_owner(struct rw_semaphore *sem);
134extern void up_read_non_owner(struct rw_semaphore *sem);
135#else
136# define down_read_nested(sem, subclass) down_read(sem)
137# define down_write_nested(sem, subclass) down_write(sem)
138# define down_read_non_owner(sem) down_read(sem)
139# define up_read_non_owner(sem) up_read(sem)
140#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#endif /* _LINUX_RWSEM_H */