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