2 * License. See the file "COPYING" in the main directory of this archive
5 * Copyright (C) 1996 Linus Torvalds
6 * Copyright (C) 1998, 1999, 2000 Ralf Baechle
7 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9 #ifndef _ASM_SEMAPHORE_H
10 #define _ASM_SEMAPHORE_H
12 #include <asm/system.h>
13 #include <asm/atomic.h>
14 #include <linux/spinlock.h>
15 #include <linux/wait.h>
25 wait_queue_head_t wait
;
32 # define __SEM_DEBUG_INIT(name) \
33 , (long)&(name).__magic
35 # define __SEM_DEBUG_INIT(name)
39 #define __SEMAPHORE_INITIALIZER(name,count) \
40 { ATOMIC_INIT(count), ATOMIC_INIT(0), __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
41 __SEM_DEBUG_INIT(name) }
43 #define __SEMAPHORE_INITIALIZER(name,count) \
44 { ATOMIC_INIT(0), ATOMIC_INIT(count), __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
45 __SEM_DEBUG_INIT(name) }
48 #define __MUTEX_INITIALIZER(name) \
49 __SEMAPHORE_INITIALIZER(name,1)
51 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
52 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
54 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
55 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
57 static inline void sema_init (struct semaphore
*sem
, int val
)
59 atomic_set(&sem
->count
, val
);
60 atomic_set(&sem
->waking
, 0);
61 init_waitqueue_head(&sem
->wait
);
63 sem
->__magic
= (long)&sem
->__magic
;
67 static inline void init_MUTEX (struct semaphore
*sem
)
72 static inline void init_MUTEX_LOCKED (struct semaphore
*sem
)
77 asmlinkage
void __down(struct semaphore
* sem
);
78 asmlinkage
int __down_interruptible(struct semaphore
* sem
);
79 asmlinkage
int __down_trylock(struct semaphore
* sem
);
80 asmlinkage
void __up(struct semaphore
* sem
);
82 static inline void down(struct semaphore
* sem
)
85 CHECK_MAGIC(sem
->__magic
);
87 if (atomic_dec_return(&sem
->count
) < 0)
91 static inline int down_interruptible(struct semaphore
* sem
)
96 CHECK_MAGIC(sem
->__magic
);
98 if (atomic_dec_return(&sem
->count
) < 0)
99 ret
= __down_interruptible(sem
);
104 * down_trylock returns 0 on success, 1 if we failed to get the lock.
106 * We must manipulate count and waking simultaneously and atomically.
107 * Here, we this by using ll/sc on the pair of 32-bit words.
111 * Decrement(sem->count)
112 * If(sem->count >=0) {
113 * Return(SUCCESS) // resource is free
115 * If(sem->waking <= 0) { // if no wakeup pending
116 * Increment(sem->count) // undo decrement
119 * Decrement(sem->waking) // otherwise "steal" wakeup
124 static inline int down_trylock(struct semaphore
* sem
)
126 long ret
, tmp
, tmp2
, sub
;
129 CHECK_MAGIC(sem
->__magic
);
132 __asm__
__volatile__("
136 dli %3, 0x0000000100000000
152 : "=&r"(ret
), "=&r"(tmp
), "=&r"(tmp2
), "=&r"(sub
)
160 * Note! This is subtle. We jump to wake people up only if
161 * the semaphore was negative (== somebody was waiting on it).
163 static inline void up(struct semaphore
* sem
)
166 CHECK_MAGIC(sem
->__magic
);
168 if (atomic_inc_return(&sem
->count
) <= 0)
173 * rw mutexes (should that be mutices? =) -- throw rw spinlocks and
174 * semaphores together, and this is what we end up with...
176 * The lock is initialized to BIAS. This way, a writer subtracts BIAS ands
177 * gets 0 for the case of an uncontended lock. Readers decrement by 1 and
178 * see a positive value when uncontended, negative if there are writers
179 * waiting (in which case it goes to sleep).
181 * The value 0x01000000 supports up to 128 processors and lots of processes.
182 * BIAS must be chosen such that subtracting BIAS once per CPU will result
183 * in the int remaining negative. In terms of fairness, this should result
184 * in the lock flopping back and forth between readers and writers under
187 * Once we start supporting machines with more than 128 CPUs, we should go
188 * for using a 64bit atomic type instead of 32bit as counter. We shall
189 * probably go for bias 0x80000000 then, so that single sethi can set it.
192 #define RW_LOCK_BIAS 0x01000000
194 struct rw_semaphore
{
196 /* bit 0 means read bias granted;
197 bit 1 means write bias granted. */
198 unsigned long granted
;
199 wait_queue_head_t wait
;
200 wait_queue_head_t write_bias_wait
;
209 #define __RWSEM_DEBUG_INIT , ATOMIC_INIT(0), ATOMIC_INIT(0)
211 #define __RWSEM_DEBUG_INIT /* */
214 #define __RWSEM_INITIALIZER(name,count) \
215 { ATOMIC_INIT(count), 0, \
216 __WAIT_QUEUE_HEAD_INITIALIZER((name).wait), \
217 __WAIT_QUEUE_HEAD_INITIALIZER((name).write_bias_wait) \
218 __SEM_DEBUG_INIT(name) __RWSEM_DEBUG_INIT }
220 #define __DECLARE_RWSEM_GENERIC(name,count) \
221 struct rw_semaphore name = __RWSEM_INITIALIZER(name,count)
223 #define DECLARE_RWSEM(name) \
224 __DECLARE_RWSEM_GENERIC(name, RW_LOCK_BIAS)
225 #define DECLARE_RWSEM_READ_LOCKED(name) \
226 __DECLARE_RWSEM_GENERIC(name, RW_LOCK_BIAS-1)
227 #define DECLARE_RWSEM_WRITE_LOCKED(name) \
228 __DECLARE_RWSEM_GENERIC(name, 0)
230 static inline void init_rwsem(struct rw_semaphore
*sem
)
232 atomic_set(&sem
->count
, RW_LOCK_BIAS
);
234 init_waitqueue_head(&sem
->wait
);
235 init_waitqueue_head(&sem
->write_bias_wait
);
237 sem
->__magic
= (long)&sem
->__magic
;
238 atomic_set(&sem
->readers
, 0);
239 atomic_set(&sem
->writers
, 0);
243 /* The expensive part is outlined. */
244 extern void __down_read(struct rw_semaphore
*sem
, int count
);
245 extern void __down_write(struct rw_semaphore
*sem
, int count
);
246 extern void __rwsem_wake(struct rw_semaphore
*sem
, unsigned long readers
);
248 static inline void down_read(struct rw_semaphore
*sem
)
253 CHECK_MAGIC(sem
->__magic
);
256 count
= atomic_dec_return(&sem
->count
);
258 __down_read(sem
, count
);
263 if (sem
->granted
& 2)
265 if (atomic_read(&sem
->writers
))
267 atomic_inc(&sem
->readers
);
271 static inline void down_write(struct rw_semaphore
*sem
)
276 CHECK_MAGIC(sem
->__magic
);
279 count
= atomic_sub_return(RW_LOCK_BIAS
, &sem
->count
);
281 __down_write(sem
, count
);
286 if (atomic_read(&sem
->writers
))
288 if (atomic_read(&sem
->readers
))
290 if (sem
->granted
& 3)
292 atomic_inc(&sem
->writers
);
296 /* When a reader does a release, the only significant case is when
297 there was a writer waiting, and we've bumped the count to 0: we must
298 wake the writer up. */
300 static inline void up_read(struct rw_semaphore
*sem
)
305 CHECK_MAGIC(sem
->__magic
);
306 if (sem
->granted
& 2)
308 if (atomic_read(&sem
->writers
))
310 atomic_dec(&sem
->readers
);
314 count
= atomic_inc_return(&sem
->count
);
316 __rwsem_wake(sem
, 0);
321 * Releasing the writer is easy -- just release it and wake up any sleepers.
323 static inline void up_write(struct rw_semaphore
*sem
)
328 CHECK_MAGIC(sem
->__magic
);
329 if (sem
->granted
& 3)
331 if (atomic_read(&sem
->readers
))
333 if (atomic_read(&sem
->writers
) != 1)
335 atomic_dec(&sem
->writers
);
339 count
= atomic_add_return(RW_LOCK_BIAS
, &sem
->count
);
340 if (count
- RW_LOCK_BIAS
< 0 && count
>= 0) {
341 /* Only do the wake if we're no longer negative. */
342 __rwsem_wake(sem
, count
);
346 #endif /* _ASM_SEMAPHORE_H */