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; |
David S. Miller | 10e2672 | 2006-11-16 13:38:57 -0800 | [diff] [blame] | 26 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 27 | struct lockdep_map dep_map; |
| 28 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
David S. Miller | 10e2672 | 2006-11-16 13:38:57 -0800 | [diff] [blame] | 31 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #define __RWSEM_INITIALIZER(name) \ |
David S. Miller | 10e2672 | 2006-11-16 13:38:57 -0800 | [diff] [blame] | 38 | { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, LIST_HEAD_INIT((name).wait_list) \ |
| 39 | __RWSEM_DEP_MAP_INIT(name) } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | #define DECLARE_RWSEM(name) \ |
| 42 | struct rw_semaphore name = __RWSEM_INITIALIZER(name) |
| 43 | |
David S. Miller | 10e2672 | 2006-11-16 13:38:57 -0800 | [diff] [blame] | 44 | extern void __init_rwsem(struct rw_semaphore *sem, const char *name, |
| 45 | struct lock_class_key *key); |
| 46 | |
| 47 | #define init_rwsem(sem) \ |
| 48 | do { \ |
| 49 | static struct lock_class_key __key; \ |
| 50 | \ |
| 51 | __init_rwsem((sem), #sem, &__key); \ |
| 52 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | extern void __down_read(struct rw_semaphore *sem); |
| 55 | extern int __down_read_trylock(struct rw_semaphore *sem); |
| 56 | extern void __down_write(struct rw_semaphore *sem); |
| 57 | extern int __down_write_trylock(struct rw_semaphore *sem); |
| 58 | extern void __up_read(struct rw_semaphore *sem); |
| 59 | extern void __up_write(struct rw_semaphore *sem); |
| 60 | extern void __downgrade_write(struct rw_semaphore *sem); |
| 61 | |
David S. Miller | 10e2672 | 2006-11-16 13:38:57 -0800 | [diff] [blame] | 62 | static inline void __down_write_nested(struct rw_semaphore *sem, int subclass) |
| 63 | { |
| 64 | __down_write(sem); |
| 65 | } |
| 66 | |
David S. Miller | 620de54 | 2005-07-24 19:35:42 -0700 | [diff] [blame] | 67 | static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
David S. Miller | 620de54 | 2005-07-24 19:35:42 -0700 | [diff] [blame] | 69 | return atomic_add_return(delta, (atomic_t *)(&sem->count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
David S. Miller | 620de54 | 2005-07-24 19:35:42 -0700 | [diff] [blame] | 72 | static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
David S. Miller | 620de54 | 2005-07-24 19:35:42 -0700 | [diff] [blame] | 74 | atomic_add(delta, (atomic_t *)(&sem->count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Rik Van Riel | eb92f4e | 2005-10-29 18:15:44 -0700 | [diff] [blame] | 77 | static inline int rwsem_is_locked(struct rw_semaphore *sem) |
| 78 | { |
| 79 | return (sem->count != 0); |
| 80 | } |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | #endif /* __KERNEL__ */ |
| 83 | |
| 84 | #endif /* _SPARC64_RWSEM_H */ |