Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | #ifndef __ASM_SPINLOCK_H |
2 | #define __ASM_SPINLOCK_H | |
3 | ||
4 | #include <asm/system.h> | |
fb1c8f93 IM |
5 | #include <asm/processor.h> |
6 | #include <asm/spinlock_types.h> | |
1da177e4 | 7 | |
fb1c8f93 | 8 | static inline int __raw_spin_is_locked(raw_spinlock_t *x) |
1da177e4 LT |
9 | { |
10 | volatile unsigned int *a = __ldcw_align(x); | |
11 | return *a == 0; | |
12 | } | |
13 | ||
08dc2ca6 | 14 | #define __raw_spin_lock(lock) __raw_spin_lock_flags(lock, 0) |
fb1c8f93 IM |
15 | #define __raw_spin_unlock_wait(x) \ |
16 | do { cpu_relax(); } while (__raw_spin_is_locked(x)) | |
1da177e4 | 17 | |
08dc2ca6 JB |
18 | static inline void __raw_spin_lock_flags(raw_spinlock_t *x, |
19 | unsigned long flags) | |
1da177e4 LT |
20 | { |
21 | volatile unsigned int *a; | |
22 | ||
23 | mb(); | |
24 | a = __ldcw_align(x); | |
25 | while (__ldcw(a) == 0) | |
08dc2ca6 JB |
26 | while (*a == 0) |
27 | if (flags & PSW_SM_I) { | |
28 | local_irq_enable(); | |
29 | cpu_relax(); | |
30 | local_irq_disable(); | |
31 | } else | |
32 | cpu_relax(); | |
1da177e4 LT |
33 | mb(); |
34 | } | |
35 | ||
fb1c8f93 | 36 | static inline void __raw_spin_unlock(raw_spinlock_t *x) |
1da177e4 LT |
37 | { |
38 | volatile unsigned int *a; | |
39 | mb(); | |
40 | a = __ldcw_align(x); | |
41 | *a = 1; | |
42 | mb(); | |
43 | } | |
44 | ||
fb1c8f93 | 45 | static inline int __raw_spin_trylock(raw_spinlock_t *x) |
1da177e4 LT |
46 | { |
47 | volatile unsigned int *a; | |
48 | int ret; | |
49 | ||
50 | mb(); | |
51 | a = __ldcw_align(x); | |
52 | ret = __ldcw(a) != 0; | |
53 | mb(); | |
54 | ||
55 | return ret; | |
56 | } | |
1da177e4 LT |
57 | |
58 | /* | |
6e071852 MW |
59 | * Read-write spinlocks, allowing multiple readers but only one writer. |
60 | * The spinlock is held by the writer, preventing any readers or other | |
61 | * writers from grabbing the rwlock. Readers use the lock to serialise their | |
62 | * access to the counter (which records how many readers currently hold the | |
63 | * lock). Linux rwlocks are unfair to writers; they can be starved for | |
64 | * an indefinite time by readers. They can also be taken in interrupt context, | |
65 | * so we have to disable interrupts when acquiring the spin lock to be sure | |
66 | * that an interrupting reader doesn't get an inconsistent view of the lock. | |
1da177e4 | 67 | */ |
1da177e4 | 68 | |
fb1c8f93 | 69 | static __inline__ void __raw_read_lock(raw_rwlock_t *rw) |
1da177e4 | 70 | { |
6e071852 MW |
71 | unsigned long flags; |
72 | local_irq_save(flags); | |
fb1c8f93 | 73 | __raw_spin_lock(&rw->lock); |
1da177e4 | 74 | rw->counter++; |
fb1c8f93 | 75 | __raw_spin_unlock(&rw->lock); |
6e071852 | 76 | local_irq_restore(flags); |
1da177e4 | 77 | } |
1da177e4 | 78 | |
fb1c8f93 | 79 | static __inline__ void __raw_read_unlock(raw_rwlock_t *rw) |
1da177e4 | 80 | { |
6e071852 MW |
81 | unsigned long flags; |
82 | local_irq_save(flags); | |
fb1c8f93 | 83 | __raw_spin_lock(&rw->lock); |
1da177e4 | 84 | rw->counter--; |
fb1c8f93 | 85 | __raw_spin_unlock(&rw->lock); |
6e071852 | 86 | local_irq_restore(flags); |
1da177e4 LT |
87 | } |
88 | ||
6e071852 MW |
89 | static __inline__ int __raw_read_trylock(raw_rwlock_t *rw) |
90 | { | |
91 | unsigned long flags; | |
92 | retry: | |
93 | local_irq_save(flags); | |
94 | if (__raw_spin_trylock(&rw->lock)) { | |
95 | rw->counter++; | |
96 | __raw_spin_unlock(&rw->lock); | |
97 | local_irq_restore(flags); | |
98 | return 1; | |
99 | } | |
100 | ||
101 | local_irq_restore(flags); | |
102 | /* If write-locked, we fail to acquire the lock */ | |
103 | if (rw->counter < 0) | |
104 | return 0; | |
105 | ||
106 | /* Wait until we have a realistic chance at the lock */ | |
107 | while (__raw_spin_is_locked(&rw->lock) && rw->counter >= 0) | |
108 | cpu_relax(); | |
109 | ||
110 | goto retry; | |
111 | } | |
1da177e4 | 112 | |
6e071852 | 113 | static __inline__ void __raw_write_lock(raw_rwlock_t *rw) |
1da177e4 | 114 | { |
6e071852 | 115 | unsigned long flags; |
1da177e4 | 116 | retry: |
6e071852 | 117 | local_irq_save(flags); |
fb1c8f93 | 118 | __raw_spin_lock(&rw->lock); |
1da177e4 | 119 | |
6e071852 | 120 | if (rw->counter != 0) { |
fb1c8f93 | 121 | __raw_spin_unlock(&rw->lock); |
6e071852 | 122 | local_irq_restore(flags); |
1da177e4 | 123 | |
fb1c8f93 IM |
124 | while (rw->counter != 0) |
125 | cpu_relax(); | |
1da177e4 LT |
126 | |
127 | goto retry; | |
128 | } | |
129 | ||
6e071852 MW |
130 | rw->counter = -1; /* mark as write-locked */ |
131 | mb(); | |
132 | local_irq_restore(flags); | |
1da177e4 | 133 | } |
1da177e4 | 134 | |
6e071852 | 135 | static __inline__ void __raw_write_unlock(raw_rwlock_t *rw) |
1da177e4 LT |
136 | { |
137 | rw->counter = 0; | |
fb1c8f93 | 138 | __raw_spin_unlock(&rw->lock); |
1da177e4 LT |
139 | } |
140 | ||
6e071852 | 141 | static __inline__ int __raw_write_trylock(raw_rwlock_t *rw) |
1da177e4 | 142 | { |
6e071852 MW |
143 | unsigned long flags; |
144 | int result = 0; | |
145 | ||
146 | local_irq_save(flags); | |
147 | if (__raw_spin_trylock(&rw->lock)) { | |
148 | if (rw->counter == 0) { | |
149 | rw->counter = -1; | |
150 | result = 1; | |
151 | } else { | |
152 | /* Read-locked. Oh well. */ | |
153 | __raw_spin_unlock(&rw->lock); | |
154 | } | |
1da177e4 | 155 | } |
6e071852 | 156 | local_irq_restore(flags); |
1da177e4 | 157 | |
6e071852 | 158 | return result; |
1da177e4 | 159 | } |
1da177e4 | 160 | |
bc8846c5 KM |
161 | /* |
162 | * read_can_lock - would read_trylock() succeed? | |
163 | * @lock: the rwlock in question. | |
164 | */ | |
165 | static __inline__ int __raw_read_can_lock(raw_rwlock_t *rw) | |
1da177e4 | 166 | { |
bc8846c5 | 167 | return rw->counter >= 0; |
1da177e4 LT |
168 | } |
169 | ||
bc8846c5 KM |
170 | /* |
171 | * write_can_lock - would write_trylock() succeed? | |
172 | * @lock: the rwlock in question. | |
173 | */ | |
174 | static __inline__ int __raw_write_can_lock(raw_rwlock_t *rw) | |
1da177e4 | 175 | { |
bc8846c5 | 176 | return !rw->counter; |
1da177e4 LT |
177 | } |
178 | ||
ef6edc97 MS |
179 | #define _raw_spin_relax(lock) cpu_relax() |
180 | #define _raw_read_relax(lock) cpu_relax() | |
181 | #define _raw_write_relax(lock) cpu_relax() | |
182 | ||
1da177e4 | 183 | #endif /* __ASM_SPINLOCK_H */ |