2 * BK Id: SCCS/s.rwsem.h 1.7 08/29/02 12:10:29 benh
5 * include/asm-ppc/rwsem.h: R/W semaphores for PPC using the stuff
6 * in lib/rwsem.c. Adapted largely from include/asm-i386/rwsem.h
7 * by Paul Mackerras <paulus@samba.org>.
14 #include <linux/list.h>
15 #include <linux/spinlock.h>
16 #include <asm/atomic.h>
17 #include <asm/system.h>
20 * the semaphore definition
23 /* XXX this should be able to be an atomic_t -- paulus */
25 #define RWSEM_UNLOCKED_VALUE 0x00000000
26 #define RWSEM_ACTIVE_BIAS 0x00000001
27 #define RWSEM_ACTIVE_MASK 0x0000ffff
28 #define RWSEM_WAITING_BIAS (-0x00010000)
29 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
30 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
32 struct list_head wait_list;
42 #define __RWSEM_DEBUG_INIT , 0
44 #define __RWSEM_DEBUG_INIT /* */
47 #define __RWSEM_INITIALIZER(name) \
48 { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, \
49 LIST_HEAD_INIT((name).wait_list) \
52 #define DECLARE_RWSEM(name) \
53 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
55 extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
56 extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
57 extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem);
59 static inline void init_rwsem(struct rw_semaphore *sem)
61 sem->count = RWSEM_UNLOCKED_VALUE;
62 spin_lock_init(&sem->wait_lock);
63 INIT_LIST_HEAD(&sem->wait_list);
72 static inline void __down_read(struct rw_semaphore *sem)
74 if (atomic_inc_return((atomic_t *)(&sem->count)) >= 0)
77 rwsem_down_read_failed(sem);
80 static inline int __down_read_trylock(struct rw_semaphore *sem)
84 while ((tmp = sem->count) >= 0) {
85 if (tmp == cmpxchg(&sem->count, tmp,
86 tmp + RWSEM_ACTIVE_READ_BIAS)) {
97 static inline void __down_write(struct rw_semaphore *sem)
101 tmp = atomic_add_return(RWSEM_ACTIVE_WRITE_BIAS,
102 (atomic_t *)(&sem->count));
103 if (tmp == RWSEM_ACTIVE_WRITE_BIAS)
106 rwsem_down_write_failed(sem);
109 static inline int __down_write_trylock(struct rw_semaphore *sem)
113 tmp = cmpxchg(&sem->count, RWSEM_UNLOCKED_VALUE,
114 RWSEM_ACTIVE_WRITE_BIAS);
116 return tmp == RWSEM_UNLOCKED_VALUE;
120 * unlock after reading
122 static inline void __up_read(struct rw_semaphore *sem)
127 tmp = atomic_dec_return((atomic_t *)(&sem->count));
128 if (tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0)
133 * unlock after writing
135 static inline void __up_write(struct rw_semaphore *sem)
138 if (atomic_sub_return(RWSEM_ACTIVE_WRITE_BIAS,
139 (atomic_t *)(&sem->count)) < 0)
144 * implement atomic add functionality
146 static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)
148 atomic_add(delta, (atomic_t *)(&sem->count));
152 * implement exchange and add functionality
154 static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)
157 return atomic_add_return(delta, (atomic_t *)(&sem->count));
160 #endif /* __KERNEL__ */
161 #endif /* _PPC_RWSEM_XADD_H */