include/SDL_atomic.h
author Bob Pendleton <bob@pendleton.com>
Mon, 29 Jun 2009 19:54:43 +0000
changeset 3202 3aa519a5c676
parent 3199 3e1bf2b8bd81
child 3216 48a80f2a7ff2
permissions -rw-r--r--
I've made so many changes I don't dare continue until I check the current stuff in.

/test/testatomic.c performs absolutely basic tests to show that the function work as expected. Need a second test to do more detailed tests.

/include/SDL_atomic.h provides declarations for all included functions.

/src/atomic/linux/SDL_atomic.c provided all the functions. On a generic built the 64 bit functions work, but they are emulated. On a build for -march=pentium and above the 64 bit functions use native instructions
/src/atomic/dummy/SDL_atomic.c emulates all the operations using SDL_mutex.h.
/src/atomic/win32/SDL_atomic.c is a copy of dummy
/src/atomic/macosx/SDL_atomic.s is a copy of dummy

These versions of SDL_atomic.c provide a frame work for building the library with a mixture of native and emulated functions. This allows the whole library to be provided on all platforms. (I hope.)
I hope this fits with the SDL philosophy of either providing a common subset or emulating when the platform is missing a feature.

I have not added dummy, macosx, or win32 to the build. They are there as place holders for future work.

I have modified congifure.in to compile sources in /src/atomic/linux. (The SDL configure.in file is an amazing piece of work and I hope I didn't mess it up. :-)
bob@3180
     1
/*
bob@3180
     2
    SDL - Simple DirectMedia Layer
bob@3180
     3
    Copyright (C) 1997-2006 Sam Lantinga
bob@3180
     4
bob@3180
     5
    This library is free software; you can redistribute it and/or
bob@3180
     6
    modify it under the terms of the GNU Lesser General Public
bob@3180
     7
    License as published by the Free Software Foundation; either
bob@3180
     8
    version 2.1 of the License, or (at your option) any later version.
bob@3180
     9
bob@3180
    10
    This library is distributed in the hope that it will be useful,
bob@3180
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
bob@3180
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bob@3180
    13
    Lesser General Public License for more details.
bob@3180
    14
bob@3180
    15
    You should have received a copy of the GNU Lesser General Public
bob@3180
    16
    License along with this library; if not, write to the Free Software
bob@3180
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
bob@3180
    18
bob@3180
    19
    Sam Lantinga
bob@3180
    20
    slouken@libsdl.org
bob@3180
    21
 */
bob@3180
    22
bob@3180
    23
/**
bob@3180
    24
 * \file SDL_atomic.h
bob@3180
    25
 *
bob@3199
    26
 * Atomic operations.
bob@3180
    27
 */
bob@3180
    28
bob@3180
    29
#ifndef _SDL_atomic_h_
bob@3180
    30
#define _SDL_atomic_h_
bob@3180
    31
bob@3180
    32
#include "SDL_stdinc.h"
bob@3180
    33
#include "SDL_platform.h"
bob@3180
    34
bob@3180
    35
#include "begin_code.h"
bob@3180
    36
bob@3180
    37
/* Set up for C function definitions, even when using C++ */
bob@3180
    38
#ifdef __cplusplus
bob@3180
    39
/* *INDENT-OFF* */
bob@3180
    40
extern "C" {
bob@3180
    41
/* *INDENT-ON* */
bob@3180
    42
#endif
bob@3180
    43
bob@3199
    44
/**
bob@3202
    45
 * These operations may, or may not, actually be implemented using
bob@3202
    46
 * processor specific atomic operations. When possible they are
bob@3202
    47
 * implemented as true processor specific atomic operations. When that
bob@3202
    48
 * is not possible the are implemented using locks that *do* use the
bob@3202
    49
 * available atomic operations. In rare cases they may be implemented
bob@3202
    50
 * using SDL's mutex fuctions.
bob@3199
    51
 */
slouken@3186
    52
bob@3199
    53
/* Function prototypes */
bob@3180
    54
bob@3202
    55
/* 8 bit atomic operations */
bob@3202
    56
bob@3202
    57
extern DECLSPEC Uint8 SDLCALL SDL_AtomicExchange8(Uint8 * ptr, Uint8 value);
bob@3202
    58
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareThenSet8(Uint8 * ptr,
bob@3202
    59
                                                            Uint8 oldvalue, Uint8 newvalue);
bob@3202
    60
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTestThenSet8(Uint8 * ptr);
bob@3202
    61
extern DECLSPEC void SDLCALL SDL_AtomicClear8(Uint8 * ptr);
bob@3202
    62
extern DECLSPEC Uint8 SDLCALL SDL_AtomicFetchThenIncrement8(Uint8 * ptr);
bob@3202
    63
extern DECLSPEC Uint8 SDLCALL SDL_AtomicFetchThenDecrement8(Uint8 * ptr);
bob@3202
    64
extern DECLSPEC Uint8 SDLCALL SDL_AtomicFetchThenAdd8(Uint8 * ptr, Uint8 value);
bob@3202
    65
extern DECLSPEC Uint8 SDLCALL SDL_AtomicFetchThenSubtract8(Uint8 * ptr, Uint8 value);
bob@3202
    66
extern DECLSPEC Uint8 SDLCALL SDL_AtomicIncrementThenFetch8(Uint8 * ptr);
bob@3202
    67
extern DECLSPEC Uint8 SDLCALL SDL_AtomicDecrementThenFetch8(Uint8 * ptr);
bob@3202
    68
extern DECLSPEC Uint8 SDLCALL SDL_AtomicAddThenFetch8(Uint8 * ptr, Uint8 value);
bob@3202
    69
extern DECLSPEC Uint8 SDLCALL SDL_AtomicSubtractThenFetch8(Uint8 * ptr, Uint8 value);
bob@3202
    70
bob@3202
    71
/* 16 bit atomic operations */
bob@3202
    72
bob@3202
    73
extern DECLSPEC Uint16 SDLCALL SDL_AtomicExchange16(Uint16 * ptr, Uint16 value);
bob@3202
    74
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareThenSet16(Uint16 * ptr,
bob@3202
    75
                                                            Uint16 oldvalue, Uint16 newvalue);
bob@3202
    76
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTestThenSet16(Uint16 * ptr);
bob@3202
    77
extern DECLSPEC void SDLCALL SDL_AtomicClear16(Uint16 * ptr);
bob@3202
    78
extern DECLSPEC Uint16 SDLCALL SDL_AtomicFetchThenIncrement16(Uint16 * ptr);
bob@3202
    79
extern DECLSPEC Uint16 SDLCALL SDL_AtomicFetchThenDecrement16(Uint16 * ptr);
bob@3202
    80
extern DECLSPEC Uint16 SDLCALL SDL_AtomicFetchThenAdd16(Uint16 * ptr, Uint16 value);
bob@3202
    81
extern DECLSPEC Uint16 SDLCALL SDL_AtomicFetchThenSubtract16(Uint16 * ptr, Uint16 value);
bob@3202
    82
extern DECLSPEC Uint16 SDLCALL SDL_AtomicIncrementThenFetch16(Uint16 * ptr);
bob@3202
    83
extern DECLSPEC Uint16 SDLCALL SDL_AtomicDecrementThenFetch16(Uint16 * ptr);
bob@3202
    84
extern DECLSPEC Uint16 SDLCALL SDL_AtomicAddThenFetch16(Uint16 * ptr, Uint16 value);
bob@3202
    85
extern DECLSPEC Uint16 SDLCALL SDL_AtomicSubtractThenFetch16(Uint16 * ptr, Uint16 value);
bob@3202
    86
bob@3202
    87
/* 32 bit atomic operations */
bob@3202
    88
bob@3199
    89
/**
bob@3199
    90
 * \fn int SDL_AtomicExchange32(Uint32 * ptr, Uint32 value)
bob@3199
    91
 *
bob@3199
    92
 * \brief Atomically exchange two 32 bit values.
bob@3199
    93
 *
bob@3199
    94
 * \return the value point to by ptr.
bob@3199
    95
 *
bob@3199
    96
 * \param ptr points to the value to be fetched from *ptr.  
bob@3199
    97
 * \param value is value to be stored at *ptr.
bob@3199
    98
 *
bob@3199
    99
 * The current value stored at *ptr is returned and it is replaced
bob@3199
   100
 * with value. This function can be used to implement SDL_TestThenSet.
bob@3199
   101
 *
bob@3199
   102
 */
bob@3199
   103
extern DECLSPEC Uint32 SDLCALL SDL_AtomicExchange32(Uint32 * ptr, Uint32 value);
bob@3199
   104
/**
bob@3199
   105
 * \fn int SDL_AtomicCompareThenSet32(Uint32 * ptr, Uint32 oldvalue, Uint32 newvalue)
bob@3199
   106
 *
bob@3199
   107
 * \brief If *ptr == oldvalue then replace the contents of *ptr by new value. 
bob@3199
   108
 *
bob@3199
   109
 * \return true if the newvalue was stored.
bob@3199
   110
 *
bob@3199
   111
 * \param *ptr is the value to be compared and replaced.
bob@3199
   112
 * \param oldvalue is value to be compared to *ptr.
bob@3199
   113
 * \param newvalue is value to be stored at *ptr.
bob@3199
   114
 *
bob@3199
   115
 */
bob@3199
   116
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareThenSet32(Uint32 * ptr,
bob@3199
   117
                                                            Uint32 oldvalue, Uint32 newvalue);
bob@3199
   118
/**
bob@3199
   119
 * \fn  SDL_bool SDL_AtomicTestThenSet32(Uint32 * ptr);
bob@3199
   120
 *
bob@3199
   121
 * \brief Check to see if *ptr == 0 and set it to non-zero.
bob@3199
   122
 *
bob@3199
   123
 * \return SDL_True if the value pointed to by ptr was zero and
bob@3199
   124
 * SDL_False if it was not zero
bob@3199
   125
 *
bob@3199
   126
 * \param ptr points to the value to be tested and set.
bob@3199
   127
 *
bob@3199
   128
 */
bob@3199
   129
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTestThenSet32(Uint32 * ptr);
bob@3199
   130
/**
bob@3199
   131
 * \fn  void SDL_AtomicClear32(Uint32 * ptr);
bob@3199
   132
 *
bob@3199
   133
 * \brief set the value pointed to by ptr to be zero.
bob@3199
   134
 *
bob@3199
   135
 * \param ptr address of the value to be set to zero
bob@3199
   136
 *
bob@3199
   137
 */
bob@3199
   138
extern DECLSPEC void SDLCALL SDL_AtomicClear32(Uint32 * ptr);
bob@3199
   139
/**
bob@3199
   140
 * \fn  Uint32 SDL_AtomicFetchThenIncrement32(Uint32 * ptr);
bob@3199
   141
 *
bob@3199
   142
 * \brief fetch the current value of *ptr and then increment that
bob@3199
   143
 * value in place.
bob@3199
   144
 *
bob@3199
   145
 * \return the value before it was incremented.
bob@3199
   146
 *
bob@3199
   147
 * \param ptr address of the value to fetch and increment
bob@3199
   148
 *
bob@3199
   149
 */
bob@3199
   150
extern DECLSPEC Uint32 SDLCALL SDL_AtomicFetchThenIncrement32(Uint32 * ptr);
bob@3199
   151
/**
bob@3199
   152
 * \fn  Uint32 SDL_AtomicFetchThenDecrement32(Uint32 * ptr);
bob@3199
   153
 *
bob@3199
   154
 * \brief fetch *ptr and then decrement the value in place.
bob@3199
   155
 *
bob@3199
   156
 * \return the value before it was decremented.
bob@3199
   157
 *
bob@3199
   158
 * \param ptr address of the value to fetch and drement
bob@3199
   159
 *
bob@3199
   160
 */
bob@3199
   161
extern DECLSPEC Uint32 SDLCALL SDL_AtomicFetchThenDecrement32(Uint32 * ptr);
bob@3199
   162
/**
bob@3199
   163
 * \fn  Uint32 SDL_AtomicFetchThenAdd32(Uint32 * ptr, Uint32 value);
bob@3199
   164
 *
bob@3199
   165
 * \brief fetch the current value at ptr and then add value to *ptr.
bob@3199
   166
 *
bob@3199
   167
 * \return *ptr before the addition took place.
bob@3199
   168
 *
bob@3199
   169
 * \param ptr the address of data we are changing.
bob@3199
   170
 * \param value the value to add to *ptr. 
bob@3199
   171
 *
bob@3199
   172
 */
bob@3199
   173
extern DECLSPEC Uint32 SDLCALL SDL_AtomicFetchThenAdd32(Uint32 * ptr, Uint32 value);
bob@3199
   174
/**
bob@3199
   175
 * \fn  Uint32 SDL_AtomicFetchThenSubtract32(Uint32 * ptr, Uint32 value);
bob@3199
   176
 *
bob@3199
   177
 * \brief Fetch *ptr and then subtract value from it.
bob@3199
   178
 *
bob@3199
   179
 * \return *ptr before the subtraction took place.
bob@3199
   180
 *
bob@3199
   181
 * \param ptr the address of the data being changed.
bob@3199
   182
 * \param value the value to subtract from *ptr.
bob@3199
   183
 *
bob@3199
   184
 */
bob@3199
   185
extern DECLSPEC Uint32 SDLCALL SDL_AtomicFetchThenSubtract32(Uint32 * ptr, Uint32 value);
bob@3199
   186
/**
bob@3199
   187
 * \fn  Uint32 SDL_AtomicIncrementThenFetch32(Uint32 * ptr);
bob@3199
   188
 *
bob@3199
   189
 * \brief Add one to the data pointed to by ptr and return that value.
bob@3199
   190
 *
bob@3199
   191
 * \return the incremented value.
bob@3199
   192
 *
bob@3199
   193
 * \param ptr address of the data to increment.
bob@3199
   194
 *
bob@3199
   195
 */
bob@3199
   196
extern DECLSPEC Uint32 SDLCALL SDL_AtomicIncrementThenFetch32(Uint32 * ptr);
bob@3199
   197
/**
bob@3199
   198
 * \fn  Uint32 SDL_AtomicDecrementThenFetch32(Uint32 * ptr);
bob@3199
   199
 *
bob@3199
   200
 * \brief Subtract one from data pointed to by ptr and return the new value.
bob@3199
   201
 *
bob@3199
   202
 * \return The decremented value.
bob@3199
   203
 *
bob@3199
   204
 * \param ptr The address of the data to decrement.
bob@3199
   205
 *
bob@3199
   206
 */
bob@3199
   207
extern DECLSPEC Uint32 SDLCALL SDL_AtomicDecrementThenFetch32(Uint32 * ptr);
bob@3199
   208
/**
bob@3199
   209
 * \fn  Uint32 SDL_AtomicAddThenFetch32(Uint32 * ptr, Uint32 value);
bob@3199
   210
 *
bob@3199
   211
 * \brief Add value to the data pointed to by ptr and return result.
bob@3199
   212
 *
bob@3199
   213
 * \return The sum of *ptr and value.
bob@3199
   214
 *
bob@3199
   215
 * \param ptr The address of the data to be modified.
bob@3199
   216
 * \param value The value to be added.
bob@3199
   217
 *
bob@3199
   218
 */
bob@3199
   219
extern DECLSPEC Uint32 SDLCALL SDL_AtomicAddThenFetch32(Uint32 * ptr, Uint32 value);
bob@3199
   220
/**
bob@3199
   221
 * \fn  Uint32 SDL_AtomicSubtractThenFetch32(Uint32 * ptr, Uint32 value);
bob@3199
   222
 *
bob@3199
   223
 * \brief Subtract value from the data pointed to by ptr and return the result.
bob@3199
   224
 *
bob@3199
   225
 * \return the difference between *ptr and value.
bob@3199
   226
 *
bob@3199
   227
 * \param ptr The address of the data to be modified.
bob@3199
   228
 * \param value The value to be subtracted.
bob@3199
   229
 *
bob@3199
   230
 */
bob@3199
   231
extern DECLSPEC Uint32 SDLCALL SDL_AtomicSubtractThenFetch32(Uint32 * ptr, Uint32 value);
bob@3180
   232
bob@3202
   233
/* 64 bit atomic operations */
bob@3199
   234
#ifdef SDL_HAS_64BIT_TYPE
bob@3180
   235
bob@3199
   236
extern DECLSPEC Uint64 SDLCALL SDL_AtomicExchange64(Uint64 * ptr, Uint64 value);
bob@3199
   237
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareThenSet64(Uint64 * ptr,
bob@3199
   238
                                                            Uint64 oldvalue, Uint64 newvalue);
bob@3199
   239
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTestThenSet64(Uint64 * ptr);
bob@3199
   240
extern DECLSPEC void SDLCALL SDL_AtomicClear64(Uint64 * ptr);
bob@3199
   241
extern DECLSPEC Uint64 SDLCALL SDL_AtomicFetchThenIncrement64(Uint64 * ptr);
bob@3199
   242
extern DECLSPEC Uint64 SDLCALL SDL_AtomicFetchThenDecrement64(Uint64 * ptr);
bob@3199
   243
extern DECLSPEC Uint64 SDLCALL SDL_AtomicFetchThenAdd64(Uint64 * ptr, Uint64 value);
bob@3199
   244
extern DECLSPEC Uint64 SDLCALL SDL_AtomicFetchThenSubtract64(Uint64 * ptr, Uint64 value);
bob@3199
   245
extern DECLSPEC Uint64 SDLCALL SDL_AtomicIncrementThenFetch64(Uint64 * ptr);
bob@3199
   246
extern DECLSPEC Uint64 SDLCALL SDL_AtomicDecrementThenFetch64(Uint64 * ptr);
bob@3199
   247
extern DECLSPEC Uint64 SDLCALL SDL_AtomicAddThenFetch64(Uint64 * ptr, Uint64 value);
bob@3199
   248
extern DECLSPEC Uint64 SDLCALL SDL_AtomicSubtractThenFetch64(Uint64 * ptr, Uint64 value);
bob@3202
   249
#endif /*  SDL_HAS_64BIT_TYPE */
bob@3180
   250
bob@3180
   251
/* Ends C function definitions when using C++ */
bob@3180
   252
#ifdef __cplusplus
bob@3180
   253
/* *INDENT-OFF* */
bob@3180
   254
}
bob@3180
   255
/* *INDENT-ON* */
bob@3180
   256
#endif
bob@3180
   257
bob@3180
   258
#include "close_code.h"
bob@3180
   259
bob@3180
   260
#endif /* _SDL_atomic_h_ */
bob@3180
   261
bob@3180
   262
/* vi: set ts=4 sw=4 expandtab: */