test/testatomic.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 29 Sep 2009 01:19:14 +0000
changeset 3327 35387815b155
parent 3325 bb9c3d04f126
child 3338 9de326b3099c
permissions -rw-r--r--
Better cross-platform macros for printing 32 and 64 bit values
bob@3180
     1
#include "SDL.h"
bob@3180
     2
slouken@3327
     3
/* Make sure we have good macros for printing 32 and 64 bit values */
slouken@3327
     4
#ifndef PRIu32
slouken@3327
     5
#define PRIu32 "u"
slouken@3327
     6
#endif
slouken@3327
     7
#ifndef PRIu64
slouken@3327
     8
#ifdef __WIN32__
slouken@3327
     9
#define PRIu64 "I64u"
slouken@3327
    10
#else
slouken@3327
    11
#define PRIu64 "llu"
slouken@3327
    12
#endif
slouken@3327
    13
#endif
slouken@3327
    14
bob@3201
    15
/*
bob@3202
    16
  Absolutely basic tests just to see if we get the expected value
bob@3202
    17
  after calling each function.
bob@3201
    18
*/
bob@3201
    19
bob@3202
    20
char * 
bob@3202
    21
tf(SDL_bool tf)
bob@3202
    22
{
bob@3202
    23
   static char *t = "true";
bob@3202
    24
   static char *f = "false";
bob@3202
    25
bob@3202
    26
   if (tf)
bob@3202
    27
   {
bob@3202
    28
      return t;
bob@3202
    29
   }
bob@3202
    30
bob@3202
    31
   return f;
bob@3202
    32
}
bob@3202
    33
  
bob@3180
    34
int
bob@3299
    35
main(int argc, char *argv[])
bob@3180
    36
{
bob@3180
    37
bob@3216
    38
   volatile Uint32 val32 = 0;
bob@3201
    39
   Uint32 ret32 = 0;
bob@3180
    40
bob@3216
    41
   volatile Uint64 val64 = 0;
bob@3201
    42
   Uint64 ret64 = 0;
bob@3180
    43
bob@3261
    44
   SDL_SpinLock lock = 0;
bob@3261
    45
bob@3202
    46
   SDL_bool tfret = SDL_FALSE;
bob@3202
    47
bob@3261
    48
   printf("\nspin lock---------------------------------------\n\n");
slouken@3186
    49
bob@3261
    50
   SDL_AtomicLock(&lock);
bob@3261
    51
   printf("AtomicLock                   lock=%d\n", lock);
bob@3261
    52
   SDL_AtomicUnlock(&lock);
bob@3261
    53
   printf("AtomicUnlock                 lock=%d\n", lock);
bob@3202
    54
bob@3261
    55
   printf("\n32 bit -----------------------------------------\n\n");
bob@3202
    56
bob@3202
    57
   val32 = 0;
bob@3202
    58
   tfret = SDL_AtomicTestThenSet32(&val32);
slouken@3327
    59
   printf("TestThenSet32        tfret=%s val=%"PRIu32"\n", tf(tfret), val32);
bob@3202
    60
   tfret = SDL_AtomicTestThenSet32(&val32);
slouken@3327
    61
   printf("TestThenSet32        tfret=%s val=%"PRIu32"\n", tf(tfret), val32);
bob@3202
    62
bob@3201
    63
   SDL_AtomicClear32(&val32);
slouken@3327
    64
   printf("Clear32              val=%"PRIu32"\n", val32);
bob@3202
    65
bob@3201
    66
   ret32 = SDL_AtomicFetchThenIncrement32(&val32);
slouken@3327
    67
   printf("FetchThenIncrement32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
bob@3202
    68
bob@3201
    69
   ret32 = SDL_AtomicFetchThenDecrement32(&val32);
slouken@3327
    70
   printf("FetchThenDecrement32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
bob@3202
    71
bob@3201
    72
   ret32 = SDL_AtomicFetchThenAdd32(&val32, 10);
slouken@3327
    73
   printf("FetchThenAdd32       ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
bob@3202
    74
bob@3201
    75
   ret32 = SDL_AtomicFetchThenSubtract32(&val32, 10);
slouken@3327
    76
   printf("FetchThenSubtract32  ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
bob@3202
    77
bob@3201
    78
   ret32 = SDL_AtomicIncrementThenFetch32(&val32);
slouken@3327
    79
   printf("IncrementThenFetch32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
bob@3202
    80
bob@3201
    81
   ret32 = SDL_AtomicDecrementThenFetch32(&val32);
slouken@3327
    82
   printf("DecrementThenFetch32 ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
bob@3202
    83
bob@3201
    84
   ret32 = SDL_AtomicAddThenFetch32(&val32, 10);
slouken@3327
    85
   printf("AddThenFetch32       ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
bob@3202
    86
bob@3201
    87
   ret32 = SDL_AtomicSubtractThenFetch32(&val32, 10);
slouken@3327
    88
   printf("SubtractThenFetch32  ret=%"PRIu32" val=%"PRIu32"\n", ret32, val32);
slouken@3186
    89
bob@3202
    90
#ifdef SDL_HAS_64BIT_TYPE
bob@3261
    91
   printf("\n64 bit -----------------------------------------\n\n");
bob@3202
    92
bob@3202
    93
   val64 = 0;
bob@3202
    94
   tfret = SDL_AtomicTestThenSet64(&val64);
slouken@3327
    95
   printf("TestThenSet64        tfret=%s val=%"PRIu64"\n", tf(tfret), val64);
bob@3202
    96
   tfret = SDL_AtomicTestThenSet64(&val64);
slouken@3327
    97
   printf("TestThenSet64        tfret=%s val=%"PRIu64"\n", tf(tfret), val64);
bob@3202
    98
bob@3201
    99
   SDL_AtomicClear64(&val64);
slouken@3327
   100
   printf("Clear64              val=%"PRIu64"\n", val64);
bob@3202
   101
bob@3201
   102
   ret64 = SDL_AtomicFetchThenIncrement64(&val64);
slouken@3327
   103
   printf("FetchThenIncrement64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
bob@3202
   104
bob@3201
   105
   ret64 = SDL_AtomicFetchThenDecrement64(&val64);
slouken@3327
   106
   printf("FetchThenDecrement64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
bob@3202
   107
bob@3201
   108
   ret64 = SDL_AtomicFetchThenAdd64(&val64, 10);
slouken@3327
   109
   printf("FetchThenAdd64       ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
bob@3202
   110
bob@3201
   111
   ret64 = SDL_AtomicFetchThenSubtract64(&val64, 10);
slouken@3327
   112
   printf("FetchThenSubtract64  ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
bob@3202
   113
bob@3201
   114
   ret64 = SDL_AtomicIncrementThenFetch64(&val64);
slouken@3327
   115
   printf("IncrementThenFetch64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
bob@3202
   116
bob@3201
   117
   ret64 = SDL_AtomicDecrementThenFetch64(&val64);
slouken@3327
   118
   printf("DecrementThenFetch64 ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
bob@3202
   119
bob@3201
   120
   ret64 = SDL_AtomicAddThenFetch64(&val64, 10);
slouken@3327
   121
   printf("AddThenFetch64       ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
bob@3202
   122
bob@3201
   123
   ret64 = SDL_AtomicSubtractThenFetch64(&val64, 10);
slouken@3327
   124
   printf("SubtractThenFetch64  ret=%"PRIu64" val=%"PRIu64"\n", ret64, val64);
bob@3201
   125
#endif
slouken@3186
   126
bob@3201
   127
   return 0;
bob@3201
   128
   }