src/atomic/SDL_spinlock.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 29 Jun 2018 16:55:55 -0400
changeset 12041 f34f7903df97
parent 12040 58425859a4e5
child 12044 26c36b7d5f36
permissions -rw-r--r--
Corrected a comment.
slouken@8582
     1
/*
slouken@8582
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
slouken@8582
     4
slouken@8582
     5
  This software is provided 'as-is', without any express or implied
slouken@8582
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@8582
     7
  arising from the use of this software.
slouken@8582
     8
slouken@8582
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@8582
    10
  including commercial applications, and to alter it and redistribute it
slouken@8582
    11
  freely, subject to the following restrictions:
slouken@8582
    12
slouken@8582
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@8582
    14
     claim that you wrote the original software. If you use this software
slouken@8582
    15
     in a product, an acknowledgment in the product documentation would be
slouken@8582
    16
     appreciated but is not required.
slouken@8582
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@8582
    18
     misrepresented as being the original software.
slouken@8582
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@8582
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@8582
    22
slouken@8582
    23
#if defined(__WIN32__) || defined(__WINRT__)
slouken@8582
    24
#include "../core/windows/SDL_windows.h"
slouken@8582
    25
#endif
slouken@8582
    26
slouken@8582
    27
#include "SDL_atomic.h"
slouken@8582
    28
#include "SDL_mutex.h"
slouken@8582
    29
#include "SDL_timer.h"
slouken@8582
    30
binarycrusader@8979
    31
#if !defined(HAVE_GCC_ATOMICS) && defined(__SOLARIS__)
binarycrusader@8979
    32
#include <atomic.h>
binarycrusader@8979
    33
#endif
slouken@8582
    34
icculus@12038
    35
#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
icculus@12038
    36
#include <xmmintrin.h>
icculus@12038
    37
#endif
icculus@12038
    38
sezeroz@11318
    39
#if defined(__WATCOMC__) && defined(__386__)
sezeroz@11318
    40
SDL_COMPILE_TIME_ASSERT(locksize, 4==sizeof(SDL_SpinLock));
sezeroz@11318
    41
extern _inline int _SDL_xchg_watcom(volatile int *a, int v);
sezeroz@11318
    42
#pragma aux _SDL_xchg_watcom = \
sezeroz@11318
    43
  "xchg [ecx], eax" \
sezeroz@11318
    44
  parm [ecx] [eax] \
sezeroz@11318
    45
  value [eax] \
sezeroz@11318
    46
  modify exact [eax];
sezeroz@11318
    47
#endif /* __WATCOMC__ && __386__ */
sezeroz@11318
    48
slouken@8582
    49
/* This function is where all the magic happens... */
slouken@8582
    50
SDL_bool
slouken@8582
    51
SDL_AtomicTryLock(SDL_SpinLock *lock)
slouken@8582
    52
{
slouken@8582
    53
#if SDL_ATOMIC_DISABLED
slouken@8582
    54
    /* Terrible terrible damage */
slouken@8582
    55
    static SDL_mutex *_spinlock_mutex;
slouken@8582
    56
slouken@8582
    57
    if (!_spinlock_mutex) {
slouken@8582
    58
        /* Race condition on first lock... */
slouken@8582
    59
        _spinlock_mutex = SDL_CreateMutex();
slouken@8582
    60
    }
slouken@8582
    61
    SDL_LockMutex(_spinlock_mutex);
slouken@8582
    62
    if (*lock == 0) {
slouken@8582
    63
        *lock = 1;
slouken@8582
    64
        SDL_UnlockMutex(_spinlock_mutex);
slouken@8582
    65
        return SDL_TRUE;
slouken@8582
    66
    } else {
slouken@8582
    67
        SDL_UnlockMutex(_spinlock_mutex);
slouken@8582
    68
        return SDL_FALSE;
slouken@8582
    69
    }
slouken@8582
    70
slouken@8582
    71
#elif defined(_MSC_VER)
slouken@8582
    72
    SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
slouken@8582
    73
    return (InterlockedExchange((long*)lock, 1) == 0);
slouken@8582
    74
sezeroz@11318
    75
#elif defined(__WATCOMC__) && defined(__386__)
sezeroz@11318
    76
    return _SDL_xchg_watcom(lock, 1) == 0;
sezeroz@11318
    77
slouken@8582
    78
#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
slouken@8582
    79
    return (__sync_lock_test_and_set(lock, 1) == 0);
slouken@8582
    80
slouken@8582
    81
#elif defined(__GNUC__) && defined(__arm__) && \
slouken@8582
    82
        (defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \
slouken@8582
    83
         defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__) || \
slouken@8582
    84
         defined(__ARM_ARCH_5TEJ__))
slouken@8582
    85
    int result;
slouken@8582
    86
    __asm__ __volatile__ (
slouken@8582
    87
        "swp %0, %1, [%2]\n"
slouken@8582
    88
        : "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory");
slouken@8582
    89
    return (result == 0);
slouken@8582
    90
slouken@8582
    91
#elif defined(__GNUC__) && defined(__arm__)
slouken@8582
    92
    int result;
slouken@8582
    93
    __asm__ __volatile__ (
slouken@8582
    94
        "ldrex %0, [%2]\nteq   %0, #0\nstrexeq %0, %1, [%2]"
slouken@8582
    95
        : "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory");
slouken@8582
    96
    return (result == 0);
slouken@8582
    97
slouken@8582
    98
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
slouken@8582
    99
    int result;
slouken@8582
   100
    __asm__ __volatile__(
slouken@8582
   101
        "lock ; xchgl %0, (%1)\n"
slouken@8582
   102
        : "=r" (result) : "r" (lock), "0" (1) : "cc", "memory");
slouken@8582
   103
    return (result == 0);
slouken@8582
   104
slouken@8582
   105
#elif defined(__MACOSX__) || defined(__IPHONEOS__)
slouken@8582
   106
    /* Maybe used for PowerPC, but the Intel asm or gcc atomics are favored. */
slouken@8582
   107
    return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
slouken@8582
   108
binarycrusader@8979
   109
#elif defined(__SOLARIS__) && defined(_LP64)
binarycrusader@8979
   110
    /* Used for Solaris with non-gcc compilers. */
binarycrusader@8979
   111
    return (SDL_bool) ((int) atomic_cas_64((volatile uint64_t*)lock, 0, 1) == 0);
binarycrusader@8979
   112
binarycrusader@8979
   113
#elif defined(__SOLARIS__) && !defined(_LP64)
binarycrusader@8979
   114
    /* Used for Solaris with non-gcc compilers. */
binarycrusader@8979
   115
    return (SDL_bool) ((int) atomic_cas_32((volatile uint32_t*)lock, 0, 1) == 0);
binarycrusader@8979
   116
slouken@8582
   117
#else
slouken@8582
   118
#error Please implement for your platform.
slouken@8582
   119
    return SDL_FALSE;
slouken@8582
   120
#endif
slouken@8582
   121
}
slouken@8582
   122
icculus@12038
   123
/* "REP NOP" is PAUSE, coded for tools that don't know it by that name. */
icculus@12038
   124
#if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
icculus@12041
   125
    #define PAUSE_INSTRUCTION() __asm__ __volatile__("pause\n")  /* Some assemblers can't do REP NOP, so go with PAUSE. */
icculus@12038
   126
#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
icculus@12040
   127
    #define PAUSE_INSTRUCTION() _mm_pause()  /* this is actually "rep nop" and not a SIMD instruction. No inline asm in MSVC x86-64! */
icculus@12038
   128
#elif defined(__WATCOMC__) && defined(__386__)
icculus@12039
   129
    /* watcom assembler won't let you do PAUSE, but refuses REP NOP as an invalid combination. Hardcode the bytes. */
icculus@12038
   130
    extern _inline void PAUSE_INSTRUCTION(void);
icculus@12039
   131
    #pragma aux PAUSE_INSTRUCTION = "db 0f3h,90h"
icculus@12038
   132
#else
icculus@12038
   133
    #define PAUSE_INSTRUCTION()
icculus@12038
   134
#endif
icculus@12038
   135
slouken@8582
   136
void
slouken@8582
   137
SDL_AtomicLock(SDL_SpinLock *lock)
slouken@8582
   138
{
icculus@12038
   139
    int iterations = 0;
slouken@8582
   140
    /* FIXME: Should we have an eventual timeout? */
slouken@8582
   141
    while (!SDL_AtomicTryLock(lock)) {
icculus@12038
   142
        if (iterations < 32) {
icculus@12038
   143
            iterations++;
icculus@12038
   144
            PAUSE_INSTRUCTION();
icculus@12038
   145
        } else {
icculus@12038
   146
            /* !!! FIXME: this doesn't definitely give up the current timeslice, it does different things on various platforms. */
icculus@12038
   147
            SDL_Delay(0);
icculus@12038
   148
        }
slouken@8582
   149
    }
slouken@8582
   150
}
slouken@8582
   151
slouken@8582
   152
void
slouken@8582
   153
SDL_AtomicUnlock(SDL_SpinLock *lock)
slouken@8582
   154
{
slouken@8582
   155
#if defined(_MSC_VER)
slouken@8582
   156
    _ReadWriteBarrier();
slouken@8582
   157
    *lock = 0;
slouken@8582
   158
sezeroz@11318
   159
#elif defined(__WATCOMC__) && defined(__386__)
sezeroz@11318
   160
    SDL_CompilerBarrier ();
sezeroz@11318
   161
    *lock = 0;
sezeroz@11318
   162
slouken@8582
   163
#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
slouken@8582
   164
    __sync_lock_release(lock);
slouken@8582
   165
binarycrusader@8979
   166
#elif defined(__SOLARIS__)
binarycrusader@8979
   167
    /* Used for Solaris when not using gcc. */
binarycrusader@8979
   168
    *lock = 0;
binarycrusader@8979
   169
    membar_producer();
binarycrusader@8979
   170
slouken@8582
   171
#else
slouken@8582
   172
    *lock = 0;
slouken@8582
   173
#endif
slouken@8582
   174
}
slouken@8582
   175
slouken@8582
   176
/* vi: set ts=4 sw=4 expandtab: */