include/SDL_atomic.h
changeset 3180 77d6336711fc
child 3181 030899df1af5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/include/SDL_atomic.h	Tue Jun 09 17:33:44 2009 +0000
     1.3 @@ -0,0 +1,674 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2006 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 + */
    1.25 +
    1.26 +/**
    1.27 + * \file SDL_atomic.h
    1.28 + *
    1.29 + * Atomic int and pointer magic
    1.30 + */
    1.31 +
    1.32 +#ifndef _SDL_atomic_h_
    1.33 +#define _SDL_atomic_h_
    1.34 +
    1.35 +
    1.36 +#include "SDL_stdinc.h"
    1.37 +#include "SDL_platform.h"
    1.38 +
    1.39 +#include "begin_code.h"
    1.40 +
    1.41 +/* Set up for C function definitions, even when using C++ */
    1.42 +#ifdef __cplusplus
    1.43 +/* *INDENT-OFF* */
    1.44 +extern "C" {
    1.45 +/* *INDENT-ON* */
    1.46 +#endif
    1.47 +
    1.48 +#if defined(__GNUC__) && (defined(i386) || defined(__i386__)  || defined(__x86_64__))
    1.49 +static __inline__ void
    1.50 +SDL_atomic_int_add(volatile int* atomic, int value)
    1.51 +{
    1.52 +  __asm__ __volatile__("lock;"
    1.53 +                       "addl %1, %0"
    1.54 +                       : "=m" (*atomic)
    1.55 +                       : "ir" (value),
    1.56 +                         "m" (*atomic));
    1.57 +}
    1.58 +
    1.59 +static __inline__ int
    1.60 +SDL_atomic_int_xchg_add(volatile int* atomic, int value)
    1.61 +{                                              
    1.62 +  int rv;                                    
    1.63 +  __asm__ __volatile__("lock;"               
    1.64 +                       "xaddl %0, %1"        
    1.65 +                       : "=r" (rv),          
    1.66 +                         "=m" (*atomic)    
    1.67 +                       : "0" (value),        
    1.68 +                         "m" (*atomic));   
    1.69 +  return rv;                                        
    1.70 +}
    1.71 +
    1.72 +static __inline__ SDL_bool
    1.73 +SDL_atomic_int_cmp_xchg(volatile int* atomic, int oldvalue, int newvalue)
    1.74 +{
    1.75 +  int rv;                                                      
    1.76 +  __asm__ __volatile__("lock;"                               
    1.77 +                       "cmpxchgl %2, %1"                     
    1.78 +                       : "=a" (rv),                          
    1.79 +                         "=m" (*atomic)             
    1.80 +                       : "r" (newvalue),                     
    1.81 +                         "m" (*atomic),                    
    1.82 +                         "0" (oldvalue));
    1.83 +  return (rv == oldvalue);                                          
    1.84 +}
    1.85 +
    1.86 +static __inline__ SDL_bool
    1.87 +SDL_atomic_ptr_cmp_xchg(volatile void** atomic, void* oldvalue, void* newvalue)
    1.88 +{
    1.89 +  void* rv;
    1.90 +  __asm__ __volatile__("lock;"
    1.91 +# if defined(__x86_64__)                       
    1.92 +                       "cmpxchgq %q2, %1"
    1.93 +# else
    1.94 +                       "cmpxchgl %2, %1"
    1.95 +# endif                       
    1.96 +                       : "=a" (rv),
    1.97 +                         "=m" (*atomic)
    1.98 +                       : "r" (newvalue),
    1.99 +                         "m" (*atomic),
   1.100 +                         "0" (oldvalue));
   1.101 +  return (rv == oldvalue);
   1.102 +}
   1.103 +#elif defined(__GNUC__) && defined(__alpha__)
   1.104 +# define ATOMIC_MEMORY_BARRIER (__asm__ __volatile__ ("mb" : : : "memory"))
   1.105 +# define ATOMIC_INT_CMP_XCHG(atomic,value)              \
   1.106 +  ({                                                    \
   1.107 +    int rv,prev;                                        \
   1.108 +    __asm__ __volatile__("   mb\n"                      \
   1.109 +                         "1: ldl_l   %0,%2\n"           \
   1.110 +                         "   cmpeq   %0,%3,%1\n"        \
   1.111 +                         "   beq     %1,2f\n"           \
   1.112 +                         "   mov     %4,%1\n"           \
   1.113 +                         "   stl_c   %1,%2\n"           \
   1.114 +                         "   beq     %1,1b\n"           \
   1.115 +                         "   mb\n"                      \
   1.116 +                         "2:"                           \
   1.117 +                         : "=&r" (prev),                \
   1.118 +                           "=&r" (rv)                   \
   1.119 +                         : "m" (*(atomic)),             \
   1.120 +                           "Ir" (oldvalue),             \
   1.121 +                           "Ir" (newvalue)              \
   1.122 +                         : "memory");                   \
   1.123 +    (rv != 0);                                          \
   1.124 +  })
   1.125 +
   1.126 +# if (SIZEOF_VOIDP == 4)
   1.127 +static __inline__ SDL_bool
   1.128 +SDL_atomic_ptr_cmp_xchg(volatile void** atomic, void* oldvalue, void* newvalue)
   1.129 +{
   1.130 +  int rv;
   1.131 +  void* prev;
   1.132 +  __asm__ __volatile__("   mb\n"
   1.133 +                       "1: ldl_l %0,%2\n"
   1.134 +                       "   cmpeq %0,%3,%1\n"
   1.135 +                       "   beq   $1,2f\n"
   1.136 +                       "   mov   %4,%1\n"
   1.137 +                       "   stl_c %1,%2\n"
   1.138 +                       "   beq   %1,1b\n"
   1.139 +                       "   mb\n"
   1.140 +                       "2:"
   1.141 +                       : "=&r" (prev),
   1.142 +                         "=&r" (rv)
   1.143 +                       : "m" (*atomic),
   1.144 +                         "Ir" (oldvalue),
   1.145 +                         "Ir" (newvalue)
   1.146 +                       : "memory");
   1.147 +  return (rv != 0);
   1.148 +}
   1.149 +# elif (SIZEOF_VOIDP == 8)
   1.150 +static __inline__ SDL_bool
   1.151 +SDL_atomic_ptr_cmp_xchg(volatile void** atomic, void* oldvalue, void* newvalue)
   1.152 +{
   1.153 +  int rv;
   1.154 +  void* prev;
   1.155 +  __asm__ __volatile__("   mb\n"
   1.156 +                       "1: ldq_l %0,%2\n"
   1.157 +                       "   cmpeq %0,%3,%1\n"
   1.158 +                       "   beq   %1,2f\n"
   1.159 +                       "   mov   %4,%1\n"
   1.160 +                       "   stq_c %1,%2\n"
   1.161 +                       "   beq   %1,1b\n"
   1.162 +                       "   mb\n"
   1.163 +                       "2:"
   1.164 +                       : "=&r" (prev),
   1.165 +                         "=&r" (rv)
   1.166 +                       : "m" (*atomic),
   1.167 +                         "Ir" (oldvalue),
   1.168 +                         "Ir" (newvalue)
   1.169 +                       : "memory");
   1.170 +  return (rv != 0);
   1.171 +}
   1.172 +# else
   1.173 +#  error "Your system has an unsupported pointer size"  
   1.174 +# endif  /* SIZEOF_VOIDP */
   1.175 +#elif defined(__GNUC__) && defined(__sparc__)
   1.176 +# define ATOMIC_MEMORY_BARRIER                                          \
   1.177 +  (__asm__ __volatile__("membar #LoadLoad | #LoadStore"                 \
   1.178 +                        " | #StoreLoad | #StoreStore" : : : "memory"))
   1.179 +# define ATOMIC_INT_CMP_XCHG(atomic,oldvalue,newvalue)                  \
   1.180 +  ({                                                                    \
   1.181 +    int rv;                                                             \
   1.182 +    __asm__ __volatile__("cas [%4], %2, %0"                             \
   1.183 +                         : "=r" (rv), "=m" (*(atomic))                  \
   1.184 +                         : "r" (oldvalue), "m" (*(atomic)),             \
   1.185 +                         "r" (atomic), "0" (newvalue));                 \
   1.186 +    rv == oldvalue;                                                     \
   1.187 +  })
   1.188 +
   1.189 +# if (SIZEOF_VOIDP == 4)
   1.190 +static __inline__ SDL_bool
   1.191 +SDL_atomic_ptr_cmp_xchg(volatile void** atomic, void* oldvalue, void* newvalue)
   1.192 +{
   1.193 +  void* rv;
   1.194 +  __asm__ __volatile__("cas [%4], %2, %0"
   1.195 +                       : "=r" (rv),
   1.196 +                         "=m" (*atomic)
   1.197 +                       : "r" (oldvalue),
   1.198 +                         "m" (*atomic),
   1.199 +                         "r" (atomic),
   1.200 +                         "0" (newvalue));
   1.201 +  return (rv == oldvalue);
   1.202 +}
   1.203 +# elif (SIZEOF_VOIDP == 8)
   1.204 +static __inline__ SDL_bool
   1.205 +SDL_atomic_ptr_cmp_xchg(volatile void** atomic, void* oldvalue, void* newvalue)
   1.206 +{
   1.207 +  void* rv;
   1.208 +  void** a = atomic;
   1.209 +  __asm__ __volatile__("casx [%4], %2, %0"
   1.210 +                       : "=r" (rv),
   1.211 +                         "=m" (*a)
   1.212 +                       : "r" (oldvalue),
   1.213 +                         "m" (*a),
   1.214 +                         "r" (a),
   1.215 +                         "0" (newvalue));
   1.216 +  return (rv == oldvalue);
   1.217 +}
   1.218 +# else
   1.219 +#  error "Your system has an unsupported pointer size"
   1.220 +# endif /* SIZEOF_VOIDP */
   1.221 +#elif defined(__GNUC__) && (defined(__POWERPC__) || defined(__powerpc__) || defined(__ppc__) || defined(_M_PPC))
   1.222 +# define ATOMIC_MEMORY_BARRIER \
   1.223 +  (__asm__ __volatile__ ("sync" : : : "memory"))
   1.224 +static __inline__ void
   1.225 +SDL_atomic_int_add(volatile int* atomic, int value)
   1.226 +{                                           
   1.227 +  int rv,tmp;                                   
   1.228 +  __asm__ __volatile__("1: lwarx   %0,  0, %3\n" 
   1.229 +                       "   add     %1, %0, %4\n"
   1.230 +                       "   stwcx.  %1,  0, %3\n" 
   1.231 +                       "   bne-    1b"          
   1.232 +                       : "=&b" (rv),            
   1.233 +                         "=&r" (tmp),           
   1.234 +                         "=m" (*atomic)       
   1.235 +                       : "b" (atomic),          
   1.236 +                         "r" (value),           
   1.237 +                         "m" (*atomic)        
   1.238 +                       : "cr0",                 
   1.239 +                         "memory");             
   1.240 +}
   1.241 +
   1.242 +static __inline__ int
   1.243 +SDL_atomic_int_xchg_add(volatile int* atomic, int value)
   1.244 +{                                          
   1.245 +  int rv,tmp;                               
   1.246 +  __asm__ __volatile__("1: lwarx  %0, 0, %3\n"        
   1.247 +                       "   add    %1, %0, %4\n"       
   1.248 +                       "   stwcx. %1, 0, %3\n"        
   1.249 +                       "   bne-   1b"                 
   1.250 +                       : "=&b" (rv),                  
   1.251 +                         "=&r" (tmp),                 
   1.252 +                         "=m" (*atomic)
   1.253 +                       : "b" (atomic),                
   1.254 +                         "r" (value),                 
   1.255 +                         "m" (*atomic)
   1.256 +                       : "cr0",                       
   1.257 +                       "memory");                   
   1.258 +  return rv;                                                 
   1.259 +}
   1.260 +
   1.261 +# if (SIZEOF_VOIDP == 4)
   1.262 +static __inline__ SDL_bool
   1.263 +SDL_atomic_int_cmp_xchg(volatile int* atomic, int oldvalue, int newvalue)
   1.264 +{                                                        
   1.265 +  int rv;                                                 
   1.266 +  __asm__ __volatile__("   sync\n"                         
   1.267 +                       "1: lwarx   %0, 0, %1\n"           
   1.268 +                       "   subf.   %0, %2, %0\n"          
   1.269 +                       "   bne     2f\n"                  
   1.270 +                       "   stwcx.  %3, 0, %1\n"           
   1.271 +                       "   bne-    1b\n"                  
   1.272 +                       "2: isync"                         
   1.273 +                       : "=&r" (rv)                       
   1.274 +                       : "b" (atomic),                    
   1.275 +                         "r" (oldvalue),                  
   1.276 +                         "r"                              
   1.277 +                       : "cr0",                           
   1.278 +                         "memory");                         
   1.279 +  return (rv == 0);                                              
   1.280 +}
   1.281 +
   1.282 +static __inline__ SDL_bool
   1.283 +SDL_atomic_ptr_cmp_xchg(volatile void** atomic, void* oldvalue, void* newvalue)
   1.284 +{
   1.285 +  void* rv;
   1.286 +  __asm__ __volatile__("sync\n"
   1.287 +                       "1: lwarx  %0,  0, %1\n"
   1.288 +                       "   subf.  %0, %2, %0\n"
   1.289 +                       "   bne    2f\n"
   1.290 +                       "   stwcx. %3,  0, %1\n"
   1.291 +                       "   bne-   1b\n"
   1.292 +                       "2: isync"
   1.293 +                       : "=&r" (rv)
   1.294 +                       : "b" (atomic),
   1.295 +                         "r" (oldvalue),
   1.296 +                         "r" (newvalue)
   1.297 +                       : "cr0",
   1.298 +                       "memory");
   1.299 +  return (rv == 0);
   1.300 +}
   1.301 +# elif (SIZEOF_VOIDP == 8)
   1.302 +static __inline__ SDL_bool
   1.303 +SDL_atomic_int_cmp_xchg(volatile int* atomic, int oldvalue, int newvalue)
   1.304 +{                                                        
   1.305 +  int rv;                                                 
   1.306 +  __asm__ __volatile__("   sync\n"                         
   1.307 +                       "1: lwarx   %0,  0, %1\n"
   1.308 +                       "   extsw   %0, %0\n"
   1.309 +                       "   subf.   %0, %2, %0\n"          
   1.310 +                       "   bne     2f\n"                  
   1.311 +                       "   stwcx.  %3,  0, %1\n"           
   1.312 +                       "   bne-    1b\n"                  
   1.313 +                       "2: isync"                         
   1.314 +                       : "=&r" (rv)                       
   1.315 +                       : "b" (atomic),                    
   1.316 +                         "r" (oldvalue),                  
   1.317 +                         "r"                              
   1.318 +                       : "cr0",                           
   1.319 +                         "memory");                         
   1.320 +  return (rv == 0);                                              
   1.321 +}
   1.322 +
   1.323 +static __inline__ SDL_bool
   1.324 +SDL_atomic_ptr_cmp_xchg(volatile void** atomic, void* oldvalue, void* newvalue)
   1.325 +{
   1.326 +  void* rv;
   1.327 +  __asm__ __volatile__("sync\n"
   1.328 +                       "1: ldarx  %0,  0, %1\n"
   1.329 +                       "   subf.  %0, %2, %0\n"
   1.330 +                       "   bne    2f\n"
   1.331 +                       "   stdcx. %3,  0, %1\n"
   1.332 +                       "   bne-   1b\n"
   1.333 +                       "2: isync"
   1.334 +                       : "=&r" (rv)
   1.335 +                       : "b" (atomic),
   1.336 +                         "r" (oldvalue),
   1.337 +                         "r" (newvalue)
   1.338 +                       : "cr0",
   1.339 +                       "memory");
   1.340 +  return (rv == 0);
   1.341 +}
   1.342 +# else
   1.343 +#  error "Your system has an unsupported pointer size"
   1.344 +# endif /* SIZEOF_VOIDP */
   1.345 +#elif defined(__GNUC__) && (defined(__IA64__) || defined(__ia64__))
   1.346 +# define ATOMIC_MEMORY_BARRIER (__sync_synchronize())
   1.347 +# define SDL_atomic_int_xchg_add(atomic, value)     \
   1.348 +  (__sync_fetch_and_add((atomic),(value)))
   1.349 +# define SDL_atomic_int_add(atomic, value)                  \
   1.350 +  ((void)__sync_fetch_and_add((atomic),(value)))
   1.351 +# define SDL_atomic_int_cmp_xchg(atomic,oldvalue,newvalue)  \
   1.352 +  (__sync_bool_compare_and_swap((atomic),(oldvalue),(newvalue)))
   1.353 +# define SDL_atomic_ptr_cmp_xchg(atomic,oldvalue,newvalue)              \
   1.354 +  (__sync_bool_compare_and_swap((long*)(atomic),(long)(oldvalue),(long)(newvalue)))
   1.355 +#elif defined(__GNUC__) && defined(__LINUX__) && (defined(__mips__) || defined(__MIPS__))
   1.356 +static __inline__ int
   1.357 +SDL_atomic_int_xchg_add(volatile int* atomic, int value)
   1.358 +{                                            
   1.359 +  int rv,tmp;                                 
   1.360 +  __asm__ __volatile__("1:              \n"                 
   1.361 +                       ".set  push      \n"         
   1.362 +                       ".set  mips2     \n"        
   1.363 +                       "ll    %0,%3     \n"        
   1.364 +                       "addu  %1,%4,%0  \n"     
   1.365 +                       "sc    %1,%2     \n"        
   1.366 +                       ".set  pop       \n"          
   1.367 +                       "beqz  %1,1b     \n"        
   1.368 +                       : "=&r" (rv),          
   1.369 +                         "=&r" (tmp),         
   1.370 +                         "=m" (*atomic)     
   1.371 +                       : "m" (*atomic),     
   1.372 +                         "r" (value)          
   1.373 +                       : "memory");           
   1.374 +  return rv;                                         
   1.375 +}
   1.376 +
   1.377 +static __inline__ void
   1.378 +SDL_atomic_int_add(volatile int* atomic, int value)
   1.379 +{                                           
   1.380 +  int rv;                                    
   1.381 +  __asm__ __volatile__("1:               \n"                
   1.382 +                       ".set  push       \n"        
   1.383 +                       ".set  mips2      \n"       
   1.384 +                       "ll    %0,%2      \n"       
   1.385 +                       "addu  %0,%3,%0   \n"    
   1.386 +                       "sc    %0,%1      \n"       
   1.387 +                       ".set  pop        \n"         
   1.388 +                       "beqz  %0,1b      \n"       
   1.389 +                       : "=&r" (rv),         
   1.390 +                         "=m" (*atomic)    
   1.391 +                       : "m" (*atomic),    
   1.392 +                         "r" (value)         
   1.393 +                       : "memory");          
   1.394 +}
   1.395 +
   1.396 +static __inline__ SDL_bool
   1.397 +SDL_atomic_int_cmp_xchg(volatile int* atomic, int oldvalue, int newvalue)
   1.398 +{
   1.399 +  int rv;
   1.400 +  __asm__ __volatile__("     .set push        \n"
   1.401 +                       "     .set noat        \n"
   1.402 +                       "     .set mips3       \n"
   1.403 +                       "1:   ll   %0, %2      \n"
   1.404 +                       "     bne  %0, %z3, 2f \n"
   1.405 +                       "     .set mips0       \n"
   1.406 +                       "     move $1, %z4     \n"
   1.407 +                       "     .set mips3       \n"
   1.408 +                       "     sc   $1, %1      \n"
   1.409 +                       "     beqz $1, 1b      \n"
   1.410 +                       "     sync             \n"
   1.411 +                       "2:                    \n"
   1.412 +                       "     .set pop         \n"
   1.413 +                       : "=&r" (rv),
   1.414 +                         "=R" (*atomic)
   1.415 +                       : "R" (*atomic),
   1.416 +                         "Jr" (oldvalue),
   1.417 +                         "Jr" (newvalue)
   1.418 +                       : "memory");
   1.419 +  return (SDL_bool)rv;                  
   1.420 +}
   1.421 +
   1.422 +static __inline__ SDL_bool
   1.423 +SDL_atomic_ptr_cmp_xchg(volatile void** atomic, void* oldvalue, void* newvalue)
   1.424 +{                                                     
   1.425 +  int rv;
   1.426 +  __asm__ __volatile__("     .set push        \n"
   1.427 +                       "     .set noat        \n"
   1.428 +                       "     .set mips3       \n"
   1.429 +# if defined(__mips64)
   1.430 +                       "1:   lld  %0, %2      \n"
   1.431 +# else
   1.432 +                       "1:   ll   %0, %2      \n"
   1.433 +# endif                       
   1.434 +                       "     bne  %0, %z3, 2f \n"
   1.435 +                       "     move $1, %z4     \n"
   1.436 +# if defined(__mips64)
   1.437 +                       "     sc   $1, %1      \n"
   1.438 +# else
   1.439 +                       "     scd  $1, %1      \n"
   1.440 +# endif                       
   1.441 +                       "     beqz $1, 1b      \n"
   1.442 +                       "     sync             \n"
   1.443 +                       "2:                    \n"
   1.444 +                       "     .set pop         \n"
   1.445 +                       : "=&r" (rv),
   1.446 +                         "=R" (*atomic)
   1.447 +                       : "R" (*atomic),
   1.448 +                         "Jr" (oldvalue),
   1.449 +                         "Jr" (newvalue)
   1.450 +                       : "memory");
   1.451 +  return (SDL_bool)rv;                                                  
   1.452 +}
   1.453 +#elif defined(__GNUC__) && defined(__m68k__)
   1.454 +static __inline__ int
   1.455 +SDL_atomic_int_xchg_add(volatile int* atomic, int value)
   1.456 +{                                          
   1.457 +  int rv = *atomic;
   1.458 +  int tmp;
   1.459 +  __asm__ __volatile__("1: move%.l %0,%1    \n"
   1.460 +                       "   add%.l  %2,%1    \n"
   1.461 +                       "   cas%.l  %0,%1,%3 \n"
   1.462 +                       "   jbne    1b       \n"
   1.463 +                       : "=d" (rv),
   1.464 +                         "=&d" (tmp)
   1.465 +                       : "d" (value),
   1.466 +                         "m" (*atomic),
   1.467 +                         "0" (rv)
   1.468 +                       : "memory");
   1.469 +  return rv;
   1.470 +}
   1.471 +
   1.472 +static __inline__ void
   1.473 +SDL_atomic_int_add(volatile int* atomic, int value)
   1.474 +{                                           
   1.475 +  __asm__ __volatile__("add%.l %0,%1"        
   1.476 +                       :                     
   1.477 +                       : "id" (value),       
   1.478 +                         "m" (*atomic)
   1.479 +                       : "memory");          
   1.480 +}
   1.481 +
   1.482 +static __inline__ SDL_bool
   1.483 +SDL_atomic_int_cmp_xchg(volatile int* atomic, int oldvalue, int newvalue)
   1.484 +{                                           
   1.485 +  char rv;                                   
   1.486 +  int readvalue;                             
   1.487 +  __asm__ __volatile__("cas%.l %2,%3,%1\n"   
   1.488 +                       "seq    %0"           
   1.489 +                       : "=dm" (rv),         
   1.490 +                         "=m" (*atomic),   
   1.491 +                         "=d" (readvalue)    
   1.492 +                       : "d" (newvalue),     
   1.493 +                         "m" (*atomic),    
   1.494 +                         "2" (oldvalue));    
   1.495 +    return rv;                                        
   1.496 +}
   1.497 +
   1.498 +static __inline__ SDL_bool
   1.499 +SDL_atomic_ptr_cmp_xchg(volatile void** atomic, void* oldvalue, void* newvalue)
   1.500 +{
   1.501 +  char rv;                                   
   1.502 +  int readvalue;                             
   1.503 +  __asm__ __volatile__("cas%.l %2,%3,%1\n"   
   1.504 +                       "seq    %0"           
   1.505 +                       : "=dm" (rv),         
   1.506 +                         "=m" (*atomic),   
   1.507 +                         "=d" (readvalue)    
   1.508 +                       : "d" (newvalue),     
   1.509 +                         "m" (*atomic),    
   1.510 +                         "2" (oldvalue));    
   1.511 +    return rv;                                        
   1.512 +}
   1.513 +#elif defined(__GNUC__) && defined(__s390__)
   1.514 +# define ATOMIC_INT_CMP_XCHG(atomic,oldvalue,newvalue)  \
   1.515 +  ({                                                    \
   1.516 +    int rv = oldvalue;                                  \
   1.517 +    __asm__ __volatile__("cs %0, %2, %1"                \
   1.518 +                         : "+d" (rv),                   \
   1.519 +                           "=Q" (*(atomic))             \
   1.520 +                         : "d" (newvalue),              \
   1.521 +                           "m" (*(atomic))              \
   1.522 +                         : "cc");                       \
   1.523 +    rv == oldvalue;                                     \
   1.524 +  })
   1.525 +# if (SIZEOF_VOIDP == 4)
   1.526 +static __inline__ SDL_bool
   1.527 +SDL_atomic_ptr_cmp_xchg(volatile void** atomic, void* oldvalue, void* newvalue)
   1.528 +{
   1.529 +  void* rv = oldvalue;
   1.530 +  __asm__ __volatile__("cs %0, %2, %1"
   1.531 +                       : "+d" (rv),
   1.532 +                         "=Q" (*atomic)
   1.533 +                       : "d" (newvalue),
   1.534 +                         "m" (*atomic)
   1.535 +                       : "cc");
   1.536 +  return (rv == oldvalue);
   1.537 +}
   1.538 +# elif (SIZEOF_VOIDP == 8)
   1.539 +static __inline__ SDL_bool
   1.540 +SDL_atomic_ptr_cmp_xchg(volatile void** atomic, void* oldvalue, void* newvalue)
   1.541 +{
   1.542 +  void* rv = oldvalue;
   1.543 +  void** a = atomic;
   1.544 +  __asm__ __volatile__("csg %0, %2, %1"
   1.545 +                       : "+d" (rv),
   1.546 +                         "=Q" (*a)
   1.547 +                       : "d" ((long)(newvalue)),
   1.548 +                         "m" (*a)
   1.549 +                       : "cc");
   1.550 +  return (rv == oldvalue);
   1.551 +}
   1.552 +# else
   1.553 +#  error "Your system has an unsupported pointer size"
   1.554 +# endif /* SIZEOF_VOIDP */
   1.555 +#elif defined(__WIN32__)
   1.556 +# include <windows.h>
   1.557 +static __inline__ int
   1.558 +SDL_atomic_int_xchg_add(volatile int* atomic, int value)
   1.559 +{
   1.560 +  return InterlockedExchangeAdd(atomic, value);
   1.561 +}
   1.562 +
   1.563 +static __inline__ void
   1.564 +SDL_atomic_int_add(volatile int* atomic, int value)
   1.565 +{
   1.566 +  InterlockedExchangeAdd(atomic, value);
   1.567 +}
   1.568 +
   1.569 +# if (WINVER > 0X0400)
   1.570 +static __inline__ SDL_bool
   1.571 +SDL_atmoic_int_cmp_xchg(volatile int* atomic, int oldvalue, int newvalue)
   1.572 +{
   1.573 +  return ((SDL_bool)InterlockedCompareExchangePointer((PVOID*)atomic,
   1.574 +                                                      (PVOID)newvalue,
   1.575 +                                                      (PVOID)oldvalue) == oldvalue);
   1.576 +}
   1.577 +
   1.578 +
   1.579 +static __inline__ SDL_bool
   1.580 +SDL_atomic_ptr_cmp_xchg(volatile void** atomic, void* oldvalue, void* newvalue)
   1.581 +{
   1.582 +  return (InterlockedCompareExchangePointer(atomic, newvalue, oldvalue) == oldvalue);
   1.583 +}
   1.584 +# else /* WINVER <= 0x0400 */
   1.585 +#  if (SIZEOF_VOIDP != 4)
   1.586 +#   error "InterlockedCompareExchangePointer needed"
   1.587 +#  endif
   1.588 +
   1.589 +static __inline__ SDL_bool
   1.590 +SDL_atomic_int_cmp_xchg(volatile int* atomic, int oldvalue, int newvalue)
   1.591 +{
   1.592 +  return (InterlockedCompareExchange(atomic, newvalue, oldvalue) == oldvalue);
   1.593 +}
   1.594 +
   1.595 +static __inline__ SDL_bool
   1.596 +SDL_atomic_ptr_cmp_xchg(volatile void** atomic, void* oldvalue, void* newvalue)
   1.597 +{
   1.598 +  return (InterlockedCompareExchange(atomic, newvalue, oldvalue) == oldvalue);
   1.599 +}
   1.600 +# endif
   1.601 +#else /* when all else fails */
   1.602 +# define SDL_ATOMIC_OPS_NOT_SUPPORTED
   1.603 +# warning "Atomic Ops for this platform not supported!"
   1.604 +static __inline__ int
   1.605 +SDL_atomic_int_xchg_add(volatile int* atomic, int value)
   1.606 +{                                           
   1.607 +  int rv = *atomic;                          
   1.608 +  *(atomic) += value;                        
   1.609 +  return rv;                                        
   1.610 +}
   1.611 +
   1.612 +static __inline__ SDL_bool
   1.613 +SDL_atomic_int_cmp_xchg(volatile int* atomic, int oldvalue, int newvalue)
   1.614 +{
   1.615 +  return (*atomic == oldvalue) ?  
   1.616 +    ((*atomic = newvalue), SDL_TRUE) : SDL_FALSE;
   1.617 +}
   1.618 +
   1.619 +static __inline__ void
   1.620 +SDL_atomic_int_add(volatile int* atomic, int value)
   1.621 +{
   1.622 +  *atomic += value;
   1.623 +}
   1.624 +#endif /* arch & platforms */
   1.625 +  
   1.626 +#ifdef ATOMIC_INT_CMP_XCHG
   1.627 +static __inline__ SDL_bool
   1.628 +SDL_atomic_int_cmp_xchg(volatile int* atomic, int oldvalue, int newvalue)
   1.629 +{
   1.630 +  return ATOMIC_INT_CMP_XCHG(atomic,oldvalue,newvalue);
   1.631 +}
   1.632 +
   1.633 +static __inline__ int
   1.634 +SDL_atomic_int_xchg_add(volatile int* atomic, int value)   
   1.635 +{                                                   
   1.636 +  int rv;                                            
   1.637 +  do                                                 
   1.638 +    rv = *atomic;                                
   1.639 +  while(!ATOMIC_INT_CMP_XCHG(atomic,rv,rv+value)); 
   1.640 +  return rv;                                                
   1.641 +}
   1.642 +
   1.643 +static __inline__ void
   1.644 +SDL_atomic_int_add(volatile int* atomic, int value)
   1.645 +{
   1.646 +  int rv;
   1.647 +  do
   1.648 +    rv = *atomic;
   1.649 +  while(!ATOMIC_INT_CMP_XCHG(atomic,rv,rv+value));
   1.650 +}
   1.651 +#endif /* ATOMIC_CMP_XCHG */
   1.652 +
   1.653 +#ifdef ATOMIC_MEMORY_BARRIER
   1.654 +# define SDL_atomic_int_get(atomic) \
   1.655 +  (ATOMIC_MEMORY_BARRIER,*(atomic))
   1.656 +# define SDL_atomic_int_set(atomic,value) \
   1.657 +  (*(atomic)=value,ATOMIC_MEMORY_BARRIER)
   1.658 +#else
   1.659 +# define SDL_atomic_int_get(atomic) (*(atomic))
   1.660 +# define SDL_atomic_int_set(atomic, newvalue) ((void)(*(atomic) = (newvalue)))
   1.661 +#endif /* MEMORY_BARRIER_NEEDED */
   1.662 +
   1.663 +#define SDL_atomic_int_inc(atomic) (SDL_atomic_int_add((atomic),1))
   1.664 +#define SDL_atomic_int_dec_test(atomic) (SDL_atomic_int_xchg_add((atomic),-1) == 1)
   1.665 +
   1.666 +/* Ends C function definitions when using C++ */
   1.667 +#ifdef __cplusplus
   1.668 +/* *INDENT-OFF* */
   1.669 +}
   1.670 +/* *INDENT-ON* */
   1.671 +#endif
   1.672 +
   1.673 +#include "close_code.h"
   1.674 +
   1.675 +#endif /* _SDL_atomic_h_ */
   1.676 +
   1.677 +/* vi: set ts=4 sw=4 expandtab: */