src/atomic/SDL_spinlock.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 24 Nov 2013 23:56:17 -0500
changeset 8093 b43765095a6f
parent 7828 1451063c8ecd
child 8149 681eb46b8ac4
permissions -rw-r--r--
Make internal SDL sources include SDL_internal.h instead of SDL_config.h

The new header will include SDL_config.h, but allows for other global stuff.
slouken@5003
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@5003
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@5003
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@5003
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@5003
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@5003
    22
slouken@7828
    23
#ifdef __WIN32__
slouken@7828
    24
#include "../core/windows/SDL_windows.h"
slouken@7828
    25
#endif
slouken@7828
    26
slouken@5003
    27
#include "SDL_atomic.h"
slouken@5225
    28
#include "SDL_mutex.h"
slouken@5003
    29
#include "SDL_timer.h"
slouken@5003
    30
slouken@5003
    31
slouken@5003
    32
/* This function is where all the magic happens... */
slouken@5003
    33
SDL_bool
slouken@5003
    34
SDL_AtomicTryLock(SDL_SpinLock *lock)
slouken@5003
    35
{
slouken@5225
    36
#if SDL_ATOMIC_DISABLED
slouken@5225
    37
    /* Terrible terrible damage */
slouken@5225
    38
    static SDL_mutex *_spinlock_mutex;
slouken@5225
    39
slouken@5225
    40
    if (!_spinlock_mutex) {
slouken@5225
    41
        /* Race condition on first lock... */
slouken@5225
    42
        _spinlock_mutex = SDL_CreateMutex();
slouken@5225
    43
    }
slouken@6977
    44
    SDL_LockMutex(_spinlock_mutex);
slouken@5225
    45
    if (*lock == 0) {
slouken@5225
    46
        *lock = 1;
slouken@6977
    47
        SDL_UnlockMutex(_spinlock_mutex);
slouken@5225
    48
        return SDL_TRUE;
slouken@5225
    49
    } else {
slouken@6977
    50
        SDL_UnlockMutex(_spinlock_mutex);
slouken@5225
    51
        return SDL_FALSE;
slouken@5225
    52
    }
slouken@5225
    53
slouken@5225
    54
#elif defined(_MSC_VER)
slouken@5015
    55
    SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
slouken@5090
    56
    return (InterlockedExchange((long*)lock, 1) == 0);
slouken@5003
    57
slouken@5095
    58
#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
slouken@5004
    59
    return (__sync_lock_test_and_set(lock, 1) == 0);
slouken@5004
    60
slouken@5328
    61
#elif defined(__GNUC__) && defined(__arm__) && \
slouken@5328
    62
        (defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \
icculus@5999
    63
         defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__) || \
icculus@5999
    64
         defined(__ARM_ARCH_5TEJ__))
slouken@5003
    65
    int result;
slouken@5003
    66
    __asm__ __volatile__ (
slouken@5003
    67
        "swp %0, %1, [%2]\n"
slouken@5003
    68
        : "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory");
slouken@5003
    69
    return (result == 0);
slouken@5004
    70
slouken@5097
    71
#elif defined(__GNUC__) && defined(__arm__)
slouken@5003
    72
    int result;
slouken@5003
    73
    __asm__ __volatile__ (
slouken@5003
    74
        "ldrex %0, [%2]\nteq   %0, #0\nstrexeq %0, %1, [%2]"
slouken@5003
    75
        : "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory");
slouken@5003
    76
    return (result == 0);
slouken@5003
    77
icculus@5957
    78
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
slouken@7191
    79
    int result;
slouken@7191
    80
    __asm__ __volatile__(
icculus@5957
    81
        "lock ; xchgl %0, (%1)\n"
icculus@5957
    82
        : "=r" (result) : "r" (lock), "0" (1) : "cc", "memory");
slouken@7191
    83
    return (result == 0);
icculus@5957
    84
icculus@5998
    85
#elif defined(__MACOSX__) || defined(__IPHONEOS__)
icculus@5957
    86
    /* Maybe used for PowerPC, but the Intel asm or gcc atomics are favored. */
icculus@5957
    87
    return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
icculus@5957
    88
nathanheisey@5557
    89
#elif HAVE_PTHREAD_SPINLOCK
nathanheisey@5557
    90
    /* pthread instructions */
nathanheisey@5557
    91
    return (pthread_spin_trylock(lock) == 0);
icculus@5957
    92
icculus@5957
    93
#else
icculus@5956
    94
#error Please implement for your platform.
icculus@5956
    95
    return SDL_FALSE;
slouken@5003
    96
#endif
slouken@5003
    97
}
slouken@5003
    98
slouken@5003
    99
void
slouken@5003
   100
SDL_AtomicLock(SDL_SpinLock *lock)
slouken@5003
   101
{
slouken@5003
   102
    /* FIXME: Should we have an eventual timeout? */
slouken@5003
   103
    while (!SDL_AtomicTryLock(lock)) {
slouken@5003
   104
        SDL_Delay(0);
slouken@5003
   105
    }
slouken@5003
   106
}
slouken@5003
   107
slouken@5003
   108
void
slouken@5003
   109
SDL_AtomicUnlock(SDL_SpinLock *lock)
slouken@5003
   110
{
slouken@5095
   111
#if defined(_MSC_VER)
slouken@5095
   112
    _ReadWriteBarrier();
slouken@5003
   113
    *lock = 0;
slouken@5095
   114
slouken@5095
   115
#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
slouken@5095
   116
    __sync_lock_release(lock);
slouken@7191
   117
nathanheisey@5557
   118
#elif HAVE_PTHREAD_SPINLOCK
nathanheisey@5557
   119
    pthread_spin_unlock(lock);
slouken@7191
   120
slouken@5095
   121
#else
slouken@5095
   122
    *lock = 0;
slouken@5095
   123
#endif
slouken@5003
   124
}
slouken@5003
   125
slouken@5003
   126
/* vi: set ts=4 sw=4 expandtab: */