Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * asm-ia64/rwsem.h: R/W semaphores for ia64 |
| 3 | * |
| 4 | * Copyright (C) 2003 Ken Chen <kenneth.w.chen@intel.com> |
| 5 | * Copyright (C) 2003 Asit Mallick <asit.k.mallick@intel.com> |
Christoph Lameter | 16592d2 | 2005-08-22 12:20:00 -0700 | [diff] [blame] | 6 | * Copyright (C) 2005 Christoph Lameter <clameter@sgi.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * Based on asm-i386/rwsem.h and other architecture implementation. |
| 9 | * |
| 10 | * The MSW of the count is the negated number of active writers and |
| 11 | * waiting lockers, and the LSW is the total number of active locks. |
| 12 | * |
| 13 | * The lock count is initialized to 0 (no active and no waiting lockers). |
| 14 | * |
Christoph Lameter | 16592d2 | 2005-08-22 12:20:00 -0700 | [diff] [blame] | 15 | * When a writer subtracts WRITE_BIAS, it'll get 0xffffffff00000001 for |
| 16 | * the case of an uncontended lock. Readers increment by 1 and see a positive |
| 17 | * value when uncontended, negative if there are writers (and maybe) readers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * waiting (in which case it goes to sleep). |
| 19 | */ |
| 20 | |
| 21 | #ifndef _ASM_IA64_RWSEM_H |
| 22 | #define _ASM_IA64_RWSEM_H |
| 23 | |
| 24 | #include <linux/list.h> |
| 25 | #include <linux/spinlock.h> |
| 26 | |
| 27 | #include <asm/intrinsics.h> |
| 28 | |
| 29 | /* |
| 30 | * the semaphore definition |
| 31 | */ |
| 32 | struct rw_semaphore { |
Christoph Lameter | 16592d2 | 2005-08-22 12:20:00 -0700 | [diff] [blame] | 33 | signed long count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | spinlock_t wait_lock; |
| 35 | struct list_head wait_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
Christoph Lameter | 16592d2 | 2005-08-22 12:20:00 -0700 | [diff] [blame] | 38 | #define RWSEM_UNLOCKED_VALUE __IA64_UL_CONST(0x0000000000000000) |
| 39 | #define RWSEM_ACTIVE_BIAS __IA64_UL_CONST(0x0000000000000001) |
| 40 | #define RWSEM_ACTIVE_MASK __IA64_UL_CONST(0x00000000ffffffff) |
| 41 | #define RWSEM_WAITING_BIAS -__IA64_UL_CONST(0x0000000100000000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS |
| 43 | #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS) |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #define __RWSEM_INITIALIZER(name) \ |
| 46 | { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, \ |
Ingo Molnar | 61f4c3d | 2006-07-03 00:24:29 -0700 | [diff] [blame] | 47 | LIST_HEAD_INIT((name).wait_list) } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | #define DECLARE_RWSEM(name) \ |
| 50 | struct rw_semaphore name = __RWSEM_INITIALIZER(name) |
| 51 | |
| 52 | extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem); |
| 53 | extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem); |
| 54 | extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem); |
| 55 | extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem); |
| 56 | |
| 57 | static inline void |
| 58 | init_rwsem (struct rw_semaphore *sem) |
| 59 | { |
| 60 | sem->count = RWSEM_UNLOCKED_VALUE; |
| 61 | spin_lock_init(&sem->wait_lock); |
| 62 | INIT_LIST_HEAD(&sem->wait_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /* |
| 66 | * lock for reading |
| 67 | */ |
| 68 | static inline void |
| 69 | __down_read (struct rw_semaphore *sem) |
| 70 | { |
Christoph Lameter | 16592d2 | 2005-08-22 12:20:00 -0700 | [diff] [blame] | 71 | long result = ia64_fetchadd8_acq((unsigned long *)&sem->count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | if (result < 0) |
| 74 | rwsem_down_read_failed(sem); |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * lock for writing |
| 79 | */ |
| 80 | static inline void |
| 81 | __down_write (struct rw_semaphore *sem) |
| 82 | { |
Christoph Lameter | 16592d2 | 2005-08-22 12:20:00 -0700 | [diff] [blame] | 83 | long old, new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | do { |
| 86 | old = sem->count; |
| 87 | new = old + RWSEM_ACTIVE_WRITE_BIAS; |
| 88 | } while (cmpxchg_acq(&sem->count, old, new) != old); |
| 89 | |
| 90 | if (old != 0) |
| 91 | rwsem_down_write_failed(sem); |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * unlock after reading |
| 96 | */ |
| 97 | static inline void |
| 98 | __up_read (struct rw_semaphore *sem) |
| 99 | { |
Christoph Lameter | 16592d2 | 2005-08-22 12:20:00 -0700 | [diff] [blame] | 100 | long result = ia64_fetchadd8_rel((unsigned long *)&sem->count, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | if (result < 0 && (--result & RWSEM_ACTIVE_MASK) == 0) |
| 103 | rwsem_wake(sem); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * unlock after writing |
| 108 | */ |
| 109 | static inline void |
| 110 | __up_write (struct rw_semaphore *sem) |
| 111 | { |
Christoph Lameter | 16592d2 | 2005-08-22 12:20:00 -0700 | [diff] [blame] | 112 | long old, new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | do { |
| 115 | old = sem->count; |
| 116 | new = old - RWSEM_ACTIVE_WRITE_BIAS; |
| 117 | } while (cmpxchg_rel(&sem->count, old, new) != old); |
| 118 | |
| 119 | if (new < 0 && (new & RWSEM_ACTIVE_MASK) == 0) |
| 120 | rwsem_wake(sem); |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * trylock for reading -- returns 1 if successful, 0 if contention |
| 125 | */ |
| 126 | static inline int |
| 127 | __down_read_trylock (struct rw_semaphore *sem) |
| 128 | { |
Christoph Lameter | 16592d2 | 2005-08-22 12:20:00 -0700 | [diff] [blame] | 129 | long tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | while ((tmp = sem->count) >= 0) { |
| 131 | if (tmp == cmpxchg_acq(&sem->count, tmp, tmp+1)) { |
| 132 | return 1; |
| 133 | } |
| 134 | } |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * trylock for writing -- returns 1 if successful, 0 if contention |
| 140 | */ |
| 141 | static inline int |
| 142 | __down_write_trylock (struct rw_semaphore *sem) |
| 143 | { |
Christoph Lameter | 16592d2 | 2005-08-22 12:20:00 -0700 | [diff] [blame] | 144 | long tmp = cmpxchg_acq(&sem->count, RWSEM_UNLOCKED_VALUE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | RWSEM_ACTIVE_WRITE_BIAS); |
| 146 | return tmp == RWSEM_UNLOCKED_VALUE; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * downgrade write lock to read lock |
| 151 | */ |
| 152 | static inline void |
| 153 | __downgrade_write (struct rw_semaphore *sem) |
| 154 | { |
Christoph Lameter | 16592d2 | 2005-08-22 12:20:00 -0700 | [diff] [blame] | 155 | long old, new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
| 157 | do { |
| 158 | old = sem->count; |
| 159 | new = old - RWSEM_WAITING_BIAS; |
| 160 | } while (cmpxchg_rel(&sem->count, old, new) != old); |
| 161 | |
| 162 | if (old < 0) |
| 163 | rwsem_downgrade_wake(sem); |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Implement atomic add functionality. These used to be "inline" functions, but GCC v3.1 |
| 168 | * doesn't quite optimize this stuff right and ends up with bad calls to fetchandadd. |
| 169 | */ |
Christoph Lameter | 16592d2 | 2005-08-22 12:20:00 -0700 | [diff] [blame] | 170 | #define rwsem_atomic_add(delta, sem) atomic64_add(delta, (atomic64_t *)(&(sem)->count)) |
| 171 | #define rwsem_atomic_update(delta, sem) atomic64_add_return(delta, (atomic64_t *)(&(sem)->count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Rik Van Riel | eb92f4e | 2005-10-29 18:15:44 -0700 | [diff] [blame] | 173 | static inline int rwsem_is_locked(struct rw_semaphore *sem) |
| 174 | { |
| 175 | return (sem->count != 0); |
| 176 | } |
| 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | #endif /* _ASM_IA64_RWSEM_H */ |