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 | |
Andi Kleen | 8b059d2 | 2006-09-26 10:52:32 +0200 | [diff] [blame^] | 19 | static inline int __raw_spin_is_locked(raw_spinlock_t *lock) |
| 20 | { |
| 21 | return *(volatile signed int *)(&(lock)->slock) <= 0; |
| 22 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 24 | static inline void __raw_spin_lock(raw_spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | { |
Andi Kleen | 8b059d2 | 2006-09-26 10:52:32 +0200 | [diff] [blame^] | 26 | asm volatile( |
| 27 | "\n1:\t" |
| 28 | LOCK_PREFIX " ; decl %0\n\t" |
| 29 | "jns 2f\n" |
| 30 | "3:\n" |
| 31 | "rep;nop\n\t" |
| 32 | "cmpl $0,%0\n\t" |
| 33 | "jle 3b\n\t" |
| 34 | "jmp 1b\n" |
| 35 | "2:\t" : "=m" (lock->slock) : : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 38 | #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 40 | static inline int __raw_spin_trylock(raw_spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
Andi Kleen | 485832a | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 42 | int oldval; |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 43 | |
Andi Kleen | 8b059d2 | 2006-09-26 10:52:32 +0200 | [diff] [blame^] | 44 | asm volatile( |
Andi Kleen | 485832a | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 45 | "xchgl %0,%1" |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 46 | :"=q" (oldval), "=m" (lock->slock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | :"0" (0) : "memory"); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | return oldval > 0; |
| 50 | } |
| 51 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 52 | static inline void __raw_spin_unlock(raw_spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | { |
Andi Kleen | 8b059d2 | 2006-09-26 10:52:32 +0200 | [diff] [blame^] | 54 | asm volatile("movl $1,%0" :"=m" (lock->slock) :: "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Andi Kleen | 8b059d2 | 2006-09-26 10:52:32 +0200 | [diff] [blame^] | 57 | static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock) |
| 58 | { |
| 59 | while (__raw_spin_is_locked(lock)) |
| 60 | cpu_relax(); |
| 61 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | /* |
| 64 | * Read-write spinlocks, allowing multiple readers |
| 65 | * but only one writer. |
| 66 | * |
| 67 | * NOTE! it is quite common to have readers in interrupts |
| 68 | * but no interrupt writers. For those circumstances we |
| 69 | * can "mix" irq-safe locks - any writer needs to get a |
| 70 | * irq-safe write-lock, but readers can get non-irqsafe |
| 71 | * read-locks. |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 72 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | * On x86, we implement read-write locks as a 32-bit counter |
| 74 | * with the high bit (sign) being the "contended" bit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Andi Kleen | 8b059d2 | 2006-09-26 10:52:32 +0200 | [diff] [blame^] | 77 | static inline int __raw_read_can_lock(raw_rwlock_t *lock) |
| 78 | { |
| 79 | return (int)(lock)->lock > 0; |
| 80 | } |
| 81 | |
| 82 | static inline int __raw_write_can_lock(raw_rwlock_t *lock) |
| 83 | { |
| 84 | return (lock)->lock == RW_LOCK_BIAS; |
| 85 | } |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 86 | |
| 87 | static inline void __raw_read_lock(raw_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
Andi Kleen | 8b059d2 | 2006-09-26 10:52:32 +0200 | [diff] [blame^] | 89 | asm volatile(LOCK_PREFIX "subl $1,(%0)\n\t" |
| 90 | "jns 1f\n" |
| 91 | "call __read_lock_failed\n" |
| 92 | "1:\n" |
| 93 | ::"D" (rw), "i" (RW_LOCK_BIAS) : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 96 | static inline void __raw_write_lock(raw_rwlock_t *rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
Andi Kleen | 8b059d2 | 2006-09-26 10:52:32 +0200 | [diff] [blame^] | 98 | asm volatile(LOCK_PREFIX "subl %1,(%0)\n\t" |
| 99 | "jz 1f\n" |
| 100 | "\tcall __write_lock_failed\n\t" |
| 101 | "1:\n" |
| 102 | ::"D" (rw), "i" (RW_LOCK_BIAS) : "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 105 | static inline int __raw_read_trylock(raw_rwlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
| 107 | atomic_t *count = (atomic_t *)lock; |
| 108 | atomic_dec(count); |
| 109 | if (atomic_read(count) >= 0) |
| 110 | return 1; |
| 111 | atomic_inc(count); |
| 112 | return 0; |
| 113 | } |
| 114 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 115 | static inline int __raw_write_trylock(raw_rwlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
| 117 | atomic_t *count = (atomic_t *)lock; |
| 118 | if (atomic_sub_and_test(RW_LOCK_BIAS, count)) |
| 119 | return 1; |
| 120 | atomic_add(RW_LOCK_BIAS, count); |
| 121 | return 0; |
| 122 | } |
| 123 | |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 124 | static inline void __raw_read_unlock(raw_rwlock_t *rw) |
| 125 | { |
Andi Kleen | 841be8d | 2006-08-30 19:37:13 +0200 | [diff] [blame] | 126 | asm volatile(LOCK_PREFIX " ; incl %0" :"=m" (rw->lock) : : "memory"); |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static inline void __raw_write_unlock(raw_rwlock_t *rw) |
| 130 | { |
Andi Kleen | 841be8d | 2006-08-30 19:37:13 +0200 | [diff] [blame] | 131 | asm volatile(LOCK_PREFIX " ; addl $" RW_LOCK_BIAS_STR ",%0" |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 132 | : "=m" (rw->lock) : : "memory"); |
| 133 | } |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | #endif /* __ASM_SPINLOCK_H */ |