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.
bob@3199
     1
/*
bob@3199
     2
    SDL - Simple DirectMedia Layer
bob@3199
     3
    Copyright (C) 1997-2009 Sam Lantinga
bob@3199
     4
bob@3199
     5
    This library is free software; you can redistribute it and/or
bob@3199
     6
    modify it under the terms of the GNU Lesser General Public
bob@3199
     7
    License as published by the Free Software Foundation; either
bob@3199
     8
    version 2.1 of the License, or (at your option) any later version.
bob@3199
     9
bob@3199
    10
    This library is distributed in the hope that it will be useful,
bob@3199
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
bob@3199
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bob@3199
    13
    Lesser General Public License for more details.
bob@3199
    14
bob@3199
    15
    You should have received a copy of the GNU Lesser General Public
bob@3199
    16
    License along with this library; if not, write to the Free Software
bob@3199
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
bob@3199
    18
bob@3199
    19
    Sam Lantinga
bob@3199
    20
    slouken@libsdl.org
bob@3199
    21
*/
bob@3199
    22
bob@3199
    23
#include "SDL.h"
bob@3199
    24
#include "SDL_config.h"
bob@3199
    25
#include "SDL_atomic.h"
bob@3199
    26
bob@3199
    27
Uint32
bob@3199
    28
SDL_AtomicExchange32(Uint32 * ptr, Uint32 value)
bob@3199
    29
{
bob@3200
    30
   return __sync_lock_test_and_set(ptr, value);
bob@3199
    31
}
bob@3199
    32
bob@3199
    33
SDL_bool
bob@3199
    34
SDL_AtomicCompareThenSet32(Uint32 * ptr, Uint32 oldvalue, Uint32 newvalue)
bob@3199
    35
{
bob@3200
    36
   return (SDL_bool)__sync_bool_compare_and_swap(ptr, oldvalue, newvalue);
bob@3199
    37
}
bob@3199
    38
bob@3199
    39
SDL_bool
bob@3199
    40
SDL_AtomicTestThenSet32(Uint32 * ptr)
bob@3199
    41
{
bob@3200
    42
   return (SDL_bool)(0 == __sync_lock_test_and_set(ptr, 1));
bob@3199
    43
}
bob@3199
    44
bob@3199
    45
void
bob@3199
    46
SDL_AtomicClear32(Uint32 * ptr)
bob@3199
    47
{
bob@3200
    48
   __sync_lock_release(ptr);
bob@3199
    49
}
bob@3199
    50
bob@3199
    51
Uint32
bob@3199
    52
SDL_AtomicFetchThenIncrement32(Uint32 * ptr)
bob@3199
    53
{
bob@3200
    54
   return __sync_fetch_and_add(ptr, 1);
bob@3199
    55
}
bob@3199
    56
bob@3199
    57
Uint32
bob@3199
    58
SDL_AtomicFetchThenDecrement32(Uint32 * ptr)
bob@3199
    59
{
bob@3200
    60
   return __sync_fetch_and_sub(ptr, 1);
bob@3199
    61
}
bob@3199
    62
bob@3199
    63
Uint32
bob@3199
    64
SDL_AtomicFetchThenAdd32(Uint32 * ptr, Uint32 value)
bob@3199
    65
{
bob@3200
    66
   return __sync_fetch_and_add(ptr, value);
bob@3199
    67
}
bob@3199
    68
bob@3199
    69
Uint32
bob@3199
    70
SDL_AtomicFetchThenSubtract32(Uint32 * ptr, Uint32 value)
bob@3199
    71
{
bob@3200
    72
   return __sync_fetch_and_sub(ptr, value);
bob@3199
    73
}
bob@3199
    74
bob@3199
    75
Uint32
bob@3199
    76
SDL_AtomicIncrementThenFetch32(Uint32 * ptr)
bob@3199
    77
{
bob@3200
    78
   return __sync_add_and_fetch(ptr, 1);
bob@3199
    79
}
bob@3199
    80
bob@3199
    81
Uint32
bob@3199
    82
SDL_AtomicDecrementThenFetch32(Uint32 * ptr)
bob@3199
    83
{
bob@3200
    84
   return __sync_sub_and_fetch(ptr, 1);
bob@3199
    85
}
bob@3199
    86
bob@3199
    87
Uint32
bob@3199
    88
SDL_AtomicAddThenFetch32(Uint32 * ptr, Uint32 value)
bob@3199
    89
{
bob@3200
    90
   return __sync_add_and_fetch(ptr, value);
bob@3199
    91
}
bob@3199
    92
bob@3199
    93
Uint32
bob@3199
    94
SDL_AtomicSubtractThenFetch32(Uint32 * ptr, Uint32 value)
bob@3199
    95
{
bob@3200
    96
   return __sync_sub_and_fetch(ptr, value);
bob@3199
    97
}
bob@3199
    98
bob@3201
    99
/* #ifdef SDL_HAS_64BIT_TYPE */
bob@3201
   100
#if 0
bob@3199
   101
bob@3199
   102
Uint64
bob@3199
   103
SDL_AtomicExchange64(Uint64 * ptr, Uint64 value)
bob@3199
   104
{
bob@3200
   105
   return __sync_lock_test_and_set(ptr, value);
bob@3199
   106
}
bob@3199
   107
bob@3199
   108
SDL_bool
bob@3200
   109
SDL_AtomicCompareThenSet64(Uint64 * ptr, Uint64 oldvalue, Uint64 newvalue)
bob@3199
   110
{
bob@3200
   111
   return (SDL_bool)__sync_bool_compare_and_swap(ptr, oldvalue, newvalue);
bob@3199
   112
}
bob@3199
   113
bob@3199
   114
SDL_bool
bob@3199
   115
SDL_AtomicTestThenSet64(Uint64 * ptr)
bob@3199
   116
{
bob@3200
   117
   return (SDL_bool)(0 == __sync_lock_test_and_set(ptr, 1));
bob@3199
   118
}
bob@3199
   119
bob@3199
   120
void
bob@3199
   121
SDL_AtomicClear64(Uint64 * ptr)
bob@3199
   122
{
bob@3200
   123
   __sync_lock_release(ptr);
bob@3199
   124
}
bob@3199
   125
bob@3199
   126
Uint64
bob@3199
   127
SDL_AtomicFetchThenIncrement64(Uint64 * ptr)
bob@3199
   128
{
bob@3200
   129
   return __sync_fetch_and_add(ptr, 1);
bob@3199
   130
}
bob@3199
   131
bob@3199
   132
Uint64
bob@3199
   133
SDL_AtomicFetchThenDecrement64(Uint64 * ptr)
bob@3199
   134
{
bob@3200
   135
   return __sync_fetch_and_sub(ptr, 1);
bob@3199
   136
}
bob@3199
   137
bob@3199
   138
Uint64
bob@3199
   139
SDL_AtomicFetchThenAdd64(Uint64 * ptr, Uint64 value)
bob@3199
   140
{
bob@3200
   141
   return __sync_fetch_and_add(ptr, value);
bob@3199
   142
}
bob@3199
   143
bob@3199
   144
Uint64
bob@3199
   145
SDL_AtomicFetchThenSubtract64(Uint64 * ptr, Uint64 value)
bob@3199
   146
{
bob@3200
   147
   return __sync_fetch_and_sub(ptr, value);
bob@3199
   148
}
bob@3199
   149
bob@3199
   150
Uint64
bob@3199
   151
SDL_AtomicIncrementThenFetch64(Uint64 * ptr)
bob@3199
   152
{
bob@3200
   153
   return __sync_add_and_fetch(ptr, 1);
bob@3199
   154
}
bob@3199
   155
bob@3199
   156
Uint64
bob@3199
   157
SDL_AtomicDecrementThenFetch64(Uint64 * ptr)
bob@3199
   158
{
bob@3200
   159
   return __sync_sub_and_fetch(ptr, 1);
bob@3199
   160
}
bob@3199
   161
bob@3199
   162
Uint64
bob@3199
   163
SDL_AtomicAddThenFetch64(Uint64 * ptr, Uint64 value)
bob@3199
   164
{
bob@3200
   165
   return __sync_add_and_fetch(ptr, value);
bob@3199
   166
}
bob@3199
   167
bob@3199
   168
Uint64
bob@3199
   169
SDL_AtomicSubtractThenFetch64(Uint64 * ptr, Uint64 value)
bob@3199
   170
{
bob@3200
   171
   return __sync_sub_and_fetch(ptr, value);
bob@3199
   172
}
bob@3199
   173
#endif