Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * include/asm-sh/spinlock.h |
| 3 | * |
| 4 | * Copyright (C) 2002, 2003 Paul Mundt |
| 5 | * |
| 6 | * This file is subject to the terms and conditions of the GNU General Public |
| 7 | * License. See the file "COPYING" in the main directory of this archive |
| 8 | * for more details. |
| 9 | */ |
| 10 | #ifndef __ASM_SH_SPINLOCK_H |
| 11 | #define __ASM_SH_SPINLOCK_H |
| 12 | |
| 13 | #include <asm/atomic.h> |
Evgeniy Polyakov | 66c5227 | 2007-05-31 13:46:21 +0900 | [diff] [blame] | 14 | #include <asm/spinlock_types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | /* |
| 17 | * Your basic SMP spinlocks, allowing only a single CPU anywhere |
| 18 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 20 | #define __raw_spin_is_locked(x) ((x)->lock != 0) |
| 21 | #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock) |
| 22 | #define __raw_spin_unlock_wait(x) \ |
| 23 | do { cpu_relax(); } while (__raw_spin_is_locked(x)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 31 | static inline void __raw_spin_lock(raw_spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
| 33 | __asm__ __volatile__ ( |
| 34 | "1:\n\t" |
| 35 | "tas.b @%0\n\t" |
| 36 | "bf/s 1b\n\t" |
| 37 | "nop\n\t" |
| 38 | : "=r" (lock->lock) |
| 39 | : "r" (&lock->lock) |
| 40 | : "t", "memory" |
| 41 | ); |
| 42 | } |
| 43 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 44 | static inline void __raw_spin_unlock(raw_spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
Evgeniy Polyakov | 66c5227 | 2007-05-31 13:46:21 +0900 | [diff] [blame] | 46 | //assert_spin_locked(lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | lock->lock = 0; |
| 49 | } |
| 50 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 51 | #define __raw_spin_trylock(x) (!test_and_set_bit(0, &(x)->lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | /* |
| 54 | * Read-write spinlocks, allowing multiple readers but only one writer. |
| 55 | * |
| 56 | * NOTE! it is quite common to have readers in interrupts but no interrupt |
| 57 | * writers. For those circumstances we can "mix" irq-safe locks - any writer |
| 58 | * needs to get a irq-safe write-lock, but readers can get non-irqsafe |
| 59 | * read-locks. |
| 60 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 62 | static inline void __raw_read_lock(raw_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 64 | __raw_spin_lock(&rw->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | atomic_inc(&rw->counter); |
| 67 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 68 | __raw_spin_unlock(&rw->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 71 | static inline void __raw_read_unlock(raw_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 73 | __raw_spin_lock(&rw->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | atomic_dec(&rw->counter); |
| 76 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 77 | __raw_spin_unlock(&rw->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 80 | static inline void __raw_write_lock(raw_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 82 | __raw_spin_lock(&rw->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | atomic_set(&rw->counter, -1); |
| 84 | } |
| 85 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 86 | static inline void __raw_write_unlock(raw_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
| 88 | atomic_set(&rw->counter, 0); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 89 | __raw_spin_unlock(&rw->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Evgeniy Polyakov | 66c5227 | 2007-05-31 13:46:21 +0900 | [diff] [blame] | 92 | static inline int __raw_write_can_lock(raw_rwlock_t *rw) |
| 93 | { |
| 94 | return (atomic_read(&rw->counter) == RW_LOCK_BIAS); |
| 95 | } |
| 96 | |
Paul Mundt | fac99d9 | 2006-10-03 14:13:09 +0900 | [diff] [blame] | 97 | static inline int __raw_read_trylock(raw_rwlock_t *lock) |
| 98 | { |
| 99 | atomic_t *count = (atomic_t*)lock; |
| 100 | if (atomic_dec_return(count) >= 0) |
| 101 | return 1; |
| 102 | atomic_inc(count); |
| 103 | return 0; |
| 104 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 106 | static inline int __raw_write_trylock(raw_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
| 108 | if (atomic_sub_and_test(RW_LOCK_BIAS, &rw->counter)) |
| 109 | return 1; |
| 110 | |
| 111 | atomic_add(RW_LOCK_BIAS, &rw->counter); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
Martin Schwidefsky | ef6edc9 | 2006-09-30 23:27:43 -0700 | [diff] [blame] | 116 | #define _raw_spin_relax(lock) cpu_relax() |
| 117 | #define _raw_read_relax(lock) cpu_relax() |
| 118 | #define _raw_write_relax(lock) cpu_relax() |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | #endif /* __ASM_SH_SPINLOCK_H */ |