src/atomic/SDL_spinlock.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Jan 2011 21:20:30 -0800
changeset 5090 327f181542f1
parent 5086 c2539ff054c8
child 5091 2164a79b5ca9
permissions -rw-r--r--
Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.

I think this also fixes the bug relating to non-latin characters in filenames, since UNICODE wasn't defined in SDL_rwops.c
     1 /*
     2   SDL - Simple DirectMedia Layer
     3   Copyright (C) 1997-2010 Sam Lantinga
     4 
     5   This library is free software; you can redistribute it and/or
     6   modify it under the terms of the GNU Lesser General Public
     7   License as published by the Free Software Foundation; either
     8   version 2.1 of the License, or (at your option) any later version.
     9 
    10   This library is distributed in the hope that it will be useful,
    11   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13   Lesser General Public License for more details.
    14 
    15   You should have received a copy of the GNU Lesser General Public
    16   License along with this library; if not, write to the Free Software
    17   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19   Sam Lantinga
    20   slouken@libsdl.org
    21 */
    22 #include "SDL_stdinc.h"
    23 
    24 #include "SDL_atomic.h"
    25 #include "SDL_timer.h"
    26 
    27 /* Don't do the check for Visual Studio 2005, it's safe here */
    28 #include "../core/windows/SDL_windows.h"
    29 
    30 /* This function is where all the magic happens... */
    31 SDL_bool
    32 SDL_AtomicTryLock(SDL_SpinLock *lock)
    33 {
    34 #if defined(_MSC_VER)
    35     SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
    36     return (InterlockedExchange((long*)lock, 1) == 0);
    37 
    38 #elif defined(__MACOSX__)
    39     return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
    40 
    41 #elif defined(HAVE_GCC_ATOMICS) || defined(HAVE_GCC_SYNC_LOCK_TEST_AND_SET)
    42     return (__sync_lock_test_and_set(lock, 1) == 0);
    43 
    44 #elif defined(__GNUC__) && defined(__arm__) && defined(__ARM_ARCH_5__)
    45     int result;
    46     __asm__ __volatile__ (
    47         "swp %0, %1, [%2]\n"
    48         : "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory");
    49     return (result == 0);
    50 
    51 #elif defined(__GNUC__) && defined(__arm__)
    52     int result;
    53     __asm__ __volatile__ (
    54         "ldrex %0, [%2]\nteq   %0, #0\nstrexeq %0, %1, [%2]"
    55         : "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory");
    56     return (result == 0);
    57 
    58 #else
    59     /* Need CPU instructions for spinlock here! */
    60     __need_spinlock_implementation__
    61 #endif
    62 }
    63 
    64 void
    65 SDL_AtomicLock(SDL_SpinLock *lock)
    66 {
    67     /* FIXME: Should we have an eventual timeout? */
    68     while (!SDL_AtomicTryLock(lock)) {
    69         SDL_Delay(0);
    70     }
    71 }
    72 
    73 void
    74 SDL_AtomicUnlock(SDL_SpinLock *lock)
    75 {
    76     /* Assuming atomic assignment operation and full memory barrier in lock */
    77     *lock = 0;
    78 }
    79 
    80 /* vi: set ts=4 sw=4 expandtab: */