Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
at v2.6.18-rc4 138 lines 3.7 kB view raw
1/* 2 * include/asm-s390/spinlock.h 3 * 4 * S390 version 5 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation 6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com) 7 * 8 * Derived from "include/asm-i386/spinlock.h" 9 */ 10 11#ifndef __ASM_SPINLOCK_H 12#define __ASM_SPINLOCK_H 13 14static inline int 15_raw_compare_and_swap(volatile unsigned int *lock, 16 unsigned int old, unsigned int new) 17{ 18 asm volatile ("cs %0,%3,0(%4)" 19 : "=d" (old), "=m" (*lock) 20 : "0" (old), "d" (new), "a" (lock), "m" (*lock) 21 : "cc", "memory" ); 22 return old; 23} 24 25/* 26 * Simple spin lock operations. There are two variants, one clears IRQ's 27 * on the local processor, one does not. 28 * 29 * We make no fairness assumptions. They have a cost. 30 * 31 * (the type definitions are in asm/spinlock_types.h) 32 */ 33 34#define __raw_spin_is_locked(x) ((x)->lock != 0) 35#define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock) 36#define __raw_spin_unlock_wait(lock) \ 37 do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0) 38 39extern void _raw_spin_lock_wait(raw_spinlock_t *lp, unsigned int pc); 40extern int _raw_spin_trylock_retry(raw_spinlock_t *lp, unsigned int pc); 41 42static inline void __raw_spin_lock(raw_spinlock_t *lp) 43{ 44 unsigned long pc = 1 | (unsigned long) __builtin_return_address(0); 45 46 if (unlikely(_raw_compare_and_swap(&lp->lock, 0, pc) != 0)) 47 _raw_spin_lock_wait(lp, pc); 48} 49 50static inline int __raw_spin_trylock(raw_spinlock_t *lp) 51{ 52 unsigned long pc = 1 | (unsigned long) __builtin_return_address(0); 53 54 if (likely(_raw_compare_and_swap(&lp->lock, 0, pc) == 0)) 55 return 1; 56 return _raw_spin_trylock_retry(lp, pc); 57} 58 59static inline void __raw_spin_unlock(raw_spinlock_t *lp) 60{ 61 _raw_compare_and_swap(&lp->lock, lp->lock, 0); 62} 63 64/* 65 * Read-write spinlocks, allowing multiple readers 66 * but only one writer. 67 * 68 * NOTE! it is quite common to have readers in interrupts 69 * but no interrupt writers. For those circumstances we 70 * can "mix" irq-safe locks - any writer needs to get a 71 * irq-safe write-lock, but readers can get non-irqsafe 72 * read-locks. 73 */ 74 75/** 76 * read_can_lock - would read_trylock() succeed? 77 * @lock: the rwlock in question. 78 */ 79#define __raw_read_can_lock(x) ((int)(x)->lock >= 0) 80 81/** 82 * write_can_lock - would write_trylock() succeed? 83 * @lock: the rwlock in question. 84 */ 85#define __raw_write_can_lock(x) ((x)->lock == 0) 86 87extern void _raw_read_lock_wait(raw_rwlock_t *lp); 88extern int _raw_read_trylock_retry(raw_rwlock_t *lp); 89extern void _raw_write_lock_wait(raw_rwlock_t *lp); 90extern int _raw_write_trylock_retry(raw_rwlock_t *lp); 91 92static inline void __raw_read_lock(raw_rwlock_t *rw) 93{ 94 unsigned int old; 95 old = rw->lock & 0x7fffffffU; 96 if (_raw_compare_and_swap(&rw->lock, old, old + 1) != old) 97 _raw_read_lock_wait(rw); 98} 99 100static inline void __raw_read_unlock(raw_rwlock_t *rw) 101{ 102 unsigned int old, cmp; 103 104 old = rw->lock; 105 do { 106 cmp = old; 107 old = _raw_compare_and_swap(&rw->lock, old, old - 1); 108 } while (cmp != old); 109} 110 111static inline void __raw_write_lock(raw_rwlock_t *rw) 112{ 113 if (unlikely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) != 0)) 114 _raw_write_lock_wait(rw); 115} 116 117static inline void __raw_write_unlock(raw_rwlock_t *rw) 118{ 119 _raw_compare_and_swap(&rw->lock, 0x80000000, 0); 120} 121 122static inline int __raw_read_trylock(raw_rwlock_t *rw) 123{ 124 unsigned int old; 125 old = rw->lock & 0x7fffffffU; 126 if (likely(_raw_compare_and_swap(&rw->lock, old, old + 1) == old)) 127 return 1; 128 return _raw_read_trylock_retry(rw); 129} 130 131static inline int __raw_write_trylock(raw_rwlock_t *rw) 132{ 133 if (likely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0)) 134 return 1; 135 return _raw_write_trylock_retry(rw); 136} 137 138#endif /* __ASM_SPINLOCK_H */