2 * Out of line spinlock code.
4 * Copyright IBM Corp. 2004, 2006
5 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
8 #include <linux/types.h>
9 #include <linux/module.h>
10 #include <linux/spinlock.h>
11 #include <linux/init.h>
12 #include <linux/smp.h>
15 int spin_retry = 1000;
18 * spin_retry= parameter
20 static int __init spin_retry_setup(char *str)
22 spin_retry = simple_strtoul(str, &str, 0);
25 __setup("spin_retry=", spin_retry_setup);
27 void arch_spin_lock_wait(arch_spinlock_t *lp)
29 unsigned int cpu = SPINLOCK_LOCKVAL;
35 if (!owner || smp_vcpu_scheduled(~owner)) {
38 if (arch_spin_is_locked(lp))
40 if (_raw_compare_and_swap(&lp->lock, 0, cpu))
42 } while (count-- > 0);
48 smp_yield_cpu(~owner);
49 if (_raw_compare_and_swap(&lp->lock, 0, cpu))
53 EXPORT_SYMBOL(arch_spin_lock_wait);
55 void arch_spin_lock_wait_flags(arch_spinlock_t *lp, unsigned long flags)
57 unsigned int cpu = SPINLOCK_LOCKVAL;
61 local_irq_restore(flags);
64 if (!owner || smp_vcpu_scheduled(~owner)) {
67 if (arch_spin_is_locked(lp))
70 if (_raw_compare_and_swap(&lp->lock, 0, cpu))
72 local_irq_restore(flags);
73 } while (count-- > 0);
79 smp_yield_cpu(~owner);
81 if (_raw_compare_and_swap(&lp->lock, 0, cpu))
83 local_irq_restore(flags);
86 EXPORT_SYMBOL(arch_spin_lock_wait_flags);
88 void arch_spin_relax(arch_spinlock_t *lp)
90 unsigned int cpu = lp->lock;
92 if (MACHINE_IS_VM || MACHINE_IS_KVM ||
93 !smp_vcpu_scheduled(~cpu))
97 EXPORT_SYMBOL(arch_spin_relax);
99 int arch_spin_trylock_retry(arch_spinlock_t *lp)
103 for (count = spin_retry; count > 0; count--) {
104 if (arch_spin_is_locked(lp))
106 if (arch_spin_trylock_once(lp))
111 EXPORT_SYMBOL(arch_spin_trylock_retry);
113 void _raw_read_lock_wait(arch_rwlock_t *rw)
116 int count = spin_retry;
123 if (!arch_read_can_lock(rw))
125 old = rw->lock & 0x7fffffffU;
126 if (_raw_compare_and_swap(&rw->lock, old, old + 1))
130 EXPORT_SYMBOL(_raw_read_lock_wait);
132 void _raw_read_lock_wait_flags(arch_rwlock_t *rw, unsigned long flags)
135 int count = spin_retry;
137 local_irq_restore(flags);
143 if (!arch_read_can_lock(rw))
145 old = rw->lock & 0x7fffffffU;
147 if (_raw_compare_and_swap(&rw->lock, old, old + 1))
151 EXPORT_SYMBOL(_raw_read_lock_wait_flags);
153 int _raw_read_trylock_retry(arch_rwlock_t *rw)
156 int count = spin_retry;
158 while (count-- > 0) {
159 if (!arch_read_can_lock(rw))
161 old = rw->lock & 0x7fffffffU;
162 if (_raw_compare_and_swap(&rw->lock, old, old + 1))
167 EXPORT_SYMBOL(_raw_read_trylock_retry);
169 void _raw_write_lock_wait(arch_rwlock_t *rw)
171 int count = spin_retry;
178 if (!arch_write_can_lock(rw))
180 if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000))
184 EXPORT_SYMBOL(_raw_write_lock_wait);
186 void _raw_write_lock_wait_flags(arch_rwlock_t *rw, unsigned long flags)
188 int count = spin_retry;
190 local_irq_restore(flags);
196 if (!arch_write_can_lock(rw))
199 if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000))
203 EXPORT_SYMBOL(_raw_write_lock_wait_flags);
205 int _raw_write_trylock_retry(arch_rwlock_t *rw)
207 int count = spin_retry;
209 while (count-- > 0) {
210 if (!arch_write_can_lock(rw))
212 if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000))
217 EXPORT_SYMBOL(_raw_write_trylock_retry);