ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-parisc / spinlock.h
1 #ifndef __ASM_SPINLOCK_H
2 #define __ASM_SPINLOCK_H
3
4 #include <asm/system.h>
5
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.
8  */
9
10 #undef SPIN_LOCK_UNLOCKED
11 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 1 }
12
13 #define spin_lock_init(x)       do { (x)->lock = 1; } while(0)
14
15 #define spin_is_locked(x) ((x)->lock == 0)
16
17 #define spin_unlock_wait(x)     do { barrier(); } while(((volatile spinlock_t *)(x))->lock == 0)
18
19 #if 1
20 #define _raw_spin_lock(x) do { \
21         while (__ldcw (&(x)->lock) == 0) \
22                 while (((x)->lock) == 0) ; } while (0)
23
24 #else
25 #define _raw_spin_lock(x) \
26         do { while(__ldcw(&(x)->lock) == 0); } while(0)
27 #endif
28         
29 #define _raw_spin_unlock(x) \
30         do { (x)->lock = 1; } while(0)
31
32 #define _raw_spin_trylock(x) (__ldcw(&(x)->lock) != 0)
33
34
35
36 /*
37  * Read-write spinlocks, allowing multiple readers
38  * but only one writer.
39  */
40 typedef struct {
41         spinlock_t lock;
42         volatile int counter;
43 } rwlock_t;
44
45 #define RW_LOCK_UNLOCKED (rwlock_t) { {1}, 0 }
46
47 #define rwlock_init(lp) do { *(lp) = RW_LOCK_UNLOCKED; } while (0)
48
49 #define rwlock_is_locked(lp) ((lp)->counter != 0)
50
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. */
53
54 static  __inline__ void _raw_read_lock(rwlock_t *rw)
55 {
56         unsigned long flags;
57         local_irq_save(flags);
58         _raw_spin_lock(&rw->lock); 
59
60         rw->counter++;
61
62         _raw_spin_unlock(&rw->lock);
63         local_irq_restore(flags);
64 }
65
66 static  __inline__ void _raw_read_unlock(rwlock_t *rw)
67 {
68         unsigned long flags;
69         local_irq_save(flags);
70         _raw_spin_lock(&rw->lock); 
71
72         rw->counter--;
73
74         _raw_spin_unlock(&rw->lock);
75         local_irq_restore(flags);
76 }
77
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
81  *
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 */
86
87 static  __inline__ void _raw_write_lock(rwlock_t *rw)
88 {
89 retry:
90         _raw_spin_lock(&rw->lock);
91
92         if(rw->counter != 0) {
93                 /* this basically never happens */
94                 _raw_spin_unlock(&rw->lock);
95
96                 while(rw->counter != 0);
97
98                 goto retry;
99         }
100
101         /* got it.  now leave without unlocking */
102         rw->counter = -1; /* remember we are locked */
103 }
104
105 /* write_unlock is absolutely trivial - we don't have to wait for anything */
106
107 static  __inline__ void _raw_write_unlock(rwlock_t *rw)
108 {
109         rw->counter = 0;
110         _raw_spin_unlock(&rw->lock);
111 }
112
113 static __inline__ int is_read_locked(rwlock_t *rw)
114 {
115         return rw->counter > 0;
116 }
117
118 static __inline__ int is_write_locked(rwlock_t *rw)
119 {
120         return rw->counter < 0;
121 }
122
123 #endif /* __ASM_SPINLOCK_H */