blob: be7a9e629fb262ebecef04fe3bd454752816c7ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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 Torvalds1da177e2005-04-16 15:20:36 -07007
Linus Torvalds1da177e2005-04-16 15:20:36 -07008/*
9 * Your basic SMP spinlocks, allowing only a single CPU anywhere
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * 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 Molnarfb1c8f92005-09-10 00:25:56 -070015 *
16 * (the type definitions are in asm/spinlock_types.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
Andi Kleen8b059d22006-09-26 10:52:32 +020019static inline int __raw_spin_is_locked(raw_spinlock_t *lock)
20{
21 return *(volatile signed int *)(&(lock)->slock) <= 0;
22}
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070024static inline void __raw_spin_lock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Andi Kleen8b059d22006-09-26 10:52:32 +020026 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 Torvalds1da177e2005-04-16 15:20:36 -070036}
37
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070038#define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070040static inline int __raw_spin_trylock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Andi Kleen485832a2005-11-05 17:25:54 +010042 int oldval;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070043
Andi Kleen8b059d22006-09-26 10:52:32 +020044 asm volatile(
Andi Kleen485832a2005-11-05 17:25:54 +010045 "xchgl %0,%1"
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070046 :"=q" (oldval), "=m" (lock->slock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 :"0" (0) : "memory");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return oldval > 0;
50}
51
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070052static inline void __raw_spin_unlock(raw_spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Andi Kleen8b059d22006-09-26 10:52:32 +020054 asm volatile("movl $1,%0" :"=m" (lock->slock) :: "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Andi Kleen8b059d22006-09-26 10:52:32 +020057static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock)
58{
59 while (__raw_spin_is_locked(lock))
60 cpu_relax();
61}
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
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 Molnarfb1c8f92005-09-10 00:25:56 -070072 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * On x86, we implement read-write locks as a 32-bit counter
74 * with the high bit (sign) being the "contended" bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Andi Kleen8b059d22006-09-26 10:52:32 +020077static inline int __raw_read_can_lock(raw_rwlock_t *lock)
78{
79 return (int)(lock)->lock > 0;
80}
81
82static inline int __raw_write_can_lock(raw_rwlock_t *lock)
83{
84 return (lock)->lock == RW_LOCK_BIAS;
85}
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070086
87static inline void __raw_read_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Andi Kleen8b059d22006-09-26 10:52:32 +020089 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 Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070096static inline void __raw_write_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Andi Kleen8b059d22006-09-26 10:52:32 +020098 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 Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700105static inline int __raw_read_trylock(raw_rwlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
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 Molnarfb1c8f92005-09-10 00:25:56 -0700115static inline int __raw_write_trylock(raw_rwlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
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 Molnarfb1c8f92005-09-10 00:25:56 -0700124static inline void __raw_read_unlock(raw_rwlock_t *rw)
125{
Andi Kleen841be8d2006-08-30 19:37:13 +0200126 asm volatile(LOCK_PREFIX " ; incl %0" :"=m" (rw->lock) : : "memory");
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700127}
128
129static inline void __raw_write_unlock(raw_rwlock_t *rw)
130{
Andi Kleen841be8d2006-08-30 19:37:13 +0200131 asm volatile(LOCK_PREFIX " ; addl $" RW_LOCK_BIAS_STR ",%0"
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700132 : "=m" (rw->lock) : : "memory");
133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#endif /* __ASM_SPINLOCK_H */