src/atomic/SDL_spinlock.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 09 Mar 2014 11:06:11 -0700
changeset 8582 c3e9a2b93517
parent 8536 a2be4a225f91
child 8583 fb2933ca805f
permissions -rw-r--r--
Fixed line endings on WinRT source code
slouken@8582
     1
/*
slouken@8582
     2
  Simple DirectMedia Layer
slouken@8582
     3
  Copyright (C) 1997-2013 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
*/
slouken@8582
    21
#include "SDL_config.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
slouken@8582
    31
slouken@8582
    32
/* This function is where all the magic happens... */
slouken@8582
    33
SDL_bool
slouken@8582
    34
SDL_AtomicTryLock(SDL_SpinLock *lock)
slouken@8582
    35
{
slouken@8582
    36
#if SDL_ATOMIC_DISABLED
slouken@8582
    37
    /* Terrible terrible damage */
slouken@8582
    38
    static SDL_mutex *_spinlock_mutex;
slouken@8582
    39
slouken@8582
    40
    if (!_spinlock_mutex) {
slouken@8582
    41
        /* Race condition on first lock... */
slouken@8582
    42
        _spinlock_mutex = SDL_CreateMutex();
slouken@8582
    43
    }
slouken@8582
    44
    SDL_LockMutex(_spinlock_mutex);
slouken@8582
    45
    if (*lock == 0) {
slouken@8582
    46
        *lock = 1;
slouken@8582
    47
        SDL_UnlockMutex(_spinlock_mutex);
slouken@8582
    48
        return SDL_TRUE;
slouken@8582
    49
    } else {
slouken@8582
    50
        SDL_UnlockMutex(_spinlock_mutex);
slouken@8582
    51
        return SDL_FALSE;
slouken@8582
    52
    }
slouken@8582
    53
slouken@8582
    54
#elif defined(_MSC_VER)
slouken@8582
    55
    SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
slouken@8582
    56
    return (InterlockedExchange((long*)lock, 1) == 0);
slouken@8582
    57
slouken@8582
    58
#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
slouken@8582
    59
    return (__sync_lock_test_and_set(lock, 1) == 0);
slouken@8582
    60
slouken@8582
    61
#elif defined(__GNUC__) && defined(__arm__) && \
slouken@8582
    62
        (defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \
slouken@8582
    63
         defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__) || \
slouken@8582
    64
         defined(__ARM_ARCH_5TEJ__))
slouken@8582
    65
    int result;
slouken@8582
    66
    __asm__ __volatile__ (
slouken@8582
    67
        "swp %0, %1, [%2]\n"
slouken@8582
    68
        : "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory");
slouken@8582
    69
    return (result == 0);
slouken@8582
    70
slouken@8582
    71
#elif defined(__GNUC__) && defined(__arm__)
slouken@8582
    72
    int result;
slouken@8582
    73
    __asm__ __volatile__ (
slouken@8582
    74
        "ldrex %0, [%2]\nteq   %0, #0\nstrexeq %0, %1, [%2]"
slouken@8582
    75
        : "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory");
slouken@8582
    76
    return (result == 0);
slouken@8582
    77
slouken@8582
    78
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
slouken@8582
    79
    int result;
slouken@8582
    80
    __asm__ __volatile__(
slouken@8582
    81
        "lock ; xchgl %0, (%1)\n"
slouken@8582
    82
        : "=r" (result) : "r" (lock), "0" (1) : "cc", "memory");
slouken@8582
    83
    return (result == 0);
slouken@8582
    84
slouken@8582
    85
#elif defined(__MACOSX__) || defined(__IPHONEOS__)
slouken@8582
    86
    /* Maybe used for PowerPC, but the Intel asm or gcc atomics are favored. */
slouken@8582
    87
    return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
slouken@8582
    88
slouken@8582
    89
#elif HAVE_PTHREAD_SPINLOCK
slouken@8582
    90
    /* pthread instructions */
slouken@8582
    91
    return (pthread_spin_trylock(lock) == 0);
slouken@8582
    92
slouken@8582
    93
#else
slouken@8582
    94
#error Please implement for your platform.
slouken@8582
    95
    return SDL_FALSE;
slouken@8582
    96
#endif
slouken@8582
    97
}
slouken@8582
    98
slouken@8582
    99
void
slouken@8582
   100
SDL_AtomicLock(SDL_SpinLock *lock)
slouken@8582
   101
{
slouken@8582
   102
    /* FIXME: Should we have an eventual timeout? */
slouken@8582
   103
    while (!SDL_AtomicTryLock(lock)) {
slouken@8582
   104
        SDL_Delay(0);
slouken@8582
   105
    }
slouken@8582
   106
}
slouken@8582
   107
slouken@8582
   108
void
slouken@8582
   109
SDL_AtomicUnlock(SDL_SpinLock *lock)
slouken@8582
   110
{
slouken@8582
   111
#if defined(_MSC_VER)
slouken@8582
   112
    _ReadWriteBarrier();
slouken@8582
   113
    *lock = 0;
slouken@8582
   114
slouken@8582
   115
#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
slouken@8582
   116
    __sync_lock_release(lock);
slouken@8582
   117
slouken@8582
   118
#elif HAVE_PTHREAD_SPINLOCK
slouken@8582
   119
    pthread_spin_unlock(lock);
slouken@8582
   120
slouken@8582
   121
#else
slouken@8582
   122
    *lock = 0;
slouken@8582
   123
#endif
slouken@8582
   124
}
slouken@8582
   125
slouken@8582
   126
/* vi: set ts=4 sw=4 expandtab: */