src/atomic/SDL_spinlock.c
changeset 5225 5d01d426f2ea
parent 5097 b938ad843e52
child 5262 b530ef003506
equal deleted inserted replaced
5224:2178ffe17222 5225:5d01d426f2ea
    20   slouken@libsdl.org
    20   slouken@libsdl.org
    21 */
    21 */
    22 #include "SDL_stdinc.h"
    22 #include "SDL_stdinc.h"
    23 
    23 
    24 #include "SDL_atomic.h"
    24 #include "SDL_atomic.h"
       
    25 #include "SDL_mutex.h"
    25 #include "SDL_timer.h"
    26 #include "SDL_timer.h"
    26 
    27 
    27 /* Don't do the check for Visual Studio 2005, it's safe here */
    28 /* Don't do the check for Visual Studio 2005, it's safe here */
    28 #ifdef __WIN32__
    29 #ifdef __WIN32__
    29 #include "../core/windows/SDL_windows.h"
    30 #include "../core/windows/SDL_windows.h"
    31 
    32 
    32 /* This function is where all the magic happens... */
    33 /* This function is where all the magic happens... */
    33 SDL_bool
    34 SDL_bool
    34 SDL_AtomicTryLock(SDL_SpinLock *lock)
    35 SDL_AtomicTryLock(SDL_SpinLock *lock)
    35 {
    36 {
    36 #if defined(_MSC_VER)
    37 #if SDL_ATOMIC_DISABLED
       
    38     /* Terrible terrible damage */
       
    39     static SDL_mutex *_spinlock_mutex;
       
    40 
       
    41     if (!_spinlock_mutex) {
       
    42         /* Race condition on first lock... */
       
    43         _spinlock_mutex = SDL_CreateMutex();
       
    44     }
       
    45     SDL_mutexP(_spinlock_mutex);
       
    46     if (*lock == 0) {
       
    47         *lock = 1;
       
    48         SDL_mutexV(_spinlock_mutex);
       
    49         return SDL_TRUE;
       
    50     } else {
       
    51         SDL_mutexV(_spinlock_mutex);
       
    52         return SDL_FALSE;
       
    53     }
       
    54 
       
    55 #elif defined(_MSC_VER)
    37     SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
    56     SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
    38     return (InterlockedExchange((long*)lock, 1) == 0);
    57     return (InterlockedExchange((long*)lock, 1) == 0);
    39 
    58 
    40 #elif defined(__MACOSX__)
    59 #elif defined(__MACOSX__)
    41     return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
    60     return OSAtomicCompareAndSwap32Barrier(0, 1, lock);