clean
[linux-2.4.21-pre4.git] / include / asm-ppc / rwsem.h
1 /*
2  * BK Id: SCCS/s.rwsem.h 1.7 08/29/02 12:10:29 benh
3  */
4 /*
5  * include/asm-ppc/rwsem.h: R/W semaphores for PPC using the stuff
6  * in lib/rwsem.c.  Adapted largely from include/asm-i386/rwsem.h
7  * by Paul Mackerras <paulus@samba.org>.
8  */
9
10 #ifndef _PPC_RWSEM_H
11 #define _PPC_RWSEM_H
12
13 #ifdef __KERNEL__
14 #include <linux/list.h>
15 #include <linux/spinlock.h>
16 #include <asm/atomic.h>
17 #include <asm/system.h>
18
19 /*
20  * the semaphore definition
21  */
22 struct rw_semaphore {
23         /* XXX this should be able to be an atomic_t  -- paulus */
24         signed long             count;
25 #define RWSEM_UNLOCKED_VALUE            0x00000000
26 #define RWSEM_ACTIVE_BIAS               0x00000001
27 #define RWSEM_ACTIVE_MASK               0x0000ffff
28 #define RWSEM_WAITING_BIAS              (-0x00010000)
29 #define RWSEM_ACTIVE_READ_BIAS          RWSEM_ACTIVE_BIAS
30 #define RWSEM_ACTIVE_WRITE_BIAS         (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
31         spinlock_t              wait_lock;
32         struct list_head        wait_list;
33 #if RWSEM_DEBUG
34         int                     debug;
35 #endif
36 };
37
38 /*
39  * initialisation
40  */
41 #if RWSEM_DEBUG
42 #define __RWSEM_DEBUG_INIT      , 0
43 #else
44 #define __RWSEM_DEBUG_INIT      /* */
45 #endif
46
47 #define __RWSEM_INITIALIZER(name) \
48         { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, \
49           LIST_HEAD_INIT((name).wait_list) \
50           __RWSEM_DEBUG_INIT }
51
52 #define DECLARE_RWSEM(name)             \
53         struct rw_semaphore name = __RWSEM_INITIALIZER(name)
54
55 extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
56 extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
57 extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem);
58
59 static inline void init_rwsem(struct rw_semaphore *sem)
60 {
61         sem->count = RWSEM_UNLOCKED_VALUE;
62         spin_lock_init(&sem->wait_lock);
63         INIT_LIST_HEAD(&sem->wait_list);
64 #if RWSEM_DEBUG
65         sem->debug = 0;
66 #endif
67 }
68
69 /*
70  * lock for reading
71  */
72 static inline void __down_read(struct rw_semaphore *sem)
73 {
74         if (atomic_inc_return((atomic_t *)(&sem->count)) >= 0)
75                 smp_wmb();
76         else
77                 rwsem_down_read_failed(sem);
78 }
79
80 static inline int __down_read_trylock(struct rw_semaphore *sem)
81 {
82         int tmp;
83
84         while ((tmp = sem->count) >= 0) {
85                 if (tmp == cmpxchg(&sem->count, tmp,
86                                    tmp + RWSEM_ACTIVE_READ_BIAS)) {
87                         smp_wmb();
88                         return 1;
89                 }
90         }
91         return 0;
92 }
93
94 /*
95  * lock for writing
96  */
97 static inline void __down_write(struct rw_semaphore *sem)
98 {
99         int tmp;
100
101         tmp = atomic_add_return(RWSEM_ACTIVE_WRITE_BIAS,
102                                 (atomic_t *)(&sem->count));
103         if (tmp == RWSEM_ACTIVE_WRITE_BIAS)
104                 smp_wmb();
105         else
106                 rwsem_down_write_failed(sem);
107 }
108
109 static inline int __down_write_trylock(struct rw_semaphore *sem)
110 {
111         int tmp;
112
113         tmp = cmpxchg(&sem->count, RWSEM_UNLOCKED_VALUE,
114                       RWSEM_ACTIVE_WRITE_BIAS);
115         smp_wmb();
116         return tmp == RWSEM_UNLOCKED_VALUE;
117 }
118
119 /*
120  * unlock after reading
121  */
122 static inline void __up_read(struct rw_semaphore *sem)
123 {
124         int tmp;
125
126         smp_wmb();
127         tmp = atomic_dec_return((atomic_t *)(&sem->count));
128         if (tmp < -1 && (tmp & RWSEM_ACTIVE_MASK) == 0)
129                 rwsem_wake(sem);
130 }
131
132 /*
133  * unlock after writing
134  */
135 static inline void __up_write(struct rw_semaphore *sem)
136 {
137         smp_wmb();
138         if (atomic_sub_return(RWSEM_ACTIVE_WRITE_BIAS,
139                               (atomic_t *)(&sem->count)) < 0)
140                 rwsem_wake(sem);
141 }
142
143 /*
144  * implement atomic add functionality
145  */
146 static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem)
147 {
148         atomic_add(delta, (atomic_t *)(&sem->count));
149 }
150
151 /*
152  * implement exchange and add functionality
153  */
154 static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem)
155 {
156         smp_mb();
157         return atomic_add_return(delta, (atomic_t *)(&sem->count));
158 }
159
160 #endif /* __KERNEL__ */
161 #endif /* _PPC_RWSEM_XADD_H */