Vegard Nossum | 77ef50a | 2008-06-18 17:08:48 +0200 | [diff] [blame] | 1 | #ifndef ASM_X86__SPINLOCK_H |
| 2 | #define ASM_X86__SPINLOCK_H |
Glauber de Oliveira Costa | 2fed0c5 | 2008-01-30 13:30:33 +0100 | [diff] [blame] | 3 | |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 4 | #include <asm/atomic.h> |
| 5 | #include <asm/rwlock.h> |
| 6 | #include <asm/page.h> |
| 7 | #include <asm/processor.h> |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 8 | #include <linux/compiler.h> |
Jeremy Fitzhardinge | 74d4aff | 2008-07-07 12:07:50 -0700 | [diff] [blame] | 9 | #include <asm/paravirt.h> |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 10 | /* |
| 11 | * Your basic SMP spinlocks, allowing only a single CPU anywhere |
| 12 | * |
| 13 | * Simple spin lock operations. There are two variants, one clears IRQ's |
| 14 | * on the local processor, one does not. |
| 15 | * |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 16 | * These are fair FIFO ticket locks, which are currently limited to 256 |
| 17 | * CPUs. |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 18 | * |
| 19 | * (the type definitions are in asm/spinlock_types.h) |
| 20 | */ |
| 21 | |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 22 | #ifdef CONFIG_X86_32 |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 23 | # define LOCK_PTR_REG "a" |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 24 | #else |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 25 | # define LOCK_PTR_REG "D" |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 26 | #endif |
Glauber de Oliveira Costa | 2fed0c5 | 2008-01-30 13:30:33 +0100 | [diff] [blame] | 27 | |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 28 | #if defined(CONFIG_X86_32) && \ |
| 29 | (defined(CONFIG_X86_OOSTORE) || defined(CONFIG_X86_PPRO_FENCE)) |
| 30 | /* |
| 31 | * On PPro SMP or if we are using OOSTORE, we use a locked operation to unlock |
| 32 | * (PPro errata 66, 92) |
| 33 | */ |
| 34 | # define UNLOCK_LOCK_PREFIX LOCK_PREFIX |
| 35 | #else |
| 36 | # define UNLOCK_LOCK_PREFIX |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 37 | #endif |
| 38 | |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 39 | /* |
| 40 | * Ticket locks are conceptually two parts, one indicating the current head of |
| 41 | * the queue, and the other indicating the current tail. The lock is acquired |
| 42 | * by atomically noting the tail and incrementing it by one (thus adding |
| 43 | * ourself to the queue and noting our position), then waiting until the head |
| 44 | * becomes equal to the the initial value of the tail. |
| 45 | * |
| 46 | * We use an xadd covering *both* parts of the lock, to increment the tail and |
| 47 | * also load the position of the head, which takes care of memory ordering |
| 48 | * issues and should be optimal for the uncontended case. Note the tail must be |
| 49 | * in the high part, because a wide xadd increment of the low part would carry |
| 50 | * up and contaminate the high part. |
| 51 | * |
| 52 | * With fewer than 2^8 possible CPUs, we can use x86's partial registers to |
| 53 | * save some instructions and make the code more elegant. There really isn't |
| 54 | * much between them in performance though, especially as locks are out of line. |
| 55 | */ |
| 56 | #if (NR_CPUS < 256) |
Jeremy Fitzhardinge | 74d4aff | 2008-07-07 12:07:50 -0700 | [diff] [blame] | 57 | static inline int __ticket_spin_is_locked(raw_spinlock_t *lock) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 58 | { |
Linus Torvalds | 39f004b | 2008-05-10 19:52:43 -0700 | [diff] [blame] | 59 | int tmp = ACCESS_ONCE(lock->slock); |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 60 | |
| 61 | return (((tmp >> 8) & 0xff) != (tmp & 0xff)); |
| 62 | } |
| 63 | |
Jeremy Fitzhardinge | 74d4aff | 2008-07-07 12:07:50 -0700 | [diff] [blame] | 64 | static inline int __ticket_spin_is_contended(raw_spinlock_t *lock) |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 65 | { |
Linus Torvalds | 39f004b | 2008-05-10 19:52:43 -0700 | [diff] [blame] | 66 | int tmp = ACCESS_ONCE(lock->slock); |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 67 | |
Jan Beulich | 7bc069c | 2008-08-04 14:38:54 +0100 | [diff] [blame] | 68 | return (((tmp >> 8) - tmp) & 0xff) > 1; |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Jeremy Fitzhardinge | 74d4aff | 2008-07-07 12:07:50 -0700 | [diff] [blame] | 71 | static __always_inline void __ticket_spin_lock(raw_spinlock_t *lock) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 72 | { |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 73 | short inc = 0x0100; |
| 74 | |
Joe Perches | d3bf60a | 2008-03-23 01:03:31 -0700 | [diff] [blame] | 75 | asm volatile ( |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 76 | LOCK_PREFIX "xaddw %w0, %1\n" |
| 77 | "1:\t" |
| 78 | "cmpb %h0, %b0\n\t" |
| 79 | "je 2f\n\t" |
| 80 | "rep ; nop\n\t" |
| 81 | "movb %1, %b0\n\t" |
| 82 | /* don't need lfence here, because loads are in-order */ |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 83 | "jmp 1b\n" |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 84 | "2:" |
Joe Perches | d3bf60a | 2008-03-23 01:03:31 -0700 | [diff] [blame] | 85 | : "+Q" (inc), "+m" (lock->slock) |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 86 | : |
Joe Perches | d3bf60a | 2008-03-23 01:03:31 -0700 | [diff] [blame] | 87 | : "memory", "cc"); |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Jeremy Fitzhardinge | 74d4aff | 2008-07-07 12:07:50 -0700 | [diff] [blame] | 90 | static __always_inline int __ticket_spin_trylock(raw_spinlock_t *lock) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 91 | { |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 92 | int tmp; |
| 93 | short new; |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 94 | |
Joe Perches | d3bf60a | 2008-03-23 01:03:31 -0700 | [diff] [blame] | 95 | asm volatile("movw %2,%w0\n\t" |
| 96 | "cmpb %h0,%b0\n\t" |
| 97 | "jne 1f\n\t" |
| 98 | "movw %w0,%w1\n\t" |
| 99 | "incb %h1\n\t" |
Mathieu Desnoyers | 5bbd4c3 | 2008-08-15 12:56:59 -0400 | [diff] [blame] | 100 | LOCK_PREFIX "cmpxchgw %w1,%2\n\t" |
Joe Perches | d3bf60a | 2008-03-23 01:03:31 -0700 | [diff] [blame] | 101 | "1:" |
| 102 | "sete %b1\n\t" |
| 103 | "movzbl %b1,%0\n\t" |
Jan Beulich | ef1f341 | 2008-09-05 13:26:39 +0100 | [diff] [blame^] | 104 | : "=&a" (tmp), "=&Q" (new), "+m" (lock->slock) |
Joe Perches | d3bf60a | 2008-03-23 01:03:31 -0700 | [diff] [blame] | 105 | : |
| 106 | : "memory", "cc"); |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 107 | |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 108 | return tmp; |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Jeremy Fitzhardinge | 74d4aff | 2008-07-07 12:07:50 -0700 | [diff] [blame] | 111 | static __always_inline void __ticket_spin_unlock(raw_spinlock_t *lock) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 112 | { |
Joe Perches | d3bf60a | 2008-03-23 01:03:31 -0700 | [diff] [blame] | 113 | asm volatile(UNLOCK_LOCK_PREFIX "incb %0" |
| 114 | : "+m" (lock->slock) |
| 115 | : |
| 116 | : "memory", "cc"); |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 117 | } |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 118 | #else |
Jeremy Fitzhardinge | 74d4aff | 2008-07-07 12:07:50 -0700 | [diff] [blame] | 119 | static inline int __ticket_spin_is_locked(raw_spinlock_t *lock) |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 120 | { |
Linus Torvalds | 39f004b | 2008-05-10 19:52:43 -0700 | [diff] [blame] | 121 | int tmp = ACCESS_ONCE(lock->slock); |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 122 | |
| 123 | return (((tmp >> 16) & 0xffff) != (tmp & 0xffff)); |
| 124 | } |
| 125 | |
Jeremy Fitzhardinge | 74d4aff | 2008-07-07 12:07:50 -0700 | [diff] [blame] | 126 | static inline int __ticket_spin_is_contended(raw_spinlock_t *lock) |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 127 | { |
Linus Torvalds | 39f004b | 2008-05-10 19:52:43 -0700 | [diff] [blame] | 128 | int tmp = ACCESS_ONCE(lock->slock); |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 129 | |
Jan Beulich | 7bc069c | 2008-08-04 14:38:54 +0100 | [diff] [blame] | 130 | return (((tmp >> 16) - tmp) & 0xffff) > 1; |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Jeremy Fitzhardinge | 74d4aff | 2008-07-07 12:07:50 -0700 | [diff] [blame] | 133 | static __always_inline void __ticket_spin_lock(raw_spinlock_t *lock) |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 134 | { |
| 135 | int inc = 0x00010000; |
| 136 | int tmp; |
| 137 | |
Mathieu Desnoyers | 5bbd4c3 | 2008-08-15 12:56:59 -0400 | [diff] [blame] | 138 | asm volatile(LOCK_PREFIX "xaddl %0, %1\n" |
Joe Perches | d3bf60a | 2008-03-23 01:03:31 -0700 | [diff] [blame] | 139 | "movzwl %w0, %2\n\t" |
| 140 | "shrl $16, %0\n\t" |
| 141 | "1:\t" |
| 142 | "cmpl %0, %2\n\t" |
| 143 | "je 2f\n\t" |
| 144 | "rep ; nop\n\t" |
| 145 | "movzwl %1, %2\n\t" |
| 146 | /* don't need lfence here, because loads are in-order */ |
| 147 | "jmp 1b\n" |
| 148 | "2:" |
Jan Beulich | ef1f341 | 2008-09-05 13:26:39 +0100 | [diff] [blame^] | 149 | : "+r" (inc), "+m" (lock->slock), "=&r" (tmp) |
Joe Perches | d3bf60a | 2008-03-23 01:03:31 -0700 | [diff] [blame] | 150 | : |
| 151 | : "memory", "cc"); |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Jeremy Fitzhardinge | 74d4aff | 2008-07-07 12:07:50 -0700 | [diff] [blame] | 154 | static __always_inline int __ticket_spin_trylock(raw_spinlock_t *lock) |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 155 | { |
| 156 | int tmp; |
| 157 | int new; |
| 158 | |
Joe Perches | d3bf60a | 2008-03-23 01:03:31 -0700 | [diff] [blame] | 159 | asm volatile("movl %2,%0\n\t" |
| 160 | "movl %0,%1\n\t" |
| 161 | "roll $16, %0\n\t" |
| 162 | "cmpl %0,%1\n\t" |
| 163 | "jne 1f\n\t" |
| 164 | "addl $0x00010000, %1\n\t" |
Mathieu Desnoyers | 5bbd4c3 | 2008-08-15 12:56:59 -0400 | [diff] [blame] | 165 | LOCK_PREFIX "cmpxchgl %1,%2\n\t" |
Joe Perches | d3bf60a | 2008-03-23 01:03:31 -0700 | [diff] [blame] | 166 | "1:" |
| 167 | "sete %b1\n\t" |
| 168 | "movzbl %b1,%0\n\t" |
Jan Beulich | ef1f341 | 2008-09-05 13:26:39 +0100 | [diff] [blame^] | 169 | : "=&a" (tmp), "=&q" (new), "+m" (lock->slock) |
Joe Perches | d3bf60a | 2008-03-23 01:03:31 -0700 | [diff] [blame] | 170 | : |
| 171 | : "memory", "cc"); |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 172 | |
| 173 | return tmp; |
| 174 | } |
| 175 | |
Jeremy Fitzhardinge | 74d4aff | 2008-07-07 12:07:50 -0700 | [diff] [blame] | 176 | static __always_inline void __ticket_spin_unlock(raw_spinlock_t *lock) |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 177 | { |
Joe Perches | d3bf60a | 2008-03-23 01:03:31 -0700 | [diff] [blame] | 178 | asm volatile(UNLOCK_LOCK_PREFIX "incw %0" |
| 179 | : "+m" (lock->slock) |
| 180 | : |
| 181 | : "memory", "cc"); |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 182 | } |
| 183 | #endif |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 184 | |
Jeremy Fitzhardinge | 74d4aff | 2008-07-07 12:07:50 -0700 | [diff] [blame] | 185 | #define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock) |
| 186 | |
Jeremy Fitzhardinge | 8efcbab | 2008-07-07 12:07:51 -0700 | [diff] [blame] | 187 | #ifdef CONFIG_PARAVIRT |
| 188 | /* |
| 189 | * Define virtualization-friendly old-style lock byte lock, for use in |
| 190 | * pv_lock_ops if desired. |
| 191 | * |
| 192 | * This differs from the pre-2.6.24 spinlock by always using xchgb |
| 193 | * rather than decb to take the lock; this allows it to use a |
| 194 | * zero-initialized lock structure. It also maintains a 1-byte |
| 195 | * contention counter, so that we can implement |
| 196 | * __byte_spin_is_contended. |
| 197 | */ |
| 198 | struct __byte_spinlock { |
| 199 | s8 lock; |
| 200 | s8 spinners; |
| 201 | }; |
| 202 | |
| 203 | static inline int __byte_spin_is_locked(raw_spinlock_t *lock) |
| 204 | { |
| 205 | struct __byte_spinlock *bl = (struct __byte_spinlock *)lock; |
| 206 | return bl->lock != 0; |
| 207 | } |
| 208 | |
| 209 | static inline int __byte_spin_is_contended(raw_spinlock_t *lock) |
| 210 | { |
| 211 | struct __byte_spinlock *bl = (struct __byte_spinlock *)lock; |
| 212 | return bl->spinners != 0; |
| 213 | } |
| 214 | |
| 215 | static inline void __byte_spin_lock(raw_spinlock_t *lock) |
| 216 | { |
| 217 | struct __byte_spinlock *bl = (struct __byte_spinlock *)lock; |
| 218 | s8 val = 1; |
| 219 | |
| 220 | asm("1: xchgb %1, %0\n" |
| 221 | " test %1,%1\n" |
| 222 | " jz 3f\n" |
| 223 | " " LOCK_PREFIX "incb %2\n" |
| 224 | "2: rep;nop\n" |
| 225 | " cmpb $1, %0\n" |
| 226 | " je 2b\n" |
| 227 | " " LOCK_PREFIX "decb %2\n" |
| 228 | " jmp 1b\n" |
| 229 | "3:" |
| 230 | : "+m" (bl->lock), "+q" (val), "+m" (bl->spinners): : "memory"); |
| 231 | } |
| 232 | |
| 233 | static inline int __byte_spin_trylock(raw_spinlock_t *lock) |
| 234 | { |
| 235 | struct __byte_spinlock *bl = (struct __byte_spinlock *)lock; |
| 236 | u8 old = 1; |
| 237 | |
| 238 | asm("xchgb %1,%0" |
| 239 | : "+m" (bl->lock), "+q" (old) : : "memory"); |
| 240 | |
| 241 | return old == 0; |
| 242 | } |
| 243 | |
| 244 | static inline void __byte_spin_unlock(raw_spinlock_t *lock) |
| 245 | { |
| 246 | struct __byte_spinlock *bl = (struct __byte_spinlock *)lock; |
| 247 | smp_wmb(); |
| 248 | bl->lock = 0; |
| 249 | } |
| 250 | #else /* !CONFIG_PARAVIRT */ |
Jeremy Fitzhardinge | 74d4aff | 2008-07-07 12:07:50 -0700 | [diff] [blame] | 251 | static inline int __raw_spin_is_locked(raw_spinlock_t *lock) |
| 252 | { |
| 253 | return __ticket_spin_is_locked(lock); |
| 254 | } |
| 255 | |
| 256 | static inline int __raw_spin_is_contended(raw_spinlock_t *lock) |
| 257 | { |
| 258 | return __ticket_spin_is_contended(lock); |
| 259 | } |
| 260 | |
| 261 | static __always_inline void __raw_spin_lock(raw_spinlock_t *lock) |
| 262 | { |
| 263 | __ticket_spin_lock(lock); |
| 264 | } |
| 265 | |
| 266 | static __always_inline int __raw_spin_trylock(raw_spinlock_t *lock) |
| 267 | { |
| 268 | return __ticket_spin_trylock(lock); |
| 269 | } |
| 270 | |
| 271 | static __always_inline void __raw_spin_unlock(raw_spinlock_t *lock) |
| 272 | { |
| 273 | __ticket_spin_unlock(lock); |
| 274 | } |
| 275 | #endif /* CONFIG_PARAVIRT */ |
| 276 | |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 277 | static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock) |
| 278 | { |
| 279 | while (__raw_spin_is_locked(lock)) |
| 280 | cpu_relax(); |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Read-write spinlocks, allowing multiple readers |
| 285 | * but only one writer. |
| 286 | * |
| 287 | * NOTE! it is quite common to have readers in interrupts |
| 288 | * but no interrupt writers. For those circumstances we |
| 289 | * can "mix" irq-safe locks - any writer needs to get a |
| 290 | * irq-safe write-lock, but readers can get non-irqsafe |
| 291 | * read-locks. |
| 292 | * |
| 293 | * On x86, we implement read-write locks as a 32-bit counter |
| 294 | * with the high bit (sign) being the "contended" bit. |
| 295 | */ |
| 296 | |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 297 | /** |
| 298 | * read_can_lock - would read_trylock() succeed? |
| 299 | * @lock: the rwlock in question. |
| 300 | */ |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 301 | static inline int __raw_read_can_lock(raw_rwlock_t *lock) |
| 302 | { |
| 303 | return (int)(lock)->lock > 0; |
| 304 | } |
| 305 | |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 306 | /** |
| 307 | * write_can_lock - would write_trylock() succeed? |
| 308 | * @lock: the rwlock in question. |
| 309 | */ |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 310 | static inline int __raw_write_can_lock(raw_rwlock_t *lock) |
| 311 | { |
| 312 | return (lock)->lock == RW_LOCK_BIAS; |
| 313 | } |
| 314 | |
| 315 | static inline void __raw_read_lock(raw_rwlock_t *rw) |
| 316 | { |
| 317 | asm volatile(LOCK_PREFIX " subl $1,(%0)\n\t" |
| 318 | "jns 1f\n" |
| 319 | "call __read_lock_failed\n\t" |
| 320 | "1:\n" |
| 321 | ::LOCK_PTR_REG (rw) : "memory"); |
| 322 | } |
| 323 | |
| 324 | static inline void __raw_write_lock(raw_rwlock_t *rw) |
| 325 | { |
| 326 | asm volatile(LOCK_PREFIX " subl %1,(%0)\n\t" |
| 327 | "jz 1f\n" |
| 328 | "call __write_lock_failed\n\t" |
| 329 | "1:\n" |
| 330 | ::LOCK_PTR_REG (rw), "i" (RW_LOCK_BIAS) : "memory"); |
| 331 | } |
| 332 | |
| 333 | static inline int __raw_read_trylock(raw_rwlock_t *lock) |
| 334 | { |
| 335 | atomic_t *count = (atomic_t *)lock; |
| 336 | |
| 337 | atomic_dec(count); |
| 338 | if (atomic_read(count) >= 0) |
| 339 | return 1; |
| 340 | atomic_inc(count); |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static inline int __raw_write_trylock(raw_rwlock_t *lock) |
| 345 | { |
| 346 | atomic_t *count = (atomic_t *)lock; |
| 347 | |
| 348 | if (atomic_sub_and_test(RW_LOCK_BIAS, count)) |
| 349 | return 1; |
| 350 | atomic_add(RW_LOCK_BIAS, count); |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static inline void __raw_read_unlock(raw_rwlock_t *rw) |
| 355 | { |
| 356 | asm volatile(LOCK_PREFIX "incl %0" :"+m" (rw->lock) : : "memory"); |
| 357 | } |
| 358 | |
| 359 | static inline void __raw_write_unlock(raw_rwlock_t *rw) |
| 360 | { |
| 361 | asm volatile(LOCK_PREFIX "addl %1, %0" |
| 362 | : "+m" (rw->lock) : "i" (RW_LOCK_BIAS) : "memory"); |
| 363 | } |
| 364 | |
| 365 | #define _raw_spin_relax(lock) cpu_relax() |
| 366 | #define _raw_read_relax(lock) cpu_relax() |
| 367 | #define _raw_write_relax(lock) cpu_relax() |
| 368 | |
Vegard Nossum | 77ef50a | 2008-06-18 17:08:48 +0200 | [diff] [blame] | 369 | #endif /* ASM_X86__SPINLOCK_H */ |