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