~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/include/asm-mips/semaphore.h

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* $Id: semaphore.h,v 1.12 1999/12/08 22:05:10 harald Exp $
  2  *
  3  * SMP- and interrupt-safe semaphores..
  4  *
  5  * This file is subject to the terms and conditions of the GNU General Public
  6  * License.  See the file "COPYING" in the main directory of this archive
  7  * for more details.
  8  *
  9  * (C) Copyright 1996  Linus Torvalds
 10  * (C) Copyright 1998, 1999, 2000  Ralf Baechle
 11  * (C) Copyright 1999, 2000  Silicon Graphics, Inc.
 12  */
 13 #ifndef _ASM_SEMAPHORE_H
 14 #define _ASM_SEMAPHORE_H
 15 
 16 #include <asm/system.h>
 17 #include <asm/atomic.h>
 18 #include <linux/spinlock.h>
 19 #include <linux/wait.h>
 20 #include <linux/config.h>
 21 
 22 struct semaphore {
 23 #ifdef __MIPSEB__
 24         atomic_t count;
 25         atomic_t waking;
 26 #else
 27         atomic_t waking;
 28         atomic_t count;
 29 #endif
 30         wait_queue_head_t wait;
 31 #if WAITQUEUE_DEBUG
 32         long __magic;
 33 #endif
 34 } __attribute__((aligned(8)));
 35 
 36 #if WAITQUEUE_DEBUG
 37 # define __SEM_DEBUG_INIT(name) \
 38                 , (long)&(name).__magic
 39 #else
 40 # define __SEM_DEBUG_INIT(name)
 41 #endif
 42 
 43 #ifdef __MIPSEB__
 44 #define __SEMAPHORE_INITIALIZER(name,count) \
 45 { ATOMIC_INIT(count), ATOMIC_INIT(0), __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
 46         __SEM_DEBUG_INIT(name) }
 47 #else
 48 #define __SEMAPHORE_INITIALIZER(name,count) \
 49 { ATOMIC_INIT(0), ATOMIC_INIT(count), __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
 50         __SEM_DEBUG_INIT(name) }
 51 #endif
 52 
 53 #define __MUTEX_INITIALIZER(name) \
 54         __SEMAPHORE_INITIALIZER(name,1)
 55 
 56 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
 57         struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
 58 
 59 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
 60 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
 61 
 62 extern inline void sema_init (struct semaphore *sem, int val)
 63 {
 64         atomic_set(&sem->count, val);
 65         atomic_set(&sem->waking, 0);
 66         init_waitqueue_head(&sem->wait);
 67 #if WAITQUEUE_DEBUG
 68         sem->__magic = (long)&sem->__magic;
 69 #endif
 70 }
 71 
 72 static inline void init_MUTEX (struct semaphore *sem)
 73 {
 74         sema_init(sem, 1);
 75 }
 76 
 77 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
 78 {
 79         sema_init(sem, 0);
 80 }
 81 
 82 asmlinkage void __down(struct semaphore * sem);
 83 asmlinkage int  __down_interruptible(struct semaphore * sem);
 84 asmlinkage int __down_trylock(struct semaphore * sem);
 85 asmlinkage void __up(struct semaphore * sem);
 86 
 87 extern inline void down(struct semaphore * sem)
 88 {
 89 #if WAITQUEUE_DEBUG
 90         CHECK_MAGIC(sem->__magic);
 91 #endif
 92         if (atomic_dec_return(&sem->count) < 0)
 93                 __down(sem);
 94 }
 95 
 96 extern inline int down_interruptible(struct semaphore * sem)
 97 {
 98         int ret = 0;
 99 
100 #if WAITQUEUE_DEBUG
101         CHECK_MAGIC(sem->__magic);
102 #endif
103         if (atomic_dec_return(&sem->count) < 0)
104                 ret = __down_interruptible(sem);
105         return ret;
106 }
107 
108 #if !defined(CONFIG_CPU_HAS_LLSC)
109 
110 extern inline int down_trylock(struct semaphore * sem)
111 {
112         int ret = 0;
113         if (atomic_dec_return(&sem->count) < 0)
114                 ret = __down_trylock(sem);
115         return ret;
116 }
117 
118 #else
119 
120 /*
121  * down_trylock returns 0 on success, 1 if we failed to get the lock.
122  *
123  * We must manipulate count and waking simultaneously and atomically.
124  * Here, we this by using ll/sc on the pair of 32-bit words.  This
125  * won't work on MIPS32 platforms, however, and must be rewritten.
126  *
127  * Pseudocode:
128  *
129  *   Decrement(sem->count)
130  *   If(sem->count >=0) {
131  *      Return(SUCCESS)                 // resource is free
132  *   } else {
133  *      If(sem->waking <= 0) {          // if no wakeup pending
134  *         Increment(sem->count)        // undo decrement
135  *         Return(FAILURE)
136  *      } else {
137  *         Decrement(sem->waking)       // otherwise "steal" wakeup
138  *         Return(SUCCESS)
139  *      }
140  *   }
141  */
142 extern inline int down_trylock(struct semaphore * sem)
143 {
144         long ret, tmp, tmp2, sub;
145 
146 #if WAITQUEUE_DEBUG
147         CHECK_MAGIC(sem->__magic);
148 #endif
149 
150         __asm__ __volatile__("
151                         .set    mips3
152 
153                 0:      lld     %1, %4
154                         dli     %3, 0x0000000100000000
155                         dsubu   %1, %3
156                         li      %0, 0
157                         bgez    %1, 2f
158                         sll     %2, %1, 0
159                         blez    %2, 1f
160                         daddiu  %1, %1, -1
161                         b       2f
162                 1:
163                         daddu   %1, %1, %3
164                         li      %0, 1
165                 2:
166                         scd     %1, %4
167                         beqz    %1, 0b
168 
169                         .set    mips0"
170                 : "=&r"(ret), "=&r"(tmp), "=&r"(tmp2), "=&r"(sub)
171                 : "m"(*sem)
172                 : "memory");
173 
174         return ret;
175 }
176 
177 #endif
178 
179 /*
180  * Note! This is subtle. We jump to wake people up only if
181  * the semaphore was negative (== somebody was waiting on it).
182  */
183 extern inline void up(struct semaphore * sem)
184 {
185 #if WAITQUEUE_DEBUG
186         CHECK_MAGIC(sem->__magic);
187 #endif
188         if (atomic_inc_return(&sem->count) <= 0)
189                 __up(sem);
190 }
191 
192 /*
193  * rw mutexes (should that be mutices? =) -- throw rw spinlocks and
194  * semaphores together, and this is what we end up with...
195  *
196  * The lock is initialized to BIAS.  This way, a writer subtracts BIAS ands
197  * gets 0 for the case of an uncontended lock.  Readers decrement by 1 and
198  * see a positive value when uncontended, negative if there are writers
199  * waiting (in which case it goes to sleep).
200  *
201  * The value 0x01000000 supports up to 128 processors and lots of processes.
202  * BIAS must be chosen such that subtracting BIAS once per CPU will result
203  * in the int remaining negative.  In terms of fairness, this should result
204  * in the lock flopping back and forth between readers and writers under
205  * heavy use.
206  *
207  * Once we start supporting machines with more than 128 CPUs, we should go
208  * for using a 64bit atomic type instead of 32bit as counter. We shall
209  * probably go for bias 0x80000000 then, so that single sethi can set it.
210  * */
211 
212 #define RW_LOCK_BIAS            0x01000000
213 
214 struct rw_semaphore {
215         atomic_t                count;
216         /* bit 0 means read bias granted;
217            bit 1 means write bias granted.  */
218         unsigned                granted;
219         wait_queue_head_t       wait;
220         wait_queue_head_t       write_bias_wait;
221 #if WAITQUEUE_DEBUG
222         long                    __magic;
223         atomic_t                readers;
224         atomic_t                writers;
225 #endif
226 };
227 
228 #if WAITQUEUE_DEBUG
229 #define __RWSEM_DEBUG_INIT      , ATOMIC_INIT(0), ATOMIC_INIT(0)
230 #else
231 #define __RWSEM_DEBUG_INIT      /* */
232 #endif
233 
234 #define __RWSEM_INITIALIZER(name,count)                                 \
235         { ATOMIC_INIT(count), 0,                                        \
236           __WAIT_QUEUE_HEAD_INITIALIZER((name).wait),                   \
237           __WAIT_QUEUE_HEAD_INITIALIZER((name).write_bias_wait)         \
238           __SEM_DEBUG_INIT(name) __RWSEM_DEBUG_INIT }
239 
240 #define __DECLARE_RWSEM_GENERIC(name,count) \
241         struct rw_semaphore name = __RWSEM_INITIALIZER(name,count)
242 
243 #define DECLARE_RWSEM(name) \
244         __DECLARE_RWSEM_GENERIC(name, RW_LOCK_BIAS)
245 #define DECLARE_RWSEM_READ_LOCKED(name) \
246         __DECLARE_RWSEM_GENERIC(name, RW_LOCK_BIAS-1)
247 #define DECLARE_RWSEM_WRITE_LOCKED(name) \
248         __DECLARE_RWSEM_GENERIC(name, 0)
249 
250 extern inline void init_rwsem(struct rw_semaphore *sem)
251 {
252         atomic_set(&sem->count, RW_LOCK_BIAS);
253         sem->granted = 0;
254         init_waitqueue_head(&sem->wait);
255         init_waitqueue_head(&sem->write_bias_wait);
256 #if WAITQUEUE_DEBUG
257         sem->__magic = (long)&sem->__magic;
258         atomic_set(&sem->readers, 0);
259         atomic_set(&sem->writers, 0);
260 #endif
261 }
262 
263 /* The expensive part is outlined.  */
264 extern void __down_read(struct rw_semaphore *sem, int count);
265 extern void __down_write(struct rw_semaphore *sem, int count);
266 extern void __rwsem_wake(struct rw_semaphore *sem, unsigned long readers);
267 
268 extern inline void down_read(struct rw_semaphore *sem)
269 {
270         int count;
271 
272 #if WAITQUEUE_DEBUG
273         CHECK_MAGIC(sem->__magic);
274 #endif
275 
276         count = atomic_dec_return(&sem->count);
277         if (count < 0) {
278                 __down_read(sem, count);
279         }
280         mb();
281 
282 #if WAITQUEUE_DEBUG
283         if (sem->granted & 2)
284                 BUG();
285         if (atomic_read(&sem->writers))
286                 BUG();
287         atomic_inc(&sem->readers);
288 #endif
289 }
290 
291 extern inline void down_write(struct rw_semaphore *sem)
292 {
293         int count;
294 
295 #if WAITQUEUE_DEBUG
296         CHECK_MAGIC(sem->__magic);
297 #endif
298 
299         count = atomic_sub_return(RW_LOCK_BIAS, &sem->count);
300         if (count) {
301                 __down_write(sem, count);
302         }
303         mb();
304 
305 #if WAITQUEUE_DEBUG
306         if (atomic_read(&sem->writers))
307                 BUG();
308         if (atomic_read(&sem->readers))
309                 BUG();
310         if (sem->granted & 3)
311                 BUG();
312         atomic_inc(&sem->writers);
313 #endif
314 }
315 
316 /* When a reader does a release, the only significant case is when
317    there was a writer waiting, and we've bumped the count to 0: we must
318    wake the writer up.  */
319 
320 extern inline void up_read(struct rw_semaphore *sem)
321 {
322 #if WAITQUEUE_DEBUG
323         CHECK_MAGIC(sem->__magic);
324         if (sem->granted & 2)
325                 BUG();
326         if (atomic_read(&sem->writers))
327                 BUG();
328         atomic_dec(&sem->readers);
329 #endif
330 
331         mb();
332         if (atomic_inc_return(&sem->count) == 0)
333                 __rwsem_wake(sem, 0);
334 }
335 
336 /*
337  * Releasing the writer is easy -- just release it and wake up any sleepers.
338  */
339 extern inline void up_write(struct rw_semaphore *sem)
340 {
341         int count;
342 
343 #if WAITQUEUE_DEBUG
344         CHECK_MAGIC(sem->__magic);
345         if (sem->granted & 3)
346                 BUG();
347         if (atomic_read(&sem->readers))
348                 BUG();
349         if (atomic_read(&sem->writers) != 1)
350                 BUG();
351         atomic_dec(&sem->writers);
352 #endif
353 
354         mb();
355         count = atomic_add_return(RW_LOCK_BIAS, &sem->count);
356         if (count - RW_LOCK_BIAS < 0 && count >= 0) {
357                 /* Only do the wake if we're no longer negative.  */
358                 __rwsem_wake(sem, count);
359         }
360 }
361 
362 #endif /* _ASM_SEMAPHORE_H */
363 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.