2 * include/asm-s390/spinlock.h
5 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
8 * Derived from "include/asm-i386/spinlock.h"
11 #ifndef __ASM_SPINLOCK_H
12 #define __ASM_SPINLOCK_H
14 #include <linux/smp.h>
16 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
19 _raw_compare_and_swap(volatile unsigned int *lock,
20 unsigned int old, unsigned int new)
24 : "=d" (old), "=Q" (*lock)
25 : "0" (old), "d" (new), "Q" (*lock)
33 _raw_compare_and_swap(volatile unsigned int *lock,
34 unsigned int old, unsigned int new)
38 : "=d" (old), "=m" (*lock)
39 : "0" (old), "d" (new), "a" (lock), "m" (*lock)
47 * Simple spin lock operations. There are two variants, one clears IRQ's
48 * on the local processor, one does not.
50 * We make no fairness assumptions. They have a cost.
52 * (the type definitions are in asm/spinlock_types.h)
55 #define __raw_spin_is_locked(x) ((x)->owner_cpu != 0)
56 #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
57 #define __raw_spin_unlock_wait(lock) \
58 do { while (__raw_spin_is_locked(lock)) \
59 _raw_spin_relax(lock); } while (0)
61 extern void _raw_spin_lock_wait(raw_spinlock_t *, unsigned int pc);
62 extern int _raw_spin_trylock_retry(raw_spinlock_t *, unsigned int pc);
63 extern void _raw_spin_relax(raw_spinlock_t *lock);
65 static inline void __raw_spin_lock(raw_spinlock_t *lp)
67 unsigned long pc = 1 | (unsigned long) __builtin_return_address(0);
70 old = _raw_compare_and_swap(&lp->owner_cpu, 0, ~smp_processor_id());
71 if (likely(old == 0)) {
75 _raw_spin_lock_wait(lp, pc);
78 static inline int __raw_spin_trylock(raw_spinlock_t *lp)
80 unsigned long pc = 1 | (unsigned long) __builtin_return_address(0);
83 old = _raw_compare_and_swap(&lp->owner_cpu, 0, ~smp_processor_id());
84 if (likely(old == 0)) {
88 return _raw_spin_trylock_retry(lp, pc);
91 static inline void __raw_spin_unlock(raw_spinlock_t *lp)
94 _raw_compare_and_swap(&lp->owner_cpu, lp->owner_cpu, 0);
98 * Read-write spinlocks, allowing multiple readers
99 * but only one writer.
101 * NOTE! it is quite common to have readers in interrupts
102 * but no interrupt writers. For those circumstances we
103 * can "mix" irq-safe locks - any writer needs to get a
104 * irq-safe write-lock, but readers can get non-irqsafe
109 * read_can_lock - would read_trylock() succeed?
110 * @lock: the rwlock in question.
112 #define __raw_read_can_lock(x) ((int)(x)->lock >= 0)
115 * write_can_lock - would write_trylock() succeed?
116 * @lock: the rwlock in question.
118 #define __raw_write_can_lock(x) ((x)->lock == 0)
120 extern void _raw_read_lock_wait(raw_rwlock_t *lp);
121 extern int _raw_read_trylock_retry(raw_rwlock_t *lp);
122 extern void _raw_write_lock_wait(raw_rwlock_t *lp);
123 extern int _raw_write_trylock_retry(raw_rwlock_t *lp);
125 static inline void __raw_read_lock(raw_rwlock_t *rw)
128 old = rw->lock & 0x7fffffffU;
129 if (_raw_compare_and_swap(&rw->lock, old, old + 1) != old)
130 _raw_read_lock_wait(rw);
133 static inline void __raw_read_unlock(raw_rwlock_t *rw)
135 unsigned int old, cmp;
140 old = _raw_compare_and_swap(&rw->lock, old, old - 1);
141 } while (cmp != old);
144 static inline void __raw_write_lock(raw_rwlock_t *rw)
146 if (unlikely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) != 0))
147 _raw_write_lock_wait(rw);
150 static inline void __raw_write_unlock(raw_rwlock_t *rw)
152 _raw_compare_and_swap(&rw->lock, 0x80000000, 0);
155 static inline int __raw_read_trylock(raw_rwlock_t *rw)
158 old = rw->lock & 0x7fffffffU;
159 if (likely(_raw_compare_and_swap(&rw->lock, old, old + 1) == old))
161 return _raw_read_trylock_retry(rw);
164 static inline int __raw_write_trylock(raw_rwlock_t *rw)
166 if (likely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0))
168 return _raw_write_trylock_retry(rw);
171 #define _raw_read_relax(lock) cpu_relax()
172 #define _raw_write_relax(lock) cpu_relax()
174 #endif /* __ASM_SPINLOCK_H */