2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 1996 Linus Torvalds
7 * Copyright (C) 1998, 99, 2000, 01 Ralf Baechle
8 * Copyright (C) 1999, 2000, 01 Silicon Graphics, Inc.
9 * Copyright (C) 2000, 01 MIPS Technologies, Inc.
11 #ifndef _ASM_SEMAPHORE_H
12 #define _ASM_SEMAPHORE_H
14 #include <linux/config.h>
15 #include <asm/system.h>
16 #include <asm/atomic.h>
17 #include <linux/spinlock.h>
18 #include <linux/wait.h>
19 #include <linux/rwsem.h>
29 wait_queue_head_t wait;
33 } __attribute__((aligned(8)));
36 # define __SEM_DEBUG_INIT(name) \
37 , (long)&(name).__magic
39 # define __SEM_DEBUG_INIT(name)
43 #define __SEMAPHORE_INITIALIZER(name,count) \
44 { ATOMIC_INIT(count), ATOMIC_INIT(0), __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
45 __SEM_DEBUG_INIT(name) }
47 #define __SEMAPHORE_INITIALIZER(name,count) \
48 { ATOMIC_INIT(0), ATOMIC_INIT(count), __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
49 __SEM_DEBUG_INIT(name) }
52 #define __MUTEX_INITIALIZER(name) \
53 __SEMAPHORE_INITIALIZER(name,1)
55 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
56 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
58 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
59 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
61 static inline void sema_init (struct semaphore *sem, int val)
63 atomic_set(&sem->count, val);
64 atomic_set(&sem->waking, 0);
65 init_waitqueue_head(&sem->wait);
67 sem->__magic = (long)&sem->__magic;
71 static inline void init_MUTEX (struct semaphore *sem)
76 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
81 asmlinkage void __down(struct semaphore * sem);
82 asmlinkage int __down_interruptible(struct semaphore * sem);
83 asmlinkage int __down_trylock(struct semaphore * sem);
84 asmlinkage void __up(struct semaphore * sem);
86 static inline void down(struct semaphore * sem)
89 CHECK_MAGIC(sem->__magic);
91 if (atomic_dec_return(&sem->count) < 0)
96 * Interruptible try to acquire a semaphore. If we obtained
97 * it, return zero. If we were interrupted, returns -EINTR
99 static inline int down_interruptible(struct semaphore * sem)
104 CHECK_MAGIC(sem->__magic);
106 if (atomic_dec_return(&sem->count) < 0)
107 ret = __down_interruptible(sem);
111 #ifndef CONFIG_CPU_HAS_LLDSCD
114 * Non-blockingly attempt to down() a semaphore.
115 * Returns zero if we acquired it
117 static inline int down_trylock(struct semaphore * sem)
120 if (atomic_dec_return(&sem->count) < 0)
121 ret = __down_trylock(sem);
128 * down_trylock returns 0 on success, 1 if we failed to get the lock.
130 * We must manipulate count and waking simultaneously and atomically.
131 * Here, we do this by using lld/scd on the pair of 32-bit words.
135 * Decrement(sem->count)
136 * If(sem->count >=0) {
137 * Return(SUCCESS) // resource is free
139 * If(sem->waking <= 0) { // if no wakeup pending
140 * Increment(sem->count) // undo decrement
143 * Decrement(sem->waking) // otherwise "steal" wakeup
148 static inline int down_trylock(struct semaphore * sem)
150 long ret, tmp, tmp2, sub;
153 CHECK_MAGIC(sem->__magic);
156 __asm__ __volatile__(
157 ".set\tmips3\t\t\t# down_trylock\n"
158 "0:\tlld\t%1, %4\n\t"
159 "dli\t%3, 0x0000000100000000\n\t"
165 "daddiu\t%1, %1, -1\n\t"
167 "1:\tdaddu\t%1, %1, %3\n\t"
169 "2:\tscd\t%1, %4\n\t"
172 : "=&r"(ret), "=&r"(tmp), "=&r"(tmp2), "=&r"(sub)
179 #endif /* CONFIG_CPU_HAS_LLDSCD */
182 * Note! This is subtle. We jump to wake people up only if
183 * the semaphore was negative (== somebody was waiting on it).
185 static inline void up(struct semaphore * sem)
188 CHECK_MAGIC(sem->__magic);
190 if (atomic_inc_return(&sem->count) <= 0)
194 #endif /* _ASM_SEMAPHORE_H */