src/atomic/SDL_spinlock.c
author Shawn Walker <binarycrusader@gmail.com>
Sat, 05 Jul 2014 16:11:23 -0700
changeset 8979 1e283b7a1580
parent 8583 fb2933ca805f
child 8991 13b0df1793e8
permissions -rw-r--r--
2620 solaris port missing atomics if not using gcc
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "../SDL_internal.h"
    22 
    23 #if defined(__WIN32__) || defined(__WINRT__)
    24 #include "../core/windows/SDL_windows.h"
    25 #endif
    26 
    27 #include "SDL_atomic.h"
    28 #include "SDL_mutex.h"
    29 #include "SDL_timer.h"
    30 
    31 #if !defined(HAVE_GCC_ATOMICS) && defined(__SOLARIS__)
    32 #include <atomic.h>
    33 #endif
    34 
    35 /* This function is where all the magic happens... */
    36 SDL_bool
    37 SDL_AtomicTryLock(SDL_SpinLock *lock)
    38 {
    39 #if SDL_ATOMIC_DISABLED
    40     /* Terrible terrible damage */
    41     static SDL_mutex *_spinlock_mutex;
    42 
    43     if (!_spinlock_mutex) {
    44         /* Race condition on first lock... */
    45         _spinlock_mutex = SDL_CreateMutex();
    46     }
    47     SDL_LockMutex(_spinlock_mutex);
    48     if (*lock == 0) {
    49         *lock = 1;
    50         SDL_UnlockMutex(_spinlock_mutex);
    51         return SDL_TRUE;
    52     } else {
    53         SDL_UnlockMutex(_spinlock_mutex);
    54         return SDL_FALSE;
    55     }
    56 
    57 #elif defined(_MSC_VER)
    58     SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
    59     return (InterlockedExchange((long*)lock, 1) == 0);
    60 
    61 #elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
    62     return (__sync_lock_test_and_set(lock, 1) == 0);
    63 
    64 #elif defined(__GNUC__) && defined(__arm__) && \
    65         (defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \
    66          defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__) || \
    67          defined(__ARM_ARCH_5TEJ__))
    68     int result;
    69     __asm__ __volatile__ (
    70         "swp %0, %1, [%2]\n"
    71         : "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory");
    72     return (result == 0);
    73 
    74 #elif defined(__GNUC__) && defined(__arm__)
    75     int result;
    76     __asm__ __volatile__ (
    77         "ldrex %0, [%2]\nteq   %0, #0\nstrexeq %0, %1, [%2]"
    78         : "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory");
    79     return (result == 0);
    80 
    81 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
    82     int result;
    83     __asm__ __volatile__(
    84         "lock ; xchgl %0, (%1)\n"
    85         : "=r" (result) : "r" (lock), "0" (1) : "cc", "memory");
    86     return (result == 0);
    87 
    88 #elif defined(__MACOSX__) || defined(__IPHONEOS__)
    89     /* Maybe used for PowerPC, but the Intel asm or gcc atomics are favored. */
    90     return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
    91 
    92 #elif HAVE_PTHREAD_SPINLOCK
    93     /* pthread instructions */
    94     return (pthread_spin_trylock(lock) == 0);
    95 
    96 #elif defined(__SOLARIS__) && defined(_LP64)
    97     /* Used for Solaris with non-gcc compilers. */
    98     return (SDL_bool) ((int) atomic_cas_64((volatile uint64_t*)lock, 0, 1) == 0);
    99 
   100 #elif defined(__SOLARIS__) && !defined(_LP64)
   101     /* Used for Solaris with non-gcc compilers. */
   102     return (SDL_bool) ((int) atomic_cas_32((volatile uint32_t*)lock, 0, 1) == 0);
   103 
   104 #else
   105 #error Please implement for your platform.
   106     return SDL_FALSE;
   107 #endif
   108 }
   109 
   110 void
   111 SDL_AtomicLock(SDL_SpinLock *lock)
   112 {
   113     /* FIXME: Should we have an eventual timeout? */
   114     while (!SDL_AtomicTryLock(lock)) {
   115         SDL_Delay(0);
   116     }
   117 }
   118 
   119 void
   120 SDL_AtomicUnlock(SDL_SpinLock *lock)
   121 {
   122 #if defined(_MSC_VER)
   123     _ReadWriteBarrier();
   124     *lock = 0;
   125 
   126 #elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
   127     __sync_lock_release(lock);
   128 
   129 #elif HAVE_PTHREAD_SPINLOCK
   130     pthread_spin_unlock(lock);
   131 
   132 #elif defined(__SOLARIS__)
   133     /* Used for Solaris when not using gcc. */
   134     *lock = 0;
   135     membar_producer();
   136 
   137 #else
   138     *lock = 0;
   139 #endif
   140 }
   141 
   142 /* vi: set ts=4 sw=4 expandtab: */