include/SDL_atomic.h
author Bob Pendleton <bob@pendleton.com>
Tue, 11 Aug 2009 21:27:19 +0000
changeset 3237 916f396fe65d
parent 3216 48a80f2a7ff2
child 3261 72b542f34739
permissions -rw-r--r--
Start of redesign, getting rid of 8 and 16 bit operations in .h and test files.
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
/* 32 bit atomic operations */
bob@3202
    56
bob@3199
    57
/**
bob@3216
    58
 * \fn int SDL_AtomicExchange32(volatile Uint32 * ptr, Uint32 value)
bob@3199
    59
 *
bob@3199
    60
 * \brief Atomically exchange two 32 bit values.
bob@3199
    61
 *
bob@3199
    62
 * \return the value point to by ptr.
bob@3199
    63
 *
bob@3199
    64
 * \param ptr points to the value to be fetched from *ptr.  
bob@3199
    65
 * \param value is value to be stored at *ptr.
bob@3199
    66
 *
bob@3199
    67
 * The current value stored at *ptr is returned and it is replaced
bob@3199
    68
 * with value. This function can be used to implement SDL_TestThenSet.
bob@3199
    69
 *
bob@3199
    70
 */
bob@3216
    71
extern DECLSPEC Uint32 SDLCALL SDL_AtomicExchange32(volatile Uint32 * ptr, Uint32 value);
bob@3237
    72
bob@3199
    73
/**
bob@3216
    74
 * \fn int SDL_AtomicCompareThenSet32(volatile Uint32 * ptr, Uint32 oldvalue, Uint32 newvalue)
bob@3199
    75
 *
bob@3199
    76
 * \brief If *ptr == oldvalue then replace the contents of *ptr by new value. 
bob@3199
    77
 *
bob@3199
    78
 * \return true if the newvalue was stored.
bob@3199
    79
 *
bob@3199
    80
 * \param *ptr is the value to be compared and replaced.
bob@3199
    81
 * \param oldvalue is value to be compared to *ptr.
bob@3199
    82
 * \param newvalue is value to be stored at *ptr.
bob@3199
    83
 *
bob@3199
    84
 */
bob@3216
    85
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareThenSet32(volatile Uint32 * ptr,
bob@3199
    86
                                                            Uint32 oldvalue, Uint32 newvalue);
bob@3199
    87
/**
bob@3216
    88
 * \fn  SDL_bool SDL_AtomicTestThenSet32(volatile Uint32 * ptr);
bob@3199
    89
 *
bob@3199
    90
 * \brief Check to see if *ptr == 0 and set it to non-zero.
bob@3199
    91
 *
bob@3199
    92
 * \return SDL_True if the value pointed to by ptr was zero and
bob@3199
    93
 * SDL_False if it was not zero
bob@3199
    94
 *
bob@3199
    95
 * \param ptr points to the value to be tested and set.
bob@3199
    96
 *
bob@3199
    97
 */
bob@3216
    98
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTestThenSet32(volatile Uint32 * ptr);
bob@3237
    99
bob@3199
   100
/**
bob@3216
   101
 * \fn  void SDL_AtomicClear32(volatile Uint32 * ptr);
bob@3199
   102
 *
bob@3199
   103
 * \brief set the value pointed to by ptr to be zero.
bob@3199
   104
 *
bob@3199
   105
 * \param ptr address of the value to be set to zero
bob@3199
   106
 *
bob@3199
   107
 */
bob@3216
   108
extern DECLSPEC void SDLCALL SDL_AtomicClear32(volatile Uint32 * ptr);
bob@3237
   109
bob@3199
   110
/**
bob@3216
   111
 * \fn  Uint32 SDL_AtomicFetchThenIncrement32(volatile Uint32 * ptr);
bob@3199
   112
 *
bob@3199
   113
 * \brief fetch the current value of *ptr and then increment that
bob@3199
   114
 * value in place.
bob@3199
   115
 *
bob@3199
   116
 * \return the value before it was incremented.
bob@3199
   117
 *
bob@3199
   118
 * \param ptr address of the value to fetch and increment
bob@3199
   119
 *
bob@3199
   120
 */
bob@3216
   121
extern DECLSPEC Uint32 SDLCALL SDL_AtomicFetchThenIncrement32(volatile Uint32 * ptr);
bob@3237
   122
bob@3199
   123
/**
bob@3216
   124
 * \fn  Uint32 SDL_AtomicFetchThenDecrement32(volatile Uint32 * ptr);
bob@3199
   125
 *
bob@3199
   126
 * \brief fetch *ptr and then decrement the value in place.
bob@3199
   127
 *
bob@3199
   128
 * \return the value before it was decremented.
bob@3199
   129
 *
bob@3199
   130
 * \param ptr address of the value to fetch and drement
bob@3199
   131
 *
bob@3199
   132
 */
bob@3216
   133
extern DECLSPEC Uint32 SDLCALL SDL_AtomicFetchThenDecrement32(volatile Uint32 * ptr);
bob@3237
   134
bob@3199
   135
/**
bob@3216
   136
 * \fn  Uint32 SDL_AtomicFetchThenAdd32(volatile Uint32 * ptr, Uint32 value);
bob@3199
   137
 *
bob@3199
   138
 * \brief fetch the current value at ptr and then add value to *ptr.
bob@3199
   139
 *
bob@3199
   140
 * \return *ptr before the addition took place.
bob@3199
   141
 *
bob@3199
   142
 * \param ptr the address of data we are changing.
bob@3199
   143
 * \param value the value to add to *ptr. 
bob@3199
   144
 *
bob@3199
   145
 */
bob@3216
   146
extern DECLSPEC Uint32 SDLCALL SDL_AtomicFetchThenAdd32(volatile Uint32 * ptr, Uint32 value);
bob@3237
   147
bob@3199
   148
/**
bob@3216
   149
 * \fn  Uint32 SDL_AtomicFetchThenSubtract32(volatile Uint32 * ptr, Uint32 value);
bob@3199
   150
 *
bob@3199
   151
 * \brief Fetch *ptr and then subtract value from it.
bob@3199
   152
 *
bob@3199
   153
 * \return *ptr before the subtraction took place.
bob@3199
   154
 *
bob@3199
   155
 * \param ptr the address of the data being changed.
bob@3199
   156
 * \param value the value to subtract from *ptr.
bob@3199
   157
 *
bob@3199
   158
 */
bob@3216
   159
extern DECLSPEC Uint32 SDLCALL SDL_AtomicFetchThenSubtract32(volatile Uint32 * ptr, Uint32 value);
bob@3237
   160
bob@3199
   161
/**
bob@3216
   162
 * \fn  Uint32 SDL_AtomicIncrementThenFetch32(volatile Uint32 * ptr);
bob@3199
   163
 *
bob@3199
   164
 * \brief Add one to the data pointed to by ptr and return that value.
bob@3199
   165
 *
bob@3199
   166
 * \return the incremented value.
bob@3199
   167
 *
bob@3199
   168
 * \param ptr address of the data to increment.
bob@3199
   169
 *
bob@3199
   170
 */
bob@3216
   171
extern DECLSPEC Uint32 SDLCALL SDL_AtomicIncrementThenFetch32(volatile Uint32 * ptr);
bob@3237
   172
bob@3199
   173
/**
bob@3216
   174
 * \fn  Uint32 SDL_AtomicDecrementThenFetch32(volatile Uint32 * ptr);
bob@3199
   175
 *
bob@3199
   176
 * \brief Subtract one from data pointed to by ptr and return the new value.
bob@3199
   177
 *
bob@3199
   178
 * \return The decremented value.
bob@3199
   179
 *
bob@3199
   180
 * \param ptr The address of the data to decrement.
bob@3199
   181
 *
bob@3199
   182
 */
bob@3216
   183
extern DECLSPEC Uint32 SDLCALL SDL_AtomicDecrementThenFetch32(volatile Uint32 * ptr);
bob@3237
   184
bob@3199
   185
/**
bob@3216
   186
 * \fn  Uint32 SDL_AtomicAddThenFetch32(volatile Uint32 * ptr, Uint32 value);
bob@3199
   187
 *
bob@3199
   188
 * \brief Add value to the data pointed to by ptr and return result.
bob@3199
   189
 *
bob@3199
   190
 * \return The sum of *ptr and value.
bob@3199
   191
 *
bob@3199
   192
 * \param ptr The address of the data to be modified.
bob@3199
   193
 * \param value The value to be added.
bob@3199
   194
 *
bob@3199
   195
 */
bob@3216
   196
extern DECLSPEC Uint32 SDLCALL SDL_AtomicAddThenFetch32(volatile Uint32 * ptr, Uint32 value);
bob@3237
   197
bob@3199
   198
/**
bob@3216
   199
 * \fn  Uint32 SDL_AtomicSubtractThenFetch32(volatile Uint32 * ptr, Uint32 value);
bob@3199
   200
 *
bob@3199
   201
 * \brief Subtract value from the data pointed to by ptr and return the result.
bob@3199
   202
 *
bob@3199
   203
 * \return the difference between *ptr and value.
bob@3199
   204
 *
bob@3199
   205
 * \param ptr The address of the data to be modified.
bob@3199
   206
 * \param value The value to be subtracted.
bob@3199
   207
 *
bob@3199
   208
 */
bob@3216
   209
extern DECLSPEC Uint32 SDLCALL SDL_AtomicSubtractThenFetch32(volatile Uint32 * ptr, Uint32 value);
bob@3180
   210
bob@3202
   211
/* 64 bit atomic operations */
bob@3199
   212
#ifdef SDL_HAS_64BIT_TYPE
bob@3180
   213
bob@3216
   214
extern DECLSPEC Uint64 SDLCALL SDL_AtomicExchange64(volatile Uint64 * ptr, Uint64 value);
bob@3216
   215
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareThenSet64(volatile Uint64 * ptr,
bob@3199
   216
                                                            Uint64 oldvalue, Uint64 newvalue);
bob@3216
   217
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTestThenSet64(volatile Uint64 * ptr);
bob@3216
   218
extern DECLSPEC void SDLCALL SDL_AtomicClear64(volatile Uint64 * ptr);
bob@3216
   219
extern DECLSPEC Uint64 SDLCALL SDL_AtomicFetchThenIncrement64(volatile Uint64 * ptr);
bob@3216
   220
extern DECLSPEC Uint64 SDLCALL SDL_AtomicFetchThenDecrement64(volatile Uint64 * ptr);
bob@3216
   221
extern DECLSPEC Uint64 SDLCALL SDL_AtomicFetchThenAdd64(volatile Uint64 * ptr, Uint64 value);
bob@3216
   222
extern DECLSPEC Uint64 SDLCALL SDL_AtomicFetchThenSubtract64(volatile Uint64 * ptr, Uint64 value);
bob@3216
   223
extern DECLSPEC Uint64 SDLCALL SDL_AtomicIncrementThenFetch64(volatile Uint64 * ptr);
bob@3216
   224
extern DECLSPEC Uint64 SDLCALL SDL_AtomicDecrementThenFetch64(volatile Uint64 * ptr);
bob@3216
   225
extern DECLSPEC Uint64 SDLCALL SDL_AtomicAddThenFetch64(volatile Uint64 * ptr, Uint64 value);
bob@3216
   226
extern DECLSPEC Uint64 SDLCALL SDL_AtomicSubtractThenFetch64(volatile Uint64 * ptr, Uint64 value);
bob@3202
   227
#endif /*  SDL_HAS_64BIT_TYPE */
bob@3180
   228
bob@3180
   229
/* Ends C function definitions when using C++ */
bob@3180
   230
#ifdef __cplusplus
bob@3180
   231
/* *INDENT-OFF* */
bob@3180
   232
}
bob@3180
   233
/* *INDENT-ON* */
bob@3180
   234
#endif
bob@3180
   235
bob@3180
   236
#include "close_code.h"
bob@3180
   237
bob@3180
   238
#endif /* _SDL_atomic_h_ */
bob@3180
   239
bob@3180
   240
/* vi: set ts=4 sw=4 expandtab: */