blob: 8980a876cc4e2ef0ed6120c2e72c040d5e1ec313 [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/*
Matthew Wilcox6e071852006-09-02 07:54:58 -060059 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070069static __inline__ void __raw_read_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Matthew Wilcox6e071852006-09-02 07:54:58 -060071 unsigned long flags;
72 local_irq_save(flags);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070073 __raw_spin_lock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 rw->counter++;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070075 __raw_spin_unlock(&rw->lock);
Matthew Wilcox6e071852006-09-02 07:54:58 -060076 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070079static __inline__ void __raw_read_unlock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Matthew Wilcox6e071852006-09-02 07:54:58 -060081 unsigned long flags;
82 local_irq_save(flags);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070083 __raw_spin_lock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 rw->counter--;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070085 __raw_spin_unlock(&rw->lock);
Matthew Wilcox6e071852006-09-02 07:54:58 -060086 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Matthew Wilcox6e071852006-09-02 07:54:58 -060089static __inline__ int __raw_read_trylock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Matthew Wilcox6e071852006-09-02 07:54:58 -060091 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}
112
113static __inline__ void __raw_write_lock(raw_rwlock_t *rw)
114{
115 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116retry:
Matthew Wilcox6e071852006-09-02 07:54:58 -0600117 local_irq_save(flags);
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700118 __raw_spin_lock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Matthew Wilcox6e071852006-09-02 07:54:58 -0600120 if (rw->counter != 0) {
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700121 __raw_spin_unlock(&rw->lock);
Matthew Wilcox6e071852006-09-02 07:54:58 -0600122 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700124 while (rw->counter != 0)
125 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 goto retry;
128 }
129
Matthew Wilcox6e071852006-09-02 07:54:58 -0600130 rw->counter = -1; /* mark as write-locked */
131 mb();
132 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Matthew Wilcox6e071852006-09-02 07:54:58 -0600135static __inline__ void __raw_write_unlock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 rw->counter = 0;
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700138 __raw_spin_unlock(&rw->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Matthew Wilcox6e071852006-09-02 07:54:58 -0600141static __inline__ int __raw_write_trylock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Matthew Wilcox6e071852006-09-02 07:54:58 -0600143 unsigned long flags;
144 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Matthew Wilcox6e071852006-09-02 07:54:58 -0600146 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
Matthew Wilcox6e071852006-09-02 07:54:58 -0600156 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Matthew Wilcox6e071852006-09-02 07:54:58 -0600158 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Kyle McMartinbc8846c2006-03-24 21:22:02 -0700161/*
162 * read_can_lock - would read_trylock() succeed?
163 * @lock: the rwlock in question.
164 */
165static __inline__ int __raw_read_can_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Kyle McMartinbc8846c2006-03-24 21:22:02 -0700167 return rw->counter >= 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Kyle McMartinbc8846c2006-03-24 21:22:02 -0700170/*
171 * write_can_lock - would write_trylock() succeed?
172 * @lock: the rwlock in question.
173 */
174static __inline__ int __raw_write_can_lock(raw_rwlock_t *rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Kyle McMartinbc8846c2006-03-24 21:22:02 -0700176 return !rw->counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Martin Schwidefskyef6edc92006-09-30 23:27:43 -0700179#define _raw_spin_relax(lock) cpu_relax()
180#define _raw_read_relax(lock) cpu_relax()
181#define _raw_write_relax(lock) cpu_relax()
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183#endif /* __ASM_SPINLOCK_H */