include/SDL_atomic.h
author Bob Pendleton <bob@pendleton.com>
Thu, 17 Sep 2009 20:35:12 +0000
changeset 3261 72b542f34739
parent 3237 916f396fe65d
child 3407 d3baf5ac4e37
permissions -rw-r--r--
The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
The linux code appears to be complete and *should* be the base of all Unix and GCC based versions. The macosx and win32 versions
are currently just copies of the dummy code. I will begin working on the windows version as soon as this check in is done. I
need someone to work on the Mac OS X version.

I'm afraid that this check in will break QNX (Sorry!)
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@3261
    21
bob@3261
    22
    Contributed by Bob Pendleton, bob@pendleton.com
bob@3180
    23
 */
bob@3180
    24
bob@3180
    25
/**
bob@3180
    26
 * \file SDL_atomic.h
bob@3180
    27
 *
bob@3199
    28
 * Atomic operations.
bob@3180
    29
 */
bob@3180
    30
bob@3180
    31
#ifndef _SDL_atomic_h_
bob@3180
    32
#define _SDL_atomic_h_
bob@3180
    33
bob@3180
    34
#include "SDL_stdinc.h"
bob@3180
    35
#include "SDL_platform.h"
bob@3180
    36
bob@3180
    37
#include "begin_code.h"
bob@3180
    38
bob@3180
    39
/* Set up for C function definitions, even when using C++ */
bob@3180
    40
#ifdef __cplusplus
bob@3180
    41
/* *INDENT-OFF* */
bob@3180
    42
extern "C" {
bob@3180
    43
/* *INDENT-ON* */
bob@3180
    44
#endif
bob@3180
    45
bob@3199
    46
/**
bob@3202
    47
 * These operations may, or may not, actually be implemented using
bob@3202
    48
 * processor specific atomic operations. When possible they are
bob@3202
    49
 * implemented as true processor specific atomic operations. When that
bob@3202
    50
 * is not possible the are implemented using locks that *do* use the
bob@3261
    51
 * available atomic operations.
bob@3261
    52
 *
bob@3261
    53
 * At the very minimum spin locks must be implemented. Without spin
bob@3261
    54
 * locks it is not possible (AFAICT) to emulate the rest of the atomic
bob@3261
    55
 * operations.
bob@3199
    56
 */
slouken@3186
    57
bob@3199
    58
/* Function prototypes */
bob@3180
    59
bob@3261
    60
/**
bob@3261
    61
 * SDL AtomicLock.
bob@3261
    62
 * 
bob@3261
    63
 * The spin lock functions and type are required and can not be
bob@3261
    64
 * emulated because they are used in the emulation code.
bob@3261
    65
 */
bob@3261
    66
bob@3261
    67
typedef volatile Uint32 SDL_SpinLock;
bob@3261
    68
bob@3261
    69
/**
bob@3261
    70
 * \fn  void SDL_AtomicLock(SDL_SpinLock *lock);
bob@3261
    71
 *
bob@3261
    72
 * \brief Lock a spin lock by setting it to a none zero value.
bob@3261
    73
 *
bob@3261
    74
 * \param lock points to the lock.
bob@3261
    75
 *
bob@3261
    76
 */
bob@3261
    77
extern DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock);
bob@3261
    78
bob@3261
    79
/**
bob@3261
    80
 * \fn  void SDL_AtomicUnlock(SDL_SpinLock *lock);
bob@3261
    81
 *
bob@3261
    82
 * \brief Unlock a spin lock by setting it to 0. Always returns immediately
bob@3261
    83
 *
bob@3261
    84
 * \param lock points to the lock.
bob@3261
    85
 *
bob@3261
    86
 */
bob@3261
    87
extern DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock);
bob@3261
    88
bob@3202
    89
/* 32 bit atomic operations */
bob@3202
    90
bob@3199
    91
/**
bob@3216
    92
 * \fn  SDL_bool SDL_AtomicTestThenSet32(volatile Uint32 * ptr);
bob@3199
    93
 *
bob@3261
    94
 * \brief Check to see if *ptr == 0 and set it to 1.
bob@3199
    95
 *
bob@3199
    96
 * \return SDL_True if the value pointed to by ptr was zero and
bob@3199
    97
 * SDL_False if it was not zero
bob@3199
    98
 *
bob@3199
    99
 * \param ptr points to the value to be tested and set.
bob@3199
   100
 *
bob@3199
   101
 */
bob@3216
   102
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTestThenSet32(volatile Uint32 * ptr);
bob@3237
   103
bob@3199
   104
/**
bob@3216
   105
 * \fn  void SDL_AtomicClear32(volatile Uint32 * ptr);
bob@3199
   106
 *
bob@3199
   107
 * \brief set the value pointed to by ptr to be zero.
bob@3199
   108
 *
bob@3199
   109
 * \param ptr address of the value to be set to zero
bob@3199
   110
 *
bob@3199
   111
 */
bob@3216
   112
extern DECLSPEC void SDLCALL SDL_AtomicClear32(volatile Uint32 * ptr);
bob@3237
   113
bob@3199
   114
/**
bob@3216
   115
 * \fn  Uint32 SDL_AtomicFetchThenIncrement32(volatile Uint32 * ptr);
bob@3199
   116
 *
bob@3199
   117
 * \brief fetch the current value of *ptr and then increment that
bob@3199
   118
 * value in place.
bob@3199
   119
 *
bob@3199
   120
 * \return the value before it was incremented.
bob@3199
   121
 *
bob@3199
   122
 * \param ptr address of the value to fetch and increment
bob@3199
   123
 *
bob@3199
   124
 */
bob@3216
   125
extern DECLSPEC Uint32 SDLCALL SDL_AtomicFetchThenIncrement32(volatile Uint32 * ptr);
bob@3237
   126
bob@3199
   127
/**
bob@3216
   128
 * \fn  Uint32 SDL_AtomicFetchThenDecrement32(volatile Uint32 * ptr);
bob@3199
   129
 *
bob@3199
   130
 * \brief fetch *ptr and then decrement the value in place.
bob@3199
   131
 *
bob@3199
   132
 * \return the value before it was decremented.
bob@3199
   133
 *
bob@3199
   134
 * \param ptr address of the value to fetch and drement
bob@3199
   135
 *
bob@3199
   136
 */
bob@3216
   137
extern DECLSPEC Uint32 SDLCALL SDL_AtomicFetchThenDecrement32(volatile Uint32 * ptr);
bob@3237
   138
bob@3199
   139
/**
bob@3216
   140
 * \fn  Uint32 SDL_AtomicFetchThenAdd32(volatile Uint32 * ptr, Uint32 value);
bob@3199
   141
 *
bob@3199
   142
 * \brief fetch the current value at ptr and then add value to *ptr.
bob@3199
   143
 *
bob@3199
   144
 * \return *ptr before the addition took place.
bob@3199
   145
 *
bob@3199
   146
 * \param ptr the address of data we are changing.
bob@3199
   147
 * \param value the value to add to *ptr. 
bob@3199
   148
 *
bob@3199
   149
 */
bob@3216
   150
extern DECLSPEC Uint32 SDLCALL SDL_AtomicFetchThenAdd32(volatile Uint32 * ptr, Uint32 value);
bob@3237
   151
bob@3199
   152
/**
bob@3216
   153
 * \fn  Uint32 SDL_AtomicFetchThenSubtract32(volatile Uint32 * ptr, Uint32 value);
bob@3199
   154
 *
bob@3199
   155
 * \brief Fetch *ptr and then subtract value from it.
bob@3199
   156
 *
bob@3199
   157
 * \return *ptr before the subtraction took place.
bob@3199
   158
 *
bob@3199
   159
 * \param ptr the address of the data being changed.
bob@3199
   160
 * \param value the value to subtract from *ptr.
bob@3199
   161
 *
bob@3199
   162
 */
bob@3216
   163
extern DECLSPEC Uint32 SDLCALL SDL_AtomicFetchThenSubtract32(volatile Uint32 * ptr, Uint32 value);
bob@3237
   164
bob@3199
   165
/**
bob@3216
   166
 * \fn  Uint32 SDL_AtomicIncrementThenFetch32(volatile Uint32 * ptr);
bob@3199
   167
 *
bob@3199
   168
 * \brief Add one to the data pointed to by ptr and return that value.
bob@3199
   169
 *
bob@3199
   170
 * \return the incremented value.
bob@3199
   171
 *
bob@3199
   172
 * \param ptr address of the data to increment.
bob@3199
   173
 *
bob@3199
   174
 */
bob@3216
   175
extern DECLSPEC Uint32 SDLCALL SDL_AtomicIncrementThenFetch32(volatile Uint32 * ptr);
bob@3237
   176
bob@3199
   177
/**
bob@3216
   178
 * \fn  Uint32 SDL_AtomicDecrementThenFetch32(volatile Uint32 * ptr);
bob@3199
   179
 *
bob@3199
   180
 * \brief Subtract one from data pointed to by ptr and return the new value.
bob@3199
   181
 *
bob@3199
   182
 * \return The decremented value.
bob@3199
   183
 *
bob@3199
   184
 * \param ptr The address of the data to decrement.
bob@3199
   185
 *
bob@3199
   186
 */
bob@3216
   187
extern DECLSPEC Uint32 SDLCALL SDL_AtomicDecrementThenFetch32(volatile Uint32 * ptr);
bob@3237
   188
bob@3199
   189
/**
bob@3216
   190
 * \fn  Uint32 SDL_AtomicAddThenFetch32(volatile Uint32 * ptr, Uint32 value);
bob@3199
   191
 *
bob@3199
   192
 * \brief Add value to the data pointed to by ptr and return result.
bob@3199
   193
 *
bob@3199
   194
 * \return The sum of *ptr and value.
bob@3199
   195
 *
bob@3199
   196
 * \param ptr The address of the data to be modified.
bob@3199
   197
 * \param value The value to be added.
bob@3199
   198
 *
bob@3199
   199
 */
bob@3216
   200
extern DECLSPEC Uint32 SDLCALL SDL_AtomicAddThenFetch32(volatile Uint32 * ptr, Uint32 value);
bob@3237
   201
bob@3199
   202
/**
bob@3216
   203
 * \fn  Uint32 SDL_AtomicSubtractThenFetch32(volatile Uint32 * ptr, Uint32 value);
bob@3199
   204
 *
bob@3199
   205
 * \brief Subtract value from the data pointed to by ptr and return the result.
bob@3199
   206
 *
bob@3199
   207
 * \return the difference between *ptr and value.
bob@3199
   208
 *
bob@3199
   209
 * \param ptr The address of the data to be modified.
bob@3199
   210
 * \param value The value to be subtracted.
bob@3199
   211
 *
bob@3199
   212
 */
bob@3216
   213
extern DECLSPEC Uint32 SDLCALL SDL_AtomicSubtractThenFetch32(volatile Uint32 * ptr, Uint32 value);
bob@3180
   214
bob@3202
   215
/* 64 bit atomic operations */
bob@3199
   216
#ifdef SDL_HAS_64BIT_TYPE
bob@3180
   217
bob@3216
   218
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTestThenSet64(volatile Uint64 * ptr);
bob@3216
   219
extern DECLSPEC void SDLCALL SDL_AtomicClear64(volatile Uint64 * ptr);
bob@3216
   220
extern DECLSPEC Uint64 SDLCALL SDL_AtomicFetchThenIncrement64(volatile Uint64 * ptr);
bob@3216
   221
extern DECLSPEC Uint64 SDLCALL SDL_AtomicFetchThenDecrement64(volatile Uint64 * ptr);
bob@3216
   222
extern DECLSPEC Uint64 SDLCALL SDL_AtomicFetchThenAdd64(volatile Uint64 * ptr, Uint64 value);
bob@3216
   223
extern DECLSPEC Uint64 SDLCALL SDL_AtomicFetchThenSubtract64(volatile Uint64 * ptr, Uint64 value);
bob@3216
   224
extern DECLSPEC Uint64 SDLCALL SDL_AtomicIncrementThenFetch64(volatile Uint64 * ptr);
bob@3216
   225
extern DECLSPEC Uint64 SDLCALL SDL_AtomicDecrementThenFetch64(volatile Uint64 * ptr);
bob@3216
   226
extern DECLSPEC Uint64 SDLCALL SDL_AtomicAddThenFetch64(volatile Uint64 * ptr, Uint64 value);
bob@3216
   227
extern DECLSPEC Uint64 SDLCALL SDL_AtomicSubtractThenFetch64(volatile Uint64 * ptr, Uint64 value);
bob@3202
   228
#endif /*  SDL_HAS_64BIT_TYPE */
bob@3180
   229
bob@3180
   230
/* Ends C function definitions when using C++ */
bob@3180
   231
#ifdef __cplusplus
bob@3180
   232
/* *INDENT-OFF* */
bob@3180
   233
}
bob@3180
   234
/* *INDENT-ON* */
bob@3180
   235
#endif
bob@3180
   236
bob@3180
   237
#include "close_code.h"
bob@3180
   238
bob@3180
   239
#endif /* _SDL_atomic_h_ */
bob@3180
   240
bob@3180
   241
/* vi: set ts=4 sw=4 expandtab: */