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>.
11 #include <linux/list.h>
12 #include <linux/spinlock.h>
13 #include <asm/atomic.h>
14 #include <asm/system.h>
17 * the semaphore definition
20 /* XXX this should be able to be an atomic_t -- paulus */
22 #define RWSEM_UNLOCKED_VALUE 0x00000000
23 #define RWSEM_ACTIVE_BIAS 0x00000001
24 #define RWSEM_ACTIVE_MASK 0x0000ffff
25 #define RWSEM_WAITING_BIAS (-0x00010000)
26 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
27 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
29 struct list_head wait_list
;
39 #define __RWSEM_DEBUG_INIT , 0
41 #define __RWSEM_DEBUG_INIT /* */
44 #define __RWSEM_INITIALIZER(name) \
45 { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, \
46 LIST_HEAD_INIT((name).wait_list) \
49 #define DECLARE_RWSEM(name) \
50 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
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
);
57 static inline void init_rwsem(struct rw_semaphore
*sem
)
59 sem
->count
= RWSEM_UNLOCKED_VALUE
;
60 spin_lock_init(&sem
->wait_lock
);
61 INIT_LIST_HEAD(&sem
->wait_list
);
70 static inline void __down_read(struct rw_semaphore
*sem
)
72 if (atomic_inc_return((atomic_t
*)(&sem
->count
)) > 0)
75 rwsem_down_read_failed(sem
);
78 static inline int __down_read_trylock(struct rw_semaphore
*sem
)
82 while ((tmp
= sem
->count
) >= 0) {
83 if (tmp
== cmpxchg(&sem
->count
, tmp
,
84 tmp
+ RWSEM_ACTIVE_READ_BIAS
)) {
95 static inline void __down_write(struct rw_semaphore
*sem
)
99 tmp
= atomic_add_return(RWSEM_ACTIVE_WRITE_BIAS
,
100 (atomic_t
*)(&sem
->count
));
101 if (tmp
== RWSEM_ACTIVE_WRITE_BIAS
)
104 rwsem_down_write_failed(sem
);
107 static inline int __down_write_trylock(struct rw_semaphore
*sem
)
111 tmp
= cmpxchg(&sem
->count
, RWSEM_UNLOCKED_VALUE
,
112 RWSEM_ACTIVE_WRITE_BIAS
);
114 return tmp
== RWSEM_UNLOCKED_VALUE
;
118 * unlock after reading
120 static inline void __up_read(struct rw_semaphore
*sem
)
125 tmp
= atomic_dec_return((atomic_t
*)(&sem
->count
));
126 if (tmp
< -1 && (tmp
& RWSEM_ACTIVE_MASK
) == 0)
131 * unlock after writing
133 static inline void __up_write(struct rw_semaphore
*sem
)
136 if (atomic_sub_return(RWSEM_ACTIVE_WRITE_BIAS
,
137 (atomic_t
*)(&sem
->count
)) < 0)
142 * implement atomic add functionality
144 static inline void rwsem_atomic_add(int delta
, struct rw_semaphore
*sem
)
146 atomic_add(delta
, (atomic_t
*)(&sem
->count
));
150 * downgrade write lock to read lock
152 static inline void __downgrade_write(struct rw_semaphore
*sem
)
157 tmp
= atomic_add_return(-RWSEM_WAITING_BIAS
, (atomic_t
*)(&sem
->count
));
159 rwsem_downgrade_wake(sem
);
163 * implement exchange and add functionality
165 static inline int rwsem_atomic_update(int delta
, struct rw_semaphore
*sem
)
168 return atomic_add_return(delta
, (atomic_t
*)(&sem
->count
));
171 #endif /* __KERNEL__ */
172 #endif /* _PPC_RWSEM_XADD_H */