spinlock.h 4.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
#ifndef __ASM_SPINLOCK_H
#define __ASM_SPINLOCK_H

#include <asm/system.h>
I
Ingo Molnar 已提交
5 6
#include <asm/processor.h>
#include <asm/spinlock_types.h>
L
Linus Torvalds 已提交
7

I
Ingo Molnar 已提交
8
static inline int __raw_spin_is_locked(raw_spinlock_t *x)
L
Linus Torvalds 已提交
9 10 11 12 13
{
	volatile unsigned int *a = __ldcw_align(x);
	return *a == 0;
}

14
#define __raw_spin_lock(lock) __raw_spin_lock_flags(lock, 0)
I
Ingo Molnar 已提交
15 16
#define __raw_spin_unlock_wait(x) \
		do { cpu_relax(); } while (__raw_spin_is_locked(x))
L
Linus Torvalds 已提交
17

18 19
static inline void __raw_spin_lock_flags(raw_spinlock_t *x,
					 unsigned long flags)
L
Linus Torvalds 已提交
20 21 22 23 24 25
{
	volatile unsigned int *a;

	mb();
	a = __ldcw_align(x);
	while (__ldcw(a) == 0)
26 27 28 29 30 31 32
		while (*a == 0)
			if (flags & PSW_SM_I) {
				local_irq_enable();
				cpu_relax();
				local_irq_disable();
			} else
				cpu_relax();
L
Linus Torvalds 已提交
33 34 35
	mb();
}

I
Ingo Molnar 已提交
36
static inline void __raw_spin_unlock(raw_spinlock_t *x)
L
Linus Torvalds 已提交
37 38 39 40 41 42 43 44
{
	volatile unsigned int *a;
	mb();
	a = __ldcw_align(x);
	*a = 1;
	mb();
}

I
Ingo Molnar 已提交
45
static inline int __raw_spin_trylock(raw_spinlock_t *x)
L
Linus Torvalds 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58
{
	volatile unsigned int *a;
	int ret;

	mb();
	a = __ldcw_align(x);
        ret = __ldcw(a) != 0;
	mb();

	return ret;
}

/*
59
 * Read-write spinlocks, allowing multiple readers but only one writer.
60 61 62 63 64 65 66 67
 * Linux rwlocks are unfair to writers; they can be starved for an indefinite
 * time by readers.  With care, they can also be taken in interrupt context.
 *
 * In the PA-RISC implementation, we have a spinlock and a counter.
 * Readers use the lock to serialise their access to the counter (which
 * records how many readers currently hold the lock).
 * Writers hold the spinlock, preventing any readers or other writers from
 * grabbing the rwlock.
L
Linus Torvalds 已提交
68 69
 */

70 71
/* Note that we have to ensure interrupts are disabled in case we're
 * interrupted by some other code that wants to grab the same read lock */
I
Ingo Molnar 已提交
72
static  __inline__ void __raw_read_lock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
73
{
74 75
	unsigned long flags;
	local_irq_save(flags);
76
	__raw_spin_lock_flags(&rw->lock, flags);
L
Linus Torvalds 已提交
77
	rw->counter++;
I
Ingo Molnar 已提交
78
	__raw_spin_unlock(&rw->lock);
79
	local_irq_restore(flags);
L
Linus Torvalds 已提交
80 81
}

82 83
/* Note that we have to ensure interrupts are disabled in case we're
 * interrupted by some other code that wants to grab the same read lock */
I
Ingo Molnar 已提交
84
static  __inline__ void __raw_read_unlock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
85
{
86 87
	unsigned long flags;
	local_irq_save(flags);
88
	__raw_spin_lock_flags(&rw->lock, flags);
L
Linus Torvalds 已提交
89
	rw->counter--;
I
Ingo Molnar 已提交
90
	__raw_spin_unlock(&rw->lock);
91
	local_irq_restore(flags);
L
Linus Torvalds 已提交
92 93
}

94 95
/* Note that we have to ensure interrupts are disabled in case we're
 * interrupted by some other code that wants to grab the same read lock */
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
static __inline__ int __raw_read_trylock(raw_rwlock_t *rw)
{
	unsigned long flags;
 retry:
	local_irq_save(flags);
	if (__raw_spin_trylock(&rw->lock)) {
		rw->counter++;
		__raw_spin_unlock(&rw->lock);
		local_irq_restore(flags);
		return 1;
	}

	local_irq_restore(flags);
	/* If write-locked, we fail to acquire the lock */
	if (rw->counter < 0)
		return 0;

	/* Wait until we have a realistic chance at the lock */
	while (__raw_spin_is_locked(&rw->lock) && rw->counter >= 0)
		cpu_relax();

	goto retry;
}
L
Linus Torvalds 已提交
119

120 121
/* Note that we have to ensure interrupts are disabled in case we're
 * interrupted by some other code that wants to read_trylock() this lock */
122
static __inline__ void __raw_write_lock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
123
{
124
	unsigned long flags;
L
Linus Torvalds 已提交
125
retry:
126
	local_irq_save(flags);
127
	__raw_spin_lock_flags(&rw->lock, flags);
L
Linus Torvalds 已提交
128

129
	if (rw->counter != 0) {
I
Ingo Molnar 已提交
130
		__raw_spin_unlock(&rw->lock);
131
		local_irq_restore(flags);
L
Linus Torvalds 已提交
132

I
Ingo Molnar 已提交
133 134
		while (rw->counter != 0)
			cpu_relax();
L
Linus Torvalds 已提交
135 136 137 138

		goto retry;
	}

139 140 141
	rw->counter = -1; /* mark as write-locked */
	mb();
	local_irq_restore(flags);
L
Linus Torvalds 已提交
142 143
}

144
static __inline__ void __raw_write_unlock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
145 146
{
	rw->counter = 0;
I
Ingo Molnar 已提交
147
	__raw_spin_unlock(&rw->lock);
L
Linus Torvalds 已提交
148 149
}

150 151
/* Note that we have to ensure interrupts are disabled in case we're
 * interrupted by some other code that wants to read_trylock() this lock */
152
static __inline__ int __raw_write_trylock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
153
{
154 155 156 157 158 159 160 161 162 163 164 165
	unsigned long flags;
	int result = 0;

	local_irq_save(flags);
	if (__raw_spin_trylock(&rw->lock)) {
		if (rw->counter == 0) {
			rw->counter = -1;
			result = 1;
		} else {
			/* Read-locked.  Oh well. */
			__raw_spin_unlock(&rw->lock);
		}
L
Linus Torvalds 已提交
166
	}
167
	local_irq_restore(flags);
L
Linus Torvalds 已提交
168

169
	return result;
L
Linus Torvalds 已提交
170 171
}

172 173 174 175 176
/*
 * read_can_lock - would read_trylock() succeed?
 * @lock: the rwlock in question.
 */
static __inline__ int __raw_read_can_lock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
177
{
178
	return rw->counter >= 0;
L
Linus Torvalds 已提交
179 180
}

181 182 183 184 185
/*
 * write_can_lock - would write_trylock() succeed?
 * @lock: the rwlock in question.
 */
static __inline__ int __raw_write_can_lock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
186
{
187
	return !rw->counter;
L
Linus Torvalds 已提交
188 189
}

190 191 192 193
#define _raw_spin_relax(lock)	cpu_relax()
#define _raw_read_relax(lock)	cpu_relax()
#define _raw_write_relax(lock)	cpu_relax()

L
Linus Torvalds 已提交
194
#endif /* __ASM_SPINLOCK_H */