src/atomic/SDL_spinlock.c
author Nathan Heisey <nathanheisey@gmail.com>
Wed, 22 Jun 2011 10:33:48 +0000
changeset 5557 c1ed57cbfd66
parent 5535 96594ac5fd1a
child 5956 c7aebf1841ca
permissions -rw-r--r--
Implemented pthread spinlocks.
slouken@5003
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 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
*/
slouken@5003
    21
#include "SDL_stdinc.h"
slouken@5003
    22
slouken@5003
    23
#include "SDL_atomic.h"
slouken@5225
    24
#include "SDL_mutex.h"
slouken@5003
    25
#include "SDL_timer.h"
slouken@5003
    26
slouken@5073
    27
/* Don't do the check for Visual Studio 2005, it's safe here */
slouken@5091
    28
#ifdef __WIN32__
slouken@5090
    29
#include "../core/windows/SDL_windows.h"
slouken@5091
    30
#endif
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@5225
    44
    SDL_mutexP(_spinlock_mutex);
slouken@5225
    45
    if (*lock == 0) {
slouken@5225
    46
        *lock = 1;
slouken@5225
    47
        SDL_mutexV(_spinlock_mutex);
slouken@5225
    48
        return SDL_TRUE;
slouken@5225
    49
    } else {
slouken@5225
    50
        SDL_mutexV(_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@5097
    58
#elif defined(__MACOSX__)
slouken@5003
    59
    return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
slouken@5003
    60
slouken@5095
    61
#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
slouken@5004
    62
    return (__sync_lock_test_and_set(lock, 1) == 0);
slouken@5004
    63
slouken@5328
    64
#elif defined(__GNUC__) && defined(__arm__) && \
slouken@5328
    65
        (defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \
slouken@5328
    66
         defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__))
slouken@5003
    67
    int result;
slouken@5003
    68
    __asm__ __volatile__ (
slouken@5003
    69
        "swp %0, %1, [%2]\n"
slouken@5003
    70
        : "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory");
slouken@5003
    71
    return (result == 0);
slouken@5004
    72
slouken@5097
    73
#elif defined(__GNUC__) && defined(__arm__)
slouken@5003
    74
    int result;
slouken@5003
    75
    __asm__ __volatile__ (
slouken@5003
    76
        "ldrex %0, [%2]\nteq   %0, #0\nstrexeq %0, %1, [%2]"
slouken@5003
    77
        : "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory");
slouken@5003
    78
    return (result == 0);
slouken@5003
    79
nathanheisey@5557
    80
#elif HAVE_PTHREAD_SPINLOCK
nathanheisey@5557
    81
    /* pthread instructions */
nathanheisey@5557
    82
    return (pthread_spin_trylock(lock) == 0);
nathanheisey@5557
    83
#else	
slouken@5003
    84
    /* Need CPU instructions for spinlock here! */
slouken@5003
    85
    __need_spinlock_implementation__
nathanheisey@5557
    86
	
slouken@5003
    87
#endif
slouken@5003
    88
}
slouken@5003
    89
slouken@5003
    90
void
slouken@5003
    91
SDL_AtomicLock(SDL_SpinLock *lock)
slouken@5003
    92
{
slouken@5003
    93
    /* FIXME: Should we have an eventual timeout? */
slouken@5003
    94
    while (!SDL_AtomicTryLock(lock)) {
slouken@5003
    95
        SDL_Delay(0);
slouken@5003
    96
    }
slouken@5003
    97
}
slouken@5003
    98
slouken@5003
    99
void
slouken@5003
   100
SDL_AtomicUnlock(SDL_SpinLock *lock)
slouken@5003
   101
{
slouken@5095
   102
#if defined(_MSC_VER)
slouken@5095
   103
    _ReadWriteBarrier();
slouken@5003
   104
    *lock = 0;
slouken@5095
   105
slouken@5095
   106
#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
slouken@5095
   107
    __sync_lock_release(lock);
nathanheisey@5557
   108
    
nathanheisey@5557
   109
#elif HAVE_PTHREAD_SPINLOCK
nathanheisey@5557
   110
    pthread_spin_unlock(lock);
nathanheisey@5557
   111
	
slouken@5095
   112
#else
slouken@5095
   113
    *lock = 0;
slouken@5095
   114
#endif
slouken@5003
   115
}
slouken@5003
   116
slouken@5003
   117
/* vi: set ts=4 sw=4 expandtab: */