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