include/SDL_atomic.h
author Sam Lantinga <slouken@libsdl.org>
Wed, 10 Jun 2009 13:34:20 +0000
changeset 3186 51750b7a966f
parent 3181 030899df1af5
child 3187 e041d2c603fe
permissions -rw-r--r--
indent
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
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
     3
    Copyright (C) 1997-2006 Sam Lantinga
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
/**
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    24
 * \file SDL_atomic.h
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    25
 *
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    26
 * Atomic int and pointer magic
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    27
 */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    28
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    29
#ifndef _SDL_atomic_h_
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    30
#define _SDL_atomic_h_
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    31
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    32
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    33
#include "SDL_stdinc.h"
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    34
#include "SDL_platform.h"
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    35
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    36
#include "begin_code.h"
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    37
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    38
/* 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
    39
#ifdef __cplusplus
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    40
/* *INDENT-OFF* */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    41
extern "C" {
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    42
/* *INDENT-ON* */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    43
#endif
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    44
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    45
#if defined(__GNUC__) && (defined(i386) || defined(__i386__)  || defined(__x86_64__))
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    46
static __inline__ void
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    47
SDL_atomic_int_add(volatile int *atomic, int value)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    48
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    49
    __asm__ __volatile__("lock;" "addl %1, %0":"=m"(*atomic)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    50
                         :"ir"(value), "m"(*atomic));
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    51
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    52
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    53
static __inline__ int
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    54
SDL_atomic_int_xchg_add(volatile int *atomic, int value)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    55
{
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    56
    int rv;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    57
    __asm__ __volatile__("lock;" "xaddl %0, %1":"=r"(rv), "=m"(*atomic)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    58
                         :"0"(value), "m"(*atomic));
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    59
    return rv;
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    60
}
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
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    63
SDL_atomic_int_cmp_xchg(volatile int *atomic, int oldvalue, int newvalue)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    64
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    65
    int rv;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    66
    __asm__ __volatile__("lock;" "cmpxchgl %2, %1":"=a"(rv), "=m"(*atomic)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    67
                         :"r"(newvalue), "m"(*atomic), "0"(oldvalue));
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    68
    return (SDL_bool) (rv == oldvalue);
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    69
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    70
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    71
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    72
SDL_atomic_ptr_cmp_xchg(volatile void **atomic, void *oldvalue,
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    73
                        void *newvalue)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    74
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    75
    void *rv;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    76
    __asm__ __volatile__("lock;"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    77
# if defined(__x86_64__)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    78
                         "cmpxchgq %q2, %1"
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    79
# else
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    80
                         "cmpxchgl %2, %1"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    81
# endif
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    82
                         :"=a"(rv), "=m"(*atomic)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    83
                         :"r"(newvalue), "m"(*atomic), "0"(oldvalue));
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
    84
    return (SDL_bool) (rv == oldvalue);
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    85
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    86
#elif defined(__GNUC__) && defined(__alpha__)
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    87
# define ATOMIC_MEMORY_BARRIER (__asm__ __volatile__ ("mb" : : : "memory"))
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    88
# define ATOMIC_INT_CMP_XCHG(atomic,value)              \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    89
  ({                                                    \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    90
    int rv,prev;                                        \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    91
    __asm__ __volatile__("   mb\n"                      \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    92
                         "1: ldl_l   %0,%2\n"           \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    93
                         "   cmpeq   %0,%3,%1\n"        \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    94
                         "   beq     %1,2f\n"           \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    95
                         "   mov     %4,%1\n"           \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    96
                         "   stl_c   %1,%2\n"           \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    97
                         "   beq     %1,1b\n"           \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    98
                         "   mb\n"                      \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
    99
                         "2:"                           \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   100
                         : "=&r" (prev),                \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   101
                           "=&r" (rv)                   \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   102
                         : "m" (*(atomic)),             \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   103
                           "Ir" (oldvalue),             \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   104
                           "Ir" (newvalue)              \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   105
                         : "memory");                   \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   106
    (rv != 0);                                          \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   107
  })
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   108
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   109
# if (SIZEOF_VOIDP == 4)
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   110
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   111
SDL_atomic_ptr_cmp_xchg(volatile void **atomic, void *oldvalue,
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   112
                        void *newvalue)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   113
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   114
    int rv;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   115
    void *prev;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   116
    __asm__ __volatile__("   mb\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   117
                         "1: ldl_l %0,%2\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   118
                         "   cmpeq %0,%3,%1\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   119
                         "   beq   $1,2f\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   120
                         "   mov   %4,%1\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   121
                         "   stl_c %1,%2\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   122
                         "   beq   %1,1b\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   123
                         "   mb\n" "2:":"=&r"(prev), "=&r"(rv)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   124
                         :"m"(*atomic), "Ir"(oldvalue), "Ir"(newvalue)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   125
                         :"memory");
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   126
    return (SDL_bool) (rv != 0);
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   127
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   128
# elif (SIZEOF_VOIDP == 8)
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   129
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   130
SDL_atomic_ptr_cmp_xchg(volatile void **atomic, void *oldvalue,
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   131
                        void *newvalue)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   132
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   133
    int rv;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   134
    void *prev;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   135
    __asm__ __volatile__("   mb\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   136
                         "1: ldq_l %0,%2\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   137
                         "   cmpeq %0,%3,%1\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   138
                         "   beq   %1,2f\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   139
                         "   mov   %4,%1\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   140
                         "   stq_c %1,%2\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   141
                         "   beq   %1,1b\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   142
                         "   mb\n" "2:":"=&r"(prev), "=&r"(rv)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   143
                         :"m"(*atomic), "Ir"(oldvalue), "Ir"(newvalue)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   144
                         :"memory");
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   145
    return (SDL_bool) (rv != 0);
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   146
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   147
# else
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   148
#  error "Your system has an unsupported pointer size"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   149
# endif /* SIZEOF_VOIDP */
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   150
#elif defined(__GNUC__) && defined(__sparc__)
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   151
# define ATOMIC_MEMORY_BARRIER                                          \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   152
  (__asm__ __volatile__("membar #LoadLoad | #LoadStore"                 \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   153
                        " | #StoreLoad | #StoreStore" : : : "memory"))
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   154
# define ATOMIC_INT_CMP_XCHG(atomic,oldvalue,newvalue)                  \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   155
  ({                                                                    \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   156
    int rv;                                                             \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   157
    __asm__ __volatile__("cas [%4], %2, %0"                             \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   158
                         : "=r" (rv), "=m" (*(atomic))                  \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   159
                         : "r" (oldvalue), "m" (*(atomic)),             \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   160
                         "r" (atomic), "0" (newvalue));                 \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   161
    rv == oldvalue;                                                     \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   162
  })
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   163
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   164
# if (SIZEOF_VOIDP == 4)
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   165
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   166
SDL_atomic_ptr_cmp_xchg(volatile void **atomic, void *oldvalue,
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   167
                        void *newvalue)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   168
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   169
    void *rv;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   170
    __asm__ __volatile__("cas [%4], %2, %0":"=r"(rv), "=m"(*atomic)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   171
                         :"r"(oldvalue),
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   172
                         "m"(*atomic), "r"(atomic), "0"(newvalue));
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   173
    return (SDL_bool) (rv == oldvalue);
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   174
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   175
# elif (SIZEOF_VOIDP == 8)
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   176
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   177
SDL_atomic_ptr_cmp_xchg(volatile void **atomic, void *oldvalue,
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   178
                        void *newvalue)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   179
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   180
    void *rv;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   181
    void **a = atomic;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   182
    __asm__ __volatile__("casx [%4], %2, %0":"=r"(rv), "=m"(*a)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   183
                         :"r"(oldvalue), "m"(*a), "r"(a), "0"(newvalue));
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   184
    return (SDL_bool) (rv == oldvalue);
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   185
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   186
# else
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   187
#  error "Your system has an unsupported pointer size"
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   188
# endif /* SIZEOF_VOIDP */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   189
#elif defined(__GNUC__) && (defined(__POWERPC__) || defined(__powerpc__) || defined(__ppc__) || defined(_M_PPC))
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   190
# define ATOMIC_MEMORY_BARRIER \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   191
  (__asm__ __volatile__ ("sync" : : : "memory"))
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   192
static __inline__ void
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   193
SDL_atomic_int_add(volatile int *atomic, int value)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   194
{
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   195
    int rv, tmp;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   196
    __asm__ __volatile__("1: lwarx   %0,  0, %3\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   197
                         "   add     %1, %0, %4\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   198
                         "   stwcx.  %1,  0, %3\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   199
                         "   bne-    1b":"=&b"(rv), "=&r"(tmp), "=m"(*atomic)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   200
                         :"b"(atomic), "r"(value), "m"(*atomic)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   201
                         :"cr0", "memory");
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   202
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   203
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   204
static __inline__ int
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   205
SDL_atomic_int_xchg_add(volatile int *atomic, int value)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   206
{
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   207
    int rv, tmp;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   208
    __asm__ __volatile__("1: lwarx  %0, 0, %3\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   209
                         "   add    %1, %0, %4\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   210
                         "   stwcx. %1, 0, %3\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   211
                         "   bne-   1b":"=&b"(rv), "=&r"(tmp), "=m"(*atomic)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   212
                         :"b"(atomic), "r"(value), "m"(*atomic)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   213
                         :"cr0", "memory");
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   214
    return rv;
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   215
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   216
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   217
# if (SIZEOF_VOIDP == 4)
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   218
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   219
SDL_atomic_int_cmp_xchg(volatile int *atomic, int oldvalue, int newvalue)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   220
{
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   221
    int rv;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   222
    __asm__ __volatile__("   sync\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   223
                         "1: lwarx   %0, 0, %1\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   224
                         "   subf.   %0, %2, %0\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   225
                         "   bne     2f\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   226
                         "   stwcx.  %3, 0, %1\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   227
                         "   bne-    1b\n" "2: isync":"=&r"(rv)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   228
                         :"b"(atomic), "r"(oldvalue), "r":"cr0", "memory");
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   229
    return (SDL_bool) (rv == 0);
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   230
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   231
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   232
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   233
SDL_atomic_ptr_cmp_xchg(volatile void **atomic, void *oldvalue,
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   234
                        void *newvalue)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   235
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   236
    void *rv;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   237
    __asm__ __volatile__("sync\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   238
                         "1: lwarx  %0,  0, %1\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   239
                         "   subf.  %0, %2, %0\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   240
                         "   bne    2f\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   241
                         "   stwcx. %3,  0, %1\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   242
                         "   bne-   1b\n" "2: isync":"=&r"(rv)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   243
                         :"b"(atomic), "r"(oldvalue), "r"(newvalue)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   244
                         :"cr0", "memory");
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   245
    return (SDL_bool) (rv == 0);
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   246
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   247
# elif (SIZEOF_VOIDP == 8)
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   248
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   249
SDL_atomic_int_cmp_xchg(volatile int *atomic, int oldvalue, int newvalue)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   250
{
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   251
    int rv;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   252
    __asm__ __volatile__("   sync\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   253
                         "1: lwarx   %0,  0, %1\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   254
                         "   extsw   %0, %0\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   255
                         "   subf.   %0, %2, %0\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   256
                         "   bne     2f\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   257
                         "   stwcx.  %3,  0, %1\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   258
                         "   bne-    1b\n" "2: isync":"=&r"(rv)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   259
                         :"b"(atomic), "r"(oldvalue), "r":"cr0", "memory");
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   260
    return (SDL_bool) (rv == 0);
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   261
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   262
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   263
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   264
SDL_atomic_ptr_cmp_xchg(volatile void **atomic, void *oldvalue,
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   265
                        void *newvalue)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   266
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   267
    void *rv;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   268
    __asm__ __volatile__("sync\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   269
                         "1: ldarx  %0,  0, %1\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   270
                         "   subf.  %0, %2, %0\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   271
                         "   bne    2f\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   272
                         "   stdcx. %3,  0, %1\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   273
                         "   bne-   1b\n" "2: isync":"=&r"(rv)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   274
                         :"b"(atomic), "r"(oldvalue), "r"(newvalue)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   275
                         :"cr0", "memory");
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   276
    return (SDL_bool) (rv == 0);
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   277
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   278
# else
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   279
#  error "Your system has an unsupported pointer size"
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   280
# endif /* SIZEOF_VOIDP */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   281
#elif defined(__GNUC__) && (defined(__IA64__) || defined(__ia64__))
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   282
# define ATOMIC_MEMORY_BARRIER (__sync_synchronize())
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   283
# define SDL_atomic_int_xchg_add(atomic, value)     \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   284
  (__sync_fetch_and_add((atomic),(value)))
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   285
# define SDL_atomic_int_add(atomic, value)                  \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   286
  ((void)__sync_fetch_and_add((atomic),(value)))
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   287
# define SDL_atomic_int_cmp_xchg(atomic,oldvalue,newvalue)  \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   288
  (__sync_bool_compare_and_swap((atomic),(oldvalue),(newvalue)))
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   289
# define SDL_atomic_ptr_cmp_xchg(atomic,oldvalue,newvalue)              \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   290
  (__sync_bool_compare_and_swap((long*)(atomic),(long)(oldvalue),(long)(newvalue)))
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   291
#elif defined(__GNUC__) && defined(__LINUX__) && (defined(__mips__) || defined(__MIPS__))
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   292
static __inline__ int
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   293
SDL_atomic_int_xchg_add(volatile int *atomic, int value)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   294
{
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   295
    int rv, tmp;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   296
    __asm__ __volatile__("1:              \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   297
                         ".set  push      \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   298
                         ".set  mips2     \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   299
                         "ll    %0,%3     \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   300
                         "addu  %1,%4,%0  \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   301
                         "sc    %1,%2     \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   302
                         ".set  pop       \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   303
                         "beqz  %1,1b     \n":"=&r"(rv),
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   304
                         "=&r"(tmp), "=m"(*atomic)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   305
                         :"m"(*atomic), "r"(value)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   306
                         :"memory");
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   307
    return rv;
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   308
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   309
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   310
static __inline__ void
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   311
SDL_atomic_int_add(volatile int *atomic, int value)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   312
{
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   313
    int rv;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   314
    __asm__ __volatile__("1:               \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   315
                         ".set  push       \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   316
                         ".set  mips2      \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   317
                         "ll    %0,%2      \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   318
                         "addu  %0,%3,%0   \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   319
                         "sc    %0,%1      \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   320
                         ".set  pop        \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   321
                         "beqz  %0,1b      \n":"=&r"(rv), "=m"(*atomic)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   322
                         :"m"(*atomic), "r"(value)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   323
                         :"memory");
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   324
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   325
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   326
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   327
SDL_atomic_int_cmp_xchg(volatile int *atomic, int oldvalue, int newvalue)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   328
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   329
    int rv;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   330
    __asm__ __volatile__("     .set push        \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   331
                         "     .set noat        \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   332
                         "     .set mips3       \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   333
                         "1:   ll   %0, %2      \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   334
                         "     bne  %0, %z3, 2f \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   335
                         "     .set mips0       \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   336
                         "     move $1, %z4     \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   337
                         "     .set mips3       \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   338
                         "     sc   $1, %1      \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   339
                         "     beqz $1, 1b      \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   340
                         "     sync             \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   341
                         "2:                    \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   342
                         "     .set pop         \n":"=&r"(rv), "=R"(*atomic)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   343
                         :"R"(*atomic), "Jr"(oldvalue), "Jr"(newvalue)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   344
                         :"memory");
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   345
    return (SDL_bool) rv;
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   346
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   347
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   348
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   349
SDL_atomic_ptr_cmp_xchg(volatile void **atomic, void *oldvalue,
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   350
                        void *newvalue)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   351
{
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   352
    int rv;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   353
    __asm__ __volatile__("     .set push        \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   354
                         "     .set noat        \n" "     .set mips3       \n"
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   355
# if defined(__mips64)
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   356
                         "1:   lld  %0, %2      \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   357
# else
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   358
                         "1:   ll   %0, %2      \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   359
# endif
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   360
                         "     bne  %0, %z3, 2f \n" "     move $1, %z4     \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   361
# if defined(__mips64)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   362
                         "     sc   $1, %1      \n"
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   363
# else
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   364
                         "     scd  $1, %1      \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   365
# endif
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   366
                         "     beqz $1, 1b      \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   367
                         "     sync             \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   368
                         "2:                    \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   369
                         "     .set pop         \n":"=&r"(rv), "=R"(*atomic)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   370
                         :"R"(*atomic), "Jr"(oldvalue), "Jr"(newvalue)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   371
                         :"memory");
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   372
    return (SDL_bool) rv;
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   373
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   374
#elif defined(__GNUC__) && defined(__m68k__)
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   375
static __inline__ int
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   376
SDL_atomic_int_xchg_add(volatile int *atomic, int value)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   377
{
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   378
    int rv = *atomic;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   379
    int tmp;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   380
    __asm__ __volatile__("1: move%.l %0,%1    \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   381
                         "   add%.l  %2,%1    \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   382
                         "   cas%.l  %0,%1,%3 \n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   383
                         "   jbne    1b       \n":"=d"(rv), "=&d"(tmp)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   384
                         :"d"(value), "m"(*atomic), "0"(rv)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   385
                         :"memory");
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   386
    return (SDL_bool) rv;
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   387
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   388
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   389
static __inline__ void
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   390
SDL_atomic_int_add(volatile int *atomic, int value)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   391
{
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   392
    __asm__ __volatile__("add%.l %0,%1"::"id"(value), "m"(*atomic)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   393
                         :"memory");
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   394
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   395
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   396
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   397
SDL_atomic_int_cmp_xchg(volatile int *atomic, int oldvalue, int newvalue)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   398
{
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   399
    char rv;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   400
    int readvalue;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   401
    __asm__ __volatile__("cas%.l %2,%3,%1\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   402
                         "seq    %0":"=dm"(rv), "=m"(*atomic), "=d"(readvalue)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   403
                         :"d"(newvalue), "m"(*atomic), "2"(oldvalue));
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   404
    return (SDL_bool) rv;
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   405
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   406
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   407
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   408
SDL_atomic_ptr_cmp_xchg(volatile void **atomic, void *oldvalue,
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   409
                        void *newvalue)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   410
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   411
    char rv;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   412
    int readvalue;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   413
    __asm__ __volatile__("cas%.l %2,%3,%1\n"
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   414
                         "seq    %0":"=dm"(rv), "=m"(*atomic), "=d"(readvalue)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   415
                         :"d"(newvalue), "m"(*atomic), "2"(oldvalue));
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   416
    return (SDL_bool) rv;
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   417
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   418
#elif defined(__GNUC__) && defined(__s390__)
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   419
# define ATOMIC_INT_CMP_XCHG(atomic,oldvalue,newvalue)  \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   420
  ({                                                    \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   421
    int rv = oldvalue;                                  \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   422
    __asm__ __volatile__("cs %0, %2, %1"                \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   423
                         : "+d" (rv),                   \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   424
                           "=Q" (*(atomic))             \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   425
                         : "d" (newvalue),              \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   426
                           "m" (*(atomic))              \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   427
                         : "cc");                       \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   428
    rv == oldvalue;                                     \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   429
  })
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   430
# if (SIZEOF_VOIDP == 4)
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   431
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   432
SDL_atomic_ptr_cmp_xchg(volatile void **atomic, void *oldvalue,
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   433
                        void *newvalue)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   434
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   435
    void *rv = oldvalue;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   436
    __asm__ __volatile__("cs %0, %2, %1":"+d"(rv), "=Q"(*atomic)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   437
                         :"d"(newvalue), "m"(*atomic)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   438
                         :"cc");
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   439
    return (SDL_bool) (rv == oldvalue);
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   440
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   441
# elif (SIZEOF_VOIDP == 8)
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   442
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   443
SDL_atomic_ptr_cmp_xchg(volatile void **atomic, void *oldvalue,
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   444
                        void *newvalue)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   445
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   446
    void *rv = oldvalue;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   447
    void **a = atomic;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   448
    __asm__ __volatile__("csg %0, %2, %1":"+d"(rv), "=Q"(*a)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   449
                         :"d"((long) (newvalue)), "m"(*a)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   450
                         :"cc");
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   451
    return (SDL_bool) (rv == oldvalue);
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   452
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   453
# else
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   454
#  error "Your system has an unsupported pointer size"
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   455
# endif /* SIZEOF_VOIDP */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   456
#elif defined(__WIN32__)
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   457
# include <windows.h>
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   458
static __inline__ int
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   459
SDL_atomic_int_xchg_add(volatile int *atomic, int value)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   460
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   461
    return InterlockedExchangeAdd(atomic, value);
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   462
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   463
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   464
static __inline__ void
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   465
SDL_atomic_int_add(volatile int *atomic, int value)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   466
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   467
    InterlockedExchangeAdd(atomic, value);
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   468
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   469
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   470
# if (WINVER > 0X0400)
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   471
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   472
SDL_atmoic_int_cmp_xchg(volatile int *atomic, int oldvalue, int newvalue)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   473
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   474
    return (SDL_bool) (InterlockedCompareExchangePointer((PVOID *) atomic,
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   475
                                                         (PVOID) newvalue,
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   476
                                                         (PVOID) oldvalue) ==
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   477
                       oldvalue);
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   478
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   479
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   480
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   481
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   482
SDL_atomic_ptr_cmp_xchg(volatile void **atomic, void *oldvalue,
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   483
                        void *newvalue)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   484
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   485
    return (InterlockedCompareExchangePointer(atomic, newvalue, oldvalue) ==
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   486
            oldvalue);
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   487
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   488
# else /* WINVER <= 0x0400 */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   489
#  if (SIZEOF_VOIDP != 4)
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   490
#   error "InterlockedCompareExchangePointer needed"
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   491
#  endif
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   492
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   493
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   494
SDL_atomic_int_cmp_xchg(volatile int *atomic, int oldvalue, int newvalue)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   495
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   496
    return (InterlockedCompareExchange(atomic, newvalue, oldvalue) ==
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   497
            oldvalue);
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   498
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   499
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   500
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   501
SDL_atomic_ptr_cmp_xchg(volatile void **atomic, void *oldvalue,
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   502
                        void *newvalue)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   503
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   504
    return (InterlockedCompareExchange(atomic, newvalue, oldvalue) ==
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   505
            oldvalue);
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   506
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   507
# endif
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   508
#else /* when all else fails */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   509
# define SDL_ATOMIC_OPS_NOT_SUPPORTED
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   510
# warning "Atomic Ops for this platform not supported!"
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   511
static __inline__ int
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   512
SDL_atomic_int_xchg_add(volatile int *atomic, int value)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   513
{
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   514
    int rv = *atomic;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   515
    *(atomic) += value;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   516
    return rv;
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   517
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   518
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   519
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   520
SDL_atomic_int_cmp_xchg(volatile int *atomic, int oldvalue, int newvalue)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   521
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   522
    return (*atomic == oldvalue) ?
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   523
        ((*atomic = newvalue), SDL_TRUE) : SDL_FALSE;
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   524
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   525
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   526
static __inline__ void
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   527
SDL_atomic_int_add(volatile int *atomic, int value)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   528
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   529
    *atomic += value;
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   530
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   531
#endif /* arch & platforms */
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   532
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   533
#ifdef ATOMIC_INT_CMP_XCHG
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   534
static __inline__ SDL_bool
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   535
SDL_atomic_int_cmp_xchg(volatile int *atomic, int oldvalue, int newvalue)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   536
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   537
    return (SDL_bool) ATOMIC_INT_CMP_XCHG(atomic, oldvalue, newvalue);
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   538
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   539
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   540
static __inline__ int
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   541
SDL_atomic_int_xchg_add(volatile int *atomic, int value)
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   542
{
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   543
    int rv;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   544
    do
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   545
        rv = *atomic;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   546
    while (!ATOMIC_INT_CMP_XCHG(atomic, rv, rv + value));
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   547
    return rv;
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   548
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   549
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   550
static __inline__ void
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   551
SDL_atomic_int_add(volatile int *atomic, int value)
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   552
{
3186
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   553
    int rv;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   554
    do
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   555
        rv = *atomic;
Sam Lantinga <slouken@libsdl.org>
parents: 3181
diff changeset
   556
    while (!ATOMIC_INT_CMP_XCHG(atomic, rv, rv + value));
3180
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   557
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   558
#endif /* ATOMIC_CMP_XCHG */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   559
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   560
#ifdef ATOMIC_MEMORY_BARRIER
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   561
# define SDL_atomic_int_get(atomic) \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   562
  (ATOMIC_MEMORY_BARRIER,*(atomic))
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   563
# define SDL_atomic_int_set(atomic,value) \
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   564
  (*(atomic)=value,ATOMIC_MEMORY_BARRIER)
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   565
#else
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   566
# define SDL_atomic_int_get(atomic) (*(atomic))
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   567
# define SDL_atomic_int_set(atomic, newvalue) ((void)(*(atomic) = (newvalue)))
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   568
#endif /* MEMORY_BARRIER_NEEDED */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   569
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   570
#define SDL_atomic_int_inc(atomic) (SDL_atomic_int_add((atomic),1))
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   571
#define SDL_atomic_int_dec_test(atomic) (SDL_atomic_int_xchg_add((atomic),-1) == 1)
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   572
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   573
/* Ends C function definitions when using C++ */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   574
#ifdef __cplusplus
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   575
/* *INDENT-OFF* */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   576
}
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   577
/* *INDENT-ON* */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   578
#endif
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   579
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   580
#include "close_code.h"
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   581
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   582
#endif /* _SDL_atomic_h_ */
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   583
77d6336711fc First commit for SDL atomic operations.
Bob Pendleton <bob@pendleton.com>
parents:
diff changeset
   584
/* vi: set ts=4 sw=4 expandtab: */