1 /* rwsem.h: R/W semaphores, public interface
3 * Written by David Howells (dhowells@redhat.com).
4 * Derived from asm-i386/semaphore.h
10 #include <linux/linkage.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/list.h>
15 #include <linux/spinlock.h>
17 #include <asm/system.h>
18 #include <asm/atomic.h>
22 #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
23 #include <linux/rwsem-spinlock.h> /* use a generic implementation */
25 /* All arch specific implementations share the same struct */
29 struct list_head wait_list
;
30 #ifdef CONFIG_DEBUG_LOCK_ALLOC
31 struct lockdep_map dep_map
;
35 extern struct rw_semaphore
*rwsem_down_read_failed(struct rw_semaphore
*sem
);
36 extern struct rw_semaphore
*rwsem_down_write_failed(struct rw_semaphore
*sem
);
37 extern struct rw_semaphore
*rwsem_wake(struct rw_semaphore
*);
38 extern struct rw_semaphore
*rwsem_downgrade_wake(struct rw_semaphore
*sem
);
40 /* Include the arch specific part */
41 #include <asm/rwsem.h>
43 /* In all implementations count != 0 means locked */
44 static inline int rwsem_is_locked(struct rw_semaphore
*sem
)
46 return sem
->count
!= 0;
51 /* Common initializer macros and functions */
53 #ifdef CONFIG_DEBUG_LOCK_ALLOC
54 # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
56 # define __RWSEM_DEP_MAP_INIT(lockname)
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) }
63 #define DECLARE_RWSEM(name) \
64 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
66 extern void __init_rwsem(struct rw_semaphore
*sem
, const char *name
,
67 struct lock_class_key
*key
);
69 #define init_rwsem(sem) \
71 static struct lock_class_key __key; \
73 __init_rwsem((sem), #sem, &__key); \
79 extern void down_read(struct rw_semaphore
*sem
);
82 * trylock for reading -- returns 1 if successful, 0 if contention
84 extern int down_read_trylock(struct rw_semaphore
*sem
);
89 extern void down_write(struct rw_semaphore
*sem
);
92 * trylock for writing -- returns 1 if successful, 0 if contention
94 extern int down_write_trylock(struct rw_semaphore
*sem
);
99 extern void up_read(struct rw_semaphore
*sem
);
102 * release a write lock
104 extern void up_write(struct rw_semaphore
*sem
);
107 * downgrade write lock to read lock
109 extern void downgrade_write(struct rw_semaphore
*sem
);
111 #ifdef CONFIG_DEBUG_LOCK_ALLOC
113 * 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.)
125 extern void down_read_nested(struct rw_semaphore
*sem
, int subclass
);
126 extern void down_write_nested(struct rw_semaphore
*sem
, int subclass
);
128 * Take/release a lock when not the owner will release it.
130 * [ This API should be avoided as much as possible - the
131 * proper abstraction for this case is completions. ]
133 extern void down_read_non_owner(struct rw_semaphore
*sem
);
134 extern void up_read_non_owner(struct rw_semaphore
*sem
);
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)
142 #endif /* _LINUX_RWSEM_H */