blob: 1dc129ac2feb0aab36a2111c419647e851db7359 [file] [log] [blame]
Adrian Bunkb00dc832008-05-19 16:52:27 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * rwsem.h: R/W semaphores implemented using CAS
3 *
4 * Written by David S. Miller (davem@redhat.com), 2001.
5 * Derived from asm-i386/rwsem.h
6 */
7#ifndef _SPARC64_RWSEM_H
8#define _SPARC64_RWSEM_H
9
10#ifndef _LINUX_RWSEM_H
11#error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
12#endif
13
14#ifdef __KERNEL__
15
16#include <linux/list.h>
17#include <linux/spinlock.h>
18#include <asm/rwsem-const.h>
19
20struct rwsem_waiter;
21
22struct rw_semaphore {
23 signed int count;
24 spinlock_t wait_lock;
25 struct list_head wait_list;
David S. Miller10e26722006-11-16 13:38:57 -080026#ifdef CONFIG_DEBUG_LOCK_ALLOC
27 struct lockdep_map dep_map;
28#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070029};
30
David S. Miller10e26722006-11-16 13:38:57 -080031#ifdef CONFIG_DEBUG_LOCK_ALLOC
32# define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname }
33#else
34# define __RWSEM_DEP_MAP_INIT(lockname)
35#endif
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define __RWSEM_INITIALIZER(name) \
David S. Miller10e26722006-11-16 13:38:57 -080038{ RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, LIST_HEAD_INIT((name).wait_list) \
39 __RWSEM_DEP_MAP_INIT(name) }
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#define DECLARE_RWSEM(name) \
42 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
43
David S. Miller10e26722006-11-16 13:38:57 -080044extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
45 struct lock_class_key *key);
46
47#define init_rwsem(sem) \
48do { \
49 static struct lock_class_key __key; \
50 \
51 __init_rwsem((sem), #sem, &__key); \
52} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54extern void __down_read(struct rw_semaphore *sem);
55extern int __down_read_trylock(struct rw_semaphore *sem);
56extern void __down_write(struct rw_semaphore *sem);
57extern int __down_write_trylock(struct rw_semaphore *sem);
58extern void __up_read(struct rw_semaphore *sem);
59extern void __up_write(struct rw_semaphore *sem);
60extern void __downgrade_write(struct rw_semaphore *sem);
61
David S. Miller10e26722006-11-16 13:38:57 -080062static inline void __down_write_nested(struct rw_semaphore *sem, int subclass)
63{
64 __down_write(sem);
65}
66
David S. Miller620de542005-07-24 19:35:42 -070067static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
David S. Miller620de542005-07-24 19:35:42 -070069 return atomic_add_return(delta, (atomic_t *)(&sem->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
David S. Miller620de542005-07-24 19:35:42 -070072static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
David S. Miller620de542005-07-24 19:35:42 -070074 atomic_add(delta, (atomic_t *)(&sem->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Rik Van Rieleb92f4e2005-10-29 18:15:44 -070077static inline int rwsem_is_locked(struct rw_semaphore *sem)
78{
79 return (sem->count != 0);
80}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#endif /* __KERNEL__ */
83
84#endif /* _SPARC64_RWSEM_H */