include/SDL_atomic.h
author Ryan C. Gordon <icculus@icculus.org>
Tue, 15 Dec 2009 18:00:16 +0000
changeset 3567 fb9ea4b549c3
parent 3407 d3baf5ac4e37
child 3697 f7b03b6838cb
permissions -rw-r--r--
Added a warning comment to SDL_putenv().

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