spinlock.h 3.3 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 8 9 10 11 12

/* Note that PA-RISC has to use `1' to mean unlocked and `0' to mean locked
 * since it only has load-and-zero. Moreover, at least on some PA processors,
 * the semaphore address has to be 16-byte aligned.
 */

I
Ingo Molnar 已提交
13
static inline int __raw_spin_is_locked(raw_spinlock_t *x)
L
Linus Torvalds 已提交
14 15 16 17 18
{
	volatile unsigned int *a = __ldcw_align(x);
	return *a == 0;
}

I
Ingo Molnar 已提交
19 20 21
#define __raw_spin_lock_flags(lock, flags) __raw_spin_lock(lock)
#define __raw_spin_unlock_wait(x) \
		do { cpu_relax(); } while (__raw_spin_is_locked(x))
L
Linus Torvalds 已提交
22

I
Ingo Molnar 已提交
23
static inline void __raw_spin_lock(raw_spinlock_t *x)
L
Linus Torvalds 已提交
24 25 26 27 28 29 30 31 32 33
{
	volatile unsigned int *a;

	mb();
	a = __ldcw_align(x);
	while (__ldcw(a) == 0)
		while (*a == 0);
	mb();
}

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

I
Ingo Molnar 已提交
43
static inline int __raw_spin_trylock(raw_spinlock_t *x)
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
{
	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 已提交
61
#define __raw_read_trylock(lock) generic__raw_read_trylock(lock)
L
Linus Torvalds 已提交
62 63 64 65

/* 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 已提交
66
static  __inline__ void __raw_read_lock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
67 68 69
{
	unsigned long flags;
	local_irq_save(flags);
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 77
	local_irq_restore(flags);
}

I
Ingo Molnar 已提交
78
static  __inline__ void __raw_read_unlock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
79 80 81
{
	unsigned long flags;
	local_irq_save(flags);
I
Ingo Molnar 已提交
82
	__raw_spin_lock(&rw->lock);
L
Linus Torvalds 已提交
83 84 85

	rw->counter--;

I
Ingo Molnar 已提交
86
	__raw_spin_unlock(&rw->lock);
L
Linus Torvalds 已提交
87 88 89 90 91 92 93 94 95 96 97 98
	local_irq_restore(flags);
}

/* 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 已提交
99
static  __inline__ void __raw_write_lock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
100 101
{
retry:
I
Ingo Molnar 已提交
102
	__raw_spin_lock(&rw->lock);
L
Linus Torvalds 已提交
103 104 105

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

I
Ingo Molnar 已提交
108 109
		while (rw->counter != 0)
			cpu_relax();
L
Linus Torvalds 已提交
110 111 112 113 114 115 116 117 118 119

		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 已提交
120
static  __inline__ void __raw_write_unlock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
121 122
{
	rw->counter = 0;
I
Ingo Molnar 已提交
123
	__raw_spin_unlock(&rw->lock);
L
Linus Torvalds 已提交
124 125
}

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

		return 0;
	}

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

I
Ingo Molnar 已提交
141
static __inline__ int __raw_is_read_locked(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
142 143 144 145
{
	return rw->counter > 0;
}

I
Ingo Molnar 已提交
146
static __inline__ int __raw_is_write_locked(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
147 148 149 150 151
{
	return rw->counter < 0;
}

#endif /* __ASM_SPINLOCK_H */