6 * Copyright IBM Corp. 2002
7 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
9 * Based on asm-alpha/semaphore.h and asm-i386/rwsem.h
14 * The MSW of the count is the negated number of active writers and waiting
15 * lockers, and the LSW is the total number of active locks
17 * The lock count is initialized to 0 (no active and no waiting lockers).
19 * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case of an
20 * uncontended lock. This can be determined because XADD returns the old value.
21 * Readers increment by 1 and see a positive value when uncontended, negative
22 * if there are writers (and maybe) readers waiting (in which case it goes to
25 * The value of WAITING_BIAS supports up to 32766 waiting processes. This can
26 * be extended to 65534 by manually checking the whole MSW rather than relying
29 * The value of ACTIVE_BIAS supports up to 65535 active processes.
31 * This should be totally fair - if anything is waiting, a process that wants a
32 * lock will go to the back of the queue. When the currently active lock is
33 * released, if there's a writer at the front of the queue, then that and only
34 * that will be woken up; if there's a bunch of consequtive readers at the
35 * front, then they'll all be woken up, but no other readers will be.
38 #ifndef _LINUX_RWSEM_H
39 #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
43 #define RWSEM_UNLOCKED_VALUE 0x00000000
44 #define RWSEM_ACTIVE_BIAS 0x00000001
45 #define RWSEM_ACTIVE_MASK 0x0000ffff
46 #define RWSEM_WAITING_BIAS (-0x00010000)
47 #else /* CONFIG_64BIT */
48 #define RWSEM_UNLOCKED_VALUE 0x0000000000000000L
49 #define RWSEM_ACTIVE_BIAS 0x0000000000000001L
50 #define RWSEM_ACTIVE_MASK 0x00000000ffffffffL
51 #define RWSEM_WAITING_BIAS (-0x0000000100000000L)
52 #endif /* CONFIG_64BIT */
53 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
54 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
59 static inline void __down_read(struct rw_semaphore
*sem
)
70 #else /* CONFIG_64BIT */
76 #endif /* CONFIG_64BIT */
77 : "=&d" (old
), "=&d" (new), "=Q" (sem
->count
)
78 : "Q" (sem
->count
), "i" (RWSEM_ACTIVE_READ_BIAS
)
81 rwsem_down_read_failed(sem
);
85 * trylock for reading -- returns 1 if successful, 0 if contention
87 static inline int __down_read_trylock(struct rw_semaphore
*sem
)
100 #else /* CONFIG_64BIT */
108 #endif /* CONFIG_64BIT */
109 : "=&d" (old
), "=&d" (new), "=Q" (sem
->count
)
110 : "Q" (sem
->count
), "i" (RWSEM_ACTIVE_READ_BIAS
)
112 return old
>= 0 ? 1 : 0;
118 static inline void __down_write_nested(struct rw_semaphore
*sem
, int subclass
)
120 signed long old
, new, tmp
;
122 tmp
= RWSEM_ACTIVE_WRITE_BIAS
;
130 #else /* CONFIG_64BIT */
136 #endif /* CONFIG_64BIT */
137 : "=&d" (old
), "=&d" (new), "=Q" (sem
->count
)
138 : "Q" (sem
->count
), "m" (tmp
)
141 rwsem_down_write_failed(sem
);
144 static inline void __down_write(struct rw_semaphore
*sem
)
146 __down_write_nested(sem
, 0);
150 * trylock for writing -- returns 1 if successful, 0 if contention
152 static inline int __down_write_trylock(struct rw_semaphore
*sem
)
163 #else /* CONFIG_64BIT */
169 #endif /* CONFIG_64BIT */
171 : "=&d" (old
), "=Q" (sem
->count
)
172 : "Q" (sem
->count
), "d" (RWSEM_ACTIVE_WRITE_BIAS
)
174 return (old
== RWSEM_UNLOCKED_VALUE
) ? 1 : 0;
178 * unlock after reading
180 static inline void __up_read(struct rw_semaphore
*sem
)
182 signed long old
, new;
191 #else /* CONFIG_64BIT */
197 #endif /* CONFIG_64BIT */
198 : "=&d" (old
), "=&d" (new), "=Q" (sem
->count
)
199 : "Q" (sem
->count
), "i" (-RWSEM_ACTIVE_READ_BIAS
)
202 if ((new & RWSEM_ACTIVE_MASK
) == 0)
207 * unlock after writing
209 static inline void __up_write(struct rw_semaphore
*sem
)
211 signed long old
, new, tmp
;
213 tmp
= -RWSEM_ACTIVE_WRITE_BIAS
;
221 #else /* CONFIG_64BIT */
227 #endif /* CONFIG_64BIT */
228 : "=&d" (old
), "=&d" (new), "=Q" (sem
->count
)
229 : "Q" (sem
->count
), "m" (tmp
)
232 if ((new & RWSEM_ACTIVE_MASK
) == 0)
237 * downgrade write lock to read lock
239 static inline void __downgrade_write(struct rw_semaphore
*sem
)
241 signed long old
, new, tmp
;
243 tmp
= -RWSEM_WAITING_BIAS
;
251 #else /* CONFIG_64BIT */
257 #endif /* CONFIG_64BIT */
258 : "=&d" (old
), "=&d" (new), "=Q" (sem
->count
)
259 : "Q" (sem
->count
), "m" (tmp
)
262 rwsem_downgrade_wake(sem
);
266 * implement atomic add functionality
268 static inline void rwsem_atomic_add(long delta
, struct rw_semaphore
*sem
)
270 signed long old
, new;
279 #else /* CONFIG_64BIT */
285 #endif /* CONFIG_64BIT */
286 : "=&d" (old
), "=&d" (new), "=Q" (sem
->count
)
287 : "Q" (sem
->count
), "d" (delta
)
292 * implement exchange and add functionality
294 static inline long rwsem_atomic_update(long delta
, struct rw_semaphore
*sem
)
296 signed long old
, new;
305 #else /* CONFIG_64BIT */
311 #endif /* CONFIG_64BIT */
312 : "=&d" (old
), "=&d" (new), "=Q" (sem
->count
)
313 : "Q" (sem
->count
), "d" (delta
)
318 #endif /* _S390_RWSEM_H */