spinlock.h 3.1 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;
}

I
Ingo Molnar 已提交
14 15 16
#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 已提交
17

I
Ingo Molnar 已提交
18
static inline void __raw_spin_lock(raw_spinlock_t *x)
L
Linus Torvalds 已提交
19 20 21 22 23 24 25 26 27 28
{
	volatile unsigned int *a;

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

I
Ingo Molnar 已提交
29
static inline void __raw_spin_unlock(raw_spinlock_t *x)
L
Linus Torvalds 已提交
30 31 32 33 34 35 36 37
{
	volatile unsigned int *a;
	mb();
	a = __ldcw_align(x);
	*a = 1;
	mb();
}

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

/* 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 已提交
61
static  __inline__ void __raw_read_lock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
62 63 64
{
	unsigned long flags;
	local_irq_save(flags);
I
Ingo Molnar 已提交
65
	__raw_spin_lock(&rw->lock);
L
Linus Torvalds 已提交
66 67 68

	rw->counter++;

I
Ingo Molnar 已提交
69
	__raw_spin_unlock(&rw->lock);
L
Linus Torvalds 已提交
70 71 72
	local_irq_restore(flags);
}

I
Ingo Molnar 已提交
73
static  __inline__ void __raw_read_unlock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
74 75 76
{
	unsigned long flags;
	local_irq_save(flags);
I
Ingo Molnar 已提交
77
	__raw_spin_lock(&rw->lock);
L
Linus Torvalds 已提交
78 79 80

	rw->counter--;

I
Ingo Molnar 已提交
81
	__raw_spin_unlock(&rw->lock);
L
Linus Torvalds 已提交
82 83 84 85 86 87 88 89 90 91 92 93
	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 已提交
94
static  __inline__ void __raw_write_lock(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
95 96
{
retry:
I
Ingo Molnar 已提交
97
	__raw_spin_lock(&rw->lock);
L
Linus Torvalds 已提交
98 99 100

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

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

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

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

		return 0;
	}

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

I
Ingo Molnar 已提交
136
static __inline__ int __raw_is_read_locked(raw_rwlock_t *rw)
L
Linus Torvalds 已提交
137 138 139 140
{
	return rw->counter > 0;
}

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

#endif /* __ASM_SPINLOCK_H */