1 #ifndef __ASM_SPINLOCK_H
2 #define __ASM_SPINLOCK_H
4 #include <asm/atomic.h>
5 #include <asm/rwlock.h>
7 #include <asm/processor.h>
10 * Your basic SMP spinlocks, allowing only a single CPU anywhere
12 * Simple spin lock operations. There are two variants, one clears IRQ's
13 * on the local processor, one does not.
15 * We make no fairness assumptions. They have a cost.
17 * (the type definitions are in asm/spinlock_types.h)
20 static inline int __raw_spin_is_locked(raw_spinlock_t
*lock
)
22 return *(volatile signed int *)(&(lock
)->slock
) <= 0;
25 static inline void __raw_spin_lock(raw_spinlock_t
*lock
)
29 LOCK_PREFIX
" ; decl %0\n\t"
36 "2:\t" : "=m" (lock
->slock
) : : "memory");
40 * Same as __raw_spin_lock, but reenable interrupts during spinning.
42 #ifndef CONFIG_PROVE_LOCKING
43 static inline void __raw_spin_lock_flags(raw_spinlock_t
*lock
, unsigned long flags
)
47 LOCK_PREFIX
" ; decl %0\n\t"
49 "testl $0x200, %1\n\t" /* interrupts were disabled? */
64 : "+m" (lock
->slock
) : "r" ((unsigned)flags
) : "memory");
68 static inline int __raw_spin_trylock(raw_spinlock_t
*lock
)
74 :"=q" (oldval
), "=m" (lock
->slock
)
80 static inline void __raw_spin_unlock(raw_spinlock_t
*lock
)
82 asm volatile("movl $1,%0" :"=m" (lock
->slock
) :: "memory");
85 static inline void __raw_spin_unlock_wait(raw_spinlock_t
*lock
)
87 while (__raw_spin_is_locked(lock
))
92 * Read-write spinlocks, allowing multiple readers
93 * but only one writer.
95 * NOTE! it is quite common to have readers in interrupts
96 * but no interrupt writers. For those circumstances we
97 * can "mix" irq-safe locks - any writer needs to get a
98 * irq-safe write-lock, but readers can get non-irqsafe
101 * On x86, we implement read-write locks as a 32-bit counter
102 * with the high bit (sign) being the "contended" bit.
105 static inline int __raw_read_can_lock(raw_rwlock_t
*lock
)
107 return (int)(lock
)->lock
> 0;
110 static inline int __raw_write_can_lock(raw_rwlock_t
*lock
)
112 return (lock
)->lock
== RW_LOCK_BIAS
;
115 static inline void __raw_read_lock(raw_rwlock_t
*rw
)
117 asm volatile(LOCK_PREFIX
"subl $1,(%0)\n\t"
119 "call __read_lock_failed\n"
121 ::"D" (rw
), "i" (RW_LOCK_BIAS
) : "memory");
124 static inline void __raw_write_lock(raw_rwlock_t
*rw
)
126 asm volatile(LOCK_PREFIX
"subl %1,(%0)\n\t"
128 "\tcall __write_lock_failed\n\t"
130 ::"D" (rw
), "i" (RW_LOCK_BIAS
) : "memory");
133 static inline int __raw_read_trylock(raw_rwlock_t
*lock
)
135 atomic_t
*count
= (atomic_t
*)lock
;
137 if (atomic_read(count
) >= 0)
143 static inline int __raw_write_trylock(raw_rwlock_t
*lock
)
145 atomic_t
*count
= (atomic_t
*)lock
;
146 if (atomic_sub_and_test(RW_LOCK_BIAS
, count
))
148 atomic_add(RW_LOCK_BIAS
, count
);
152 static inline void __raw_read_unlock(raw_rwlock_t
*rw
)
154 asm volatile(LOCK_PREFIX
" ; incl %0" :"=m" (rw
->lock
) : : "memory");
157 static inline void __raw_write_unlock(raw_rwlock_t
*rw
)
159 asm volatile(LOCK_PREFIX
" ; addl $" RW_LOCK_BIAS_STR
",%0"
160 : "=m" (rw
->lock
) : : "memory");
163 #define _raw_spin_relax(lock) cpu_relax()
164 #define _raw_read_relax(lock) cpu_relax()
165 #define _raw_write_relax(lock) cpu_relax()
167 #endif /* __ASM_SPINLOCK_H */