1 #ifndef __ASM_SPINLOCK_H
2 #define __ASM_SPINLOCK_H
4 #include <asm/atomic.h>
5 #include <asm/rwlock.h>
8 extern int printk(const char * fmt
, ...)
9 __attribute__ ((format (printf
, 1, 2)));
11 /* It seems that people are forgetting to
12 * initialize their spinlocks properly, tsk tsk.
13 * Remember to turn this off in 2.4. -ben
15 #define SPINLOCK_DEBUG 0
18 * Your basic SMP spinlocks, allowing only a single CPU anywhere
22 volatile unsigned int lock
;
28 #define SPINLOCK_MAGIC 0xdead4ead
31 #define SPINLOCK_MAGIC_INIT , SPINLOCK_MAGIC
33 #define SPINLOCK_MAGIC_INIT /* */
36 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 1 SPINLOCK_MAGIC_INIT }
38 #define spin_lock_init(x) do { *(x) = SPIN_LOCK_UNLOCKED; } while(0)
41 * Simple spin lock operations. There are two variants, one clears IRQ's
42 * on the local processor, one does not.
44 * We make no fairness assumptions. They have a cost.
47 #define spin_is_locked(x) (*(volatile char *)(&(x)->lock) <= 0)
48 #define spin_unlock_wait(x) do { barrier(); } while(spin_is_locked(x))
50 #define spin_lock_string \
52 "lock ; decb %0\n\t" \
54 ".section .text.lock,\"ax\"\n" \
63 * This works. Despite all the confusion.
65 #define spin_unlock_string \
68 static inline int spin_trylock(spinlock_t
*lock
)
73 :"=q" (oldval
), "=m" (lock
->lock
)
78 static inline void spin_lock(spinlock_t
*lock
)
83 if (lock
->magic
!= SPINLOCK_MAGIC
) {
84 printk("eip: %p\n", &&here
);
90 :"=m" (lock
->lock
) : : "memory");
93 static inline void spin_unlock(spinlock_t
*lock
)
96 if (lock
->magic
!= SPINLOCK_MAGIC
)
98 if (!spin_is_locked(lock
))
101 __asm__
__volatile__(
103 :"=m" (lock
->lock
) : : "memory");
107 * Read-write spinlocks, allowing multiple readers
108 * but only one writer.
110 * NOTE! it is quite common to have readers in interrupts
111 * but no interrupt writers. For those circumstances we
112 * can "mix" irq-safe locks - any writer needs to get a
113 * irq-safe write-lock, but readers can get non-irqsafe
117 volatile unsigned int lock
;
123 #define RWLOCK_MAGIC 0xdeaf1eed
126 #define RWLOCK_MAGIC_INIT , RWLOCK_MAGIC
128 #define RWLOCK_MAGIC_INIT /* */
131 #define RW_LOCK_UNLOCKED (rwlock_t) { RW_LOCK_BIAS RWLOCK_MAGIC_INIT }
133 #define rwlock_init(x) do { *(x) = RW_LOCK_UNLOCKED; } while(0)
136 * On x86, we implement read-write locks as a 32-bit counter
137 * with the high bit (sign) being the "contended" bit.
139 * The inline assembly is non-obvious. Think about it.
141 * Changed to use the same technique as rw semaphores. See
142 * semaphore.h for details. -ben
144 /* the spinlock helpers are in arch/i386/kernel/semaphore.c */
146 static inline void read_lock(rwlock_t
*rw
)
149 if (rw
->magic
!= RWLOCK_MAGIC
)
152 __build_read_lock(rw
, "__read_lock_failed");
155 static inline void write_lock(rwlock_t
*rw
)
158 if (rw
->magic
!= RWLOCK_MAGIC
)
161 __build_write_lock(rw
, "__write_lock_failed");
164 #define read_unlock(rw) asm volatile("lock ; incl %0" :"=m" ((rw)->lock) : : "memory")
165 #define write_unlock(rw) asm volatile("lock ; addl $" RW_LOCK_BIAS_STR ",%0":"=m" ((rw)->lock) : : "memory")
167 static inline int write_trylock(rwlock_t
*lock
)
169 atomic_t
*count
= (atomic_t
*)lock
;
170 if (atomic_sub_and_test(RW_LOCK_BIAS
, count
))
172 atomic_add(RW_LOCK_BIAS
, count
);
176 #endif /* __ASM_SPINLOCK_H */