spinlock.h 3.2 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 59 60 61 62
{
	volatile unsigned int *a;
	int ret;

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

	return ret;
}

/*
 * Read-write spinlocks, allowing multiple readers
 * but only one writer.
 */

I
Ingo Molnar 已提交
63
#define __raw_read_trylock(lock) generic__raw_read_trylock(lock)
L
Linus Torvalds 已提交
64 65 66 67

/* read_lock, read_unlock are pretty straightforward.  Of course it somehow
 * sucks we end up saving/restoring flags twice for read_lock_irqsave aso. */

I
Ingo Molnar 已提交
68
static  __inline__ void __raw_read_lock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
69
{
I
Ingo Molnar 已提交
70
	__raw_spin_lock(&rw->lock);
L
Linus Torvalds 已提交
71 72 73

	rw->counter++;

I
Ingo Molnar 已提交
74
	__raw_spin_unlock(&rw->lock);
L
Linus Torvalds 已提交
75 76
}

I
Ingo Molnar 已提交
77
static  __inline__ void __raw_read_unlock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
78
{
I
Ingo Molnar 已提交
79
	__raw_spin_lock(&rw->lock);
L
Linus Torvalds 已提交
80 81 82

	rw->counter--;

I
Ingo Molnar 已提交
83
	__raw_spin_unlock(&rw->lock);
L
Linus Torvalds 已提交
84 85 86 87 88 89 90 91 92 93 94
}

/* write_lock is less trivial.  We optimistically grab the lock and check
 * if we surprised any readers.  If so we release the lock and wait till
 * they're all gone before trying again
 *
 * Also note that we don't use the _irqsave / _irqrestore suffixes here.
 * If we're called with interrupts enabled and we've got readers (or other
 * writers) in interrupt handlers someone fucked up and we'd dead-lock
 * sooner or later anyway.   prumpf */

I
Ingo Molnar 已提交
95
static  __inline__ void __raw_write_lock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
96 97
{
retry:
I
Ingo Molnar 已提交
98
	__raw_spin_lock(&rw->lock);
L
Linus Torvalds 已提交
99 100 101

	if(rw->counter != 0) {
		/* this basically never happens */
I
Ingo Molnar 已提交
102
		__raw_spin_unlock(&rw->lock);
L
Linus Torvalds 已提交
103

I
Ingo Molnar 已提交
104 105
		while (rw->counter != 0)
			cpu_relax();
L
Linus Torvalds 已提交
106 107 108 109 110 111 112 113 114 115

		goto retry;
	}

	/* got it.  now leave without unlocking */
	rw->counter = -1; /* remember we are locked */
}

/* write_unlock is absolutely trivial - we don't have to wait for anything */

I
Ingo Molnar 已提交
116
static  __inline__ void __raw_write_unlock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
117 118
{
	rw->counter = 0;
I
Ingo Molnar 已提交
119
	__raw_spin_unlock(&rw->lock);
L
Linus Torvalds 已提交
120 121
}

I
Ingo Molnar 已提交
122
static  __inline__ int __raw_write_trylock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
123
{
I
Ingo Molnar 已提交
124
	__raw_spin_lock(&rw->lock);
L
Linus Torvalds 已提交
125 126
	if (rw->counter != 0) {
		/* this basically never happens */
I
Ingo Molnar 已提交
127
		__raw_spin_unlock(&rw->lock);
L
Linus Torvalds 已提交
128 129 130 131 132 133 134 135 136

		return 0;
	}

	/* got it.  now leave without unlocking */
	rw->counter = -1; /* remember we are locked */
	return 1;
}

137 138 139 140 141
/*
 * 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 已提交
142
{
143
	return rw->counter >= 0;
L
Linus Torvalds 已提交
144 145
}

146 147 148 149 150
/*
 * 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 已提交
151
{
152
	return !rw->counter;
L
Linus Torvalds 已提交
153 154 155
}

#endif /* __ASM_SPINLOCK_H */