5 * include/asm-s390/rwsem.h
8 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH, IBM Corporation
9 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
11 * Based on asm-alpha/semaphore.h and asm-i386/rwsem.h
16 * The MSW of the count is the negated number of active writers and waiting
17 * lockers, and the LSW is the total number of active locks
19 * The lock count is initialized to 0 (no active and no waiting lockers).
21 * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case of an
22 * uncontended lock. This can be determined because XADD returns the old value.
23 * Readers increment by 1 and see a positive value when uncontended, negative
24 * if there are writers (and maybe) readers waiting (in which case it goes to
27 * The value of WAITING_BIAS supports up to 32766 waiting processes. This can
28 * be extended to 65534 by manually checking the whole MSW rather than relying
31 * The value of ACTIVE_BIAS supports up to 65535 active processes.
33 * This should be totally fair - if anything is waiting, a process that wants a
34 * lock will go to the back of the queue. When the currently active lock is
35 * released, if there's a writer at the front of the queue, then that and only
36 * that will be woken up; if there's a bunch of consequtive readers at the
37 * front, then they'll all be woken up, but no other readers will be.
40 #ifndef _LINUX_RWSEM_H
41 #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead"
47 #define RWSEM_UNLOCKED_VALUE 0x00000000
48 #define RWSEM_ACTIVE_BIAS 0x00000001
49 #define RWSEM_ACTIVE_MASK 0x0000ffff
50 #define RWSEM_WAITING_BIAS (-0x00010000)
52 #define RWSEM_UNLOCKED_VALUE 0x0000000000000000L
53 #define RWSEM_ACTIVE_BIAS 0x0000000000000001L
54 #define RWSEM_ACTIVE_MASK 0x00000000ffffffffL
55 #define RWSEM_WAITING_BIAS (-0x0000000100000000L)
56 #endif /* __s390x__ */
57 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
58 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
63 static inline void __down_read(struct rw_semaphore
*sem
)
80 #endif /* __s390x__ */
81 : "=&d" (old
), "=&d" (new), "=Q" (sem
->count
)
82 : "Q" (sem
->count
), "i" (RWSEM_ACTIVE_READ_BIAS
)
85 rwsem_down_read_failed(sem
);
89 * trylock for reading -- returns 1 if successful, 0 if contention
91 static inline int __down_read_trylock(struct rw_semaphore
*sem
)
104 #else /* __s390x__ */
112 #endif /* __s390x__ */
113 : "=&d" (old
), "=&d" (new), "=Q" (sem
->count
)
114 : "Q" (sem
->count
), "i" (RWSEM_ACTIVE_READ_BIAS
)
116 return old
>= 0 ? 1 : 0;
122 static inline void __down_write_nested(struct rw_semaphore
*sem
, int subclass
)
124 signed long old
, new, tmp
;
126 tmp
= RWSEM_ACTIVE_WRITE_BIAS
;
134 #else /* __s390x__ */
140 #endif /* __s390x__ */
141 : "=&d" (old
), "=&d" (new), "=Q" (sem
->count
)
142 : "Q" (sem
->count
), "m" (tmp
)
145 rwsem_down_write_failed(sem
);
148 static inline void __down_write(struct rw_semaphore
*sem
)
150 __down_write_nested(sem
, 0);
154 * trylock for writing -- returns 1 if successful, 0 if contention
156 static inline int __down_write_trylock(struct rw_semaphore
*sem
)
167 #else /* __s390x__ */
173 #endif /* __s390x__ */
175 : "=&d" (old
), "=Q" (sem
->count
)
176 : "Q" (sem
->count
), "d" (RWSEM_ACTIVE_WRITE_BIAS
)
178 return (old
== RWSEM_UNLOCKED_VALUE
) ? 1 : 0;
182 * unlock after reading
184 static inline void __up_read(struct rw_semaphore
*sem
)
186 signed long old
, new;
195 #else /* __s390x__ */
201 #endif /* __s390x__ */
202 : "=&d" (old
), "=&d" (new), "=Q" (sem
->count
)
203 : "Q" (sem
->count
), "i" (-RWSEM_ACTIVE_READ_BIAS
)
206 if ((new & RWSEM_ACTIVE_MASK
) == 0)
211 * unlock after writing
213 static inline void __up_write(struct rw_semaphore
*sem
)
215 signed long old
, new, tmp
;
217 tmp
= -RWSEM_ACTIVE_WRITE_BIAS
;
225 #else /* __s390x__ */
231 #endif /* __s390x__ */
232 : "=&d" (old
), "=&d" (new), "=Q" (sem
->count
)
233 : "Q" (sem
->count
), "m" (tmp
)
236 if ((new & RWSEM_ACTIVE_MASK
) == 0)
241 * downgrade write lock to read lock
243 static inline void __downgrade_write(struct rw_semaphore
*sem
)
245 signed long old
, new, tmp
;
247 tmp
= -RWSEM_WAITING_BIAS
;
255 #else /* __s390x__ */
261 #endif /* __s390x__ */
262 : "=&d" (old
), "=&d" (new), "=Q" (sem
->count
)
263 : "Q" (sem
->count
), "m" (tmp
)
266 rwsem_downgrade_wake(sem
);
270 * implement atomic add functionality
272 static inline void rwsem_atomic_add(long delta
, struct rw_semaphore
*sem
)
274 signed long old
, new;
283 #else /* __s390x__ */
289 #endif /* __s390x__ */
290 : "=&d" (old
), "=&d" (new), "=Q" (sem
->count
)
291 : "Q" (sem
->count
), "d" (delta
)
296 * implement exchange and add functionality
298 static inline long rwsem_atomic_update(long delta
, struct rw_semaphore
*sem
)
300 signed long old
, new;
309 #else /* __s390x__ */
315 #endif /* __s390x__ */
316 : "=&d" (old
), "=&d" (new), "=Q" (sem
->count
)
317 : "Q" (sem
->count
), "d" (delta
)
322 #endif /* __KERNEL__ */
323 #endif /* _S390_RWSEM_H */