1 #ifndef __ASM_SPINLOCK_H
2 #define __ASM_SPINLOCK_H
4 #include <asm/system.h>
6 /* Note that PA-RISC has to use `1' to mean unlocked and `0' to mean locked
7 * since it only has load-and-zero.
10 #undef SPIN_LOCK_UNLOCKED
11 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 1 }
13 #define spin_lock_init(x) do { (x)->lock = 1; } while(0)
15 #define spin_is_locked(x) ((x)->lock == 0)
17 #define spin_unlock_wait(x) do { barrier(); } while(((volatile spinlock_t *)(x))->lock == 0)
20 #define _raw_spin_lock(x) do { \
21 while (__ldcw (&(x)->lock) == 0) \
22 while (((x)->lock) == 0) ; } while (0)
25 #define _raw_spin_lock(x) \
26 do { while(__ldcw(&(x)->lock) == 0); } while(0)
29 #define _raw_spin_unlock(x) \
30 do { (x)->lock = 1; } while(0)
32 #define _raw_spin_trylock(x) (__ldcw(&(x)->lock) != 0)
37 * Read-write spinlocks, allowing multiple readers
38 * but only one writer.
45 #define RW_LOCK_UNLOCKED (rwlock_t) { {1}, 0 }
47 #define rwlock_init(lp) do { *(lp) = RW_LOCK_UNLOCKED; } while (0)
49 #define rwlock_is_locked(lp) ((lp)->counter != 0)
51 /* read_lock, read_unlock are pretty straightforward. Of course it somehow
52 * sucks we end up saving/restoring flags twice for read_lock_irqsave aso. */
54 static __inline__ void _raw_read_lock(rwlock_t *rw)
57 local_irq_save(flags);
58 _raw_spin_lock(&rw->lock);
62 _raw_spin_unlock(&rw->lock);
63 local_irq_restore(flags);
66 static __inline__ void _raw_read_unlock(rwlock_t *rw)
69 local_irq_save(flags);
70 _raw_spin_lock(&rw->lock);
74 _raw_spin_unlock(&rw->lock);
75 local_irq_restore(flags);
78 /* write_lock is less trivial. We optimistically grab the lock and check
79 * if we surprised any readers. If so we release the lock and wait till
80 * they're all gone before trying again
82 * Also note that we don't use the _irqsave / _irqrestore suffixes here.
83 * If we're called with interrupts enabled and we've got readers (or other
84 * writers) in interrupt handlers someone fucked up and we'd dead-lock
85 * sooner or later anyway. prumpf */
87 static __inline__ void _raw_write_lock(rwlock_t *rw)
90 _raw_spin_lock(&rw->lock);
92 if(rw->counter != 0) {
93 /* this basically never happens */
94 _raw_spin_unlock(&rw->lock);
96 while(rw->counter != 0);
101 /* got it. now leave without unlocking */
102 rw->counter = -1; /* remember we are locked */
105 /* write_unlock is absolutely trivial - we don't have to wait for anything */
107 static __inline__ void _raw_write_unlock(rwlock_t *rw)
110 _raw_spin_unlock(&rw->lock);
113 static __inline__ int is_read_locked(rwlock_t *rw)
115 return rw->counter > 0;
118 static __inline__ int is_write_locked(rwlock_t *rw)
120 return rw->counter < 0;
123 #endif /* __ASM_SPINLOCK_H */