include/SDL_atomic.h
author Sam Lantinga <slouken@libsdl.org>
Mon, 07 Feb 2011 22:57:33 -0800
changeset 5225 5d01d426f2ea
parent 5136 1a47d41912ff
child 5262 b530ef003506
permissions -rw-r--r--
It's now possible to disable the fast atomic operations, at a huge performance penalty.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
     1
/*
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
     2
    SDL - Simple DirectMedia Layer
3697
f7b03b6838cb Fixed bug #926
Sam Lantinga <slouken@libsdl.org>
parents: 3407
diff changeset
     3
    Copyright (C) 1997-2010 Sam Lantinga
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
     4
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
     5
    This library is free software; you can redistribute it and/or
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
     6
    modify it under the terms of the GNU Lesser General Public
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
     7
    License as published by the Free Software Foundation; either
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
     8
    version 2.1 of the License, or (at your option) any later version.
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
     9
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    10
    This library is distributed in the hope that it will be useful,
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    13
    Lesser General Public License for more details.
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    14
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    15
    You should have received a copy of the GNU Lesser General Public
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    16
    License along with this library; if not, write to the Free Software
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    18
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    19
    Sam Lantinga
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    20
    slouken@libsdl.org
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    21
 */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    22
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    23
/**
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    24
 * \file SDL_atomic.h
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    25
 * 
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    26
 * Atomic operations.
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    27
 * 
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    28
 * IMPORTANT:
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    29
 * If you are not an expert in concurrent lockless programming, you should
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    30
 * only be using the atomic lock and reference counting functions in this
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    31
 * file.  In all other cases you should be protecting your data structures
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    32
 * with full mutexes.
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    33
 * 
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    34
 * The list of "safe" functions to use are:
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    35
 *  SDL_AtomicLock()
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    36
 *  SDL_AtomicUnlock()
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    37
 *  SDL_AtomicIncRef()
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    38
 *  SDL_AtomicDecRef()
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    39
 * 
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    40
 * Seriously, here be dragons!
5095
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
    41
 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    42
 *
5095
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
    43
 * You can find out a little more about lockless programming and the 
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
    44
 * subtle issues that can arise here:
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
    45
 * http://msdn.microsoft.com/en-us/library/ee418650%28v=vs.85%29.aspx
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
    46
 *
5098
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5097
diff changeset
    47
 * There's also lots of good information here:
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5097
diff changeset
    48
 * http://www.1024cores.net/home/lock-free-algorithms
470ede30189c Added a FIFO test to the atomic test suite.
Sam Lantinga <slouken@libsdl.org>
parents: 5097
diff changeset
    49
 *
5095
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
    50
 * These operations may or may not actually be implemented using
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    51
 * processor specific atomic operations. When possible they are
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    52
 * implemented as true processor specific atomic operations. When that
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    53
 * is not possible the are implemented using locks that *do* use the
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    54
 * available atomic operations.
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    55
 *
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    56
 * All of the atomic operations that modify memory are full memory barriers.
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    57
 */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    58
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    59
#ifndef _SDL_atomic_h_
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    60
#define _SDL_atomic_h_
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    61
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    62
#include "SDL_stdinc.h"
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    63
#include "SDL_platform.h"
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    64
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    65
#include "begin_code.h"
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    66
5073
1e94e68525d5 Fixed bug #1097
Sam Lantinga <slouken@libsdl.org>
parents: 5024
diff changeset
    67
/* Need to do this here because intrin.h has C++ code in it */
1e94e68525d5 Fixed bug #1097
Sam Lantinga <slouken@libsdl.org>
parents: 5024
diff changeset
    68
/* Visual Studio 2005 has a bug where intrin.h conflicts with winnt.h */
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5073
diff changeset
    69
#if defined(_MSC_VER) && (_MSC_VER >= 1500) && !defined(_WIN32_WCE)
5073
1e94e68525d5 Fixed bug #1097
Sam Lantinga <slouken@libsdl.org>
parents: 5024
diff changeset
    70
#include <intrin.h>
5096
e4301cde4de1 Fixed compile error on Visual C++
Sam Lantinga <slouken@libsdl.org>
parents: 5095
diff changeset
    71
#define HAVE_MSC_ATOMICS 1
5073
1e94e68525d5 Fixed bug #1097
Sam Lantinga <slouken@libsdl.org>
parents: 5024
diff changeset
    72
#endif
1e94e68525d5 Fixed bug #1097
Sam Lantinga <slouken@libsdl.org>
parents: 5024
diff changeset
    73
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    74
/* Set up for C function definitions, even when using C++ */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    75
#ifdef __cplusplus
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    76
/* *INDENT-OFF* */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    77
extern "C" {
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    78
/* *INDENT-ON* */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    79
#endif
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    80
3261
72b542f34739 The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
Bob Pendleton <bob@pendleton.com>
parents: 3237
diff changeset
    81
/**
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    82
 * \name SDL AtomicLock
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    83
 * 
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    84
 * The atomic locks are efficient spinlocks using CPU instructions,
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    85
 * but are vulnerable to starvation and can spin forever if a thread
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    86
 * holding a lock has been terminated.  For this reason you should
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    87
 * minimize the code executed inside an atomic lock and never do
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    88
 * expensive things like API or system calls while holding them.
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    89
 *
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    90
 * The atomic locks are not safe to lock recursively.
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    91
 *
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    92
 * Porting Note:
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    93
 * The spin lock functions and type are required and can not be
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    94
 * emulated because they are used in the atomic emulation code.
3261
72b542f34739 The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
Bob Pendleton <bob@pendleton.com>
parents: 3237
diff changeset
    95
 */
3407
d3baf5ac4e37 Partial fix for bug #859
Sam Lantinga <slouken@libsdl.org>
parents: 3261
diff changeset
    96
/*@{*/
3261
72b542f34739 The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
Bob Pendleton <bob@pendleton.com>
parents: 3237
diff changeset
    97
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
    98
typedef int SDL_SpinLock;
3261
72b542f34739 The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
Bob Pendleton <bob@pendleton.com>
parents: 3237
diff changeset
    99
72b542f34739 The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
Bob Pendleton <bob@pendleton.com>
parents: 3237
diff changeset
   100
/**
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   101
 * \brief Try to lock a spin lock by setting it to a non-zero value.
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   102
 * 
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   103
 * \param lock Points to the lock.
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   104
 *
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   105
 * \return SDL_TRUE if the lock succeeded, SDL_FALSE if the lock is already held.
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   106
 */
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   107
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTryLock(SDL_SpinLock *lock);
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   108
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   109
/**
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   110
 * \brief Lock a spin lock by setting it to a non-zero value.
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   111
 * 
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   112
 * \param lock Points to the lock.
3261
72b542f34739 The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
Bob Pendleton <bob@pendleton.com>
parents: 3237
diff changeset
   113
 */
72b542f34739 The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
Bob Pendleton <bob@pendleton.com>
parents: 3237
diff changeset
   114
extern DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock);
72b542f34739 The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
Bob Pendleton <bob@pendleton.com>
parents: 3237
diff changeset
   115
72b542f34739 The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
Bob Pendleton <bob@pendleton.com>
parents: 3237
diff changeset
   116
/**
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   117
 * \brief Unlock a spin lock by setting it to 0. Always returns immediately
3261
72b542f34739 The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
Bob Pendleton <bob@pendleton.com>
parents: 3237
diff changeset
   118
 *
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   119
 * \param lock Points to the lock.
3261
72b542f34739 The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
Bob Pendleton <bob@pendleton.com>
parents: 3237
diff changeset
   120
 */
72b542f34739 The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
Bob Pendleton <bob@pendleton.com>
parents: 3237
diff changeset
   121
extern DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock);
72b542f34739 The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
Bob Pendleton <bob@pendleton.com>
parents: 3237
diff changeset
   122
3407
d3baf5ac4e37 Partial fix for bug #859
Sam Lantinga <slouken@libsdl.org>
parents: 3261
diff changeset
   123
/*@}*//*SDL AtomicLock*/
3202
3aa519a5c676 I've made so many changes I don't dare continue until I check the current stuff in.
Bob Pendleton <bob@pendleton.com>
parents: 3199
diff changeset
   124
5095
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   125
5135
aee04c635ec6 Tweak for documentation
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   126
/**
aee04c635ec6 Tweak for documentation
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   127
 * The compiler barrier prevents the compiler from reordering
aee04c635ec6 Tweak for documentation
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   128
 * reads and writes to globally visible variables across the call.
aee04c635ec6 Tweak for documentation
Sam Lantinga <slouken@libsdl.org>
parents: 5098
diff changeset
   129
 */
5095
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   130
#ifdef _MSC_VER
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   131
void _ReadWriteBarrier(void);
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   132
#pragma intrinsic(_ReadWriteBarrier)
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   133
#define SDL_CompilerBarrier()   _ReadWriteBarrier()
5097
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   134
#elif defined(__GNUC__)
5095
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   135
#define SDL_CompilerBarrier()   __asm__ __volatile__ ("" : : : "memory")
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   136
#else
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   137
#define SDL_CompilerBarrier()   \
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   138
({ SDL_SpinLock _tmp = 0; SDL_AtomicLock(&_tmp); SDL_AtomicUnlock(&_tmp); })
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   139
#endif
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   140
5006
8e8876e4aec6 Include windows.h in SDL_atomic.h by default, but don't include the atomic API in SDL.h
Sam Lantinga <slouken@libsdl.org>
parents: 5005
diff changeset
   141
/* Platform specific optimized versions of the atomic functions,
8e8876e4aec6 Include windows.h in SDL_atomic.h by default, but don't include the atomic API in SDL.h
Sam Lantinga <slouken@libsdl.org>
parents: 5005
diff changeset
   142
 * you can disable these by defining SDL_DISABLE_ATOMIC_INLINE
8e8876e4aec6 Include windows.h in SDL_atomic.h by default, but don't include the atomic API in SDL.h
Sam Lantinga <slouken@libsdl.org>
parents: 5005
diff changeset
   143
 */
5225
5d01d426f2ea It's now possible to disable the fast atomic operations, at a huge performance penalty.
Sam Lantinga <slouken@libsdl.org>
parents: 5136
diff changeset
   144
#if SDL_ATOMIC_DISABLED
5d01d426f2ea It's now possible to disable the fast atomic operations, at a huge performance penalty.
Sam Lantinga <slouken@libsdl.org>
parents: 5136
diff changeset
   145
#define SDL_DISABLE_ATOMIC_INLINE
5d01d426f2ea It's now possible to disable the fast atomic operations, at a huge performance penalty.
Sam Lantinga <slouken@libsdl.org>
parents: 5136
diff changeset
   146
#endif
5006
8e8876e4aec6 Include windows.h in SDL_atomic.h by default, but don't include the atomic API in SDL.h
Sam Lantinga <slouken@libsdl.org>
parents: 5005
diff changeset
   147
#ifndef SDL_DISABLE_ATOMIC_INLINE
8e8876e4aec6 Include windows.h in SDL_atomic.h by default, but don't include the atomic API in SDL.h
Sam Lantinga <slouken@libsdl.org>
parents: 5005
diff changeset
   148
5097
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   149
#ifdef HAVE_MSC_ATOMICS
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   150
5016
cc879bc6c4cf Fixed compiler warnings using Windows intrinsics
Sam Lantinga <slouken@libsdl.org>
parents: 5012
diff changeset
   151
#define SDL_AtomicSet(a, v)     _InterlockedExchange((long*)&(a)->value, (v))
cc879bc6c4cf Fixed compiler warnings using Windows intrinsics
Sam Lantinga <slouken@libsdl.org>
parents: 5012
diff changeset
   152
#define SDL_AtomicAdd(a, v)     _InterlockedExchangeAdd((long*)&(a)->value, (v))
cc879bc6c4cf Fixed compiler warnings using Windows intrinsics
Sam Lantinga <slouken@libsdl.org>
parents: 5012
diff changeset
   153
#define SDL_AtomicCAS(a, oldval, newval) (_InterlockedCompareExchange((long*)&(a)->value, (newval), (oldval)) == (oldval))
5095
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   154
#define SDL_AtomicSetPtr(a, v)  _InterlockedExchangePointer((a), (v))
5012
2e282002bac3 Use compiler intrinsics, where available
Sam Lantinga <slouken@libsdl.org>
parents: 5006
diff changeset
   155
#if _M_IX86
2e282002bac3 Use compiler intrinsics, where available
Sam Lantinga <slouken@libsdl.org>
parents: 5006
diff changeset
   156
#define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchange((long*)(a), (long)(newval), (long)(oldval)) == (long)(oldval))
2e282002bac3 Use compiler intrinsics, where available
Sam Lantinga <slouken@libsdl.org>
parents: 5006
diff changeset
   157
#else
2e282002bac3 Use compiler intrinsics, where available
Sam Lantinga <slouken@libsdl.org>
parents: 5006
diff changeset
   158
#define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchangePointer((a), (newval), (oldval)) == (oldval))
2e282002bac3 Use compiler intrinsics, where available
Sam Lantinga <slouken@libsdl.org>
parents: 5006
diff changeset
   159
#endif
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   160
5097
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   161
#elif defined(__MACOSX__)
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   162
#include <libkern/OSAtomic.h>
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   163
5095
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   164
#define SDL_AtomicCAS(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((oldval), (newval), &(a)->value)
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   165
#if SIZEOF_VOIDP == 4
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   166
#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((int32_t)(oldval), (int32_t)(newval), (int32_t*)(a))
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   167
#elif SIZEOF_VOIDP == 8
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   168
#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap64Barrier((int64_t)(oldval), (int64_t)(newval), (int64_t*)(a))
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   169
#endif
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   170
5097
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   171
#elif defined(HAVE_GCC_ATOMICS)
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   172
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   173
#define SDL_AtomicSet(a, v)     __sync_lock_test_and_set(&(a)->value, v)
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   174
#define SDL_AtomicAdd(a, v)     __sync_fetch_and_add(&(a)->value, v)
5095
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   175
#define SDL_AtomicSetPtr(a, v)  __sync_lock_test_and_set(a, v)
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   176
#define SDL_AtomicCAS(a, oldval, newval) __sync_bool_compare_and_swap(&(a)->value, oldval, newval)
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   177
#define SDL_AtomicCASPtr(a, oldval, newval) __sync_bool_compare_and_swap(a, oldval, newval)
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   178
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   179
#endif
3407
d3baf5ac4e37 Partial fix for bug #859
Sam Lantinga <slouken@libsdl.org>
parents: 3261
diff changeset
   180
5006
8e8876e4aec6 Include windows.h in SDL_atomic.h by default, but don't include the atomic API in SDL.h
Sam Lantinga <slouken@libsdl.org>
parents: 5005
diff changeset
   181
#endif /* !SDL_DISABLE_ATOMIC_INLINE */
8e8876e4aec6 Include windows.h in SDL_atomic.h by default, but don't include the atomic API in SDL.h
Sam Lantinga <slouken@libsdl.org>
parents: 5005
diff changeset
   182
8e8876e4aec6 Include windows.h in SDL_atomic.h by default, but don't include the atomic API in SDL.h
Sam Lantinga <slouken@libsdl.org>
parents: 5005
diff changeset
   183
3407
d3baf5ac4e37 Partial fix for bug #859
Sam Lantinga <slouken@libsdl.org>
parents: 3261
diff changeset
   184
/**
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   185
 * \brief A type representing an atomic integer value.  It is a struct
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   186
 *        so people don't accidentally use numeric operations on it.
3199
3e1bf2b8bd81 This check in updates SDL_atomic.h to reflect the new set of atomic operations in 32 and 64 bit form.
Bob Pendleton <bob@pendleton.com>
parents: 3187
diff changeset
   187
 */
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   188
#ifndef SDL_atomic_t_defined
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   189
typedef struct { int value; } SDL_atomic_t;
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   190
#endif
3237
916f396fe65d Start of redesign, getting rid of 8 and 16 bit operations in .h and test files.
Bob Pendleton <bob@pendleton.com>
parents: 3216
diff changeset
   191
3199
3e1bf2b8bd81 This check in updates SDL_atomic.h to reflect the new set of atomic operations in 32 and 64 bit form.
Bob Pendleton <bob@pendleton.com>
parents: 3187
diff changeset
   192
/**
5097
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   193
 * \brief Set an atomic variable to a new value if it is currently an old value.
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   194
 *
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   195
 * \return SDL_TRUE if the atomic variable was set, SDL_FALSE otherwise.
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   196
 *
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   197
 * \note If you don't know what this function is for, you shouldn't use it!
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   198
*/
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   199
#ifndef SDL_AtomicCAS
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   200
#define SDL_AtomicCAS SDL_AtomicCAS_
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   201
#endif
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   202
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS_(SDL_atomic_t *a, int oldval, int newval);
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   203
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   204
/**
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   205
 * \brief Set an atomic variable to a value.
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   206
 *
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   207
 * \return The previous value of the atomic variable.
3199
3e1bf2b8bd81 This check in updates SDL_atomic.h to reflect the new set of atomic operations in 32 and 64 bit form.
Bob Pendleton <bob@pendleton.com>
parents: 3187
diff changeset
   208
 */
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   209
#ifndef SDL_AtomicSet
5097
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   210
static __inline__ int SDL_AtomicSet(SDL_atomic_t *a, int v)
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   211
{
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   212
    int value;
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   213
    do {
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   214
        value = a->value;
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   215
    } while (!SDL_AtomicCAS(a, value, v));
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   216
    return value;
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   217
}
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   218
#endif
3237
916f396fe65d Start of redesign, getting rid of 8 and 16 bit operations in .h and test files.
Bob Pendleton <bob@pendleton.com>
parents: 3216
diff changeset
   219
3199
3e1bf2b8bd81 This check in updates SDL_atomic.h to reflect the new set of atomic operations in 32 and 64 bit form.
Bob Pendleton <bob@pendleton.com>
parents: 3187
diff changeset
   220
/**
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   221
 * \brief Get the value of an atomic variable
3199
3e1bf2b8bd81 This check in updates SDL_atomic.h to reflect the new set of atomic operations in 32 and 64 bit form.
Bob Pendleton <bob@pendleton.com>
parents: 3187
diff changeset
   222
 */
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   223
#ifndef SDL_AtomicGet
5097
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   224
static __inline__ int SDL_AtomicGet(SDL_atomic_t *a)
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   225
{
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   226
    int value = a->value;
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   227
    SDL_CompilerBarrier();
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   228
    return value;
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   229
}
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   230
#endif
3237
916f396fe65d Start of redesign, getting rid of 8 and 16 bit operations in .h and test files.
Bob Pendleton <bob@pendleton.com>
parents: 3216
diff changeset
   231
3199
3e1bf2b8bd81 This check in updates SDL_atomic.h to reflect the new set of atomic operations in 32 and 64 bit form.
Bob Pendleton <bob@pendleton.com>
parents: 3187
diff changeset
   232
/**
5095
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   233
 * \brief Add to an atomic variable.
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   234
 *
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   235
 * \return The previous value of the atomic variable.
5095
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   236
 *
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   237
 * \note This same style can be used for any number operation
3199
3e1bf2b8bd81 This check in updates SDL_atomic.h to reflect the new set of atomic operations in 32 and 64 bit form.
Bob Pendleton <bob@pendleton.com>
parents: 3187
diff changeset
   238
 */
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   239
#ifndef SDL_AtomicAdd
5097
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   240
static __inline__ int SDL_AtomicAdd(SDL_atomic_t *a, int v)
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   241
{
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   242
    int value;
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   243
    do {
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   244
        value = a->value;
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   245
    } while (!SDL_AtomicCAS(a, value, (value + v)));
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   246
    return value;
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   247
}
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   248
#endif
3237
916f396fe65d Start of redesign, getting rid of 8 and 16 bit operations in .h and test files.
Bob Pendleton <bob@pendleton.com>
parents: 3216
diff changeset
   249
3199
3e1bf2b8bd81 This check in updates SDL_atomic.h to reflect the new set of atomic operations in 32 and 64 bit form.
Bob Pendleton <bob@pendleton.com>
parents: 3187
diff changeset
   250
/**
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   251
 * \brief Increment an atomic variable used as a reference count.
3199
3e1bf2b8bd81 This check in updates SDL_atomic.h to reflect the new set of atomic operations in 32 and 64 bit form.
Bob Pendleton <bob@pendleton.com>
parents: 3187
diff changeset
   252
 */
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   253
#ifndef SDL_AtomicIncRef
5095
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   254
#define SDL_AtomicIncRef(a)    SDL_AtomicAdd(a, 1)
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   255
#endif
3237
916f396fe65d Start of redesign, getting rid of 8 and 16 bit operations in .h and test files.
Bob Pendleton <bob@pendleton.com>
parents: 3216
diff changeset
   256
3199
3e1bf2b8bd81 This check in updates SDL_atomic.h to reflect the new set of atomic operations in 32 and 64 bit form.
Bob Pendleton <bob@pendleton.com>
parents: 3187
diff changeset
   257
/**
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   258
 * \brief Decrement an atomic variable used as a reference count.
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   259
 *
5095
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   260
 * \return SDL_TRUE if the variable reached zero after decrementing,
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   261
 *         SDL_FALSE otherwise
3199
3e1bf2b8bd81 This check in updates SDL_atomic.h to reflect the new set of atomic operations in 32 and 64 bit form.
Bob Pendleton <bob@pendleton.com>
parents: 3187
diff changeset
   262
 */
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   263
#ifndef SDL_AtomicDecRef
5095
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   264
#define SDL_AtomicDecRef(a)    (SDL_AtomicAdd(a, -1) == 1)
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   265
#endif
3237
916f396fe65d Start of redesign, getting rid of 8 and 16 bit operations in .h and test files.
Bob Pendleton <bob@pendleton.com>
parents: 3216
diff changeset
   266
3199
3e1bf2b8bd81 This check in updates SDL_atomic.h to reflect the new set of atomic operations in 32 and 64 bit form.
Bob Pendleton <bob@pendleton.com>
parents: 3187
diff changeset
   267
/**
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   268
 * \brief Set a pointer to a new value if it is currently an old value.
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   269
 *
5004
0c72ae7b7cb2 Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Sam Lantinga <slouken@libsdl.org>
parents: 5003
diff changeset
   270
 * \return SDL_TRUE if the pointer was set, SDL_FALSE otherwise.
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   271
 *
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   272
 * \note If you don't know what this function is for, you shouldn't use it!
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   273
*/
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   274
#ifndef SDL_AtomicCASPtr
5095
dceec93471e7 Improvements based on feedback from Anthony Williams
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   275
#define SDL_AtomicCASPtr SDL_AtomicCASPtr_
5003
3a95a2b93eb3 Updated the atomic API for better use cases
Sam Lantinga <slouken@libsdl.org>
parents: 4474
diff changeset
   276
#endif
5136
1a47d41912ff Spacing for documentation tweaks
Sam Lantinga <slouken@libsdl.org>
parents: 5135
diff changeset
   277
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr_(void* *a, void *oldval, void *newval);
3407
d3baf5ac4e37 Partial fix for bug #859
Sam Lantinga <slouken@libsdl.org>
parents: 3261
diff changeset
   278
5097
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   279
/**
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   280
 * \brief Set a pointer to a value atomically.
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   281
 *
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   282
 * \return The previous value of the pointer.
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   283
 */
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   284
#ifndef SDL_AtomicSetPtr
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   285
static __inline__ void* SDL_AtomicSetPtr(void* *a, void* v)
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   286
{
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   287
    void* value;
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   288
    do {
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   289
        value = *a;
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   290
    } while (!SDL_AtomicCASPtr(a, value, v));
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   291
    return value;
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   292
}
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   293
#endif
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   294
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   295
/**
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   296
 * \brief Get the value of a pointer atomically.
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   297
 */
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   298
#ifndef SDL_AtomicGetPtr
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   299
static __inline__ void* SDL_AtomicGetPtr(void* *a)
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   300
{
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   301
    void* value = *a;
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   302
    SDL_CompilerBarrier();
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   303
    return value;
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   304
}
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   305
#endif
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   306
b938ad843e52 More fixes for compilation on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5096
diff changeset
   307
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   308
/* Ends C function definitions when using C++ */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   309
#ifdef __cplusplus
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   310
/* *INDENT-OFF* */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   311
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   312
/* *INDENT-ON* */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   313
#endif
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   314
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   315
#include "close_code.h"
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   316
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   317
#endif /* _SDL_atomic_h_ */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   318
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   319
/* vi: set ts=4 sw=4 expandtab: */