qtronix.c: Handle kmalloc failure.
[linux-2.6/linux-mips.git] / kernel / spinlock.c
blob0375fcd5921df5fe390ec6a3f1c1ce402d240f80
1 /*
2 * Copyright (2004) Linus Torvalds
4 * Author: Zwane Mwaikambo <zwane@fsmlabs.com>
6 * Copyright (2004, 2005) Ingo Molnar
8 * This file contains the spinlock/rwlock implementations for the
9 * SMP and the DEBUG_SPINLOCK cases. (UP-nondebug inlines them)
12 #include <linux/config.h>
13 #include <linux/linkage.h>
14 #include <linux/preempt.h>
15 #include <linux/spinlock.h>
16 #include <linux/interrupt.h>
17 #include <linux/module.h>
20 * Generic declaration of the raw read_trylock() function,
21 * architectures are supposed to optimize this:
23 int __lockfunc generic__raw_read_trylock(raw_rwlock_t *lock)
25 __raw_read_lock(lock);
26 return 1;
28 EXPORT_SYMBOL(generic__raw_read_trylock);
30 int __lockfunc _spin_trylock(spinlock_t *lock)
32 preempt_disable();
33 if (_raw_spin_trylock(lock))
34 return 1;
36 preempt_enable();
37 return 0;
39 EXPORT_SYMBOL(_spin_trylock);
41 int __lockfunc _read_trylock(rwlock_t *lock)
43 preempt_disable();
44 if (_raw_read_trylock(lock))
45 return 1;
47 preempt_enable();
48 return 0;
50 EXPORT_SYMBOL(_read_trylock);
52 int __lockfunc _write_trylock(rwlock_t *lock)
54 preempt_disable();
55 if (_raw_write_trylock(lock))
56 return 1;
58 preempt_enable();
59 return 0;
61 EXPORT_SYMBOL(_write_trylock);
63 #if !defined(CONFIG_PREEMPT) || !defined(CONFIG_SMP)
65 void __lockfunc _read_lock(rwlock_t *lock)
67 preempt_disable();
68 _raw_read_lock(lock);
70 EXPORT_SYMBOL(_read_lock);
72 unsigned long __lockfunc _spin_lock_irqsave(spinlock_t *lock)
74 unsigned long flags;
76 local_irq_save(flags);
77 preempt_disable();
78 _raw_spin_lock_flags(lock, &flags);
79 return flags;
81 EXPORT_SYMBOL(_spin_lock_irqsave);
83 void __lockfunc _spin_lock_irq(spinlock_t *lock)
85 local_irq_disable();
86 preempt_disable();
87 _raw_spin_lock(lock);
89 EXPORT_SYMBOL(_spin_lock_irq);
91 void __lockfunc _spin_lock_bh(spinlock_t *lock)
93 local_bh_disable();
94 preempt_disable();
95 _raw_spin_lock(lock);
97 EXPORT_SYMBOL(_spin_lock_bh);
99 unsigned long __lockfunc _read_lock_irqsave(rwlock_t *lock)
101 unsigned long flags;
103 local_irq_save(flags);
104 preempt_disable();
105 _raw_read_lock(lock);
106 return flags;
108 EXPORT_SYMBOL(_read_lock_irqsave);
110 void __lockfunc _read_lock_irq(rwlock_t *lock)
112 local_irq_disable();
113 preempt_disable();
114 _raw_read_lock(lock);
116 EXPORT_SYMBOL(_read_lock_irq);
118 void __lockfunc _read_lock_bh(rwlock_t *lock)
120 local_bh_disable();
121 preempt_disable();
122 _raw_read_lock(lock);
124 EXPORT_SYMBOL(_read_lock_bh);
126 unsigned long __lockfunc _write_lock_irqsave(rwlock_t *lock)
128 unsigned long flags;
130 local_irq_save(flags);
131 preempt_disable();
132 _raw_write_lock(lock);
133 return flags;
135 EXPORT_SYMBOL(_write_lock_irqsave);
137 void __lockfunc _write_lock_irq(rwlock_t *lock)
139 local_irq_disable();
140 preempt_disable();
141 _raw_write_lock(lock);
143 EXPORT_SYMBOL(_write_lock_irq);
145 void __lockfunc _write_lock_bh(rwlock_t *lock)
147 local_bh_disable();
148 preempt_disable();
149 _raw_write_lock(lock);
151 EXPORT_SYMBOL(_write_lock_bh);
153 void __lockfunc _spin_lock(spinlock_t *lock)
155 preempt_disable();
156 _raw_spin_lock(lock);
159 EXPORT_SYMBOL(_spin_lock);
161 void __lockfunc _write_lock(rwlock_t *lock)
163 preempt_disable();
164 _raw_write_lock(lock);
167 EXPORT_SYMBOL(_write_lock);
169 #else /* CONFIG_PREEMPT: */
172 * This could be a long-held lock. We both prepare to spin for a long
173 * time (making _this_ CPU preemptable if possible), and we also signal
174 * towards that other CPU that it should break the lock ASAP.
176 * (We do this in a function because inlining it would be excessive.)
179 #define BUILD_LOCK_OPS(op, locktype) \
180 void __lockfunc _##op##_lock(locktype##_t *lock) \
182 preempt_disable(); \
183 for (;;) { \
184 if (likely(_raw_##op##_trylock(lock))) \
185 break; \
186 preempt_enable(); \
187 if (!(lock)->break_lock) \
188 (lock)->break_lock = 1; \
189 while (!op##_can_lock(lock) && (lock)->break_lock) \
190 cpu_relax(); \
191 preempt_disable(); \
193 (lock)->break_lock = 0; \
196 EXPORT_SYMBOL(_##op##_lock); \
198 unsigned long __lockfunc _##op##_lock_irqsave(locktype##_t *lock) \
200 unsigned long flags; \
202 preempt_disable(); \
203 for (;;) { \
204 local_irq_save(flags); \
205 if (likely(_raw_##op##_trylock(lock))) \
206 break; \
207 local_irq_restore(flags); \
209 preempt_enable(); \
210 if (!(lock)->break_lock) \
211 (lock)->break_lock = 1; \
212 while (!op##_can_lock(lock) && (lock)->break_lock) \
213 cpu_relax(); \
214 preempt_disable(); \
216 (lock)->break_lock = 0; \
217 return flags; \
220 EXPORT_SYMBOL(_##op##_lock_irqsave); \
222 void __lockfunc _##op##_lock_irq(locktype##_t *lock) \
224 _##op##_lock_irqsave(lock); \
227 EXPORT_SYMBOL(_##op##_lock_irq); \
229 void __lockfunc _##op##_lock_bh(locktype##_t *lock) \
231 unsigned long flags; \
233 /* */ \
234 /* Careful: we must exclude softirqs too, hence the */ \
235 /* irq-disabling. We use the generic preemption-aware */ \
236 /* function: */ \
237 /**/ \
238 flags = _##op##_lock_irqsave(lock); \
239 local_bh_disable(); \
240 local_irq_restore(flags); \
243 EXPORT_SYMBOL(_##op##_lock_bh)
246 * Build preemption-friendly versions of the following
247 * lock-spinning functions:
249 * _[spin|read|write]_lock()
250 * _[spin|read|write]_lock_irq()
251 * _[spin|read|write]_lock_irqsave()
252 * _[spin|read|write]_lock_bh()
254 BUILD_LOCK_OPS(spin, spinlock);
255 BUILD_LOCK_OPS(read, rwlock);
256 BUILD_LOCK_OPS(write, rwlock);
258 #endif /* CONFIG_PREEMPT */
260 void __lockfunc _spin_unlock(spinlock_t *lock)
262 _raw_spin_unlock(lock);
263 preempt_enable();
265 EXPORT_SYMBOL(_spin_unlock);
267 void __lockfunc _write_unlock(rwlock_t *lock)
269 _raw_write_unlock(lock);
270 preempt_enable();
272 EXPORT_SYMBOL(_write_unlock);
274 void __lockfunc _read_unlock(rwlock_t *lock)
276 _raw_read_unlock(lock);
277 preempt_enable();
279 EXPORT_SYMBOL(_read_unlock);
281 void __lockfunc _spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags)
283 _raw_spin_unlock(lock);
284 local_irq_restore(flags);
285 preempt_enable();
287 EXPORT_SYMBOL(_spin_unlock_irqrestore);
289 void __lockfunc _spin_unlock_irq(spinlock_t *lock)
291 _raw_spin_unlock(lock);
292 local_irq_enable();
293 preempt_enable();
295 EXPORT_SYMBOL(_spin_unlock_irq);
297 void __lockfunc _spin_unlock_bh(spinlock_t *lock)
299 _raw_spin_unlock(lock);
300 preempt_enable_no_resched();
301 local_bh_enable();
303 EXPORT_SYMBOL(_spin_unlock_bh);
305 void __lockfunc _read_unlock_irqrestore(rwlock_t *lock, unsigned long flags)
307 _raw_read_unlock(lock);
308 local_irq_restore(flags);
309 preempt_enable();
311 EXPORT_SYMBOL(_read_unlock_irqrestore);
313 void __lockfunc _read_unlock_irq(rwlock_t *lock)
315 _raw_read_unlock(lock);
316 local_irq_enable();
317 preempt_enable();
319 EXPORT_SYMBOL(_read_unlock_irq);
321 void __lockfunc _read_unlock_bh(rwlock_t *lock)
323 _raw_read_unlock(lock);
324 preempt_enable_no_resched();
325 local_bh_enable();
327 EXPORT_SYMBOL(_read_unlock_bh);
329 void __lockfunc _write_unlock_irqrestore(rwlock_t *lock, unsigned long flags)
331 _raw_write_unlock(lock);
332 local_irq_restore(flags);
333 preempt_enable();
335 EXPORT_SYMBOL(_write_unlock_irqrestore);
337 void __lockfunc _write_unlock_irq(rwlock_t *lock)
339 _raw_write_unlock(lock);
340 local_irq_enable();
341 preempt_enable();
343 EXPORT_SYMBOL(_write_unlock_irq);
345 void __lockfunc _write_unlock_bh(rwlock_t *lock)
347 _raw_write_unlock(lock);
348 preempt_enable_no_resched();
349 local_bh_enable();
351 EXPORT_SYMBOL(_write_unlock_bh);
353 int __lockfunc _spin_trylock_bh(spinlock_t *lock)
355 local_bh_disable();
356 preempt_disable();
357 if (_raw_spin_trylock(lock))
358 return 1;
360 preempt_enable_no_resched();
361 local_bh_enable();
362 return 0;
364 EXPORT_SYMBOL(_spin_trylock_bh);
366 int in_lock_functions(unsigned long addr)
368 /* Linker adds these: start and end of __lockfunc functions */
369 extern char __lock_text_start[], __lock_text_end[];
371 return addr >= (unsigned long)__lock_text_start
372 && addr < (unsigned long)__lock_text_end;
374 EXPORT_SYMBOL(in_lock_functions);