vserver 1.9.5.x5
[linux-2.6.git] / include / asm-i386 / semaphore.h
1 #ifndef _I386_SEMAPHORE_H
2 #define _I386_SEMAPHORE_H
3
4 #include <linux/linkage.h>
5
6 #ifdef __KERNEL__
7
8 /*
9  * SMP- and interrupt-safe semaphores..
10  *
11  * (C) Copyright 1996 Linus Torvalds
12  *
13  * Modified 1996-12-23 by Dave Grothe <dave@gcom.com> to fix bugs in
14  *                     the original code and to make semaphore waits
15  *                     interruptible so that processes waiting on
16  *                     semaphores can be killed.
17  * Modified 1999-02-14 by Andrea Arcangeli, split the sched.c helper
18  *                     functions in asm/sempahore-helper.h while fixing a
19  *                     potential and subtle race discovered by Ulrich Schmid
20  *                     in down_interruptible(). Since I started to play here I
21  *                     also implemented the `trylock' semaphore operation.
22  *          1999-07-02 Artur Skawina <skawina@geocities.com>
23  *                     Optimized "0(ecx)" -> "(ecx)" (the assembler does not
24  *                     do this). Changed calling sequences from push/jmp to
25  *                     traditional call/ret.
26  * Modified 2001-01-01 Andreas Franck <afranck@gmx.de>
27  *                     Some hacks to ensure compatibility with recent
28  *                     GCC snapshots, to avoid stack corruption when compiling
29  *                     with -fomit-frame-pointer. It's not sure if this will
30  *                     be fixed in GCC, as our previous implementation was a
31  *                     bit dubious.
32  *
33  * If you would like to see an analysis of this implementation, please
34  * ftp to gcom.com and download the file
35  * /pub/linux/src/semaphore/semaphore-2.0.24.tar.gz.
36  *
37  */
38
39 #include <asm/system.h>
40 #include <asm/atomic.h>
41 #include <linux/wait.h>
42 #include <linux/rwsem.h>
43
44 struct semaphore {
45         atomic_t count;
46         int sleepers;
47         wait_queue_head_t wait;
48 };
49
50
51 #define __SEMAPHORE_INITIALIZER(name, n)                                \
52 {                                                                       \
53         .count          = ATOMIC_INIT(n),                               \
54         .sleepers       = 0,                                            \
55         .wait           = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait)    \
56 }
57
58 #define __MUTEX_INITIALIZER(name) \
59         __SEMAPHORE_INITIALIZER(name,1)
60
61 #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
62         struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
63
64 #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
65 #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
66
67 static inline void sema_init (struct semaphore *sem, int val)
68 {
69 /*
70  *      *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
71  *
72  * i'd rather use the more flexible initialization above, but sadly
73  * GCC 2.7.2.3 emits a bogus warning. EGCS doesn't. Oh well.
74  */
75         atomic_set(&sem->count, val);
76         sem->sleepers = 0;
77         init_waitqueue_head(&sem->wait);
78 }
79
80 static inline void init_MUTEX (struct semaphore *sem)
81 {
82         sema_init(sem, 1);
83 }
84
85 static inline void init_MUTEX_LOCKED (struct semaphore *sem)
86 {
87         sema_init(sem, 0);
88 }
89
90 fastcall void __down_failed(void /* special register calling convention */);
91 fastcall int  __down_failed_interruptible(void  /* params in registers */);
92 fastcall int  __down_failed_trylock(void  /* params in registers */);
93 fastcall void __up_wakeup(void /* special register calling convention */);
94
95 fastcall void __down(struct semaphore * sem);
96 fastcall int  __down_interruptible(struct semaphore * sem);
97 fastcall int  __down_trylock(struct semaphore * sem);
98 fastcall void __up(struct semaphore * sem);
99
100 /*
101  * This is ugly, but we want the default case to fall through.
102  * "__down_failed" is a special asm handler that calls the C
103  * routine that actually waits. See arch/i386/kernel/semaphore.c
104  */
105 static inline void down(struct semaphore * sem)
106 {
107         might_sleep();
108         __asm__ __volatile__(
109                 "# atomic down operation\n\t"
110                 LOCK "decl %0\n\t"     /* --sem->count */
111                 "js 2f\n"
112                 "1:\n"
113                 LOCK_SECTION_START("")
114                 "2:\tlea %0,%%eax\n\t"
115                 "call __down_failed\n\t"
116                 "jmp 1b\n"
117                 LOCK_SECTION_END
118                 :"=m" (sem->count)
119                 :
120                 :"memory","ax");
121 }
122
123 /*
124  * Interruptible try to acquire a semaphore.  If we obtained
125  * it, return zero.  If we were interrupted, returns -EINTR
126  */
127 static inline int down_interruptible(struct semaphore * sem)
128 {
129         int result;
130
131         might_sleep();
132         __asm__ __volatile__(
133                 "# atomic interruptible down operation\n\t"
134                 LOCK "decl %1\n\t"     /* --sem->count */
135                 "js 2f\n\t"
136                 "xorl %0,%0\n"
137                 "1:\n"
138                 LOCK_SECTION_START("")
139                 "2:\tlea %1,%%eax\n\t"
140                 "call __down_failed_interruptible\n\t"
141                 "jmp 1b\n"
142                 LOCK_SECTION_END
143                 :"=a" (result), "=m" (sem->count)
144                 :
145                 :"memory");
146         return result;
147 }
148
149 /*
150  * Non-blockingly attempt to down() a semaphore.
151  * Returns zero if we acquired it
152  */
153 static inline int down_trylock(struct semaphore * sem)
154 {
155         int result;
156
157         __asm__ __volatile__(
158                 "# atomic interruptible down operation\n\t"
159                 LOCK "decl %1\n\t"     /* --sem->count */
160                 "js 2f\n\t"
161                 "xorl %0,%0\n"
162                 "1:\n"
163                 LOCK_SECTION_START("")
164                 "2:\tlea %1,%%eax\n\t"
165                 "call __down_failed_trylock\n\t"
166                 "jmp 1b\n"
167                 LOCK_SECTION_END
168                 :"=a" (result), "=m" (sem->count)
169                 :
170                 :"memory");
171         return result;
172 }
173
174 /*
175  * Note! This is subtle. We jump to wake people up only if
176  * the semaphore was negative (== somebody was waiting on it).
177  * The default case (no contention) will result in NO
178  * jumps for both down() and up().
179  */
180 static inline void up(struct semaphore * sem)
181 {
182         __asm__ __volatile__(
183                 "# atomic up operation\n\t"
184                 LOCK "incl %0\n\t"     /* ++sem->count */
185                 "jle 2f\n"
186                 "1:\n"
187                 LOCK_SECTION_START("")
188                 "2:\tlea %0,%%eax\n\t"
189                 "call __up_wakeup\n\t"
190                 "jmp 1b\n"
191                 LOCK_SECTION_END
192                 ".subsection 0\n"
193                 :"=m" (sem->count)
194                 :
195                 :"memory","ax");
196 }
197
198 #endif
199 #endif