2 * interlocked functions
4 * Copyright 1996 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
29 __ASM_GLOBAL_FUNC(interlocked_cmpxchg,
30 "movl 12(%esp),%eax\n\t"
31 "movl 8(%esp),%ecx\n\t"
32 "movl 4(%esp),%edx\n\t"
33 "lock; cmpxchgl %ecx,(%edx)\n\t"
35 __ASM_GLOBAL_FUNC(interlocked_cmpxchg_ptr,
36 "movl 12(%esp),%eax\n\t"
37 "movl 8(%esp),%ecx\n\t"
38 "movl 4(%esp),%edx\n\t"
39 "lock; cmpxchgl %ecx,(%edx)\n\t"
41 __ASM_GLOBAL_FUNC(interlocked_cmpxchg64,
43 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
44 __ASM_CFI(".cfi_rel_offset %ebx,0\n\t")
46 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
47 __ASM_CFI(".cfi_rel_offset %esi,0\n\t")
48 "movl 12(%esp),%esi\n\t"
49 "movl 16(%esp),%ebx\n\t"
50 "movl 20(%esp),%ecx\n\t"
51 "movl 24(%esp),%eax\n\t"
52 "movl 28(%esp),%edx\n\t"
53 "lock; cmpxchg8b (%esi)\n\t"
55 __ASM_CFI(".cfi_same_value %esi\n\t")
56 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
58 __ASM_CFI(".cfi_same_value %ebx\n\t")
59 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
61 __ASM_GLOBAL_FUNC(interlocked_xchg,
62 "movl 8(%esp),%eax\n\t"
63 "movl 4(%esp),%edx\n\t"
64 "lock; xchgl %eax,(%edx)\n\t"
66 __ASM_GLOBAL_FUNC(interlocked_xchg_ptr,
67 "movl 8(%esp),%eax\n\t"
68 "movl 4(%esp),%edx\n\t"
69 "lock; xchgl %eax,(%edx)\n\t"
71 __ASM_GLOBAL_FUNC(interlocked_xchg_add,
72 "movl 8(%esp),%eax\n\t"
73 "movl 4(%esp),%edx\n\t"
74 "lock; xaddl %eax,(%edx)\n\t"
77 #elif defined(_MSC_VER)
79 __declspec(naked) int interlocked_cmpxchg( int *dest, int xchg, int compare )
81 __asm mov eax, 12[esp];
82 __asm mov ecx, 8[esp];
83 __asm mov edx, 4[esp];
84 __asm lock cmpxchg [edx], ecx;
88 __declspec(naked) void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare )
90 __asm mov eax, 12[esp];
91 __asm mov ecx, 8[esp];
92 __asm mov edx, 4[esp];
93 __asm lock cmpxchg [edx], ecx;
97 __declspec(naked) __int64 interlocked_cmpxchg64( __int64 *dest, __int64 xchg, __int64 compare)
101 __asm mov esi, 12[esp];
102 __asm mov ebx, 16[esp];
103 __asm mov ecx, 20[esp];
104 __asm mov eax, 24[esp];
105 __asm mov edx, 28[esp];
106 __asm lock cmpxchg8b [esi];
112 __declspec(naked) int interlocked_xchg( int *dest, int val )
114 __asm mov eax, 8[esp];
115 __asm mov edx, 4[esp];
116 __asm lock xchg [edx], eax;
120 __declspec(naked) void *interlocked_xchg_ptr( void **dest, void *val )
122 __asm mov eax, 8[esp];
123 __asm mov edx, 4[esp];
124 __asm lock xchg [edx], eax;
128 __declspec(naked) int interlocked_xchg_add( int *dest, int incr )
130 __asm mov eax, 8[esp];
131 __asm mov edx, 4[esp];
132 __asm lock xadd [edx], eax;
137 # error You must implement the interlocked* functions for your compiler
140 #elif defined(__x86_64__)
144 __ASM_GLOBAL_FUNC(interlocked_cmpxchg,
146 "lock cmpxchgl %esi,(%rdi)\n\t"
148 __ASM_GLOBAL_FUNC(interlocked_cmpxchg_ptr,
150 "lock cmpxchgq %rsi,(%rdi)\n\t"
152 __ASM_GLOBAL_FUNC(interlocked_cmpxchg64,
154 "lock cmpxchgq %rsi,(%rdi)\n\t"
156 __ASM_GLOBAL_FUNC(interlocked_xchg,
158 "lock xchgl %eax, (%rdi)\n\t"
160 __ASM_GLOBAL_FUNC(interlocked_xchg_ptr,
162 "lock xchgq %rax,(%rdi)\n\t"
164 __ASM_GLOBAL_FUNC(interlocked_xchg_add,
166 "lock xaddl %eax, (%rdi)\n\t"
170 # error You must implement the interlocked* functions for your compiler
173 #elif defined(__powerpc__)
174 void* interlocked_cmpxchg_ptr( void **dest, void* xchg, void* compare)
178 __asm__ __volatile__(
186 : "=&r"(ret), "=&r"(scratch)
187 : "r"(dest), "r"(xchg), "r"(compare)
192 __int64 interlocked_cmpxchg64( __int64 *dest, __int64 xchg, __int64 compare)
194 /* FIXME: add code */
198 int interlocked_cmpxchg( int *dest, int xchg, int compare)
202 __asm__ __volatile__(
210 : "=&r"(ret), "=&r"(scratch)
211 : "r"(dest), "r"(xchg), "r"(compare)
212 : "cr0","memory","r0");
216 int interlocked_xchg_add( int *dest, int incr )
220 __asm__ __volatile__(
221 "0: lwarx %0, %3, %1\n"
223 " stwcx. %0, %3, %1\n"
227 : "r"(dest), "r"(incr), "r"(zero)
228 : "cr0", "memory", "r0"
233 int interlocked_xchg( int* dest, int val )
236 __asm__ __volatile__(
242 : "r"(dest), "r"(val)
243 : "cr0","memory","r0");
247 void* interlocked_xchg_ptr( void** dest, void* val )
250 __asm__ __volatile__(
256 : "r"(dest), "r"(val)
257 : "cr0","memory","r0");
261 #elif defined(__sparc__) && defined(__sun__)
264 * As the earlier Sparc processors lack necessary atomic instructions,
265 * I'm simply falling back to the library-provided _lwp_mutex routines
266 * to ensure mutual exclusion in a way appropriate for the current
269 * FIXME: If we have the compare-and-swap instruction (Sparc v9 and above)
270 * we could use this to speed up the Interlocked operations ...
273 static lwp_mutex_t interlocked_mutex = DEFAULTMUTEX;
275 int interlocked_cmpxchg( int *dest, int xchg, int compare )
277 _lwp_mutex_lock( &interlocked_mutex );
278 if (*dest == compare) *dest = xchg;
279 else compare = *dest;
280 _lwp_mutex_unlock( &interlocked_mutex );
284 void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare )
286 _lwp_mutex_lock( &interlocked_mutex );
287 if (*dest == compare) *dest = xchg;
288 else compare = *dest;
289 _lwp_mutex_unlock( &interlocked_mutex );
293 __int64 interlocked_cmpxchg64( __int64 *dest, __int64 xchg, __int64 compare )
295 _lwp_mutex_lock( &interlocked_mutex );
296 if (*dest == compare) *dest = xchg;
297 else compare = *dest;
298 _lwp_mutex_unlock( &interlocked_mutex );
302 int interlocked_xchg( int *dest, int val )
305 _lwp_mutex_lock( &interlocked_mutex );
308 _lwp_mutex_unlock( &interlocked_mutex );
312 void *interlocked_xchg_ptr( void **dest, void *val )
315 _lwp_mutex_lock( &interlocked_mutex );
318 _lwp_mutex_unlock( &interlocked_mutex );
322 int interlocked_xchg_add( int *dest, int incr )
325 _lwp_mutex_lock( &interlocked_mutex );
328 _lwp_mutex_unlock( &interlocked_mutex );
332 #elif defined(__ALPHA__) && defined(__GNUC__)
334 __ASM_GLOBAL_FUNC(interlocked_cmpxchg,
336 "ldl_l $0,0($16)\n\t"
337 "cmpeq $0,$18,$1\n\t"
338 "beq $1,L1cmpxchg\n\t"
340 "stl_c $0,0($16)\n\t"
341 "beq $0,L0cmpxchg\n\t"
346 __ASM_GLOBAL_FUNC(interlocked_cmpxchg_ptr,
348 "ldq_l $0,0($16)\n\t"
349 "cmpeq $0,$18,$1\n\t"
350 "beq $1,L1cmpxchg_ptr\n\t"
352 "stq_c $0,0($16)\n\t"
353 "beq $0,L0cmpxchg_ptr\n\t"
358 __int64 interlocked_cmpxchg64(__int64 *dest, __int64 xchg, __int64 compare)
360 /* FIXME: add code */
364 __ASM_GLOBAL_FUNC(interlocked_xchg,
366 "ldl_l $0,0($16)\n\t"
368 "stl_c $1,0($16)\n\t"
372 __ASM_GLOBAL_FUNC(interlocked_xchg_ptr,
374 "ldq_l $0,0($16)\n\t"
376 "stq_c $1,0($16)\n\t"
377 "beq $1,L0xchg_ptr\n\t"
380 __ASM_GLOBAL_FUNC(interlocked_xchg_add,
382 "ldl_l $0,0($16)\n\t"
384 "stl_c $1,0($16)\n\t"
385 "beq $1,L0xchg_add\n\t"
389 # error You must implement the interlocked* functions for your CPU