2 * asm-ia64/rwsem.h: R/W semaphores for ia64
4 * Copyright (C) 2003 Ken Chen <kenneth.w.chen@intel.com>
5 * Copyright (C) 2003 Asit Mallick <asit.k.mallick@intel.com>
7 * Based on asm-i386/rwsem.h and other architecture implementation.
9 * The MSW of the count is the negated number of active writers and
10 * waiting lockers, and the LSW is the total number of active locks.
12 * The lock count is initialized to 0 (no active and no waiting lockers).
14 * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case
15 * of an uncontended lock. Readers increment by 1 and see a positive value
16 * when uncontended, negative if there are writers (and maybe) readers
17 * waiting (in which case it goes to sleep).
20 #ifndef _ASM_IA64_RWSEM_H
21 #define _ASM_IA64_RWSEM_H
23 #include <linux/list.h>
24 #include <linux/spinlock.h>
26 #include <asm/intrinsics.h>
29 * the semaphore definition
34 struct list_head wait_list;
40 #define RWSEM_UNLOCKED_VALUE 0x00000000
41 #define RWSEM_ACTIVE_BIAS 0x00000001
42 #define RWSEM_ACTIVE_MASK 0x0000ffff
43 #define RWSEM_WAITING_BIAS (-0x00010000)
44 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
45 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
51 #define __RWSEM_DEBUG_INIT , 0
53 #define __RWSEM_DEBUG_INIT /* */
56 #define __RWSEM_INITIALIZER(name) \
57 { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, \
58 LIST_HEAD_INIT((name).wait_list) \
61 #define DECLARE_RWSEM(name) \
62 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
64 extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
65 extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
66 extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem);
67 extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
70 init_rwsem (struct rw_semaphore *sem)
72 sem->count = RWSEM_UNLOCKED_VALUE;
73 spin_lock_init(&sem->wait_lock);
74 INIT_LIST_HEAD(&sem->wait_list);
84 __down_read (struct rw_semaphore *sem)
86 int result = ia64_fetchadd4_acq((unsigned int *)&sem->count, 1);
89 rwsem_down_read_failed(sem);
96 __down_write (struct rw_semaphore *sem)
102 new = old + RWSEM_ACTIVE_WRITE_BIAS;
103 } while (cmpxchg_acq(&sem->count, old, new) != old);
106 rwsem_down_write_failed(sem);
110 * unlock after reading
113 __up_read (struct rw_semaphore *sem)
115 int result = ia64_fetchadd4_rel((unsigned int *)&sem->count, -1);
117 if (result < 0 && (--result & RWSEM_ACTIVE_MASK) == 0)
122 * unlock after writing
125 __up_write (struct rw_semaphore *sem)
131 new = old - RWSEM_ACTIVE_WRITE_BIAS;
132 } while (cmpxchg_rel(&sem->count, old, new) != old);
134 if (new < 0 && (new & RWSEM_ACTIVE_MASK) == 0)
139 * trylock for reading -- returns 1 if successful, 0 if contention
142 __down_read_trylock (struct rw_semaphore *sem)
145 while ((tmp = sem->count) >= 0) {
146 if (tmp == cmpxchg_acq(&sem->count, tmp, tmp+1)) {
154 * trylock for writing -- returns 1 if successful, 0 if contention
157 __down_write_trylock (struct rw_semaphore *sem)
159 int tmp = cmpxchg_acq(&sem->count, RWSEM_UNLOCKED_VALUE,
160 RWSEM_ACTIVE_WRITE_BIAS);
161 return tmp == RWSEM_UNLOCKED_VALUE;
165 * downgrade write lock to read lock
168 __downgrade_write (struct rw_semaphore *sem)
174 new = old - RWSEM_WAITING_BIAS;
175 } while (cmpxchg_rel(&sem->count, old, new) != old);
178 rwsem_downgrade_wake(sem);
182 * Implement atomic add functionality. These used to be "inline" functions, but GCC v3.1
183 * doesn't quite optimize this stuff right and ends up with bad calls to fetchandadd.
185 #define rwsem_atomic_add(delta, sem) atomic_add(delta, (atomic_t *)(&(sem)->count))
186 #define rwsem_atomic_update(delta, sem) atomic_add_return(delta, (atomic_t *)(&(sem)->count))
188 #endif /* _ASM_IA64_RWSEM_H */