Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _X86_64_SEMAPHORE_H |
| 2 | #define _X86_64_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 <asm/rwlock.h> |
| 42 | #include <linux/wait.h> |
| 43 | #include <linux/rwsem.h> |
| 44 | #include <linux/stringify.h> |
| 45 | |
| 46 | struct semaphore { |
| 47 | atomic_t count; |
| 48 | int sleepers; |
| 49 | wait_queue_head_t wait; |
| 50 | }; |
| 51 | |
| 52 | #define __SEMAPHORE_INITIALIZER(name, n) \ |
| 53 | { \ |
| 54 | .count = ATOMIC_INIT(n), \ |
| 55 | .sleepers = 0, \ |
| 56 | .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \ |
| 57 | } |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #define __DECLARE_SEMAPHORE_GENERIC(name,count) \ |
| 60 | struct semaphore name = __SEMAPHORE_INITIALIZER(name,count) |
| 61 | |
| 62 | #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1) |
| 63 | #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0) |
| 64 | |
| 65 | static inline void sema_init (struct semaphore *sem, int val) |
| 66 | { |
| 67 | /* |
| 68 | * *sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val); |
| 69 | * |
| 70 | * i'd rather use the more flexible initialization above, but sadly |
| 71 | * GCC 2.7.2.3 emits a bogus warning. EGCS doesn't. Oh well. |
| 72 | */ |
| 73 | atomic_set(&sem->count, val); |
| 74 | sem->sleepers = 0; |
| 75 | init_waitqueue_head(&sem->wait); |
| 76 | } |
| 77 | |
| 78 | static inline void init_MUTEX (struct semaphore *sem) |
| 79 | { |
| 80 | sema_init(sem, 1); |
| 81 | } |
| 82 | |
| 83 | static inline void init_MUTEX_LOCKED (struct semaphore *sem) |
| 84 | { |
| 85 | sema_init(sem, 0); |
| 86 | } |
| 87 | |
| 88 | asmlinkage void __down_failed(void /* special register calling convention */); |
| 89 | asmlinkage int __down_failed_interruptible(void /* params in registers */); |
| 90 | asmlinkage int __down_failed_trylock(void /* params in registers */); |
| 91 | asmlinkage void __up_wakeup(void /* special register calling convention */); |
| 92 | |
| 93 | asmlinkage void __down(struct semaphore * sem); |
| 94 | asmlinkage int __down_interruptible(struct semaphore * sem); |
| 95 | asmlinkage int __down_trylock(struct semaphore * sem); |
| 96 | asmlinkage void __up(struct semaphore * sem); |
| 97 | |
| 98 | /* |
| 99 | * This is ugly, but we want the default case to fall through. |
| 100 | * "__down_failed" is a special asm handler that calls the C |
| 101 | * routine that actually waits. See arch/x86_64/kernel/semaphore.c |
| 102 | */ |
| 103 | static inline void down(struct semaphore * sem) |
| 104 | { |
| 105 | might_sleep(); |
| 106 | |
| 107 | __asm__ __volatile__( |
| 108 | "# atomic down operation\n\t" |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 109 | LOCK_PREFIX "decl %0\n\t" /* --sem->count */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | "js 2f\n" |
| 111 | "1:\n" |
| 112 | LOCK_SECTION_START("") |
| 113 | "2:\tcall __down_failed\n\t" |
| 114 | "jmp 1b\n" |
| 115 | LOCK_SECTION_END |
| 116 | :"=m" (sem->count) |
| 117 | :"D" (sem) |
| 118 | :"memory"); |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Interruptible try to acquire a semaphore. If we obtained |
| 123 | * it, return zero. If we were interrupted, returns -EINTR |
| 124 | */ |
| 125 | static inline int down_interruptible(struct semaphore * sem) |
| 126 | { |
| 127 | int result; |
| 128 | |
| 129 | might_sleep(); |
| 130 | |
| 131 | __asm__ __volatile__( |
| 132 | "# atomic interruptible down operation\n\t" |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 133 | LOCK_PREFIX "decl %1\n\t" /* --sem->count */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | "js 2f\n\t" |
| 135 | "xorl %0,%0\n" |
| 136 | "1:\n" |
| 137 | LOCK_SECTION_START("") |
| 138 | "2:\tcall __down_failed_interruptible\n\t" |
| 139 | "jmp 1b\n" |
| 140 | LOCK_SECTION_END |
| 141 | :"=a" (result), "=m" (sem->count) |
| 142 | :"D" (sem) |
| 143 | :"memory"); |
| 144 | return result; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Non-blockingly attempt to down() a semaphore. |
| 149 | * Returns zero if we acquired it |
| 150 | */ |
| 151 | static inline int down_trylock(struct semaphore * sem) |
| 152 | { |
| 153 | int result; |
| 154 | |
| 155 | __asm__ __volatile__( |
| 156 | "# atomic interruptible down operation\n\t" |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 157 | LOCK_PREFIX "decl %1\n\t" /* --sem->count */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | "js 2f\n\t" |
| 159 | "xorl %0,%0\n" |
| 160 | "1:\n" |
| 161 | LOCK_SECTION_START("") |
| 162 | "2:\tcall __down_failed_trylock\n\t" |
| 163 | "jmp 1b\n" |
| 164 | LOCK_SECTION_END |
| 165 | :"=a" (result), "=m" (sem->count) |
| 166 | :"D" (sem) |
| 167 | :"memory","cc"); |
| 168 | return result; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Note! This is subtle. We jump to wake people up only if |
| 173 | * the semaphore was negative (== somebody was waiting on it). |
| 174 | * The default case (no contention) will result in NO |
| 175 | * jumps for both down() and up(). |
| 176 | */ |
| 177 | static inline void up(struct semaphore * sem) |
| 178 | { |
| 179 | __asm__ __volatile__( |
| 180 | "# atomic up operation\n\t" |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 181 | LOCK_PREFIX "incl %0\n\t" /* ++sem->count */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | "jle 2f\n" |
| 183 | "1:\n" |
| 184 | LOCK_SECTION_START("") |
| 185 | "2:\tcall __up_wakeup\n\t" |
| 186 | "jmp 1b\n" |
| 187 | LOCK_SECTION_END |
| 188 | :"=m" (sem->count) |
| 189 | :"D" (sem) |
| 190 | :"memory"); |
| 191 | } |
| 192 | #endif /* __KERNEL__ */ |
| 193 | #endif |