2 * include/asm-ppc/rwsem.h: R/W semaphores for PPC using the stuff
3 * in lib/rwsem.c. Adapted largely from include/asm-i386/rwsem.h
4 * by Paul Mackerras <paulus@samba.org>.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #ifndef _PPC64_RWSEM_H
13 #define _PPC64_RWSEM_H
16 #include <linux/list.h>
17 #include <linux/spinlock.h>
18 #include <asm/atomic.h>
19 #include <asm/system.h>
22 * the semaphore definition
25 /* XXX this should be able to be an atomic_t -- paulus */
27 #define RWSEM_UNLOCKED_VALUE 0x00000000
28 #define RWSEM_ACTIVE_BIAS 0x00000001
29 #define RWSEM_ACTIVE_MASK 0x0000ffff
30 #define RWSEM_WAITING_BIAS (-0x00010000)
31 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
32 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
34 struct list_head wait_list;
44 #define __RWSEM_DEBUG_INIT , 0
46 #define __RWSEM_DEBUG_INIT /* */
49 #define __RWSEM_INITIALIZER(name) \
50 { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, \
51 LIST_HEAD_INIT((name).wait_list) \
54 #define DECLARE_RWSEM(name) \
55 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
57 extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
58 extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
59 extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem);
61 static inline void init_rwsem(struct rw_semaphore *sem)
63 sem->count = RWSEM_UNLOCKED_VALUE;
64 spin_lock_init(&sem->wait_lock);
65 INIT_LIST_HEAD(&sem->wait_list);
74 static inline void __down_read(struct rw_semaphore *sem)
76 if (atomic_inc_return((atomic_t *)(&sem->count)) >= 0)
79 rwsem_down_read_failed(sem);
82 static inline int __down_read_trylock(struct rw_semaphore *sem)
86 while ((tmp = sem->count) >= 0) {
87 if (tmp == cmpxchg(&sem->count, tmp,
88 tmp + RWSEM_ACTIVE_READ_BIAS)) {
99 static inline void __down_write(struct rw_semaphore *sem)
103 tmp = atomic_add_return(RWSEM_ACTIVE_WRITE_BIAS,
104 (atomic_t *)(&sem->count));
105 if (tmp == RWSEM_ACTIVE_WRITE_BIAS)
108 rwsem_down_write_failed(sem);
111 static inline int __down_write_trylock(struct rw_semaphore *sem)
115 tmp = cmpxchg(&sem->count, RWSEM_UNLOCKED_VALUE,
116 RWSEM_ACTIVE_WRITE_BIAS);
118 return tmp == RWSEM_UNLOCKED_VALUE;
122 * unlock after reading
124 static inline void __up_read(struct rw_semaphore *sem)
129 tmp = atomic_dec_return((atomic_t *)(&sem->count));
130 if (tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0)
135 * unlock after writing
137 static inline void __up_write(struct rw_semaphore *sem)
140 if (atomic_sub_return(RWSEM_ACTIVE_WRITE_BIAS,
141 (atomic_t *)(&sem->count)) < 0)
146 * implement atomic add functionality
148 static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)
150 atomic_add(delta, (atomic_t *)(&sem->count));
154 * implement exchange and add functionality
156 static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)
159 return atomic_add_return(delta, (atomic_t *)(&sem->count));
162 #endif /* __KERNEL__ */
163 #endif /* _PPC_RWSEM_XADD_H */