src/atomic/SDL_spinlock.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Feb 2011 22:37:15 -0800
changeset 5262 b530ef003506
parent 5225 5d01d426f2ea
child 5289 1916a9e9714d
permissions -rw-r--r--
Happy 2011! :)
slouken@5003
     1
/*
slouken@5003
     2
  SDL - Simple DirectMedia Layer
slouken@5262
     3
  Copyright (C) 1997-2011 Sam Lantinga
slouken@5003
     4
slouken@5003
     5
  This library is free software; you can redistribute it and/or
slouken@5003
     6
  modify it under the terms of the GNU Lesser General Public
slouken@5003
     7
  License as published by the Free Software Foundation; either
slouken@5003
     8
  version 2.1 of the License, or (at your option) any later version.
slouken@5003
     9
slouken@5003
    10
  This library is distributed in the hope that it will be useful,
slouken@5003
    11
  but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@5003
    12
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@5003
    13
  Lesser General Public License for more details.
slouken@5003
    14
slouken@5003
    15
  You should have received a copy of the GNU Lesser General Public
slouken@5003
    16
  License along with this library; if not, write to the Free Software
slouken@5003
    17
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@5003
    18
slouken@5003
    19
  Sam Lantinga
slouken@5003
    20
  slouken@libsdl.org
slouken@5003
    21
*/
slouken@5003
    22
#include "SDL_stdinc.h"
slouken@5003
    23
slouken@5003
    24
#include "SDL_atomic.h"
slouken@5225
    25
#include "SDL_mutex.h"
slouken@5003
    26
#include "SDL_timer.h"
slouken@5003
    27
slouken@5073
    28
/* Don't do the check for Visual Studio 2005, it's safe here */
slouken@5091
    29
#ifdef __WIN32__
slouken@5090
    30
#include "../core/windows/SDL_windows.h"
slouken@5091
    31
#endif
slouken@5003
    32
slouken@5003
    33
/* This function is where all the magic happens... */
slouken@5003
    34
SDL_bool
slouken@5003
    35
SDL_AtomicTryLock(SDL_SpinLock *lock)
slouken@5003
    36
{
slouken@5225
    37
#if SDL_ATOMIC_DISABLED
slouken@5225
    38
    /* Terrible terrible damage */
slouken@5225
    39
    static SDL_mutex *_spinlock_mutex;
slouken@5225
    40
slouken@5225
    41
    if (!_spinlock_mutex) {
slouken@5225
    42
        /* Race condition on first lock... */
slouken@5225
    43
        _spinlock_mutex = SDL_CreateMutex();
slouken@5225
    44
    }
slouken@5225
    45
    SDL_mutexP(_spinlock_mutex);
slouken@5225
    46
    if (*lock == 0) {
slouken@5225
    47
        *lock = 1;
slouken@5225
    48
        SDL_mutexV(_spinlock_mutex);
slouken@5225
    49
        return SDL_TRUE;
slouken@5225
    50
    } else {
slouken@5225
    51
        SDL_mutexV(_spinlock_mutex);
slouken@5225
    52
        return SDL_FALSE;
slouken@5225
    53
    }
slouken@5225
    54
slouken@5225
    55
#elif defined(_MSC_VER)
slouken@5015
    56
    SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
slouken@5090
    57
    return (InterlockedExchange((long*)lock, 1) == 0);
slouken@5003
    58
slouken@5097
    59
#elif defined(__MACOSX__)
slouken@5003
    60
    return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
slouken@5003
    61
slouken@5095
    62
#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
slouken@5004
    63
    return (__sync_lock_test_and_set(lock, 1) == 0);
slouken@5004
    64
slouken@5097
    65
#elif defined(__GNUC__) && defined(__arm__) && defined(__ARM_ARCH_5__)
slouken@5003
    66
    int result;
slouken@5003
    67
    __asm__ __volatile__ (
slouken@5003
    68
        "swp %0, %1, [%2]\n"
slouken@5003
    69
        : "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory");
slouken@5003
    70
    return (result == 0);
slouken@5004
    71
slouken@5097
    72
#elif defined(__GNUC__) && defined(__arm__)
slouken@5003
    73
    int result;
slouken@5003
    74
    __asm__ __volatile__ (
slouken@5003
    75
        "ldrex %0, [%2]\nteq   %0, #0\nstrexeq %0, %1, [%2]"
slouken@5003
    76
        : "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory");
slouken@5003
    77
    return (result == 0);
slouken@5003
    78
slouken@5003
    79
#else
slouken@5003
    80
    /* Need CPU instructions for spinlock here! */
slouken@5003
    81
    __need_spinlock_implementation__
slouken@5003
    82
#endif
slouken@5003
    83
}
slouken@5003
    84
slouken@5003
    85
void
slouken@5003
    86
SDL_AtomicLock(SDL_SpinLock *lock)
slouken@5003
    87
{
slouken@5003
    88
    /* FIXME: Should we have an eventual timeout? */
slouken@5003
    89
    while (!SDL_AtomicTryLock(lock)) {
slouken@5003
    90
        SDL_Delay(0);
slouken@5003
    91
    }
slouken@5003
    92
}
slouken@5003
    93
slouken@5003
    94
void
slouken@5003
    95
SDL_AtomicUnlock(SDL_SpinLock *lock)
slouken@5003
    96
{
slouken@5095
    97
#if defined(_MSC_VER)
slouken@5095
    98
    _ReadWriteBarrier();
slouken@5003
    99
    *lock = 0;
slouken@5095
   100
slouken@5095
   101
#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
slouken@5095
   102
    __sync_lock_release(lock);
slouken@5095
   103
slouken@5095
   104
#else
slouken@5095
   105
    *lock = 0;
slouken@5095
   106
#endif
slouken@5003
   107
}
slouken@5003
   108
slouken@5003
   109
/* vi: set ts=4 sw=4 expandtab: */