diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /include/asm-x86_64/rwsem.h |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'include/asm-x86_64/rwsem.h')
-rw-r--r-- | include/asm-x86_64/rwsem.h | 278 |
1 files changed, 278 insertions, 0 deletions
diff --git a/include/asm-x86_64/rwsem.h b/include/asm-x86_64/rwsem.h new file mode 100644 index 000000000000..c002175b6e82 --- /dev/null +++ b/include/asm-x86_64/rwsem.h | |||
@@ -0,0 +1,278 @@ | |||
1 | /* rwsem.h: R/W semaphores implemented using XADD/CMPXCHG for x86_64+ | ||
2 | * | ||
3 | * Written by David Howells (dhowells@redhat.com). | ||
4 | * Ported by Andi Kleen <ak@suse.de> to x86-64. | ||
5 | * | ||
6 | * Derived from asm-i386/semaphore.h and asm-i386/rwsem.h | ||
7 | * | ||
8 | * | ||
9 | * The MSW of the count is the negated number of active writers and waiting | ||
10 | * lockers, and the LSW is the total number of active locks | ||
11 | * | ||
12 | * The lock count is initialized to 0 (no active and no waiting lockers). | ||
13 | * | ||
14 | * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case of an | ||
15 | * uncontended lock. This can be determined because XADD returns the old value. | ||
16 | * Readers increment by 1 and see a positive value when uncontended, negative | ||
17 | * if there are writers (and maybe) readers waiting (in which case it goes to | ||
18 | * sleep). | ||
19 | * | ||
20 | * The value of WAITING_BIAS supports up to 32766 waiting processes. This can | ||
21 | * be extended to 65534 by manually checking the whole MSW rather than relying | ||
22 | * on the S flag. | ||
23 | * | ||
24 | * The value of ACTIVE_BIAS supports up to 65535 active processes. | ||
25 | * | ||
26 | * This should be totally fair - if anything is waiting, a process that wants a | ||
27 | * lock will go to the back of the queue. When the currently active lock is | ||
28 | * released, if there's a writer at the front of the queue, then that and only | ||
29 | * that will be woken up; if there's a bunch of consecutive readers at the | ||
30 | * front, then they'll all be woken up, but no other readers will be. | ||
31 | */ | ||
32 | |||
33 | #ifndef _X8664_RWSEM_H | ||
34 | #define _X8664_RWSEM_H | ||
35 | |||
36 | #ifndef _LINUX_RWSEM_H | ||
37 | #error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead" | ||
38 | #endif | ||
39 | |||
40 | #ifdef __KERNEL__ | ||
41 | |||
42 | #include <linux/list.h> | ||
43 | #include <linux/spinlock.h> | ||
44 | |||
45 | struct rwsem_waiter; | ||
46 | |||
47 | extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem); | ||
48 | extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem); | ||
49 | extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *); | ||
50 | extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem); | ||
51 | |||
52 | /* | ||
53 | * the semaphore definition | ||
54 | */ | ||
55 | struct rw_semaphore { | ||
56 | signed int count; | ||
57 | #define RWSEM_UNLOCKED_VALUE 0x00000000 | ||
58 | #define RWSEM_ACTIVE_BIAS 0x00000001 | ||
59 | #define RWSEM_ACTIVE_MASK 0x0000ffff | ||
60 | #define RWSEM_WAITING_BIAS (-0x00010000) | ||
61 | #define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS | ||
62 | #define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS) | ||
63 | spinlock_t wait_lock; | ||
64 | struct list_head wait_list; | ||
65 | #if RWSEM_DEBUG | ||
66 | int debug; | ||
67 | #endif | ||
68 | }; | ||
69 | |||
70 | /* | ||
71 | * initialisation | ||
72 | */ | ||
73 | #if RWSEM_DEBUG | ||
74 | #define __RWSEM_DEBUG_INIT , 0 | ||
75 | #else | ||
76 | #define __RWSEM_DEBUG_INIT /* */ | ||
77 | #endif | ||
78 | |||
79 | #define __RWSEM_INITIALIZER(name) \ | ||
80 | { RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, LIST_HEAD_INIT((name).wait_list) \ | ||
81 | __RWSEM_DEBUG_INIT } | ||
82 | |||
83 | #define DECLARE_RWSEM(name) \ | ||
84 | struct rw_semaphore name = __RWSEM_INITIALIZER(name) | ||
85 | |||
86 | static inline void init_rwsem(struct rw_semaphore *sem) | ||
87 | { | ||
88 | sem->count = RWSEM_UNLOCKED_VALUE; | ||
89 | spin_lock_init(&sem->wait_lock); | ||
90 | INIT_LIST_HEAD(&sem->wait_list); | ||
91 | #if RWSEM_DEBUG | ||
92 | sem->debug = 0; | ||
93 | #endif | ||
94 | } | ||
95 | |||
96 | /* | ||
97 | * lock for reading | ||
98 | */ | ||
99 | static inline void __down_read(struct rw_semaphore *sem) | ||
100 | { | ||
101 | __asm__ __volatile__( | ||
102 | "# beginning down_read\n\t" | ||
103 | LOCK_PREFIX " incl (%%rdi)\n\t" /* adds 0x00000001, returns the old value */ | ||
104 | " js 2f\n\t" /* jump if we weren't granted the lock */ | ||
105 | "1:\n\t" | ||
106 | LOCK_SECTION_START("") \ | ||
107 | "2:\n\t" | ||
108 | " call rwsem_down_read_failed_thunk\n\t" | ||
109 | " jmp 1b\n" | ||
110 | LOCK_SECTION_END \ | ||
111 | "# ending down_read\n\t" | ||
112 | : "+m"(sem->count) | ||
113 | : "D"(sem) | ||
114 | : "memory", "cc"); | ||
115 | } | ||
116 | |||
117 | |||
118 | /* | ||
119 | * trylock for reading -- returns 1 if successful, 0 if contention | ||
120 | */ | ||
121 | static inline int __down_read_trylock(struct rw_semaphore *sem) | ||
122 | { | ||
123 | __s32 result, tmp; | ||
124 | __asm__ __volatile__( | ||
125 | "# beginning __down_read_trylock\n\t" | ||
126 | " movl %0,%1\n\t" | ||
127 | "1:\n\t" | ||
128 | " movl %1,%2\n\t" | ||
129 | " addl %3,%2\n\t" | ||
130 | " jle 2f\n\t" | ||
131 | LOCK_PREFIX " cmpxchgl %2,%0\n\t" | ||
132 | " jnz 1b\n\t" | ||
133 | "2:\n\t" | ||
134 | "# ending __down_read_trylock\n\t" | ||
135 | : "+m"(sem->count), "=&a"(result), "=&r"(tmp) | ||
136 | : "i"(RWSEM_ACTIVE_READ_BIAS) | ||
137 | : "memory", "cc"); | ||
138 | return result>=0 ? 1 : 0; | ||
139 | } | ||
140 | |||
141 | |||
142 | /* | ||
143 | * lock for writing | ||
144 | */ | ||
145 | static inline void __down_write(struct rw_semaphore *sem) | ||
146 | { | ||
147 | int tmp; | ||
148 | |||
149 | tmp = RWSEM_ACTIVE_WRITE_BIAS; | ||
150 | __asm__ __volatile__( | ||
151 | "# beginning down_write\n\t" | ||
152 | LOCK_PREFIX " xaddl %0,(%%rdi)\n\t" /* subtract 0x0000ffff, returns the old value */ | ||
153 | " testl %0,%0\n\t" /* was the count 0 before? */ | ||
154 | " jnz 2f\n\t" /* jump if we weren't granted the lock */ | ||
155 | "1:\n\t" | ||
156 | LOCK_SECTION_START("") | ||
157 | "2:\n\t" | ||
158 | " call rwsem_down_write_failed_thunk\n\t" | ||
159 | " jmp 1b\n" | ||
160 | LOCK_SECTION_END | ||
161 | "# ending down_write" | ||
162 | : "=&r" (tmp) | ||
163 | : "0"(tmp), "D"(sem) | ||
164 | : "memory", "cc"); | ||
165 | } | ||
166 | |||
167 | /* | ||
168 | * trylock for writing -- returns 1 if successful, 0 if contention | ||
169 | */ | ||
170 | static inline int __down_write_trylock(struct rw_semaphore *sem) | ||
171 | { | ||
172 | signed long ret = cmpxchg(&sem->count, | ||
173 | RWSEM_UNLOCKED_VALUE, | ||
174 | RWSEM_ACTIVE_WRITE_BIAS); | ||
175 | if (ret == RWSEM_UNLOCKED_VALUE) | ||
176 | return 1; | ||
177 | return 0; | ||
178 | } | ||
179 | |||
180 | /* | ||
181 | * unlock after reading | ||
182 | */ | ||
183 | static inline void __up_read(struct rw_semaphore *sem) | ||
184 | { | ||
185 | __s32 tmp = -RWSEM_ACTIVE_READ_BIAS; | ||
186 | __asm__ __volatile__( | ||
187 | "# beginning __up_read\n\t" | ||
188 | LOCK_PREFIX " xaddl %[tmp],(%%rdi)\n\t" /* subtracts 1, returns the old value */ | ||
189 | " js 2f\n\t" /* jump if the lock is being waited upon */ | ||
190 | "1:\n\t" | ||
191 | LOCK_SECTION_START("") | ||
192 | "2:\n\t" | ||
193 | " decw %w[tmp]\n\t" /* do nothing if still outstanding active readers */ | ||
194 | " jnz 1b\n\t" | ||
195 | " call rwsem_wake_thunk\n\t" | ||
196 | " jmp 1b\n" | ||
197 | LOCK_SECTION_END | ||
198 | "# ending __up_read\n" | ||
199 | : "+m"(sem->count), [tmp] "+r" (tmp) | ||
200 | : "D"(sem) | ||
201 | : "memory", "cc"); | ||
202 | } | ||
203 | |||
204 | /* | ||
205 | * unlock after writing | ||
206 | */ | ||
207 | static inline void __up_write(struct rw_semaphore *sem) | ||
208 | { | ||
209 | unsigned tmp; | ||
210 | __asm__ __volatile__( | ||
211 | "# beginning __up_write\n\t" | ||
212 | " movl %[bias],%[tmp]\n\t" | ||
213 | LOCK_PREFIX " xaddl %[tmp],(%%rdi)\n\t" /* tries to transition 0xffff0001 -> 0x00000000 */ | ||
214 | " jnz 2f\n\t" /* jump if the lock is being waited upon */ | ||
215 | "1:\n\t" | ||
216 | LOCK_SECTION_START("") | ||
217 | "2:\n\t" | ||
218 | " decw %w[tmp]\n\t" /* did the active count reduce to 0? */ | ||
219 | " jnz 1b\n\t" /* jump back if not */ | ||
220 | " call rwsem_wake_thunk\n\t" | ||
221 | " jmp 1b\n" | ||
222 | LOCK_SECTION_END | ||
223 | "# ending __up_write\n" | ||
224 | : "+m"(sem->count), [tmp] "=r" (tmp) | ||
225 | : "D"(sem), [bias] "i"(-RWSEM_ACTIVE_WRITE_BIAS) | ||
226 | : "memory", "cc"); | ||
227 | } | ||
228 | |||
229 | /* | ||
230 | * downgrade write lock to read lock | ||
231 | */ | ||
232 | static inline void __downgrade_write(struct rw_semaphore *sem) | ||
233 | { | ||
234 | __asm__ __volatile__( | ||
235 | "# beginning __downgrade_write\n\t" | ||
236 | LOCK_PREFIX " addl %[bias],(%%rdi)\n\t" /* transitions 0xZZZZ0001 -> 0xYYYY0001 */ | ||
237 | " js 2f\n\t" /* jump if the lock is being waited upon */ | ||
238 | "1:\n\t" | ||
239 | LOCK_SECTION_START("") | ||
240 | "2:\n\t" | ||
241 | " call rwsem_downgrade_thunk\n" | ||
242 | " jmp 1b\n" | ||
243 | LOCK_SECTION_END | ||
244 | "# ending __downgrade_write\n" | ||
245 | : "=m"(sem->count) | ||
246 | : "D"(sem), [bias] "i"(-RWSEM_WAITING_BIAS), "m"(sem->count) | ||
247 | : "memory", "cc"); | ||
248 | } | ||
249 | |||
250 | /* | ||
251 | * implement atomic add functionality | ||
252 | */ | ||
253 | static inline void rwsem_atomic_add(int delta, struct rw_semaphore *sem) | ||
254 | { | ||
255 | __asm__ __volatile__( | ||
256 | LOCK_PREFIX "addl %1,%0" | ||
257 | :"=m"(sem->count) | ||
258 | :"ir"(delta), "m"(sem->count)); | ||
259 | } | ||
260 | |||
261 | /* | ||
262 | * implement exchange and add functionality | ||
263 | */ | ||
264 | static inline int rwsem_atomic_update(int delta, struct rw_semaphore *sem) | ||
265 | { | ||
266 | int tmp = delta; | ||
267 | |||
268 | __asm__ __volatile__( | ||
269 | LOCK_PREFIX "xaddl %0,(%2)" | ||
270 | : "=r"(tmp), "=m"(sem->count) | ||
271 | : "r"(sem), "m"(sem->count), "0" (tmp) | ||
272 | : "memory"); | ||
273 | |||
274 | return tmp+delta; | ||
275 | } | ||
276 | |||
277 | #endif /* __KERNEL__ */ | ||
278 | #endif /* _X8664_RWSEM_H */ | ||