1 #ifndef _M68K_SEMAPHORE_H
2 #define _M68K_SEMAPHORE_H
3
4 #define RW_LOCK_BIAS 0x01000000
5
6 #ifndef __ASSEMBLY__
7
8 #include <linux/linkage.h>
9 #include <linux/wait.h>
10 #include <linux/spinlock.h>
11
12 #include <asm/system.h>
13 #include <asm/atomic.h>
14
15 /*
16 * Interrupt-safe semaphores..
17 *
18 * (C) Copyright 1996 Linus Torvalds
19 *
20 * m68k version by Andreas Schwab
21 */
22
23
24 struct semaphore {
25 atomic_t count;
26 atomic_t waking;
27 wait_queue_head_t wait;
28 #if WAITQUEUE_DEBUG
29 long __magic;
30 #endif
31 };
32
33 #if WAITQUEUE_DEBUG
34 # define __SEM_DEBUG_INIT(name) \
35 , (long)&(name).__magic
36 #else
37 # define __SEM_DEBUG_INIT(name)
38 #endif
39
40 #define __SEMAPHORE_INITIALIZER(name,count) \
41 { ATOMIC_INIT(count), ATOMIC_INIT(0), __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
42 __SEM_DEBUG_INIT(name) }
43
44 #define __MUTEX_INITIALIZER(name) \
45 __SEMAPHORE_INITIALIZER(name,1)
46
47 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
48 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
49
50 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
51 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
52
53 extern inline void sema_init (struct semaphore *sem, int val)
54 {
55 *sem = (struct semaphore)__SEMAPHORE_INITIALIZER(*sem, val);
56 }
57
58 static inline void init_MUTEX (struct semaphore *sem)
59 {
60 sema_init(sem, 1);
61 }
62
63 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
64 {
65 sema_init(sem, 0);
66 }
67
68 asmlinkage void __down_failed(void /* special register calling convention */);
69 asmlinkage int __down_failed_interruptible(void /* params in registers */);
70 asmlinkage int __down_failed_trylock(void /* params in registers */);
71 asmlinkage void __up_wakeup(void /* special register calling convention */);
72
73 asmlinkage void __down(struct semaphore * sem);
74 asmlinkage int __down_interruptible(struct semaphore * sem);
75 asmlinkage int __down_trylock(struct semaphore * sem);
76 asmlinkage void __up(struct semaphore * sem);
77
78 /*
79 * This is ugly, but we want the default case to fall through.
80 * "down_failed" is a special asm handler that calls the C
81 * routine that actually waits. See arch/m68k/lib/semaphore.S
82 */
83 extern inline void down(struct semaphore * sem)
84 {
85 register struct semaphore *sem1 __asm__ ("%a1") = sem;
86
87 #if WAITQUEUE_DEBUG
88 CHECK_MAGIC(sem->__magic);
89 #endif
90
91 __asm__ __volatile__(
92 "| atomic down operation\n\t"
93 "subql #1,%0@\n\t"
94 "jmi 2f\n\t"
95 "1:\n"
96 ".section .text.lock,\"ax\"\n"
97 ".even\n"
98 "2:\tpea 1b\n\t"
99 "jbra __down_failed\n"
100 ".previous"
101 : /* no outputs */
102 : "a" (sem1)
103 : "memory");
104 }
105
106 extern inline int down_interruptible(struct semaphore * sem)
107 {
108 register struct semaphore *sem1 __asm__ ("%a1") = sem;
109 register int result __asm__ ("%d0");
110
111 #if WAITQUEUE_DEBUG
112 CHECK_MAGIC(sem->__magic);
113 #endif
114
115 __asm__ __volatile__(
116 "| atomic interruptible down operation\n\t"
117 "subql #1,%1@\n\t"
118 "jmi 2f\n\t"
119 "clrl %0\n"
120 "1:\n"
121 ".section .text.lock,\"ax\"\n"
122 ".even\n"
123 "2:\tpea 1b\n\t"
124 "jbra __down_failed_interruptible\n"
125 ".previous"
126 : "=d" (result)
127 : "a" (sem1)
128 : "%d0", "memory");
129 return result;
130 }
131
132 extern inline int down_trylock(struct semaphore * sem)
133 {
134 register struct semaphore *sem1 __asm__ ("%a1") = sem;
135 register int result __asm__ ("%d0");
136
137 #if WAITQUEUE_DEBUG
138 CHECK_MAGIC(sem->__magic);
139 #endif
140
141 __asm__ __volatile__(
142 "| atomic down trylock operation\n\t"
143 "subql #1,%1@\n\t"
144 "jmi 2f\n\t"
145 "clrl %0\n"
146 "1:\n"
147 ".section .text.lock,\"ax\"\n"
148 ".even\n"
149 "2:\tpea 1b\n\t"
150 "jbra __down_failed_trylock\n"
151 ".previous"
152 : "=d" (result)
153 : "a" (sem1)
154 : "%d0", "memory");
155 return result;
156 }
157
158 /*
159 * Note! This is subtle. We jump to wake people up only if
160 * the semaphore was negative (== somebody was waiting on it).
161 * The default case (no contention) will result in NO
162 * jumps for both down() and up().
163 */
164 extern inline void up(struct semaphore * sem)
165 {
166 register struct semaphore *sem1 __asm__ ("%a1") = sem;
167
168 #if WAITQUEUE_DEBUG
169 CHECK_MAGIC(sem->__magic);
170 #endif
171
172 __asm__ __volatile__(
173 "| atomic up operation\n\t"
174 "addql #1,%0@\n\t"
175 "jle 2f\n"
176 "1:\n"
177 ".section .text.lock,\"ax\"\n"
178 ".even\n"
179 "2:\t"
180 "pea 1b\n\t"
181 "jbra __up_wakeup\n"
182 ".previous"
183 : /* no outputs */
184 : "a" (sem1)
185 : "memory");
186 }
187
188
189 /* rw mutexes (should that be mutices? =) -- throw rw
190 * spinlocks and semaphores together, and this is what we
191 * end up with...
192 *
193 * m68k version by Roman Zippel
194 */
195
196 struct rw_semaphore {
197 atomic_t count;
198 volatile unsigned char write_bias_granted;
199 volatile unsigned char read_bias_granted;
200 volatile unsigned char pad1;
201 volatile unsigned char pad2;
202 wait_queue_head_t wait;
203 wait_queue_head_t write_bias_wait;
204 #if WAITQUEUE_DEBUG
205 long __magic;
206 atomic_t readers;
207 atomic_t writers;
208 #endif
209 };
210
211 #if WAITQUEUE_DEBUG
212 #define __RWSEM_DEBUG_INIT , ATOMIC_INIT(0), ATOMIC_INIT(0)
213 #else
214 #define __RWSEM_DEBUG_INIT /* */
215 #endif
216
217 #define __RWSEM_INITIALIZER(name,count) \
218 { ATOMIC_INIT(count), 0, 0, 0, 0, __WAIT_QUEUE_HEAD_INITIALIZER((name).wait), \
219 __WAIT_QUEUE_HEAD_INITIALIZER((name).write_bias_wait) \
220 __SEM_DEBUG_INIT(name) __RWSEM_DEBUG_INIT }
221
222 #define __DECLARE_RWSEM_GENERIC(name,count) \
223 struct rw_semaphore name = __RWSEM_INITIALIZER(name,count)
224
225 #define DECLARE_RWSEM(name) __DECLARE_RWSEM_GENERIC(name,RW_LOCK_BIAS)
226 #define DECLARE_RWSEM_READ_LOCKED(name) __DECLARE_RWSEM_GENERIC(name,RW_LOCK_BIAS-1)
227 #define DECLARE_RWSEM_WRITE_LOCKED(name) __DECLARE_RWSEM_GENERIC(name,0)
228
229 extern inline void init_rwsem(struct rw_semaphore *sem)
230 {
231 atomic_set(&sem->count, RW_LOCK_BIAS);
232 sem->read_bias_granted = 0;
233 sem->write_bias_granted = 0;
234 init_waitqueue_head(&sem->wait);
235 init_waitqueue_head(&sem->write_bias_wait);
236 #if WAITQUEUE_DEBUG
237 sem->__magic = (long)&sem->__magic;
238 atomic_set(&sem->readers, 0);
239 atomic_set(&sem->writers, 0);
240 #endif
241 }
242
243 extern inline void down_read(struct rw_semaphore *sem)
244 {
245 register struct rw_semaphore *__sem __asm__ ("%a1") = sem;
246
247 #if WAITQUEUE_DEBUG
248 if (sem->__magic != (long)&sem->__magic)
249 BUG();
250 #endif
251 __asm__ __volatile__(
252 "| atomic down_read operation\n\t"
253 "subql #1,%0@\n\t"
254 "jmi 2f\n"
255 "1:\n"
256 ".section .text.lock,\"ax\"\n"
257 ".even\n"
258 "2:\n\t"
259 "pea 1b\n\t"
260 "jbra __down_read_failed\n"
261 ".previous"
262 : /* no outputs */
263 : "a" (__sem)
264 : "memory");
265 #if WAITQUEUE_DEBUG
266 if (sem->write_bias_granted)
267 BUG();
268 if (atomic_read(&sem->writers))
269 BUG();
270 atomic_inc(&sem->readers);
271 #endif
272 }
273
274 extern inline void down_write(struct rw_semaphore *sem)
275 {
276 register struct rw_semaphore *__sem __asm__ ("%a1") = sem;
277
278 #if WAITQUEUE_DEBUG
279 if (sem->__magic != (long)&sem->__magic)
280 BUG();
281 #endif
282 __asm__ __volatile__(
283 "| atomic down_write operation\n\t"
284 "subl %1,%0@\n\t"
285 "jne 2f\n"
286 "1:\n"
287 ".section .text.lock,\"ax\"\n"
288 ".even\n"
289 "2:\n\t"
290 "pea 1b\n\t"
291 "jbra __down_write_failed\n"
292 ".previous"
293 : /* no outputs */
294 : "a" (__sem), "id" (RW_LOCK_BIAS)
295 : "memory");
296 #if WAITQUEUE_DEBUG
297 if (atomic_read(&sem->writers))
298 BUG();
299 if (atomic_read(&sem->readers))
300 BUG();
301 if (sem->read_bias_granted)
302 BUG();
303 if (sem->write_bias_granted)
304 BUG();
305 atomic_inc(&sem->writers);
306 #endif
307 }
308
309 /* When a reader does a release, the only significant
310 * case is when there was a writer waiting, and we've
311 * bumped the count to 0: we must wake the writer up.
312 */
313 extern inline void __up_read(struct rw_semaphore *sem)
314 {
315 register struct rw_semaphore *__sem __asm__ ("%a1") = sem;
316
317 __asm__ __volatile__(
318 "| atomic up_read operation\n\t"
319 "addql #1,%0@\n\t"
320 "jeq 2f\n"
321 "1:\n"
322 ".section .text.lock,\"ax\"\n"
323 ".even\n"
324 "2:\n\t"
325 "pea 1b\n\t"
326 "jbra __rwsem_wake\n"
327 ".previous"
328 : /* no outputs */
329 : "a" (__sem)
330 : "memory");
331 }
332
333 extern inline void up_read(struct rw_semaphore *sem)
334 {
335 #if WAITQUEUE_DEBUG
336 if (sem->write_bias_granted)
337 BUG();
338 if (atomic_read(&sem->writers))
339 BUG();
340 atomic_dec(&sem->readers);
341 #endif
342 __up_read(sem);
343 }
344
345 /* releasing the writer is easy -- just release it and
346 * wake up any sleepers.
347 */
348 extern inline void __up_write(struct rw_semaphore *sem)
349 {
350 register struct rw_semaphore *__sem __asm__ ("%a1") = sem;
351
352 __asm__ __volatile__(
353 "| atomic up_write operation\n\t"
354 "addl %1,%0@\n\t"
355 "jcs 2f\n"
356 "1:\n"
357 ".section .text.lock,\"ax\"\n"
358 ".even\n"
359 "2:\n\t"
360 "pea 1b\n\t"
361 "jbra __rwsem_wake\n"
362 ".previous"
363 : /* no outputs */
364 : "a" (__sem), "id" (RW_LOCK_BIAS)
365 : "memory");
366 }
367
368 extern inline void up_write(struct rw_semaphore *sem)
369 {
370 #if WAITQUEUE_DEBUG
371 if (sem->read_bias_granted)
372 BUG();
373 if (sem->write_bias_granted)
374 BUG();
375 if (atomic_read(&sem->readers))
376 BUG();
377 if (atomic_read(&sem->writers) != 1)
378 BUG();
379 atomic_dec(&sem->writers);
380 #endif
381 __up_write(sem);
382 }
383 #endif /* __ASSEMBLY__ */
384
385 #endif
386
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.