1 #ifndef _X86_64_SEMAPHORE_H
2 #define _X86_64_SEMAPHORE_H
4 #include <linux/linkage.h>
9 * SMP- and interrupt-safe semaphores..
11 * (C) Copyright 1996 Linus Torvalds
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
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.
39 #include <asm/system.h>
40 #include <asm/atomic.h>
41 #include <asm/rwlock.h>
42 #include <linux/wait.h>
43 #include <linux/rwsem.h>
44 #include <linux/stringify.h>
49 wait_queue_head_t wait
;
52 #define __SEMAPHORE_INITIALIZER(name, n) \
54 .count = ATOMIC_INIT(n), \
56 .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
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)
64 static inline void sema_init (struct semaphore
*sem
, int val
)
67 * *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
69 * i'd rather use the more flexible initialization above, but sadly
70 * GCC 2.7.2.3 emits a bogus warning. EGCS doesn't. Oh well.
72 atomic_set(&sem
->count
, val
);
74 init_waitqueue_head(&sem
->wait
);
77 static inline void init_MUTEX (struct semaphore
*sem
)
82 static inline void init_MUTEX_LOCKED (struct semaphore
*sem
)
87 asmlinkage
void __down_failed(void /* special register calling convention */);
88 asmlinkage
int __down_failed_interruptible(void /* params in registers */);
89 asmlinkage
int __down_failed_trylock(void /* params in registers */);
90 asmlinkage
void __up_wakeup(void /* special register calling convention */);
92 asmlinkage
void __down(struct semaphore
* sem
);
93 asmlinkage
int __down_interruptible(struct semaphore
* sem
);
94 asmlinkage
int __down_trylock(struct semaphore
* sem
);
95 asmlinkage
void __up(struct semaphore
* sem
);
98 * This is ugly, but we want the default case to fall through.
99 * "__down_failed" is a special asm handler that calls the C
100 * routine that actually waits. See arch/x86_64/kernel/semaphore.c
102 static inline void down(struct semaphore
* sem
)
106 __asm__
__volatile__(
107 "# atomic down operation\n\t"
108 LOCK_PREFIX
"decl %0\n\t" /* --sem->count */
110 "call __down_failed\n"
118 * Interruptible try to acquire a semaphore. If we obtained
119 * it, return zero. If we were interrupted, returns -EINTR
121 static inline int down_interruptible(struct semaphore
* sem
)
127 __asm__
__volatile__(
128 "# atomic interruptible down operation\n\t"
130 LOCK_PREFIX
"decl %1\n\t" /* --sem->count */
132 "call __down_failed_interruptible\n"
134 :"=&a" (result
), "=m" (sem
->count
)
141 * Non-blockingly attempt to down() a semaphore.
142 * Returns zero if we acquired it
144 static inline int down_trylock(struct semaphore
* sem
)
148 __asm__
__volatile__(
149 "# atomic interruptible down operation\n\t"
151 LOCK_PREFIX
"decl %1\n\t" /* --sem->count */
153 "call __down_failed_trylock\n\t"
155 :"=&a" (result
), "=m" (sem
->count
)
162 * Note! This is subtle. We jump to wake people up only if
163 * the semaphore was negative (== somebody was waiting on it).
164 * The default case (no contention) will result in NO
165 * jumps for both down() and up().
167 static inline void up(struct semaphore
* sem
)
169 __asm__
__volatile__(
170 "# atomic up operation\n\t"
171 LOCK_PREFIX
"incl %0\n\t" /* ++sem->count */
179 #endif /* __KERNEL__ */