Import 2.3.18pre1
[davej-history.git] / include / asm-i386 / semaphore.h
blob3997b2aae1614d99ca8b340ce0bc2e1ab2eb59c7
1 #ifndef _I386_SEMAPHORE_H
2 #define _I386_SEMAPHORE_H
4 #include <linux/linkage.h>
6 /*
7 * SMP- and interrupt-safe semaphores..
9 * (C) Copyright 1996 Linus Torvalds
11 * Modified 1996-12-23 by Dave Grothe <dave@gcom.com> to fix bugs in
12 * the original code and to make semaphore waits
13 * interruptible so that processes waiting on
14 * semaphores can be killed.
15 * Modified 1999-02-14 by Andrea Arcangeli, split the sched.c helper
16 * functions in asm/sempahore-helper.h while fixing a
17 * potential and subtle race discovered by Ulrich Schmid
18 * in down_interruptible(). Since I started to play here I
19 * also implemented the `trylock' semaphore operation.
20 * 1999-07-02 Artur Skawina <skawina@geocities.com>
21 * Optimized "0(ecx)" -> "(ecx)" (the assembler does not
22 * do this). Changed calling sequences from push/jmp to
23 * traditional call/ret.
25 * If you would like to see an analysis of this implementation, please
26 * ftp to gcom.com and download the file
27 * /pub/linux/src/semaphore/semaphore-2.0.24.tar.gz.
31 #include <asm/system.h>
32 #include <asm/atomic.h>
33 #include <linux/spinlock.h>
34 #include <linux/wait.h>
36 struct semaphore {
37 atomic_t count;
38 int sleepers;
39 wait_queue_head_t wait;
40 #if WAITQUEUE_DEBUG
41 long __magic;
42 #endif
45 #if WAITQUEUE_DEBUG
46 # define __SEM_DEBUG_INIT(name) \
47 , (int)&(name).__magic
48 #else
49 # define __SEM_DEBUG_INIT(name)
50 #endif
52 #define __SEMAPHORE_INITIALIZER(name,count) \
53 { ATOMIC_INIT(count), 0, __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
54 __SEM_DEBUG_INIT(name) }
56 #define __MUTEX_INITIALIZER(name) \
57 __SEMAPHORE_INITIALIZER(name,1)
59 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
60 struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
62 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
63 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
65 extern inline void sema_init (struct semaphore *sem, int val)
68 * *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
70 * i'd rather use the more flexible initialization above, but sadly
71 * GCC 2.7.2.3 emits a bogus warning. EGCS doesnt. Oh well.
73 atomic_set(&sem->count, val);
74 sem->sleepers = 0;
75 init_waitqueue_head(&sem->wait);
76 #if WAITQUEUE_DEBUG
77 sem->__magic = (int)&sem->__magic;
78 #endif
81 static inline void init_MUTEX (struct semaphore *sem)
83 sema_init(sem, 1);
86 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
88 sema_init(sem, 0);
91 asmlinkage void __down_failed(void /* special register calling convention */);
92 asmlinkage int __down_failed_interruptible(void /* params in registers */);
93 asmlinkage int __down_failed_trylock(void /* params in registers */);
94 asmlinkage void __up_wakeup(void /* special register calling convention */);
96 asmlinkage void __down(struct semaphore * sem);
97 asmlinkage int __down_interruptible(struct semaphore * sem);
98 asmlinkage int __down_trylock(struct semaphore * sem);
99 asmlinkage void __up(struct semaphore * sem);
101 extern spinlock_t semaphore_wake_lock;
104 * This is ugly, but we want the default case to fall through.
105 * "down_failed" is a special asm handler that calls the C
106 * routine that actually waits. See arch/i386/lib/semaphore.S
108 extern inline void down(struct semaphore * sem)
110 #if WAITQUEUE_DEBUG
111 CHECK_MAGIC(sem->__magic);
112 #endif
114 __asm__ __volatile__(
115 "# atomic down operation\n\t"
116 #ifdef __SMP__
117 "lock ; "
118 #endif
119 "decl (%0)\n\t" /* --sem->count */
120 "js 2f\n"
121 "1:\n"
122 ".section .text.lock,\"ax\"\n"
123 "2:\tcall __down_failed\n\t"
124 "jmp 1b\n"
125 ".previous"
126 :/* no outputs */
127 :"c" (sem)
128 :"memory");
131 extern inline int down_interruptible(struct semaphore * sem)
133 int result;
135 #if WAITQUEUE_DEBUG
136 CHECK_MAGIC(sem->__magic);
137 #endif
139 __asm__ __volatile__(
140 "# atomic interruptible down operation\n\t"
141 #ifdef __SMP__
142 "lock ; "
143 #endif
144 "decl (%1)\n\t" /* --sem->count */
145 "js 2f\n\t"
146 "xorl %0,%0\n"
147 "1:\n"
148 ".section .text.lock,\"ax\"\n"
149 "2:\tcall __down_failed_interruptible\n\t"
150 "jmp 1b\n"
151 ".previous"
152 :"=a" (result)
153 :"c" (sem)
154 :"memory");
155 return result;
158 extern inline int down_trylock(struct semaphore * sem)
160 int result;
162 #if WAITQUEUE_DEBUG
163 CHECK_MAGIC(sem->__magic);
164 #endif
166 __asm__ __volatile__(
167 "# atomic interruptible down operation\n\t"
168 #ifdef __SMP__
169 "lock ; "
170 #endif
171 "decl (%1)\n\t" /* --sem->count */
172 "js 2f\n\t"
173 "xorl %0,%0\n"
174 "1:\n"
175 ".section .text.lock,\"ax\"\n"
176 "2:\tcall __down_failed_trylock\n\t"
177 "jmp 1b\n"
178 ".previous"
179 :"=a" (result)
180 :"c" (sem)
181 :"memory");
182 return result;
186 * Note! This is subtle. We jump to wake people up only if
187 * the semaphore was negative (== somebody was waiting on it).
188 * The default case (no contention) will result in NO
189 * jumps for both down() and up().
191 extern inline void up(struct semaphore * sem)
193 #if WAITQUEUE_DEBUG
194 CHECK_MAGIC(sem->__magic);
195 #endif
196 __asm__ __volatile__(
197 "# atomic up operation\n\t"
198 #ifdef __SMP__
199 "lock ; "
200 #endif
201 "incl (%0)\n\t" /* ++sem->count */
202 "jle 2f\n"
203 "1:\n"
204 ".section .text.lock,\"ax\"\n"
205 "2:\tcall __up_wakeup\n\t"
206 "jmp 1b\n"
207 ".previous"
208 :/* no outputs */
209 :"c" (sem)
210 :"memory");
213 #endif