Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / include / asm-x86_64 / spinlock.h
blob5aeb57a3baad30b7111aa28cc6149cf891394bd5
1 #ifndef __ASM_SPINLOCK_H
2 #define __ASM_SPINLOCK_H
4 #include <asm/atomic.h>
5 #include <asm/rwlock.h>
6 #include <asm/page.h>
7 #include <linux/config.h>
9 extern int printk(const char * fmt, ...)
10 __attribute__ ((format (printf, 1, 2)));
13 * Your basic SMP spinlocks, allowing only a single CPU anywhere
16 typedef struct {
17 volatile unsigned int lock;
18 #ifdef CONFIG_DEBUG_SPINLOCK
19 unsigned magic;
20 #endif
21 #ifdef CONFIG_PREEMPT
22 unsigned int break_lock;
23 #endif
24 } spinlock_t;
26 #define SPINLOCK_MAGIC 0xdead4ead
28 #ifdef CONFIG_DEBUG_SPINLOCK
29 #define SPINLOCK_MAGIC_INIT , SPINLOCK_MAGIC
30 #else
31 #define SPINLOCK_MAGIC_INIT /* */
32 #endif
34 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 1 SPINLOCK_MAGIC_INIT }
36 #define spin_lock_init(x) do { *(x) = SPIN_LOCK_UNLOCKED; } while(0)
39 * Simple spin lock operations. There are two variants, one clears IRQ's
40 * on the local processor, one does not.
42 * We make no fairness assumptions. They have a cost.
45 #define spin_is_locked(x) (*(volatile signed char *)(&(x)->lock) <= 0)
46 #define spin_unlock_wait(x) do { barrier(); } while(spin_is_locked(x))
47 #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
49 #define spin_lock_string \
50 "\n1:\t" \
51 "lock ; decb %0\n\t" \
52 "js 2f\n" \
53 LOCK_SECTION_START("") \
54 "2:\t" \
55 "rep;nop\n\t" \
56 "cmpb $0,%0\n\t" \
57 "jle 2b\n\t" \
58 "jmp 1b\n" \
59 LOCK_SECTION_END
62 * This works. Despite all the confusion.
63 * (except on PPro SMP or if we are using OOSTORE)
64 * (PPro errata 66, 92)
67 #if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
69 #define spin_unlock_string \
70 "movb $1,%0" \
71 :"=m" (lock->lock) : : "memory"
74 static inline void _raw_spin_unlock(spinlock_t *lock)
76 #ifdef CONFIG_DEBUG_SPINLOCK
77 BUG_ON(lock->magic != SPINLOCK_MAGIC);
78 assert_spin_locked(lock);
79 #endif
80 __asm__ __volatile__(
81 spin_unlock_string
85 #else
87 #define spin_unlock_string \
88 "xchgb %b0, %1" \
89 :"=q" (oldval), "=m" (lock->lock) \
90 :"0" (oldval) : "memory"
92 static inline void _raw_spin_unlock(spinlock_t *lock)
94 char oldval = 1;
95 #ifdef CONFIG_DEBUG_SPINLOCK
96 BUG_ON(lock->magic != SPINLOCK_MAGIC);
97 assert_spin_locked(lock);
98 #endif
99 __asm__ __volatile__(
100 spin_unlock_string
104 #endif
106 static inline int _raw_spin_trylock(spinlock_t *lock)
108 char oldval;
109 __asm__ __volatile__(
110 "xchgb %b0,%1"
111 :"=q" (oldval), "=m" (lock->lock)
112 :"0" (0) : "memory");
113 return oldval > 0;
116 static inline void _raw_spin_lock(spinlock_t *lock)
118 #ifdef CONFIG_DEBUG_SPINLOCK
119 if (lock->magic != SPINLOCK_MAGIC) {
120 printk("eip: %p\n", __builtin_return_address(0));
121 BUG();
123 #endif
124 __asm__ __volatile__(
125 spin_lock_string
126 :"=m" (lock->lock) : : "memory");
131 * Read-write spinlocks, allowing multiple readers
132 * but only one writer.
134 * NOTE! it is quite common to have readers in interrupts
135 * but no interrupt writers. For those circumstances we
136 * can "mix" irq-safe locks - any writer needs to get a
137 * irq-safe write-lock, but readers can get non-irqsafe
138 * read-locks.
140 typedef struct {
141 volatile unsigned int lock;
142 #ifdef CONFIG_DEBUG_SPINLOCK
143 unsigned magic;
144 #endif
145 #ifdef CONFIG_PREEMPT
146 unsigned int break_lock;
147 #endif
148 } rwlock_t;
150 #define RWLOCK_MAGIC 0xdeaf1eed
152 #ifdef CONFIG_DEBUG_SPINLOCK
153 #define RWLOCK_MAGIC_INIT , RWLOCK_MAGIC
154 #else
155 #define RWLOCK_MAGIC_INIT /* */
156 #endif
158 #define RW_LOCK_UNLOCKED (rwlock_t) { RW_LOCK_BIAS RWLOCK_MAGIC_INIT }
160 #define rwlock_init(x) do { *(x) = RW_LOCK_UNLOCKED; } while(0)
162 #define read_can_lock(x) ((int)(x)->lock > 0)
163 #define write_can_lock(x) ((x)->lock == RW_LOCK_BIAS)
166 * On x86, we implement read-write locks as a 32-bit counter
167 * with the high bit (sign) being the "contended" bit.
169 * The inline assembly is non-obvious. Think about it.
171 * Changed to use the same technique as rw semaphores. See
172 * semaphore.h for details. -ben
174 /* the spinlock helpers are in arch/i386/kernel/semaphore.c */
176 static inline void _raw_read_lock(rwlock_t *rw)
178 #ifdef CONFIG_DEBUG_SPINLOCK
179 BUG_ON(rw->magic != RWLOCK_MAGIC);
180 #endif
181 __build_read_lock(rw, "__read_lock_failed");
184 static inline void _raw_write_lock(rwlock_t *rw)
186 #ifdef CONFIG_DEBUG_SPINLOCK
187 BUG_ON(rw->magic != RWLOCK_MAGIC);
188 #endif
189 __build_write_lock(rw, "__write_lock_failed");
192 #define _raw_read_unlock(rw) asm volatile("lock ; incl %0" :"=m" ((rw)->lock) : : "memory")
193 #define _raw_write_unlock(rw) asm volatile("lock ; addl $" RW_LOCK_BIAS_STR ",%0":"=m" ((rw)->lock) : : "memory")
195 static inline int _raw_read_trylock(rwlock_t *lock)
197 atomic_t *count = (atomic_t *)lock;
198 atomic_dec(count);
199 if (atomic_read(count) >= 0)
200 return 1;
201 atomic_inc(count);
202 return 0;
205 static inline int _raw_write_trylock(rwlock_t *lock)
207 atomic_t *count = (atomic_t *)lock;
208 if (atomic_sub_and_test(RW_LOCK_BIAS, count))
209 return 1;
210 atomic_add(RW_LOCK_BIAS, count);
211 return 0;
214 #endif /* __ASM_SPINLOCK_H */