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"
46 #include <linux/list.h>
47 #include <linux/spinlock.h>
51 extern struct rw_semaphore
*rwsem_down_read_failed(struct rw_semaphore
*);
52 extern struct rw_semaphore
*rwsem_down_write_failed(struct rw_semaphore
*);
53 extern struct rw_semaphore
*rwsem_wake(struct rw_semaphore
*);
54 extern struct rw_semaphore
*rwsem_downgrade_wake(struct rw_semaphore
*);
55 extern struct rw_semaphore
*rwsem_downgrade_write(struct rw_semaphore
*);
58 * the semaphore definition
63 struct list_head wait_list
;
67 #define RWSEM_UNLOCKED_VALUE 0x00000000
68 #define RWSEM_ACTIVE_BIAS 0x00000001
69 #define RWSEM_ACTIVE_MASK 0x0000ffff
70 #define RWSEM_WAITING_BIAS (-0x00010000)
72 #define RWSEM_UNLOCKED_VALUE 0x0000000000000000L
73 #define RWSEM_ACTIVE_BIAS 0x0000000000000001L
74 #define RWSEM_ACTIVE_MASK 0x00000000ffffffffL
75 #define RWSEM_WAITING_BIAS (-0x0000000100000000L)
76 #endif /* __s390x__ */
77 #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS
78 #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
83 #define __RWSEM_INITIALIZER(name) \
84 { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, LIST_HEAD_INIT((name).wait_list) }
86 #define DECLARE_RWSEM(name) \
87 struct rw_semaphore name = __RWSEM_INITIALIZER(name)
89 static inline void init_rwsem(struct rw_semaphore
*sem
)
91 sem
->count
= RWSEM_UNLOCKED_VALUE
;
92 spin_lock_init(&sem
->wait_lock
);
93 INIT_LIST_HEAD(&sem
->wait_list
);
99 static inline void __down_read(struct rw_semaphore
*sem
)
101 signed long old
, new;
103 __asm__
__volatile__(
110 #else /* __s390x__ */
116 #endif /* __s390x__ */
117 : "=&d" (old
), "=&d" (new), "=m" (sem
->count
)
118 : "a" (&sem
->count
), "m" (sem
->count
),
119 "i" (RWSEM_ACTIVE_READ_BIAS
) : "cc", "memory" );
121 rwsem_down_read_failed(sem
);
125 * trylock for reading -- returns 1 if successful, 0 if contention
127 static inline int __down_read_trylock(struct rw_semaphore
*sem
)
129 signed long old
, new;
131 __asm__
__volatile__(
140 #else /* __s390x__ */
148 #endif /* __s390x__ */
149 : "=&d" (old
), "=&d" (new), "=m" (sem
->count
)
150 : "a" (&sem
->count
), "m" (sem
->count
),
151 "i" (RWSEM_ACTIVE_READ_BIAS
) : "cc", "memory" );
152 return old
>= 0 ? 1 : 0;
158 static inline void __down_write(struct rw_semaphore
*sem
)
160 signed long old
, new, tmp
;
162 tmp
= RWSEM_ACTIVE_WRITE_BIAS
;
163 __asm__
__volatile__(
170 #else /* __s390x__ */
176 #endif /* __s390x__ */
177 : "=&d" (old
), "=&d" (new), "=m" (sem
->count
)
178 : "a" (&sem
->count
), "m" (sem
->count
), "m" (tmp
)
181 rwsem_down_write_failed(sem
);
185 * trylock for writing -- returns 1 if successful, 0 if contention
187 static inline int __down_write_trylock(struct rw_semaphore
*sem
)
191 __asm__
__volatile__(
198 #else /* __s390x__ */
204 #endif /* __s390x__ */
206 : "=&d" (old
), "=m" (sem
->count
)
207 : "a" (&sem
->count
), "m" (sem
->count
),
208 "d" (RWSEM_ACTIVE_WRITE_BIAS
) : "cc", "memory" );
209 return (old
== RWSEM_UNLOCKED_VALUE
) ? 1 : 0;
213 * unlock after reading
215 static inline void __up_read(struct rw_semaphore
*sem
)
217 signed long old
, new;
219 __asm__
__volatile__(
226 #else /* __s390x__ */
232 #endif /* __s390x__ */
233 : "=&d" (old
), "=&d" (new), "=m" (sem
->count
)
234 : "a" (&sem
->count
), "m" (sem
->count
),
235 "i" (-RWSEM_ACTIVE_READ_BIAS
)
238 if ((new & RWSEM_ACTIVE_MASK
) == 0)
243 * unlock after writing
245 static inline void __up_write(struct rw_semaphore
*sem
)
247 signed long old
, new, tmp
;
249 tmp
= -RWSEM_ACTIVE_WRITE_BIAS
;
250 __asm__
__volatile__(
257 #else /* __s390x__ */
263 #endif /* __s390x__ */
264 : "=&d" (old
), "=&d" (new), "=m" (sem
->count
)
265 : "a" (&sem
->count
), "m" (sem
->count
), "m" (tmp
)
268 if ((new & RWSEM_ACTIVE_MASK
) == 0)
273 * downgrade write lock to read lock
275 static inline void __downgrade_write(struct rw_semaphore
*sem
)
277 signed long old
, new, tmp
;
279 tmp
= -RWSEM_WAITING_BIAS
;
280 __asm__
__volatile__(
287 #else /* __s390x__ */
293 #endif /* __s390x__ */
294 : "=&d" (old
), "=&d" (new), "=m" (sem
->count
)
295 : "a" (&sem
->count
), "m" (sem
->count
), "m" (tmp
)
298 rwsem_downgrade_wake(sem
);
302 * implement atomic add functionality
304 static inline void rwsem_atomic_add(long delta
, struct rw_semaphore
*sem
)
306 signed long old
, new;
308 __asm__
__volatile__(
315 #else /* __s390x__ */
321 #endif /* __s390x__ */
322 : "=&d" (old
), "=&d" (new), "=m" (sem
->count
)
323 : "a" (&sem
->count
), "m" (sem
->count
), "d" (delta
)
328 * implement exchange and add functionality
330 static inline long rwsem_atomic_update(long delta
, struct rw_semaphore
*sem
)
332 signed long old
, new;
334 __asm__
__volatile__(
341 #else /* __s390x__ */
347 #endif /* __s390x__ */
348 : "=&d" (old
), "=&d" (new), "=m" (sem
->count
)
349 : "a" (&sem
->count
), "m" (sem
->count
), "d" (delta
)
354 static inline int rwsem_is_locked(struct rw_semaphore
*sem
)
356 return (sem
->count
!= 0);
359 #endif /* __KERNEL__ */
360 #endif /* _S390_RWSEM_H */