Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* $Id: rwsem.h,v 1.5 2001/11/18 00:12:56 davem Exp $ |
| 2 | * 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 | |
| 20 | struct rwsem_waiter; |
| 21 | |
| 22 | struct rw_semaphore { |
| 23 | signed int count; |
| 24 | spinlock_t wait_lock; |
| 25 | struct list_head wait_list; |
| 26 | }; |
| 27 | |
| 28 | #define __RWSEM_INITIALIZER(name) \ |
| 29 | { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, LIST_HEAD_INIT((name).wait_list) } |
| 30 | |
| 31 | #define DECLARE_RWSEM(name) \ |
| 32 | struct rw_semaphore name = __RWSEM_INITIALIZER(name) |
| 33 | |
| 34 | static __inline__ void init_rwsem(struct rw_semaphore *sem) |
| 35 | { |
| 36 | sem->count = RWSEM_UNLOCKED_VALUE; |
| 37 | spin_lock_init(&sem->wait_lock); |
| 38 | INIT_LIST_HEAD(&sem->wait_list); |
| 39 | } |
| 40 | |
| 41 | extern void __down_read(struct rw_semaphore *sem); |
| 42 | extern int __down_read_trylock(struct rw_semaphore *sem); |
| 43 | extern void __down_write(struct rw_semaphore *sem); |
| 44 | extern int __down_write_trylock(struct rw_semaphore *sem); |
| 45 | extern void __up_read(struct rw_semaphore *sem); |
| 46 | extern void __up_write(struct rw_semaphore *sem); |
| 47 | extern void __downgrade_write(struct rw_semaphore *sem); |
| 48 | |
David S. Miller | 620de54 | 2005-07-24 19:35:42 -0700 | [diff] [blame] | 49 | static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
David S. Miller | 620de54 | 2005-07-24 19:35:42 -0700 | [diff] [blame] | 51 | return atomic_add_return(delta, (atomic_t *)(&sem->count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
David S. Miller | 620de54 | 2005-07-24 19:35:42 -0700 | [diff] [blame] | 54 | static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
David S. Miller | 620de54 | 2005-07-24 19:35:42 -0700 | [diff] [blame] | 56 | atomic_add(delta, (atomic_t *)(&sem->count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Rik Van Riel | eb92f4e | 2005-10-29 18:15:44 -0700 | [diff] [blame] | 59 | static inline int rwsem_is_locked(struct rw_semaphore *sem) |
| 60 | { |
| 61 | return (sem->count != 0); |
| 62 | } |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | #endif /* __KERNEL__ */ |
| 65 | |
| 66 | #endif /* _SPARC64_RWSEM_H */ |