blob: a93960e232cffabb43de39d78a97dc9d7542f154 [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
James Bottomley08dc2ca2005-11-17 16:35:09 -050014#define __raw_spin_lock(lock) __raw_spin_lock_flags(lock, 0)
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070015#define __raw_spin_unlock_wait(x) \
16 do { cpu_relax(); } while (__raw_spin_is_locked(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
James Bottomley08dc2ca2005-11-17 16:35:09 -050018static inline void __raw_spin_lock_flags(raw_spinlock_t *x,
19 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
21 volatile unsigned int *a;
22
23 mb();
24 a = __ldcw_align(x);
25 while (__ldcw(a) == 0)
James Bottomley08dc2ca2005-11-17 16:35:09 -050026 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();
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 mb();
34}
35
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070036static inline void __raw_spin_unlock(raw_spinlock_t *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
38 volatile unsigned int *a;
39 mb();
40 a = __ldcw_align(x);
41 *a = 1;
42 mb();
43}
44
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070045static inline int __raw_spin_trylock(raw_spinlock_t *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
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}
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/*
59 * Read-write spinlocks, allowing multiple readers
60 * but only one writer.
61 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070063#define __raw_read_trylock(lock) generic__raw_read_trylock(lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* read_lock, read_unlock are pretty straightforward. Of course it somehow
66 * sucks we end up saving/restoring flags twice for read_lock_irqsave aso. */
67
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070068static __inline__ void __raw_read_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070070 __raw_spin_lock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 rw->counter++;
73
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070074 __raw_spin_unlock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070077static __inline__ void __raw_read_unlock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070079 __raw_spin_lock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 rw->counter--;
82
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070083 __raw_spin_unlock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86/* write_lock is less trivial. We optimistically grab the lock and check
87 * if we surprised any readers. If so we release the lock and wait till
88 * they're all gone before trying again
89 *
90 * Also note that we don't use the _irqsave / _irqrestore suffixes here.
91 * If we're called with interrupts enabled and we've got readers (or other
92 * writers) in interrupt handlers someone fucked up and we'd dead-lock
93 * sooner or later anyway. prumpf */
94
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070095static __inline__ void __raw_write_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97retry:
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070098 __raw_spin_lock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 if(rw->counter != 0) {
101 /* this basically never happens */
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700102 __raw_spin_unlock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700104 while (rw->counter != 0)
105 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 goto retry;
108 }
109
110 /* got it. now leave without unlocking */
111 rw->counter = -1; /* remember we are locked */
112}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/* write_unlock is absolutely trivial - we don't have to wait for anything */
115
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700116static __inline__ void __raw_write_unlock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 rw->counter = 0;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700119 __raw_spin_unlock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700122static __inline__ int __raw_write_trylock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700124 __raw_spin_lock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (rw->counter != 0) {
126 /* this basically never happens */
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700127 __raw_spin_unlock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 return 0;
130 }
131
132 /* got it. now leave without unlocking */
133 rw->counter = -1; /* remember we are locked */
134 return 1;
135}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Kyle McMartinbc8846c2006-03-24 21:22:02 -0700137/*
138 * read_can_lock - would read_trylock() succeed?
139 * @lock: the rwlock in question.
140 */
141static __inline__ int __raw_read_can_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Kyle McMartinbc8846c2006-03-24 21:22:02 -0700143 return rw->counter >= 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Kyle McMartinbc8846c2006-03-24 21:22:02 -0700146/*
147 * write_can_lock - would write_trylock() succeed?
148 * @lock: the rwlock in question.
149 */
150static __inline__ int __raw_write_can_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Kyle McMartinbc8846c2006-03-24 21:22:02 -0700152 return !rw->counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155#endif /* __ASM_SPINLOCK_H */