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 27 28 29 30 31 32 33

#ifdef __i386__

#ifdef __GNUC__

__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"
34
                  "ret")
35 36 37 38 39
__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"
40
                  "ret")
41 42
 __ASM_GLOBAL_FUNC(interlocked_cmpxchg64,
                   "push %ebx\n\t"
43 44
                   __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
                   __ASM_CFI(".cfi_rel_offset %ebx,0\n\t")
45
                   "push %esi\n\t"
46 47
                   __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
                   __ASM_CFI(".cfi_rel_offset %esi,0\n\t")
48 49 50 51 52 53 54
                   "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"
55 56
                   __ASM_CFI(".cfi_same_value %esi\n\t")
                   __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
57
                   "pop %ebx\n\t"
58 59
                   __ASM_CFI(".cfi_same_value %ebx\n\t")
                   __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
60
                   "ret")
61 62 63 64
__ASM_GLOBAL_FUNC(interlocked_xchg,
                  "movl 8(%esp),%eax\n\t"
                  "movl 4(%esp),%edx\n\t"
                  "lock; xchgl %eax,(%edx)\n\t"
65
                  "ret")
66 67 68 69
__ASM_GLOBAL_FUNC(interlocked_xchg_ptr,
                  "movl 8(%esp),%eax\n\t"
                  "movl 4(%esp),%edx\n\t"
                  "lock; xchgl %eax,(%edx)\n\t"
70
                  "ret")
71 72 73 74
__ASM_GLOBAL_FUNC(interlocked_xchg_add,
                  "movl 8(%esp),%eax\n\t"
                  "movl 4(%esp),%edx\n\t"
                  "lock; xaddl %eax,(%edx)\n\t"
75
                  "ret")
76 77 78

#elif defined(_MSC_VER)

79
__declspec(naked) int interlocked_cmpxchg( int *dest, int xchg, int compare )
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
{
    __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;
}

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
__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;
}

112
__declspec(naked) int interlocked_xchg( int *dest, int val )
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
{
    __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;
}

128
__declspec(naked) int interlocked_xchg_add( int *dest, int incr )
129 130 131 132 133 134 135 136 137 138 139
{
    __asm mov eax, 8[esp];
    __asm mov edx, 4[esp];
    __asm lock xadd [edx], eax;
    __asm ret;
}

#else
# error You must implement the interlocked* functions for your compiler
#endif

140 141 142 143 144 145 146
#elif defined(__x86_64__)

#ifdef __GNUC__

__ASM_GLOBAL_FUNC(interlocked_cmpxchg,
                  "mov %edx, %eax\n\t"
                  "lock cmpxchgl %esi,(%rdi)\n\t"
147
                  "ret")
148 149 150
__ASM_GLOBAL_FUNC(interlocked_cmpxchg_ptr,
                  "mov %rdx, %rax\n\t"
                  "lock cmpxchgq %rsi,(%rdi)\n\t"
151
                  "ret")
152 153 154 155
__ASM_GLOBAL_FUNC(interlocked_cmpxchg64,
                  "mov %rdx, %rax\n\t"
                  "lock cmpxchgq %rsi,(%rdi)\n\t"
                  "ret")
156 157 158
__ASM_GLOBAL_FUNC(interlocked_xchg,
                  "mov %esi, %eax\n\t"
                  "lock xchgl %eax, (%rdi)\n\t"
159
                  "ret")
160 161 162
__ASM_GLOBAL_FUNC(interlocked_xchg_ptr,
                  "mov %rsi, %rax\n\t"
                  "lock xchgq %rax,(%rdi)\n\t"
163
                  "ret")
164 165 166
__ASM_GLOBAL_FUNC(interlocked_xchg_add,
                  "mov %esi, %eax\n\t"
                  "lock xaddl %eax, (%rdi)\n\t"
167
                  "ret")
168 169 170 171 172

#else
# error You must implement the interlocked* functions for your compiler
#endif

173 174 175
#elif defined(__powerpc__)
void* interlocked_cmpxchg_ptr( void **dest, void* xchg, void* compare)
{
176 177
    void *ret = 0;
    void *scratch;
178
    __asm__ __volatile__(
179 180 181 182 183
        "0:    lwarx %0,0,%2\n"
        "      xor. %1,%4,%0\n"
        "      bne 1f\n"
        "      stwcx. %3,0,%2\n"
        "      bne- 0b\n"
184
        "      isync\n"
185 186 187 188
        "1:    "
        : "=&r"(ret), "=&r"(scratch)
        : "r"(dest), "r"(xchg), "r"(compare)
        : "cr0","memory");
189
    return ret;
190 191
}

192 193 194 195 196 197
__int64 interlocked_cmpxchg64( __int64 *dest, __int64 xchg, __int64 compare)
{
    /* FIXME: add code */
    assert(0);
}

198
int interlocked_cmpxchg( int *dest, int xchg, int compare)
199
{
200 201
    int ret = 0;
    int scratch;
202
    __asm__ __volatile__(
203 204 205 206 207
        "0:    lwarx %0,0,%2\n"
        "      xor. %1,%4,%0\n"
        "      bne 1f\n"
        "      stwcx. %3,0,%2\n"
        "      bne- 0b\n"
208
        "      isync\n"
209 210 211
        "1:    "
        : "=&r"(ret), "=&r"(scratch)
        : "r"(dest), "r"(xchg), "r"(compare)
212
        : "cr0","memory","r0");
213 214 215
    return ret;
}

216
int interlocked_xchg_add( int *dest, int incr )
217
{
218 219
    int ret = 0;
    int zero = 0;
220
    __asm__ __volatile__(
221 222 223 224
        "0:    lwarx %0, %3, %1\n"
        "      add %0, %2, %0\n"
        "      stwcx. %0, %3, %1\n"
        "      bne- 0b\n"
225
        "      isync\n"
226 227
        : "=&r" (ret)
        : "r"(dest), "r"(incr), "r"(zero)
228
        : "cr0", "memory", "r0"
229 230 231 232
    );
    return ret-incr;
}

233
int interlocked_xchg( int* dest, int val )
234
{
235
    int ret = 0;
236
    __asm__ __volatile__(
237 238 239
        "0:    lwarx %0,0,%1\n"
        "      stwcx. %2,0,%1\n"
        "      bne- 0b\n"
240
        "      isync\n"
241 242
        : "=&r"(ret)
        : "r"(dest), "r"(val)
243
        : "cr0","memory","r0");
244 245 246 247 248 249 250
    return ret;
}

void* interlocked_xchg_ptr( void** dest, void* val )
{
    void *ret = NULL;
    __asm__ __volatile__(
251 252
        "0:    lwarx %0,0,%1\n"
        "      stwcx. %2,0,%1\n"
253
        "      bne- 0b\n"
254
        "      isync\n"
255 256
        : "=&r"(ret)
        : "r"(dest), "r"(val)
257
        : "cr0","memory","r0");
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    return ret;
}

#elif defined(__sparc__) && defined(__sun__)

/*
 * As the earlier Sparc processors lack necessary atomic instructions,
 * I'm simply falling back to the library-provided _lwp_mutex routines
 * to ensure mutual exclusion in a way appropriate for the current
 * architecture.
 *
 * FIXME:  If we have the compare-and-swap instruction (Sparc v9 and above)
 *         we could use this to speed up the Interlocked operations ...
 */
#include <synch.h>
static lwp_mutex_t interlocked_mutex = DEFAULTMUTEX;

275
int interlocked_cmpxchg( int *dest, int xchg, int compare )
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
{
    _lwp_mutex_lock( &interlocked_mutex );
    if (*dest == compare) *dest = xchg;
    else compare = *dest;
    _lwp_mutex_unlock( &interlocked_mutex );
    return compare;
}

void *interlocked_cmpxchg_ptr( void **dest, void *xchg, void *compare )
{
    _lwp_mutex_lock( &interlocked_mutex );
    if (*dest == compare) *dest = xchg;
    else compare = *dest;
    _lwp_mutex_unlock( &interlocked_mutex );
    return compare;
}

293 294 295 296 297 298 299 300 301
__int64 interlocked_cmpxchg64( __int64 *dest, __int64 xchg, __int64 compare )
{
    _lwp_mutex_lock( &interlocked_mutex );
    if (*dest == compare) *dest = xchg;
    else compare = *dest;
    _lwp_mutex_unlock( &interlocked_mutex );
    return compare;
}

302
int interlocked_xchg( int *dest, int val )
303
{
304
    int retv;
305 306 307 308 309 310 311 312 313
    _lwp_mutex_lock( &interlocked_mutex );
    retv = *dest;
    *dest = val;
    _lwp_mutex_unlock( &interlocked_mutex );
    return retv;
}

void *interlocked_xchg_ptr( void **dest, void *val )
{
314
    void *retv;
315 316 317 318 319 320 321
    _lwp_mutex_lock( &interlocked_mutex );
    retv = *dest;
    *dest = val;
    _lwp_mutex_unlock( &interlocked_mutex );
    return retv;
}

322
int interlocked_xchg_add( int *dest, int incr )
323
{
324
    int retv;
325 326 327 328 329 330
    _lwp_mutex_lock( &interlocked_mutex );
    retv = *dest;
    *dest += incr;
    _lwp_mutex_unlock( &interlocked_mutex );
    return retv;
}
331 332 333 334 335

#elif defined(__ALPHA__) && defined(__GNUC__)

__ASM_GLOBAL_FUNC(interlocked_cmpxchg,
                  "L0cmpxchg:\n\t"
336
                  "ldl_l $0,0($16)\n\t"
337 338 339
                  "cmpeq $0,$18,$1\n\t"
                  "beq   $1,L1cmpxchg\n\t"
                  "mov   $17,$0\n\t"
340
                  "stl_c $0,0($16)\n\t"
341 342 343
                  "beq   $0,L0cmpxchg\n\t"
                  "mov   $18,$0\n"
                  "L1cmpxchg:\n\t"
344
                  "mb")
345 346 347 348 349 350 351 352 353 354 355

__ASM_GLOBAL_FUNC(interlocked_cmpxchg_ptr,
                  "L0cmpxchg_ptr:\n\t"
                  "ldq_l $0,0($16)\n\t"
                  "cmpeq $0,$18,$1\n\t"
                  "beq   $1,L1cmpxchg_ptr\n\t"
                  "mov   $17,$0\n\t"
                  "stq_c $0,0($16)\n\t"
                  "beq   $0,L0cmpxchg_ptr\n\t"
                  "mov   $18,$0\n"
                  "L1cmpxchg_ptr:\n\t"
356
                  "mb")
357

358 359 360 361 362 363
__int64 interlocked_cmpxchg64(__int64 *dest, __int64 xchg, __int64 compare)
{
    /* FIXME: add code */
    assert(0);
}

364 365
__ASM_GLOBAL_FUNC(interlocked_xchg,
                  "L0xchg:\n\t"
366
                  "ldl_l $0,0($16)\n\t"
367
                  "mov   $17,$1\n\t"
368
                  "stl_c $1,0($16)\n\t"
369
                  "beq   $1,L0xchg\n\t"
370
                  "mb")
371 372 373 374 375 376 377

__ASM_GLOBAL_FUNC(interlocked_xchg_ptr,
                  "L0xchg_ptr:\n\t"
                  "ldq_l $0,0($16)\n\t"
                  "mov   $17,$1\n\t"
                  "stq_c $1,0($16)\n\t"
                  "beq   $1,L0xchg_ptr\n\t"
378
                  "mb")
379 380 381

__ASM_GLOBAL_FUNC(interlocked_xchg_add,
                  "L0xchg_add:\n\t"
382 383 384
                  "ldl_l $0,0($16)\n\t"
                  "addl  $0,$17,$1\n\t"
                  "stl_c $1,0($16)\n\t"
385
                  "beq   $1,L0xchg_add\n\t"
386
                  "mb")
387

388 389 390
#else
# error You must implement the interlocked* functions for your CPU
#endif