include/SDL_atomic.h
author Sam Lantinga <slouken@libsdl.org>
Mon, 19 Oct 2009 13:31:58 +0000
changeset 3407 d3baf5ac4e37
parent 3261 72b542f34739
child 3697 f7b03b6838cb
permissions -rw-r--r--
Partial fix for bug #859

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