blob: 7c3f406a746a4f0984b07efc40e533b24d076561 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __ASM_SPINLOCK_H
2#define __ASM_SPINLOCK_H
3
4#include <asm/system.h>
Ingo Molnarfb1c8f92005-09-10 00:25:56 -07005#include <asm/processor.h>
6#include <asm/spinlock_types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
Ingo Molnarfb1c8f92005-09-10 00:25:56 -07008static inline int __raw_spin_is_locked(raw_spinlock_t *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009{
10 volatile unsigned int *a = __ldcw_align(x);
11 return *a == 0;
12}
13
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070014#define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
15#define __raw_spin_unlock_wait(x) \
16 do { cpu_relax(); } while (__raw_spin_is_locked(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070018static inline void __raw_spin_lock(raw_spinlock_t *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070019{
20 volatile unsigned int *a;
21
22 mb();
23 a = __ldcw_align(x);
24 while (__ldcw(a) == 0)
25 while (*a == 0);
26 mb();
27}
28
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070029static inline void __raw_spin_unlock(raw_spinlock_t *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 volatile unsigned int *a;
32 mb();
33 a = __ldcw_align(x);
34 *a = 1;
35 mb();
36}
37
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070038static inline int __raw_spin_trylock(raw_spinlock_t *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
40 volatile unsigned int *a;
41 int ret;
42
43 mb();
44 a = __ldcw_align(x);
45 ret = __ldcw(a) != 0;
46 mb();
47
48 return ret;
49}
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/*
52 * Read-write spinlocks, allowing multiple readers
53 * but only one writer.
54 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070056#define __raw_read_trylock(lock) generic__raw_read_trylock(lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/* read_lock, read_unlock are pretty straightforward. Of course it somehow
59 * sucks we end up saving/restoring flags twice for read_lock_irqsave aso. */
60
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070061static __inline__ void __raw_read_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 unsigned long flags;
64 local_irq_save(flags);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070065 __raw_spin_lock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 rw->counter++;
68
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070069 __raw_spin_unlock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 local_irq_restore(flags);
71}
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070073static __inline__ void __raw_read_unlock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 unsigned long flags;
76 local_irq_save(flags);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070077 __raw_spin_lock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 rw->counter--;
80
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070081 __raw_spin_unlock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 local_irq_restore(flags);
83}
84
85/* write_lock is less trivial. We optimistically grab the lock and check
86 * if we surprised any readers. If so we release the lock and wait till
87 * they're all gone before trying again
88 *
89 * Also note that we don't use the _irqsave / _irqrestore suffixes here.
90 * If we're called with interrupts enabled and we've got readers (or other
91 * writers) in interrupt handlers someone fucked up and we'd dead-lock
92 * sooner or later anyway. prumpf */
93
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070094static __inline__ void __raw_write_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96retry:
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070097 __raw_spin_lock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 if(rw->counter != 0) {
100 /* this basically never happens */
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700101 __raw_spin_unlock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700103 while (rw->counter != 0)
104 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 goto retry;
107 }
108
109 /* got it. now leave without unlocking */
110 rw->counter = -1; /* remember we are locked */
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/* write_unlock is absolutely trivial - we don't have to wait for anything */
114
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700115static __inline__ void __raw_write_unlock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 rw->counter = 0;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700118 __raw_spin_unlock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700121static __inline__ int __raw_write_trylock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700123 __raw_spin_lock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (rw->counter != 0) {
125 /* this basically never happens */
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700126 __raw_spin_unlock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 return 0;
129 }
130
131 /* got it. now leave without unlocking */
132 rw->counter = -1; /* remember we are locked */
133 return 1;
134}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700136static __inline__ int __raw_is_read_locked(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 return rw->counter > 0;
139}
140
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700141static __inline__ int __raw_is_write_locked(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 return rw->counter < 0;
144}
145
146#endif /* __ASM_SPINLOCK_H */