interlocked.c 11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * interlocked functions
 *
 * Copyright 1996 Alexandre Julliard
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
19 20 21 22
 */

#include "config.h"
#include "wine/port.h"
23
#include <assert.h>
24 25 26

#ifdef __i386__

27
#if defined(_MSC_VER)
28

29
__declspec(naked) int interlocked_cmpxchg( int *dest, int xchg, int compare )
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
{
    __asm mov eax, 12[esp];
    __asm mov ecx, 8[esp];
    __asm mov edx, 4[esp];
    __asm lock cmpxchg [edx], ecx;
    __asm ret;
}

__declspec(naked) void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare )
{
    __asm mov eax, 12[esp];
    __asm mov ecx, 8[esp];
    __asm mov edx, 4[esp];
    __asm lock cmpxchg [edx], ecx;
    __asm ret;
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
__declspec(naked) __int64 interlocked_cmpxchg64( __int64 *dest, __int64 xchg, __int64 compare)
{
    __asm push ebx;
    __asm push esi;
    __asm mov esi, 12[esp];
    __asm mov ebx, 16[esp];
    __asm mov ecx, 20[esp];
    __asm mov eax, 24[esp];
    __asm mov edx, 28[esp];
    __asm lock cmpxchg8b [esi];
    __asm pop esi;
    __asm pop ebx;
    __asm ret;
}

62
__declspec(naked) int interlocked_xchg( int *dest, int val )
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
{
    __asm mov eax, 8[esp];
    __asm mov edx, 4[esp];
    __asm lock xchg [edx], eax;
    __asm ret;
}

__declspec(naked) void *interlocked_xchg_ptr( void **dest, void *val )
{
    __asm mov eax, 8[esp];
    __asm mov edx, 4[esp];
    __asm lock xchg [edx], eax;
    __asm ret;
}

78
__declspec(naked) int interlocked_xchg_add( int *dest, int incr )
79 80 81 82 83 84 85 86
{
    __asm mov eax, 8[esp];
    __asm mov edx, 4[esp];
    __asm lock xadd [edx], eax;
    __asm ret;
}

#else
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
/* use gcc compatible asm code as default for __i386__ */

__ASM_GLOBAL_FUNC(interlocked_cmpxchg,
                  "movl 12(%esp),%eax\n\t"
                  "movl 8(%esp),%ecx\n\t"
                  "movl 4(%esp),%edx\n\t"
                  "lock; cmpxchgl %ecx,(%edx)\n\t"
                  "ret")
__ASM_GLOBAL_FUNC(interlocked_cmpxchg_ptr,
                  "movl 12(%esp),%eax\n\t"
                  "movl 8(%esp),%ecx\n\t"
                  "movl 4(%esp),%edx\n\t"
                  "lock; cmpxchgl %ecx,(%edx)\n\t"
                  "ret")
 __ASM_GLOBAL_FUNC(interlocked_cmpxchg64,
                   "push %ebx\n\t"
                   __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
                   __ASM_CFI(".cfi_rel_offset %ebx,0\n\t")
                   "push %esi\n\t"
                   __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
                   __ASM_CFI(".cfi_rel_offset %esi,0\n\t")
                   "movl 12(%esp),%esi\n\t"
                   "movl 16(%esp),%ebx\n\t"
                   "movl 20(%esp),%ecx\n\t"
                   "movl 24(%esp),%eax\n\t"
                   "movl 28(%esp),%edx\n\t"
                   "lock; cmpxchg8b (%esi)\n\t"
                   "pop %esi\n\t"
                   __ASM_CFI(".cfi_same_value %esi\n\t")
                   __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
                   "pop %ebx\n\t"
                   __ASM_CFI(".cfi_same_value %ebx\n\t")
                   __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
                   "ret")
__ASM_GLOBAL_FUNC(interlocked_xchg,
                  "movl 8(%esp),%eax\n\t"
                  "movl 4(%esp),%edx\n\t"
                  "lock; xchgl %eax,(%edx)\n\t"
                  "ret")
__ASM_GLOBAL_FUNC(interlocked_xchg_ptr,
                  "movl 8(%esp),%eax\n\t"
                  "movl 4(%esp),%edx\n\t"
                  "lock; xchgl %eax,(%edx)\n\t"
                  "ret")
__ASM_GLOBAL_FUNC(interlocked_xchg_add,
                  "movl 8(%esp),%eax\n\t"
                  "movl 4(%esp),%edx\n\t"
                  "lock; xaddl %eax,(%edx)\n\t"
                  "ret")

137 138
#endif

139 140 141 142 143
#elif defined(__x86_64__)

__ASM_GLOBAL_FUNC(interlocked_cmpxchg,
                  "mov %edx, %eax\n\t"
                  "lock cmpxchgl %esi,(%rdi)\n\t"
144
                  "ret")
145 146 147
__ASM_GLOBAL_FUNC(interlocked_cmpxchg_ptr,
                  "mov %rdx, %rax\n\t"
                  "lock cmpxchgq %rsi,(%rdi)\n\t"
148
                  "ret")
149 150 151 152
__ASM_GLOBAL_FUNC(interlocked_cmpxchg64,
                  "mov %rdx, %rax\n\t"
                  "lock cmpxchgq %rsi,(%rdi)\n\t"
                  "ret")
153 154 155
__ASM_GLOBAL_FUNC(interlocked_xchg,
                  "mov %esi, %eax\n\t"
                  "lock xchgl %eax, (%rdi)\n\t"
156
                  "ret")
157 158 159
__ASM_GLOBAL_FUNC(interlocked_xchg_ptr,
                  "mov %rsi, %rax\n\t"
                  "lock xchgq %rax,(%rdi)\n\t"
160
                  "ret")
161 162 163
__ASM_GLOBAL_FUNC(interlocked_xchg_add,
                  "mov %esi, %eax\n\t"
                  "lock xaddl %eax, (%rdi)\n\t"
164
                  "ret")
165 166
__ASM_GLOBAL_FUNC(interlocked_cmpxchg128,
                  "push %rbx\n\t"
167 168
                  __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
                  __ASM_CFI(".cfi_rel_offset %rbx,0\n\t")
169 170 171 172 173 174 175 176 177 178
                  "mov %rcx,%r8\n\t"  /* compare */
                  "mov %rdx,%rbx\n\t" /* xchg_low */
                  "mov %rsi,%rcx\n\t" /* xchg_high */
                  "mov 0(%r8),%rax\n\t"
                  "mov 8(%r8),%rdx\n\t"
                  "lock cmpxchg16b (%rdi)\n\t"
                  "mov %rax,0(%r8)\n\t"
                  "mov %rdx,8(%r8)\n\t"
                  "setz %al\n\t"
                  "pop %rbx\n\t"
179 180
                  __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
                  __ASM_CFI(".cfi_same_value %rbx\n\t")
181
                  "ret")
182

183
#elif defined(__powerpc__)
184 185 186

#if !(defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) && __SIZEOF_POINTER__ == 4) \
 && !(defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8) && __SIZEOF_POINTER__ == 8)
187 188
void* interlocked_cmpxchg_ptr( void **dest, void* xchg, void* compare)
{
189 190
    void *ret = 0;
    void *scratch;
191
    __asm__ __volatile__(
192 193 194 195 196
        "0:    lwarx %0,0,%2\n"
        "      xor. %1,%4,%0\n"
        "      bne 1f\n"
        "      stwcx. %3,0,%2\n"
        "      bne- 0b\n"
197
        "      isync\n"
198 199 200 201
        "1:    "
        : "=&r"(ret), "=&r"(scratch)
        : "r"(dest), "r"(xchg), "r"(compare)
        : "cr0","memory");
202
    return ret;
203
}
204
#endif
205

206
#ifndef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
207 208 209 210 211
__int64 interlocked_cmpxchg64( __int64 *dest, __int64 xchg, __int64 compare)
{
    /* FIXME: add code */
    assert(0);
}
212
#endif
213

214
#ifndef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
215
int interlocked_cmpxchg( int *dest, int xchg, int compare)
216
{
217 218
    int ret = 0;
    int scratch;
219
    __asm__ __volatile__(
220 221 222 223 224
        "0:    lwarx %0,0,%2\n"
        "      xor. %1,%4,%0\n"
        "      bne 1f\n"
        "      stwcx. %3,0,%2\n"
        "      bne- 0b\n"
225
        "      isync\n"
226 227 228
        "1:    "
        : "=&r"(ret), "=&r"(scratch)
        : "r"(dest), "r"(xchg), "r"(compare)
229
        : "cr0","memory","r0");
230 231
    return ret;
}
232
#endif
233

234
#ifndef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
235
int interlocked_xchg_add( int *dest, int incr )
236
{
237 238
    int ret = 0;
    int zero = 0;
239
    __asm__ __volatile__(
240 241 242 243
        "0:    lwarx %0, %3, %1\n"
        "      add %0, %2, %0\n"
        "      stwcx. %0, %3, %1\n"
        "      bne- 0b\n"
244
        "      isync\n"
245 246
        : "=&r" (ret)
        : "r"(dest), "r"(incr), "r"(zero)
247
        : "cr0", "memory", "r0"
248 249 250
    );
    return ret-incr;
}
251
#endif
252

253
#ifndef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
254
int interlocked_xchg( int* dest, int val )
255
{
256
    int ret = 0;
257
    __asm__ __volatile__(
258 259 260
        "0:    lwarx %0,0,%1\n"
        "      stwcx. %2,0,%1\n"
        "      bne- 0b\n"
261
        "      isync\n"
262 263
        : "=&r"(ret)
        : "r"(dest), "r"(val)
264
        : "cr0","memory","r0");
265 266
    return ret;
}
267
#endif
268

269 270
#if !(defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) && __SIZEOF_POINTER__ == 4) \
 && !(defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8) && __SIZEOF_POINTER__ == 8)
271 272 273 274
void* interlocked_xchg_ptr( void** dest, void* val )
{
    void *ret = NULL;
    __asm__ __volatile__(
275 276
        "0:    lwarx %0,0,%1\n"
        "      stwcx. %2,0,%1\n"
277
        "      bne- 0b\n"
278
        "      isync\n"
279 280
        : "=&r"(ret)
        : "r"(dest), "r"(val)
281
        : "cr0","memory","r0");
282 283
    return ret;
}
284
#endif
285 286

#else
287 288 289 290 291

#include <pthread.h>

static pthread_mutex_t interlocked_mutex = PTHREAD_MUTEX_INITIALIZER;

292
#ifndef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
293 294 295 296 297 298 299 300 301 302 303 304
int interlocked_cmpxchg( int *dest, int xchg, int compare )
{
    pthread_mutex_lock( &interlocked_mutex );

    if (*dest == compare)
        *dest = xchg;
    else
        compare = *dest;

    pthread_mutex_unlock( &interlocked_mutex );
    return compare;
}
305
#endif
306

307 308
#if !(defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) && __SIZEOF_POINTER__ == 4) \
 && !(defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8) && __SIZEOF_POINTER__ == 8)
309 310 311 312 313 314 315 316 317 318 319 320
void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare )
{
    pthread_mutex_lock( &interlocked_mutex );

    if (*dest == compare)
        *dest = xchg;
    else
        compare = *dest;

    pthread_mutex_unlock( &interlocked_mutex );
    return compare;
}
321
#endif
322

323
#ifndef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
324 325 326 327 328 329 330 331 332 333 334 335
__int64 interlocked_cmpxchg64( __int64 *dest, __int64 xchg, __int64 compare )
{
    pthread_mutex_lock( &interlocked_mutex );

    if (*dest == compare)
        *dest = xchg;
    else
        compare = *dest;

    pthread_mutex_unlock( &interlocked_mutex );
    return compare;
}
336
#endif
337

338
#ifndef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
339 340 341 342 343 344 345 346 347
int interlocked_xchg( int *dest, int val )
{
    int retv;
    pthread_mutex_lock( &interlocked_mutex );
    retv = *dest;
    *dest = val;
    pthread_mutex_unlock( &interlocked_mutex );
    return retv;
}
348
#endif
349

350 351
#if !(defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) && __SIZEOF_POINTER__ == 4) \
 && !(defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8) && __SIZEOF_POINTER__ == 8)
352 353 354 355 356 357 358 359 360
void *interlocked_xchg_ptr( void **dest, void *val )
{
    void *retv;
    pthread_mutex_lock( &interlocked_mutex );
    retv = *dest;
    *dest = val;
    pthread_mutex_unlock( &interlocked_mutex );
    return retv;
}
361
#endif
362

363
#ifndef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
364 365 366 367 368 369 370 371 372
int interlocked_xchg_add( int *dest, int incr )
{
    int retv;
    pthread_mutex_lock( &interlocked_mutex );
    retv = *dest;
    *dest += incr;
    pthread_mutex_unlock( &interlocked_mutex );
    return retv;
}
373
#endif
374

375
unsigned char interlocked_cmpxchg128( __int64 *dest, __int64 xchg_high, __int64 xchg_low, __int64 *compare )
376
{
377
    unsigned char retv;
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    pthread_mutex_lock( &interlocked_mutex );
    if (dest[0] == compare[0] && dest[1] == compare[1])
    {
        dest[0] = xchg_low;
        dest[1] = xchg_high;
        retv = 1;
    }
    else
    {
        compare[0] = dest[0];
        compare[1] = dest[1];
        retv = 0;
    }
    pthread_mutex_unlock( &interlocked_mutex );
    return retv;
}

395
#endif