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
slouken@5003
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@5003
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@5003
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@5003
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@5003
    20
*/
slouken@6217
    21
#include "SDL_config.h"
slouken@5003
    22
slouken@5003
    23
#include "SDL_atomic.h"
slouken@5003
    24
slouken@5004
    25
/* Note that we undefine the atomic operations here, in case they are
slouken@5004
    26
   defined as compiler intrinsics while building SDL but the library user
slouken@5004
    27
   doesn't have that compiler.  That way we always have a working set of
slouken@5004
    28
   atomic operations built into the library.
slouken@5004
    29
*/
slouken@5004
    30
 
slouken@5003
    31
/* 
slouken@5003
    32
  If any of the operations are not provided then we must emulate some
slouken@5003
    33
  of them. That means we need a nice implementation of spin locks
slouken@5003
    34
  that avoids the "one big lock" problem. We use a vector of spin
slouken@5003
    35
  locks and pick which one to use based on the address of the operand
slouken@5003
    36
  of the function.
slouken@5003
    37
slouken@5003
    38
  To generate the index of the lock we first shift by 3 bits to get
slouken@5003
    39
  rid on the zero bits that result from 32 and 64 bit allignment of
slouken@5003
    40
  data. We then mask off all but 5 bits and use those 5 bits as an
slouken@5003
    41
  index into the table. 
slouken@5003
    42
slouken@5003
    43
  Picking the lock this way insures that accesses to the same data at
slouken@5003
    44
  the same time will go to the same lock. OTOH, accesses to different
slouken@5003
    45
  data have only a 1/32 chance of hitting the same lock. That should
slouken@5003
    46
  pretty much eliminate the chances of several atomic operations on
slouken@5003
    47
  different data from waiting on the same "big lock". If it isn't
slouken@5003
    48
  then the table of locks can be expanded to a new size so long as
slouken@5003
    49
  the new size is a power of two.
slouken@5003
    50
slouken@5003
    51
  Contributed by Bob Pendleton, bob@pendleton.com
slouken@5003
    52
*/
slouken@5003
    53
slouken@5003
    54
static SDL_SpinLock locks[32];
slouken@5003
    55
slouken@5003
    56
static __inline__ void
slouken@5003
    57
enterLock(void *a)
slouken@5003
    58
{
slouken@5004
    59
    uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
slouken@5003
    60
slouken@5004
    61
    SDL_AtomicLock(&locks[index]);
slouken@5003
    62
}
slouken@5003
    63
slouken@5003
    64
static __inline__ void
slouken@5003
    65
leaveLock(void *a)
slouken@5003
    66
{
slouken@5004
    67
    uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
slouken@5003
    68
slouken@5004
    69
    SDL_AtomicUnlock(&locks[index]);
slouken@5003
    70
}
slouken@5003
    71
slouken@5003
    72
SDL_bool
slouken@5095
    73
SDL_AtomicCAS_(SDL_atomic_t *a, int oldval, int newval)
slouken@5003
    74
{
slouken@5004
    75
    SDL_bool retval = SDL_FALSE;
slouken@5003
    76
slouken@5003
    77
    enterLock(a);
slouken@5004
    78
    if (a->value == oldval) {
slouken@5003
    79
        a->value = newval;
slouken@5004
    80
        retval = SDL_TRUE;
slouken@5003
    81
    }
slouken@5003
    82
    leaveLock(a);
slouken@5003
    83
slouken@5004
    84
    return retval;
slouken@5003
    85
}
slouken@5003
    86
slouken@5095
    87
SDL_bool
slouken@5095
    88
SDL_AtomicCASPtr_(void **a, void *oldval, void *newval)
slouken@5003
    89
{
slouken@5004
    90
    SDL_bool retval = SDL_FALSE;
slouken@5003
    91
slouken@5003
    92
    enterLock(a);
slouken@5003
    93
    if (*a == oldval) {
slouken@5003
    94
        *a = newval;
slouken@5004
    95
        retval = SDL_TRUE;
slouken@5003
    96
    }
slouken@5003
    97
    leaveLock(a);
slouken@5003
    98
slouken@5004
    99
    return retval;
slouken@5003
   100
}
slouken@5003
   101
slouken@5003
   102
/* vi: set ts=4 sw=4 expandtab: */