1 /* $Id: semaphore.h,v 1.12 1999/12/08 22:05:10 harald Exp $
3 * SMP- and interrupt-safe semaphores..
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
9 * (C) Copyright 1996 Linus Torvalds
10 * (C) Copyright 1998, 1999, 2000 Ralf Baechle
11 * (C) Copyright 1999, 2000 Silicon Graphics, Inc.
13 #ifndef _ASM_SEMAPHORE_H
14 #define _ASM_SEMAPHORE_H
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>
30 wait_queue_head_t wait;
34 } __attribute__((aligned(8)));
37 # define __SEM_DEBUG_INIT(name) \
38 , (long)&(name).__magic
40 # define __SEM_DEBUG_INIT(name)
44 #define __SEMAPHORE_INITIALIZER(name,count) \
45 { ATOMIC_INIT(count), ATOMIC_INIT(0), __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
46 __SEM_DEBUG_INIT(name) }
48 #define __SEMAPHORE_INITIALIZER(name,count) \
49 { ATOMIC_INIT(0), ATOMIC_INIT(count), __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
50 __SEM_DEBUG_INIT(name) }
53 #define __MUTEX_INITIALIZER(name) \
54 __SEMAPHORE_INITIALIZER(name,1)
56 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
57 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
59 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
60 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
62 extern inline void sema_init (struct semaphore *sem, int val)
64 atomic_set(&sem->count, val);
65 atomic_set(&sem->waking, 0);
66 init_waitqueue_head(&sem->wait);
68 sem->__magic = (long)&sem->__magic;
72 static inline void init_MUTEX (struct semaphore *sem)
77 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
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);
87 extern inline void down(struct semaphore * sem)
90 CHECK_MAGIC(sem->__magic);
92 if (atomic_dec_return(&sem->count) < 0)
96 extern inline int down_interruptible(struct semaphore * sem)
101 CHECK_MAGIC(sem->__magic);
103 if (atomic_dec_return(&sem->count) < 0)
104 ret = __down_interruptible(sem);
108 #if !defined(CONFIG_CPU_HAS_LLSC)
110 extern inline int down_trylock(struct semaphore * sem)
113 if (atomic_dec_return(&sem->count) < 0)
114 ret = __down_trylock(sem);
121 * down_trylock returns 0 on success, 1 if we failed to get the lock.
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.
129 * Decrement(sem->count)
130 * If(sem->count >=0) {
131 * Return(SUCCESS) // resource is free
133 * If(sem->waking <= 0) { // if no wakeup pending
134 * Increment(sem->count) // undo decrement
137 * Decrement(sem->waking) // otherwise "steal" wakeup
142 extern inline int down_trylock(struct semaphore * sem)
144 long ret, tmp, tmp2, sub;
147 CHECK_MAGIC(sem->__magic);
150 __asm__ __volatile__("
154 dli %3, 0x0000000100000000
170 : "=&r"(ret), "=&r"(tmp), "=&r"(tmp2), "=&r"(sub)
180 * Note! This is subtle. We jump to wake people up only if
181 * the semaphore was negative (== somebody was waiting on it).
183 extern inline void up(struct semaphore * sem)
186 CHECK_MAGIC(sem->__magic);
188 if (atomic_inc_return(&sem->count) <= 0)
193 * rw mutexes (should that be mutices? =) -- throw rw spinlocks and
194 * semaphores together, and this is what we end up with...
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).
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
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.
212 #define RW_LOCK_BIAS 0x01000000
214 struct rw_semaphore {
216 /* bit 0 means read bias granted;
217 bit 1 means write bias granted. */
219 wait_queue_head_t wait;
220 wait_queue_head_t write_bias_wait;
229 #define __RWSEM_DEBUG_INIT , ATOMIC_INIT(0), ATOMIC_INIT(0)
231 #define __RWSEM_DEBUG_INIT /* */
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 }
240 #define __DECLARE_RWSEM_GENERIC(name,count) \
241 struct rw_semaphore name = __RWSEM_INITIALIZER(name,count)
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)
250 extern inline void init_rwsem(struct rw_semaphore *sem)
252 atomic_set(&sem->count, RW_LOCK_BIAS);
254 init_waitqueue_head(&sem->wait);
255 init_waitqueue_head(&sem->write_bias_wait);
257 sem->__magic = (long)&sem->__magic;
258 atomic_set(&sem->readers, 0);
259 atomic_set(&sem->writers, 0);
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);
268 extern inline void down_read(struct rw_semaphore *sem)
273 CHECK_MAGIC(sem->__magic);
276 count = atomic_dec_return(&sem->count);
278 __down_read(sem, count);
283 if (sem->granted & 2)
285 if (atomic_read(&sem->writers))
287 atomic_inc(&sem->readers);
291 extern inline void down_write(struct rw_semaphore *sem)
296 CHECK_MAGIC(sem->__magic);
299 count = atomic_sub_return(RW_LOCK_BIAS, &sem->count);
301 __down_write(sem, count);
306 if (atomic_read(&sem->writers))
308 if (atomic_read(&sem->readers))
310 if (sem->granted & 3)
312 atomic_inc(&sem->writers);
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. */
320 extern inline void up_read(struct rw_semaphore *sem)
323 CHECK_MAGIC(sem->__magic);
324 if (sem->granted & 2)
326 if (atomic_read(&sem->writers))
328 atomic_dec(&sem->readers);
332 if (atomic_inc_return(&sem->count) == 0)
333 __rwsem_wake(sem, 0);
337 * Releasing the writer is easy -- just release it and wake up any sleepers.
339 extern inline void up_write(struct rw_semaphore *sem)
344 CHECK_MAGIC(sem->__magic);
345 if (sem->granted & 3)
347 if (atomic_read(&sem->readers))
349 if (atomic_read(&sem->writers) != 1)
351 atomic_dec(&sem->writers);
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);
362 #endif /* _ASM_SEMAPHORE_H */