src/atomic/linux/SDL_atomic.c
author Bob Pendleton <bob@pendleton.com>
Wed, 24 Jun 2009 22:24:23 +0000
changeset 3201 c297230efc75
parent 3200 ef2c029a3a9d
child 3202 3aa519a5c676
permissions -rw-r--r--
Disabling 64 bit atomics operations until I figure out why they do not link.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 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 
    23 #include "SDL.h"
    24 #include "SDL_config.h"
    25 #include "SDL_atomic.h"
    26 
    27 Uint32
    28 SDL_AtomicExchange32(Uint32 * ptr, Uint32 value)
    29 {
    30    return __sync_lock_test_and_set(ptr, value);
    31 }
    32 
    33 SDL_bool
    34 SDL_AtomicCompareThenSet32(Uint32 * ptr, Uint32 oldvalue, Uint32 newvalue)
    35 {
    36    return (SDL_bool)__sync_bool_compare_and_swap(ptr, oldvalue, newvalue);
    37 }
    38 
    39 SDL_bool
    40 SDL_AtomicTestThenSet32(Uint32 * ptr)
    41 {
    42    return (SDL_bool)(0 == __sync_lock_test_and_set(ptr, 1));
    43 }
    44 
    45 void
    46 SDL_AtomicClear32(Uint32 * ptr)
    47 {
    48    __sync_lock_release(ptr);
    49 }
    50 
    51 Uint32
    52 SDL_AtomicFetchThenIncrement32(Uint32 * ptr)
    53 {
    54    return __sync_fetch_and_add(ptr, 1);
    55 }
    56 
    57 Uint32
    58 SDL_AtomicFetchThenDecrement32(Uint32 * ptr)
    59 {
    60    return __sync_fetch_and_sub(ptr, 1);
    61 }
    62 
    63 Uint32
    64 SDL_AtomicFetchThenAdd32(Uint32 * ptr, Uint32 value)
    65 {
    66    return __sync_fetch_and_add(ptr, value);
    67 }
    68 
    69 Uint32
    70 SDL_AtomicFetchThenSubtract32(Uint32 * ptr, Uint32 value)
    71 {
    72    return __sync_fetch_and_sub(ptr, value);
    73 }
    74 
    75 Uint32
    76 SDL_AtomicIncrementThenFetch32(Uint32 * ptr)
    77 {
    78    return __sync_add_and_fetch(ptr, 1);
    79 }
    80 
    81 Uint32
    82 SDL_AtomicDecrementThenFetch32(Uint32 * ptr)
    83 {
    84    return __sync_sub_and_fetch(ptr, 1);
    85 }
    86 
    87 Uint32
    88 SDL_AtomicAddThenFetch32(Uint32 * ptr, Uint32 value)
    89 {
    90    return __sync_add_and_fetch(ptr, value);
    91 }
    92 
    93 Uint32
    94 SDL_AtomicSubtractThenFetch32(Uint32 * ptr, Uint32 value)
    95 {
    96    return __sync_sub_and_fetch(ptr, value);
    97 }
    98 
    99 /* #ifdef SDL_HAS_64BIT_TYPE */
   100 #if 0
   101 
   102 Uint64
   103 SDL_AtomicExchange64(Uint64 * ptr, Uint64 value)
   104 {
   105    return __sync_lock_test_and_set(ptr, value);
   106 }
   107 
   108 SDL_bool
   109 SDL_AtomicCompareThenSet64(Uint64 * ptr, Uint64 oldvalue, Uint64 newvalue)
   110 {
   111    return (SDL_bool)__sync_bool_compare_and_swap(ptr, oldvalue, newvalue);
   112 }
   113 
   114 SDL_bool
   115 SDL_AtomicTestThenSet64(Uint64 * ptr)
   116 {
   117    return (SDL_bool)(0 == __sync_lock_test_and_set(ptr, 1));
   118 }
   119 
   120 void
   121 SDL_AtomicClear64(Uint64 * ptr)
   122 {
   123    __sync_lock_release(ptr);
   124 }
   125 
   126 Uint64
   127 SDL_AtomicFetchThenIncrement64(Uint64 * ptr)
   128 {
   129    return __sync_fetch_and_add(ptr, 1);
   130 }
   131 
   132 Uint64
   133 SDL_AtomicFetchThenDecrement64(Uint64 * ptr)
   134 {
   135    return __sync_fetch_and_sub(ptr, 1);
   136 }
   137 
   138 Uint64
   139 SDL_AtomicFetchThenAdd64(Uint64 * ptr, Uint64 value)
   140 {
   141    return __sync_fetch_and_add(ptr, value);
   142 }
   143 
   144 Uint64
   145 SDL_AtomicFetchThenSubtract64(Uint64 * ptr, Uint64 value)
   146 {
   147    return __sync_fetch_and_sub(ptr, value);
   148 }
   149 
   150 Uint64
   151 SDL_AtomicIncrementThenFetch64(Uint64 * ptr)
   152 {
   153    return __sync_add_and_fetch(ptr, 1);
   154 }
   155 
   156 Uint64
   157 SDL_AtomicDecrementThenFetch64(Uint64 * ptr)
   158 {
   159    return __sync_sub_and_fetch(ptr, 1);
   160 }
   161 
   162 Uint64
   163 SDL_AtomicAddThenFetch64(Uint64 * ptr, Uint64 value)
   164 {
   165    return __sync_add_and_fetch(ptr, value);
   166 }
   167 
   168 Uint64
   169 SDL_AtomicSubtractThenFetch64(Uint64 * ptr, Uint64 value)
   170 {
   171    return __sync_sub_and_fetch(ptr, value);
   172 }
   173 #endif