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