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
15 _raw_compare_and_swap(volatile unsigned int *lock,
16 unsigned int old, unsigned int new)
18 asm volatile ("cs %0,%3,0(%4)"
19 : "=d" (old), "=m" (*lock)
20 : "0" (old), "d" (new), "a" (lock), "m" (*lock)
26 * Simple spin lock operations. There are two variants, one clears IRQ's
27 * on the local processor, one does not.
29 * We make no fairness assumptions. They have a cost.
33 volatile unsigned int lock;
35 unsigned int break_lock;
37 } __attribute__ ((aligned (4))) spinlock_t;
39 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
40 #define spin_lock_init(lp) do { (lp)->lock = 0; } while(0)
41 #define spin_unlock_wait(lp) do { barrier(); } while(((volatile spinlock_t *)(lp))->lock)
42 #define spin_is_locked(x) ((x)->lock != 0)
43 #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
45 extern void _raw_spin_lock_wait(spinlock_t *lp, unsigned int pc);
46 extern int _raw_spin_trylock_retry(spinlock_t *lp, unsigned int pc);
48 static inline void _raw_spin_lock(spinlock_t *lp)
50 unsigned long pc = 1 | (unsigned long) __builtin_return_address(0);
52 if (unlikely(_raw_compare_and_swap(&lp->lock, 0, pc) != 0))
53 _raw_spin_lock_wait(lp, pc);
56 static inline int _raw_spin_trylock(spinlock_t *lp)
58 unsigned long pc = 1 | (unsigned long) __builtin_return_address(0);
60 if (likely(_raw_compare_and_swap(&lp->lock, 0, pc) == 0))
62 return _raw_spin_trylock_retry(lp, pc);
65 static inline void _raw_spin_unlock(spinlock_t *lp)
67 _raw_compare_and_swap(&lp->lock, lp->lock, 0);
71 * Read-write spinlocks, allowing multiple readers
72 * but only one writer.
74 * NOTE! it is quite common to have readers in interrupts
75 * but no interrupt writers. For those circumstances we
76 * can "mix" irq-safe locks - any writer needs to get a
77 * irq-safe write-lock, but readers can get non-irqsafe
81 volatile unsigned int lock;
82 volatile unsigned long owner_pc;
84 unsigned int break_lock;
88 #define RW_LOCK_UNLOCKED (rwlock_t) { 0, 0 }
90 #define rwlock_init(x) do { *(x) = RW_LOCK_UNLOCKED; } while(0)
93 * read_can_lock - would read_trylock() succeed?
94 * @lock: the rwlock in question.
96 #define read_can_lock(x) ((int)(x)->lock >= 0)
99 * write_can_lock - would write_trylock() succeed?
100 * @lock: the rwlock in question.
102 #define write_can_lock(x) ((x)->lock == 0)
104 extern void _raw_read_lock_wait(rwlock_t *lp);
105 extern int _raw_read_trylock_retry(rwlock_t *lp);
106 extern void _raw_write_lock_wait(rwlock_t *lp);
107 extern int _raw_write_trylock_retry(rwlock_t *lp);
109 static inline void _raw_read_lock(rwlock_t *rw)
112 old = rw->lock & 0x7fffffffU;
113 if (_raw_compare_and_swap(&rw->lock, old, old + 1) != old)
114 _raw_read_lock_wait(rw);
117 static inline void _raw_read_unlock(rwlock_t *rw)
119 unsigned int old, cmp;
124 old = _raw_compare_and_swap(&rw->lock, old, old - 1);
125 } while (cmp != old);
128 static inline void _raw_write_lock(rwlock_t *rw)
130 if (unlikely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) != 0))
131 _raw_write_lock_wait(rw);
134 static inline void _raw_write_unlock(rwlock_t *rw)
136 _raw_compare_and_swap(&rw->lock, 0x80000000, 0);
139 static inline int _raw_read_trylock(rwlock_t *rw)
142 old = rw->lock & 0x7fffffffU;
143 if (likely(_raw_compare_and_swap(&rw->lock, old, old + 1) == old))
145 return _raw_read_trylock_retry(rw);
148 static inline int _raw_write_trylock(rwlock_t *rw)
150 if (likely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0))
152 return _raw_write_trylock_retry(rw);
155 #endif /* __ASM_SPINLOCK_H */