Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __ASM_SPINLOCK_H |
| 2 | #define __ASM_SPINLOCK_H |
| 3 | |
| 4 | #include <asm/atomic.h> |
| 5 | #include <asm/rwlock.h> |
| 6 | #include <asm/page.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | /* |
| 9 | * Your basic SMP spinlocks, allowing only a single CPU anywhere |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 10 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * Simple spin lock operations. There are two variants, one clears IRQ's |
| 12 | * on the local processor, one does not. |
| 13 | * |
| 14 | * We make no fairness assumptions. They have a cost. |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 15 | * |
| 16 | * (the type definitions are in asm/spinlock_types.h) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
| 18 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 19 | #define __raw_spin_is_locked(x) \ |
Andi Kleen | 485832a | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 20 | (*(volatile signed int *)(&(x)->slock) <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 22 | #define __raw_spin_lock_string \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | "\n1:\t" \ |
Andi Kleen | 485832a | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 24 | "lock ; decl %0\n\t" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | "js 2f\n" \ |
| 26 | LOCK_SECTION_START("") \ |
| 27 | "2:\t" \ |
| 28 | "rep;nop\n\t" \ |
Andi Kleen | 485832a | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 29 | "cmpl $0,%0\n\t" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | "jle 2b\n\t" \ |
| 31 | "jmp 1b\n" \ |
| 32 | LOCK_SECTION_END |
| 33 | |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 34 | #define __raw_spin_lock_string_up \ |
| 35 | "\n\tdecl %0" |
| 36 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 37 | #define __raw_spin_unlock_string \ |
Andi Kleen | 485832a | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 38 | "movl $1,%0" \ |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 39 | :"=m" (lock->slock) : : "memory" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 41 | static inline void __raw_spin_lock(raw_spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 43 | alternative_smp( |
| 44 | __raw_spin_lock_string, |
| 45 | __raw_spin_lock_string_up, |
| 46 | "=m" (lock->slock) : : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 49 | #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 51 | static inline int __raw_spin_trylock(raw_spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Andi Kleen | 485832a | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 53 | int oldval; |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | __asm__ __volatile__( |
Andi Kleen | 485832a | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 56 | "xchgl %0,%1" |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 57 | :"=q" (oldval), "=m" (lock->slock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | :"0" (0) : "memory"); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | return oldval > 0; |
| 61 | } |
| 62 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 63 | static inline void __raw_spin_unlock(raw_spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | __asm__ __volatile__( |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 66 | __raw_spin_unlock_string |
| 67 | ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 70 | #define __raw_spin_unlock_wait(lock) \ |
| 71 | do { while (__raw_spin_is_locked(lock)) cpu_relax(); } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * Read-write spinlocks, allowing multiple readers |
| 75 | * but only one writer. |
| 76 | * |
| 77 | * NOTE! it is quite common to have readers in interrupts |
| 78 | * but no interrupt writers. For those circumstances we |
| 79 | * can "mix" irq-safe locks - any writer needs to get a |
| 80 | * irq-safe write-lock, but readers can get non-irqsafe |
| 81 | * read-locks. |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 82 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | * On x86, we implement read-write locks as a 32-bit counter |
| 84 | * with the high bit (sign) being the "contended" bit. |
| 85 | * |
| 86 | * The inline assembly is non-obvious. Think about it. |
| 87 | * |
| 88 | * Changed to use the same technique as rw semaphores. See |
| 89 | * semaphore.h for details. -ben |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 90 | * |
| 91 | * the helpers are in arch/i386/kernel/semaphore.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 94 | #define __raw_read_can_lock(x) ((int)(x)->lock > 0) |
| 95 | #define __raw_write_can_lock(x) ((x)->lock == RW_LOCK_BIAS) |
| 96 | |
| 97 | static inline void __raw_read_lock(raw_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | __build_read_lock(rw, "__read_lock_failed"); |
| 100 | } |
| 101 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 102 | static inline void __raw_write_lock(raw_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | __build_write_lock(rw, "__write_lock_failed"); |
| 105 | } |
| 106 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 107 | static inline int __raw_read_trylock(raw_rwlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
| 109 | atomic_t *count = (atomic_t *)lock; |
| 110 | atomic_dec(count); |
| 111 | if (atomic_read(count) >= 0) |
| 112 | return 1; |
| 113 | atomic_inc(count); |
| 114 | return 0; |
| 115 | } |
| 116 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 117 | static inline int __raw_write_trylock(raw_rwlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
| 119 | atomic_t *count = (atomic_t *)lock; |
| 120 | if (atomic_sub_and_test(RW_LOCK_BIAS, count)) |
| 121 | return 1; |
| 122 | atomic_add(RW_LOCK_BIAS, count); |
| 123 | return 0; |
| 124 | } |
| 125 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 126 | static inline void __raw_read_unlock(raw_rwlock_t *rw) |
| 127 | { |
| 128 | asm volatile("lock ; incl %0" :"=m" (rw->lock) : : "memory"); |
| 129 | } |
| 130 | |
| 131 | static inline void __raw_write_unlock(raw_rwlock_t *rw) |
| 132 | { |
| 133 | asm volatile("lock ; addl $" RW_LOCK_BIAS_STR ",%0" |
| 134 | : "=m" (rw->lock) : : "memory"); |
| 135 | } |
| 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | #endif /* __ASM_SPINLOCK_H */ |