src/atomic/SDL_atomic.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 14 Jan 2012 13:21:19 -0500
changeset 6217 6952b11b7f46
parent 6138 4c64952a58fb
child 6885 700f1b25f77f
permissions -rwxr-xr-x
Make sure that we use consistent configuration options on platforms like Windows so that command line builds and IDE builds have ABI compatibility.
Make sure we don't clobber SDL_revision.h when building from Mercurial
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 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_config.h"
    22 
    23 #include "SDL_atomic.h"
    24 
    25 /* Note that we undefine the atomic operations here, in case they are
    26    defined as compiler intrinsics while building SDL but the library user
    27    doesn't have that compiler.  That way we always have a working set of
    28    atomic operations built into the library.
    29 */
    30  
    31 /* 
    32   If any of the operations are not provided then we must emulate some
    33   of them. That means we need a nice implementation of spin locks
    34   that avoids the "one big lock" problem. We use a vector of spin
    35   locks and pick which one to use based on the address of the operand
    36   of the function.
    37 
    38   To generate the index of the lock we first shift by 3 bits to get
    39   rid on the zero bits that result from 32 and 64 bit allignment of
    40   data. We then mask off all but 5 bits and use those 5 bits as an
    41   index into the table. 
    42 
    43   Picking the lock this way insures that accesses to the same data at
    44   the same time will go to the same lock. OTOH, accesses to different
    45   data have only a 1/32 chance of hitting the same lock. That should
    46   pretty much eliminate the chances of several atomic operations on
    47   different data from waiting on the same "big lock". If it isn't
    48   then the table of locks can be expanded to a new size so long as
    49   the new size is a power of two.
    50 
    51   Contributed by Bob Pendleton, bob@pendleton.com
    52 */
    53 
    54 static SDL_SpinLock locks[32];
    55 
    56 static __inline__ void
    57 enterLock(void *a)
    58 {
    59     uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
    60 
    61     SDL_AtomicLock(&locks[index]);
    62 }
    63 
    64 static __inline__ void
    65 leaveLock(void *a)
    66 {
    67     uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
    68 
    69     SDL_AtomicUnlock(&locks[index]);
    70 }
    71 
    72 SDL_bool
    73 SDL_AtomicCAS_(SDL_atomic_t *a, int oldval, int newval)
    74 {
    75     SDL_bool retval = SDL_FALSE;
    76 
    77     enterLock(a);
    78     if (a->value == oldval) {
    79         a->value = newval;
    80         retval = SDL_TRUE;
    81     }
    82     leaveLock(a);
    83 
    84     return retval;
    85 }
    86 
    87 SDL_bool
    88 SDL_AtomicCASPtr_(void **a, void *oldval, void *newval)
    89 {
    90     SDL_bool retval = SDL_FALSE;
    91 
    92     enterLock(a);
    93     if (*a == oldval) {
    94         *a = newval;
    95         retval = SDL_TRUE;
    96     }
    97     leaveLock(a);
    98 
    99     return retval;
   100 }
   101 
   102 /* vi: set ts=4 sw=4 expandtab: */