include/SDL_atomic.h
author Bob Pendleton <bob@pendleton.com>
Wed, 24 Jun 2009 20:04:08 +0000
changeset 3199 3e1bf2b8bd81
parent 3187 e041d2c603fe
child 3202 3aa519a5c676
permissions -rw-r--r--
This check in updates SDL_atomic.h to reflect the new set of atomic operations in 32 and 64 bit form.
It also update configure.in to compile the linux version of the library. The three versions are all dummies
implementations that do nothing. They are being checked in as place holders. Mostly, I just wanted to get
place holders and the configure.in checked in.
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
bob@3180
    33
#include "SDL_stdinc.h"
bob@3180
    34
#include "SDL_platform.h"
bob@3180
    35
bob@3180
    36
#include "begin_code.h"
bob@3180
    37
bob@3180
    38
/* Set up for C function definitions, even when using C++ */
bob@3180
    39
#ifdef __cplusplus
bob@3180
    40
/* *INDENT-OFF* */
bob@3180
    41
extern "C" {
bob@3180
    42
/* *INDENT-ON* */
bob@3180
    43
#endif
bob@3180
    44
slouken@3187
    45
/* *INDENT-OFF* */
bob@3199
    46
/**
bob@3199
    47
 * \def SDL_AtomicBusyWait32 (ptr)
bob@3199
    48
 *
bob@3199
    49
 * \brief Implements a simple busy wait for use with
bob@3199
    50
 * SDL_AtomicTestThenSet and SDL_AtomicClear.
bob@3199
    51
 *
bob@3199
    52
 * Note: This can be an infinite loop.
bob@3199
    53
 *
bob@3199
    54
 */
bob@3199
    55
#define SDL_AtomicBusyWait32(ptr)		\
bob@3199
    56
   {						\
bob@3199
    57
   while (!SDL_AtomicTestThenSet32(ptr)		\
bob@3199
    58
      {						\
bob@3199
    59
      };					\
bob@3199
    60
   };
slouken@3187
    61
bob@3199
    62
/**
bob@3199
    63
 * \def SDL_AtomicWait32(ptr)
bob@3199
    64
 *
bob@3199
    65
 * \brief A safer way to wait for a test-then-set lock to be cleared.
bob@3199
    66
 *
bob@3199
    67
 * This assumes that the SDL_Sleep(0) call acts as a thread_yeild
bob@3199
    68
 * operation. 
bob@3199
    69
 *
bob@3199
    70
 */
bob@3199
    71
#define SDL_AtomicWait32(ptr)			\
bob@3199
    72
   {						\
bob@3199
    73
   while (!SDL_AtomicTestThenSet32(ptr)		\
bob@3199
    74
      {						\
bob@3199
    75
	 SDL_Sleep(0);				\
bob@3199
    76
      };                                        \
bob@3199
    77
   };
bob@3180
    78
bob@3199
    79
/**
bob@3199
    80
 * \def SDL_AtomicBusyWait64(ptr)
bob@3199
    81
 *
bob@3199
    82
 * \brief 64 bit version of busy wait
bob@3199
    83
 *
bob@3199
    84
 * \sa SDL_AtomicBusyWait32
bob@3199
    85
 */
bob@3199
    86
#define SDL_AtomicBusyWait64(ptr)		\
bob@3199
    87
   {						\
bob@3199
    88
   while (!SDL_AtomicTestThenSet64(ptr)		\
bob@3199
    89
      {						\
bob@3199
    90
      };					\
bob@3199
    91
   };
bob@3180
    92
bob@3199
    93
/**
bob@3199
    94
 * \def SDL_AtomicWait64(ptr)
bob@3199
    95
 *
bob@3199
    96
 * \brief 64 bit version of SDL_AtomicWait32
bob@3199
    97
 *
bob@3199
    98
 * \sa SDL_AtomicWait32
bob@3199
    99
 */
bob@3199
   100
#define SDL_AtomicWait64(ptr)			\
bob@3199
   101
   {						\
bob@3199
   102
   while (!SDL_AtomicTestThenSet64(ptr)		\
bob@3199
   103
      {						\
bob@3199
   104
	 SDL_Sleep(0);				\
bob@3199
   105
      };                                        \
bob@3199
   106
   };
slouken@3187
   107
/* *INDENT-ON* */
slouken@3186
   108
bob@3199
   109
/* Function prototypes */
bob@3180
   110
bob@3199
   111
/**
bob@3199
   112
 * \fn int SDL_AtomicExchange32(Uint32 * ptr, Uint32 value)
bob@3199
   113
 *
bob@3199
   114
 * \brief Atomically exchange two 32 bit values.
bob@3199
   115
 *
bob@3199
   116
 * \return the value point to by ptr.
bob@3199
   117
 *
bob@3199
   118
 * \param ptr points to the value to be fetched from *ptr.  
bob@3199
   119
 * \param value is value to be stored at *ptr.
bob@3199
   120
 *
bob@3199
   121
 * The current value stored at *ptr is returned and it is replaced
bob@3199
   122
 * with value. This function can be used to implement SDL_TestThenSet.
bob@3199
   123
 *
bob@3199
   124
 */
bob@3199
   125
extern DECLSPEC Uint32 SDLCALL SDL_AtomicExchange32(Uint32 * ptr, Uint32 value);
bob@3199
   126
/**
bob@3199
   127
 * \fn int SDL_AtomicCompareThenSet32(Uint32 * ptr, Uint32 oldvalue, Uint32 newvalue)
bob@3199
   128
 *
bob@3199
   129
 * \brief If *ptr == oldvalue then replace the contents of *ptr by new value. 
bob@3199
   130
 *
bob@3199
   131
 * \return true if the newvalue was stored.
bob@3199
   132
 *
bob@3199
   133
 * \param *ptr is the value to be compared and replaced.
bob@3199
   134
 * \param oldvalue is value to be compared to *ptr.
bob@3199
   135
 * \param newvalue is value to be stored at *ptr.
bob@3199
   136
 *
bob@3199
   137
 */
bob@3199
   138
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareThenSet32(Uint32 * ptr,
bob@3199
   139
                                                            Uint32 oldvalue, Uint32 newvalue);
bob@3199
   140
/**
bob@3199
   141
 * \fn  SDL_bool SDL_AtomicTestThenSet32(Uint32 * ptr);
bob@3199
   142
 *
bob@3199
   143
 * \brief Check to see if *ptr == 0 and set it to non-zero.
bob@3199
   144
 *
bob@3199
   145
 * \return SDL_True if the value pointed to by ptr was zero and
bob@3199
   146
 * SDL_False if it was not zero
bob@3199
   147
 *
bob@3199
   148
 * \param ptr points to the value to be tested and set.
bob@3199
   149
 *
bob@3199
   150
 */
bob@3199
   151
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTestThenSet32(Uint32 * ptr);
bob@3199
   152
/**
bob@3199
   153
 * \fn  void SDL_AtomicClear32(Uint32 * ptr);
bob@3199
   154
 *
bob@3199
   155
 * \brief set the value pointed to by ptr to be zero.
bob@3199
   156
 *
bob@3199
   157
 * \param ptr address of the value to be set to zero
bob@3199
   158
 *
bob@3199
   159
 */
bob@3199
   160
extern DECLSPEC void SDLCALL SDL_AtomicClear32(Uint32 * ptr);
bob@3199
   161
/**
bob@3199
   162
 * \fn  Uint32 SDL_AtomicFetchThenIncrement32(Uint32 * ptr);
bob@3199
   163
 *
bob@3199
   164
 * \brief fetch the current value of *ptr and then increment that
bob@3199
   165
 * value in place.
bob@3199
   166
 *
bob@3199
   167
 * \return the value before it was incremented.
bob@3199
   168
 *
bob@3199
   169
 * \param ptr address of the value to fetch and increment
bob@3199
   170
 *
bob@3199
   171
 */
bob@3199
   172
extern DECLSPEC Uint32 SDLCALL SDL_AtomicFetchThenIncrement32(Uint32 * ptr);
bob@3199
   173
/**
bob@3199
   174
 * \fn  Uint32 SDL_AtomicFetchThenDecrement32(Uint32 * ptr);
bob@3199
   175
 *
bob@3199
   176
 * \brief fetch *ptr and then decrement the value in place.
bob@3199
   177
 *
bob@3199
   178
 * \return the value before it was decremented.
bob@3199
   179
 *
bob@3199
   180
 * \param ptr address of the value to fetch and drement
bob@3199
   181
 *
bob@3199
   182
 */
bob@3199
   183
extern DECLSPEC Uint32 SDLCALL SDL_AtomicFetchThenDecrement32(Uint32 * ptr);
bob@3199
   184
/**
bob@3199
   185
 * \fn  Uint32 SDL_AtomicFetchThenAdd32(Uint32 * ptr, Uint32 value);
bob@3199
   186
 *
bob@3199
   187
 * \brief fetch the current value at ptr and then add value to *ptr.
bob@3199
   188
 *
bob@3199
   189
 * \return *ptr before the addition took place.
bob@3199
   190
 *
bob@3199
   191
 * \param ptr the address of data we are changing.
bob@3199
   192
 * \param value the value to add to *ptr. 
bob@3199
   193
 *
bob@3199
   194
 */
bob@3199
   195
extern DECLSPEC Uint32 SDLCALL SDL_AtomicFetchThenAdd32(Uint32 * ptr, Uint32 value);
bob@3199
   196
/**
bob@3199
   197
 * \fn  Uint32 SDL_AtomicFetchThenSubtract32(Uint32 * ptr, Uint32 value);
bob@3199
   198
 *
bob@3199
   199
 * \brief Fetch *ptr and then subtract value from it.
bob@3199
   200
 *
bob@3199
   201
 * \return *ptr before the subtraction took place.
bob@3199
   202
 *
bob@3199
   203
 * \param ptr the address of the data being changed.
bob@3199
   204
 * \param value the value to subtract from *ptr.
bob@3199
   205
 *
bob@3199
   206
 */
bob@3199
   207
extern DECLSPEC Uint32 SDLCALL SDL_AtomicFetchThenSubtract32(Uint32 * ptr, Uint32 value);
bob@3199
   208
/**
bob@3199
   209
 * \fn  Uint32 SDL_AtomicIncrementThenFetch32(Uint32 * ptr);
bob@3199
   210
 *
bob@3199
   211
 * \brief Add one to the data pointed to by ptr and return that value.
bob@3199
   212
 *
bob@3199
   213
 * \return the incremented value.
bob@3199
   214
 *
bob@3199
   215
 * \param ptr address of the data to increment.
bob@3199
   216
 *
bob@3199
   217
 */
bob@3199
   218
extern DECLSPEC Uint32 SDLCALL SDL_AtomicIncrementThenFetch32(Uint32 * ptr);
bob@3199
   219
/**
bob@3199
   220
 * \fn  Uint32 SDL_AtomicDecrementThenFetch32(Uint32 * ptr);
bob@3199
   221
 *
bob@3199
   222
 * \brief Subtract one from data pointed to by ptr and return the new value.
bob@3199
   223
 *
bob@3199
   224
 * \return The decremented value.
bob@3199
   225
 *
bob@3199
   226
 * \param ptr The address of the data to decrement.
bob@3199
   227
 *
bob@3199
   228
 */
bob@3199
   229
extern DECLSPEC Uint32 SDLCALL SDL_AtomicDecrementThenFetch32(Uint32 * ptr);
bob@3199
   230
/**
bob@3199
   231
 * \fn  Uint32 SDL_AtomicAddThenFetch32(Uint32 * ptr, Uint32 value);
bob@3199
   232
 *
bob@3199
   233
 * \brief Add value to the data pointed to by ptr and return result.
bob@3199
   234
 *
bob@3199
   235
 * \return The sum of *ptr and value.
bob@3199
   236
 *
bob@3199
   237
 * \param ptr The address of the data to be modified.
bob@3199
   238
 * \param value The value to be added.
bob@3199
   239
 *
bob@3199
   240
 */
bob@3199
   241
extern DECLSPEC Uint32 SDLCALL SDL_AtomicAddThenFetch32(Uint32 * ptr, Uint32 value);
bob@3199
   242
/**
bob@3199
   243
 * \fn  Uint32 SDL_AtomicSubtractThenFetch32(Uint32 * ptr, Uint32 value);
bob@3199
   244
 *
bob@3199
   245
 * \brief Subtract value from the data pointed to by ptr and return the result.
bob@3199
   246
 *
bob@3199
   247
 * \return the difference between *ptr and value.
bob@3199
   248
 *
bob@3199
   249
 * \param ptr The address of the data to be modified.
bob@3199
   250
 * \param value The value to be subtracted.
bob@3199
   251
 *
bob@3199
   252
 */
bob@3199
   253
extern DECLSPEC Uint32 SDLCALL SDL_AtomicSubtractThenFetch32(Uint32 * ptr, Uint32 value);
bob@3180
   254
bob@3199
   255
#ifdef SDL_HAS_64BIT_TYPE
bob@3180
   256
bob@3199
   257
extern DECLSPEC Uint64 SDLCALL SDL_AtomicExchange64(Uint64 * ptr, Uint64 value);
bob@3199
   258
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareThenSet64(Uint64 * ptr,
bob@3199
   259
                                                            Uint64 oldvalue, Uint64 newvalue);
bob@3199
   260
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTestThenSet64(Uint64 * ptr);
bob@3199
   261
extern DECLSPEC void SDLCALL SDL_AtomicClear64(Uint64 * ptr);
bob@3199
   262
extern DECLSPEC Uint64 SDLCALL SDL_AtomicFetchThenIncrement64(Uint64 * ptr);
bob@3199
   263
extern DECLSPEC Uint64 SDLCALL SDL_AtomicFetchThenDecrement64(Uint64 * ptr);
bob@3199
   264
extern DECLSPEC Uint64 SDLCALL SDL_AtomicFetchThenAdd64(Uint64 * ptr, Uint64 value);
bob@3199
   265
extern DECLSPEC Uint64 SDLCALL SDL_AtomicFetchThenSubtract64(Uint64 * ptr, Uint64 value);
bob@3199
   266
extern DECLSPEC Uint64 SDLCALL SDL_AtomicIncrementThenFetch64(Uint64 * ptr);
bob@3199
   267
extern DECLSPEC Uint64 SDLCALL SDL_AtomicDecrementThenFetch64(Uint64 * ptr);
bob@3199
   268
extern DECLSPEC Uint64 SDLCALL SDL_AtomicAddThenFetch64(Uint64 * ptr, Uint64 value);
bob@3199
   269
extern DECLSPEC Uint64 SDLCALL SDL_AtomicSubtractThenFetch64(Uint64 * ptr, Uint64 value);
bob@3199
   270
#endif
bob@3180
   271
bob@3180
   272
/* Ends C function definitions when using C++ */
bob@3180
   273
#ifdef __cplusplus
bob@3180
   274
/* *INDENT-OFF* */
bob@3180
   275
}
bob@3180
   276
/* *INDENT-ON* */
bob@3180
   277
#endif
bob@3180
   278
bob@3180
   279
#include "close_code.h"
bob@3180
   280
bob@3180
   281
#endif /* _SDL_atomic_h_ */
bob@3180
   282
bob@3180
   283
/* vi: set ts=4 sw=4 expandtab: */