blob: 8970c3e802e21b65819d2f19120bc09a06466d99 [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
35/* Include the arch specific part */
36#include <asm/rwsem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#endif
38
Thomas Gleixner12249b32011-01-26 20:06:00 +000039/* Common initializer macros and functions */
40
41#ifdef CONFIG_DEBUG_LOCK_ALLOC
42# define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
43#else
44# define __RWSEM_DEP_MAP_INIT(lockname)
45#endif
46
47#define __RWSEM_INITIALIZER(name) \
48 { RWSEM_UNLOCKED_VALUE, __SPIN_LOCK_UNLOCKED(name.wait_lock), \
49 LIST_HEAD_INIT((name).wait_list) __RWSEM_DEP_MAP_INIT(name) }
50
51#define DECLARE_RWSEM(name) \
52 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
53
54extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
55 struct lock_class_key *key);
56
57#define init_rwsem(sem) \
58do { \
59 static struct lock_class_key __key; \
60 \
61 __init_rwsem((sem), #sem, &__key); \
62} while (0)
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
65 * lock for reading
66 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070067extern void down_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/*
70 * trylock for reading -- returns 1 if successful, 0 if contention
71 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070072extern int down_read_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/*
75 * lock for writing
76 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070077extern void down_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/*
80 * trylock for writing -- returns 1 if successful, 0 if contention
81 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070082extern int down_write_trylock(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/*
85 * release a read lock
86 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070087extern void up_read(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89/*
90 * release a write lock
91 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070092extern void up_write(struct rw_semaphore *sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/*
95 * downgrade write lock to read lock
96 */
Ingo Molnar4ea21762006-07-03 00:24:53 -070097extern void downgrade_write(struct rw_semaphore *sem);
98
99#ifdef CONFIG_DEBUG_LOCK_ALLOC
100/*
Ingo Molnar5fca80e2006-07-10 04:44:02 -0700101 * nested locking. NOTE: rwsems are not allowed to recurse
102 * (which occurs if the same task tries to acquire the same
103 * lock instance multiple times), but multiple locks of the
104 * same lock class might be taken, if the order of the locks
105 * is always the same. This ordering rule can be expressed
106 * to lockdep via the _nested() APIs, but enumerating the
107 * subclasses that are used. (If the nesting relationship is
108 * static then another method for expressing nested locking is
109 * the explicit definition of lock class keys and the use of
110 * lockdep_set_class() at lock initialization time.
111 * See Documentation/lockdep-design.txt for more details.)
Ingo Molnar4ea21762006-07-03 00:24:53 -0700112 */
113extern void down_read_nested(struct rw_semaphore *sem, int subclass);
114extern void down_write_nested(struct rw_semaphore *sem, int subclass);
115/*
Ingo Molnar5fca80e2006-07-10 04:44:02 -0700116 * Take/release a lock when not the owner will release it.
117 *
118 * [ This API should be avoided as much as possible - the
119 * proper abstraction for this case is completions. ]
Ingo Molnar4ea21762006-07-03 00:24:53 -0700120 */
121extern void down_read_non_owner(struct rw_semaphore *sem);
122extern void up_read_non_owner(struct rw_semaphore *sem);
123#else
124# define down_read_nested(sem, subclass) down_read(sem)
125# define down_write_nested(sem, subclass) down_write(sem)
126# define down_read_non_owner(sem) down_read(sem)
127# define up_read_non_owner(sem) up_read(sem)
128#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#endif /* _LINUX_RWSEM_H */