src/atomic/SDL_atomic.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 08 Jan 2014 00:40:48 -0500
changeset 8096 132b47684fbf
parent 8093 b43765095a6f
child 8149 681eb46b8ac4
permissions -rw-r--r--
Patched to compile on Android (I think).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     2
  Simple DirectMedia Layer
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6217
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     7
  arising from the use of this software.
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    11
  freely, subject to the following restrictions:
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 8092
diff changeset
    21
#include "../SDL_internal.h"
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
#include "SDL_atomic.h"
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
8088
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    25
#if defined(_MSC_VER) && (_MSC_VER >= 1500)
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    26
#include <intrin.h>
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    27
#define HAVE_MSC_ATOMICS 1
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    28
#endif
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    29
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    30
#if defined(__MACOSX__)  /* !!! FIXME: should we favor gcc atomics? */
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    31
#include <libkern/OSAtomic.h>
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    32
#endif
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6978
diff changeset
    33
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6978
diff changeset
    34
/*
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
  If any of the operations are not provided then we must emulate some
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
  of them. That means we need a nice implementation of spin locks
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
  that avoids the "one big lock" problem. We use a vector of spin
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
  locks and pick which one to use based on the address of the operand
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
  of the function.
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
  To generate the index of the lock we first shift by 3 bits to get
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
  rid on the zero bits that result from 32 and 64 bit allignment of
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
  data. We then mask off all but 5 bits and use those 5 bits as an
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6978
diff changeset
    44
  index into the table.
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
  Picking the lock this way insures that accesses to the same data at
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
  the same time will go to the same lock. OTOH, accesses to different
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
  data have only a 1/32 chance of hitting the same lock. That should
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
  pretty much eliminate the chances of several atomic operations on
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
  different data from waiting on the same "big lock". If it isn't
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
  then the table of locks can be expanded to a new size so long as
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
  the new size is a power of two.
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
  Contributed by Bob Pendleton, bob@pendleton.com
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
*/
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
8088
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    57
#if !defined(HAVE_MSC_ATOMICS) && !defined(HAVE_GCC_ATOMICS) && !defined(__MACOSX__)
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    58
#define EMULATE_CAS 1
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    59
#endif
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    60
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    61
#if EMULATE_CAS
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
static SDL_SpinLock locks[32];
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
7860
2b0bcdea3a79 Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7394
diff changeset
    64
static SDL_INLINE void
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
enterLock(void *a)
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
{
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    67
    uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    69
    SDL_AtomicLock(&locks[index]);
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
}
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
7860
2b0bcdea3a79 Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7394
diff changeset
    72
static SDL_INLINE void
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
leaveLock(void *a)
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
{
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    75
    uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    77
    SDL_AtomicUnlock(&locks[index]);
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
}
8088
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    79
#endif
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
8088
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    81
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    82
SDL_bool
6978
e851407e9294 Put the real SDL_AtomicCAS() and SDL_AtomicCASPtr() symbols into the library.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    83
SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval)
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
{
8088
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    85
#ifdef HAVE_MSC_ATOMICS
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    86
    return (_InterlockedCompareExchange((long*)&a->value, (long)newval, (long)oldval) == (long)oldval);
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    87
#elif defined(__MACOSX__)  /* !!! FIXME: should we favor gcc atomics? */
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    88
    return (SDL_bool) OSAtomicCompareAndSwap32Barrier(oldval, newval, &a->value);
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    89
#elif defined(HAVE_GCC_ATOMICS)
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    90
    return (SDL_bool) __sync_bool_compare_and_swap(&a->value, oldval, newval);
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    91
#elif EMULATE_CAS
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    92
    SDL_bool retval = SDL_FALSE;
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    94
    enterLock(a);
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    95
    if (a->value == oldval) {
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    96
        a->value = newval;
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
    97
        retval = SDL_TRUE;
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
    }
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    99
    leaveLock(a);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   100
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   101
    return retval;
8088
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   102
#else
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   103
    #error Please define your platform.
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   104
#endif
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   105
}
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   106
8088
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   107
SDL_bool
6978
e851407e9294 Put the real SDL_AtomicCAS() and SDL_AtomicCASPtr() symbols into the library.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   108
SDL_AtomicCASPtr(void **a, void *oldval, void *newval)
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
{
8088
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   110
#if defined(HAVE_MSC_ATOMICS) && (_M_IX86)
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   111
    return (_InterlockedCompareExchange((long*)a, (long)newval, (long)oldval) == (long)oldval);
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   112
#elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86)
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   113
    return (_InterlockedCompareExchangePointer(a, newval, oldval) == oldval);
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   114
#elif defined(__MACOSX__) && defined(__LP64__)   /* !!! FIXME: should we favor gcc atomics? */
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   115
    return (SDL_bool) OSAtomicCompareAndSwap64Barrier((int64_t)oldval, (int64_t)newval, (int64_t*) a);
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   116
#elif defined(__MACOSX__) && !defined(__LP64__)  /* !!! FIXME: should we favor gcc atomics? */
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   117
    return (SDL_bool) OSAtomicCompareAndSwap32Barrier((int32_t)oldval, (int32_t)newval, (int32_t*) a);
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   118
#elif defined(HAVE_GCC_ATOMICS)
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   119
    return __sync_bool_compare_and_swap(a, oldval, newval);
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   120
#elif EMULATE_CAS
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   121
    SDL_bool retval = SDL_FALSE;
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   122
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   123
    enterLock(a);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   124
    if (*a == oldval) {
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   125
        *a = newval;
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   126
        retval = SDL_TRUE;
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   127
    }
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   128
    leaveLock(a);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   129
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   130
    return retval;
8088
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   131
#else
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   132
    #error Please define your platform.
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   133
#endif
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   134
}
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   135
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   136
int
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   137
SDL_AtomicSet(SDL_atomic_t *a, int v)
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   138
{
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   139
#ifdef HAVE_MSC_ATOMICS
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   140
    return _InterlockedExchange((long*)&a->value, v);
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   141
#elif defined(HAVE_GCC_ATOMICS)
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   142
    return __sync_lock_test_and_set(&a->value, v);
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   143
#else
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   144
    int value;
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   145
    do {
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   146
        value = a->value;
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   147
    } while (!SDL_AtomicCAS(a, value, v));
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   148
    return value;
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   149
#endif
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   150
}
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   151
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   152
void*
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   153
SDL_AtomicSetPtr(void **a, void *v)
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   154
{
8092
46fb898f098d Patched stdlib changes to compile on Windows.
Ryan C. Gordon <icculus@icculus.org>
parents: 8088
diff changeset
   155
#if defined(HAVE_MSC_ATOMICS) && (_M_IX86)
46fb898f098d Patched stdlib changes to compile on Windows.
Ryan C. Gordon <icculus@icculus.org>
parents: 8088
diff changeset
   156
    return (void *) _InterlockedExchange((long *)a, (long) v);
46fb898f098d Patched stdlib changes to compile on Windows.
Ryan C. Gordon <icculus@icculus.org>
parents: 8088
diff changeset
   157
#elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86)
8088
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   158
    return _InterlockedExchangePointer(a, v);
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   159
#elif defined(HAVE_GCC_ATOMICS)
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   160
    return __sync_lock_test_and_set(a, v);
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   161
#else
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   162
    void *value;
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   163
    do {
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   164
        value = *a;
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   165
    } while (!SDL_AtomicCASPtr(a, value, v));
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   166
    return value;
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   167
#endif
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   168
}
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   169
8088
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   170
int
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   171
SDL_AtomicAdd(SDL_atomic_t *a, int v)
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   172
{
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   173
#ifdef HAVE_MSC_ATOMICS
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   174
    return _InterlockedExchangeAdd((long*)&a->value, v);
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   175
#elif defined(HAVE_GCC_ATOMICS)
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   176
    return __sync_fetch_and_add(&a->value, v);
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   177
#else
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   178
    int value;
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   179
    do {
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   180
        value = a->value;
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   181
    } while (!SDL_AtomicCAS(a, value, (value + v)));
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   182
    return value;
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   183
#endif
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   184
}
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   185
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   186
int
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   187
SDL_AtomicGet(SDL_atomic_t *a)
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   188
{
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   189
    int value;
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   190
    do {
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   191
        value = a->value;
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   192
    } while (!SDL_AtomicCAS(a, value, value));
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   193
    return value;
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   194
}
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   195
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   196
void *
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   197
SDL_AtomicGetPtr(void **a)
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   198
{
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   199
    void *value;
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   200
    do {
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   201
        value = *a;
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   202
    } while (!SDL_AtomicCASPtr(a, value, value));
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   203
    return value;
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   204
}
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   205
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   206
#ifdef __thumb__
8096
132b47684fbf Patched to compile on Android (I think).
Ryan C. Gordon <icculus@icculus.org>
parents: 8093
diff changeset
   207
#if defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6T2__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__)
7394
38dc4961ab15 Added PowerPC and ARM versions of the memory barrier functions.
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   208
__asm__(
38dc4961ab15 Added PowerPC and ARM versions of the memory barrier functions.
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   209
"   .align 2\n"
38dc4961ab15 Added PowerPC and ARM versions of the memory barrier functions.
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   210
"   .globl _SDL_MemoryBarrierRelease\n"
38dc4961ab15 Added PowerPC and ARM versions of the memory barrier functions.
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   211
"   .globl _SDL_MemoryBarrierAcquire\n"
38dc4961ab15 Added PowerPC and ARM versions of the memory barrier functions.
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   212
"_SDL_MemoryBarrierRelease:\n"
38dc4961ab15 Added PowerPC and ARM versions of the memory barrier functions.
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   213
"_SDL_MemoryBarrierAcquire:\n"
38dc4961ab15 Added PowerPC and ARM versions of the memory barrier functions.
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   214
"   mov r0, #0\n"
38dc4961ab15 Added PowerPC and ARM versions of the memory barrier functions.
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   215
"   mcr p15, 0, r0, c7, c10, 5\n"
38dc4961ab15 Added PowerPC and ARM versions of the memory barrier functions.
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   216
"   bx lr\n"
38dc4961ab15 Added PowerPC and ARM versions of the memory barrier functions.
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   217
);
8088
e5d77d5e0fe2 Moved atomic API implementation out of headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
   218
#endif
8096
132b47684fbf Patched to compile on Android (I think).
Ryan C. Gordon <icculus@icculus.org>
parents: 8093
diff changeset
   219
#endif
7394
38dc4961ab15 Added PowerPC and ARM versions of the memory barrier functions.
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   220
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   221
/* vi: set ts=4 sw=4 expandtab: */