test/testatomic.c
author Bob Pendleton <bob@pendleton.com>
Thu, 17 Sep 2009 20:35:12 +0000
changeset 3261 72b542f34739
parent 3237 916f396fe65d
child 3299 975fd903466b
permissions -rw-r--r--
The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
The linux code appears to be complete and *should* be the base of all Unix and GCC based versions. The macosx and win32 versions
are currently just copies of the dummy code. I will begin working on the windows version as soon as this check in is done. I
need someone to work on the Mac OS X version.

I'm afraid that this check in will break QNX (Sorry!)
bob@3180
     1
#include "SDL.h"
bob@3180
     2
bob@3201
     3
/*
bob@3202
     4
  Absolutely basic tests just to see if we get the expected value
bob@3202
     5
  after calling each function.
bob@3201
     6
*/
bob@3201
     7
bob@3202
     8
char * 
bob@3202
     9
tf(SDL_bool tf)
bob@3202
    10
{
bob@3202
    11
   static char *t = "true";
bob@3202
    12
   static char *f = "false";
bob@3202
    13
bob@3202
    14
   if (tf)
bob@3202
    15
   {
bob@3202
    16
      return t;
bob@3202
    17
   }
bob@3202
    18
bob@3202
    19
   return f;
bob@3202
    20
}
bob@3202
    21
  
bob@3180
    22
int
slouken@3186
    23
main(int argc, char **argv)
bob@3180
    24
{
bob@3180
    25
bob@3216
    26
   volatile Uint32 val32 = 0;
bob@3201
    27
   Uint32 ret32 = 0;
bob@3180
    28
bob@3216
    29
   volatile Uint64 val64 = 0;
bob@3201
    30
   Uint64 ret64 = 0;
bob@3180
    31
bob@3261
    32
   SDL_SpinLock lock = 0;
bob@3261
    33
bob@3202
    34
   SDL_bool tfret = SDL_FALSE;
bob@3202
    35
bob@3261
    36
   printf("\nspin lock---------------------------------------\n\n");
slouken@3186
    37
bob@3261
    38
   SDL_AtomicLock(&lock);
bob@3261
    39
   printf("AtomicLock                   lock=%d\n", lock);
bob@3261
    40
   SDL_AtomicUnlock(&lock);
bob@3261
    41
   printf("AtomicUnlock                 lock=%d\n", lock);
bob@3202
    42
bob@3261
    43
   printf("\n32 bit -----------------------------------------\n\n");
bob@3202
    44
bob@3202
    45
   val32 = 0;
bob@3202
    46
   tfret = SDL_AtomicTestThenSet32(&val32);
bob@3202
    47
   printf("TestThenSet32        tfret=%s val=%d\n", tf(tfret), val32);
bob@3202
    48
   tfret = SDL_AtomicTestThenSet32(&val32);
bob@3202
    49
   printf("TestThenSet32        tfret=%s val=%d\n", tf(tfret), val32);
bob@3202
    50
bob@3201
    51
   SDL_AtomicClear32(&val32);
bob@3202
    52
   printf("Clear32              val=%d\n", val32);
bob@3202
    53
bob@3201
    54
   ret32 = SDL_AtomicFetchThenIncrement32(&val32);
bob@3202
    55
   printf("FetchThenIncrement32 ret=%d val=%d\n", ret32, val32);
bob@3202
    56
bob@3201
    57
   ret32 = SDL_AtomicFetchThenDecrement32(&val32);
bob@3202
    58
   printf("FetchThenDecrement32 ret=%d val=%d\n", ret32, val32);
bob@3202
    59
bob@3201
    60
   ret32 = SDL_AtomicFetchThenAdd32(&val32, 10);
bob@3202
    61
   printf("FetchThenAdd32       ret=%d val=%d\n", ret32, val32);
bob@3202
    62
bob@3201
    63
   ret32 = SDL_AtomicFetchThenSubtract32(&val32, 10);
bob@3202
    64
   printf("FetchThenSubtract32  ret=%d val=%d\n", ret32, val32);
bob@3202
    65
bob@3201
    66
   ret32 = SDL_AtomicIncrementThenFetch32(&val32);
bob@3202
    67
   printf("IncrementThenFetch32 ret=%d val=%d\n", ret32, val32);
bob@3202
    68
bob@3201
    69
   ret32 = SDL_AtomicDecrementThenFetch32(&val32);
bob@3202
    70
   printf("DecrementThenFetch32 ret=%d val=%d\n", ret32, val32);
bob@3202
    71
bob@3201
    72
   ret32 = SDL_AtomicAddThenFetch32(&val32, 10);
bob@3202
    73
   printf("AddThenFetch32       ret=%d val=%d\n", ret32, val32);
bob@3202
    74
bob@3201
    75
   ret32 = SDL_AtomicSubtractThenFetch32(&val32, 10);
bob@3202
    76
   printf("SubtractThenFetch32  ret=%d val=%d\n", ret32, val32);
slouken@3186
    77
bob@3202
    78
#ifdef SDL_HAS_64BIT_TYPE
bob@3261
    79
   printf("\n64 bit -----------------------------------------\n\n");
bob@3202
    80
bob@3202
    81
   val64 = 0;
bob@3202
    82
   tfret = SDL_AtomicTestThenSet64(&val64);
bob@3202
    83
   printf("TestThenSet64        tfret=%s val=%lld\n", tf(tfret), val64);
bob@3202
    84
   tfret = SDL_AtomicTestThenSet64(&val64);
bob@3202
    85
   printf("TestThenSet64        tfret=%s val=%lld\n", tf(tfret), val64);
bob@3202
    86
bob@3201
    87
   SDL_AtomicClear64(&val64);
bob@3202
    88
   printf("Clear64              val=%lld\n", val64);
bob@3202
    89
bob@3201
    90
   ret64 = SDL_AtomicFetchThenIncrement64(&val64);
bob@3202
    91
   printf("FetchThenIncrement64 ret=%lld val=%lld\n", ret64, val64);
bob@3202
    92
bob@3201
    93
   ret64 = SDL_AtomicFetchThenDecrement64(&val64);
bob@3202
    94
   printf("FetchThenDecrement64 ret=%lld val=%lld\n", ret64, val64);
bob@3202
    95
bob@3201
    96
   ret64 = SDL_AtomicFetchThenAdd64(&val64, 10);
bob@3202
    97
   printf("FetchThenAdd64       ret=%lld val=%lld\n", ret64, val64);
bob@3202
    98
bob@3201
    99
   ret64 = SDL_AtomicFetchThenSubtract64(&val64, 10);
bob@3202
   100
   printf("FetchThenSubtract64  ret=%lld val=%lld\n", ret64, val64);
bob@3202
   101
bob@3201
   102
   ret64 = SDL_AtomicIncrementThenFetch64(&val64);
bob@3202
   103
   printf("IncrementThenFetch64 ret=%lld val=%lld\n", ret64, val64);
bob@3202
   104
bob@3201
   105
   ret64 = SDL_AtomicDecrementThenFetch64(&val64);
bob@3202
   106
   printf("DecrementThenFetch64 ret=%lld val=%lld\n", ret64, val64);
bob@3202
   107
bob@3201
   108
   ret64 = SDL_AtomicAddThenFetch64(&val64, 10);
bob@3202
   109
   printf("AddThenFetch64       ret=%lld val=%lld\n", ret64, val64);
bob@3202
   110
bob@3201
   111
   ret64 = SDL_AtomicSubtractThenFetch64(&val64, 10);
bob@3202
   112
   printf("SubtractThenFetch64  ret=%lld val=%lld\n", ret64, val64);
bob@3201
   113
#endif
slouken@3186
   114
bob@3201
   115
   return 0;
bob@3201
   116
   }