test/testatomic.c
author Bob Pendleton <bob@pendleton.com>
Mon, 29 Jun 2009 19:54:43 +0000
changeset 3202 3aa519a5c676
parent 3201 c297230efc75
child 3216 48a80f2a7ff2
permissions -rw-r--r--
I've made so many changes I don't dare continue until I check the current stuff in.

/test/testatomic.c performs absolutely basic tests to show that the function work as expected. Need a second test to do more detailed tests.

/include/SDL_atomic.h provides declarations for all included functions.

/src/atomic/linux/SDL_atomic.c provided all the functions. On a generic built the 64 bit functions work, but they are emulated. On a build for -march=pentium and above the 64 bit functions use native instructions
/src/atomic/dummy/SDL_atomic.c emulates all the operations using SDL_mutex.h.
/src/atomic/win32/SDL_atomic.c is a copy of dummy
/src/atomic/macosx/SDL_atomic.s is a copy of dummy

These versions of SDL_atomic.c provide a frame work for building the library with a mixture of native and emulated functions. This allows the whole library to be provided on all platforms. (I hope.)
I hope this fits with the SDL philosophy of either providing a common subset or emulating when the platform is missing a feature.

I have not added dummy, macosx, or win32 to the build. They are there as place holders for future work.

I have modified congifure.in to compile sources in /src/atomic/linux. (The SDL configure.in file is an amazing piece of work and I hope I didn't mess it up. :-)
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@3202
    26
   Uint8 val8 = 0;
bob@3202
    27
   Uint8 ret8 = 0;
bob@3202
    28
bob@3202
    29
   Uint16 val16 = 0;
bob@3202
    30
   Uint16 ret16 = 0;
bob@3202
    31
bob@3201
    32
   Uint32 val32 = 0;
bob@3201
    33
   Uint32 ret32 = 0;
bob@3180
    34
bob@3201
    35
   Uint64 val64 = 0;
bob@3201
    36
   Uint64 ret64 = 0;
bob@3180
    37
bob@3202
    38
   SDL_bool tfret = SDL_FALSE;
bob@3202
    39
bob@3202
    40
bob@3202
    41
   printf("8 bit -----------------------------------------\n\n");
bob@3202
    42
bob@3202
    43
   ret8 = SDL_AtomicExchange8(&val8, 10);
bob@3202
    44
   printf("Exchange8           ret=%d val=%d\n", ret8, val8);
bob@3202
    45
   ret8 = SDL_AtomicExchange8(&val8, 0);
bob@3202
    46
   printf("Exchange8           ret=%d val=%d\n", ret8, val8);
bob@3202
    47
bob@3202
    48
   val8 = 10;
bob@3202
    49
   tfret = SDL_AtomicCompareThenSet8(&val8, 10, 20);
bob@3202
    50
   printf("CompareThenSet8     tfret=%s val=%d\n", tf(tfret), val8);
bob@3202
    51
   val8 = 10;
bob@3202
    52
   tfret = SDL_AtomicCompareThenSet8(&val8, 0, 20);
bob@3202
    53
   printf("CompareThenSet8     tfret=%s val=%d\n", tf(tfret), val8);
bob@3202
    54
bob@3202
    55
   val8 = 0;
bob@3202
    56
   tfret = SDL_AtomicTestThenSet8(&val8);
bob@3202
    57
   printf("TestThenSet8        tfret=%s val=%d\n", tf(tfret), val8);
bob@3202
    58
   tfret = SDL_AtomicTestThenSet8(&val8);
bob@3202
    59
   printf("TestThenSet8        tfret=%s val=%d\n", tf(tfret), val8);
bob@3202
    60
bob@3202
    61
   SDL_AtomicClear8(&val8);
bob@3202
    62
   printf("Clear8              val=%d\n", val8);
bob@3202
    63
bob@3202
    64
   ret8 = SDL_AtomicFetchThenIncrement8(&val8);
bob@3202
    65
   printf("FetchThenIncrement8 ret=%d val=%d\n", ret8, val8);
bob@3202
    66
bob@3202
    67
   ret8 = SDL_AtomicFetchThenDecrement8(&val8);
bob@3202
    68
   printf("FetchThenDecrement8 ret=%d val=%d\n", ret8, val8);
bob@3202
    69
bob@3202
    70
   ret8 = SDL_AtomicFetchThenAdd8(&val8, 10);
bob@3202
    71
   printf("FetchThenAdd8       ret=%d val=%d\n", ret8, val8);
bob@3202
    72
bob@3202
    73
   ret8 = SDL_AtomicFetchThenSubtract8(&val8, 10);
bob@3202
    74
   printf("FetchThenSubtract8  ret=%d val=%d\n", ret8, val8);
bob@3202
    75
bob@3202
    76
   ret8 = SDL_AtomicIncrementThenFetch8(&val8);
bob@3202
    77
   printf("IncrementThenFetch8 ret=%d val=%d\n", ret8, val8);
bob@3202
    78
bob@3202
    79
   ret8 = SDL_AtomicDecrementThenFetch8(&val8);
bob@3202
    80
   printf("DecrementThenFetch8 ret=%d val=%d\n", ret8, val8);
bob@3202
    81
bob@3202
    82
   ret8 = SDL_AtomicAddThenFetch8(&val8, 10);
bob@3202
    83
   printf("AddThenFetch8       ret=%d val=%d\n", ret8, val8);
bob@3202
    84
bob@3202
    85
   ret8 = SDL_AtomicSubtractThenFetch8(&val8, 10);
bob@3202
    86
   printf("SubtractThenFetch8  ret=%d val=%d\n", ret8, val8);
bob@3202
    87
bob@3202
    88
bob@3202
    89
   printf("16 bit -----------------------------------------\n\n");
bob@3202
    90
bob@3202
    91
   ret16 = SDL_AtomicExchange16(&val16, 10);
bob@3202
    92
   printf("Exchange16           ret=%d val=%d\n", ret16, val16);
bob@3202
    93
   ret16 = SDL_AtomicExchange16(&val16, 0);
bob@3202
    94
   printf("Exchange16           ret=%d val=%d\n", ret16, val16);
bob@3202
    95
bob@3202
    96
   val16 = 10;
bob@3202
    97
   tfret = SDL_AtomicCompareThenSet16(&val16, 10, 20);
bob@3202
    98
   printf("CompareThenSet16     tfret=%s val=%d\n", tf(tfret), val16);
bob@3202
    99
   val16 = 10;
bob@3202
   100
   tfret = SDL_AtomicCompareThenSet16(&val16, 0, 20);
bob@3202
   101
   printf("CompareThenSet16     tfret=%s val=%d\n", tf(tfret), val16);
bob@3202
   102
bob@3202
   103
   val16 = 0;
bob@3202
   104
   tfret = SDL_AtomicTestThenSet16(&val16);
bob@3202
   105
   printf("TestThenSet16        tfret=%s val=%d\n", tf(tfret), val16);
bob@3202
   106
   tfret = SDL_AtomicTestThenSet16(&val16);
bob@3202
   107
   printf("TestThenSet16        tfret=%s val=%d\n", tf(tfret), val16);
bob@3202
   108
bob@3202
   109
   SDL_AtomicClear16(&val16);
bob@3202
   110
   printf("Clear16              val=%d\n", val16);
bob@3202
   111
bob@3202
   112
   ret16 = SDL_AtomicFetchThenIncrement16(&val16);
bob@3202
   113
   printf("FetchThenIncrement16 ret=%d val=%d\n", ret16, val16);
bob@3202
   114
bob@3202
   115
   ret16 = SDL_AtomicFetchThenDecrement16(&val16);
bob@3202
   116
   printf("FetchThenDecrement16 ret=%d val=%d\n", ret16, val16);
bob@3202
   117
bob@3202
   118
   ret16 = SDL_AtomicFetchThenAdd16(&val16, 10);
bob@3202
   119
   printf("FetchThenAdd16       ret=%d val=%d\n", ret16, val16);
bob@3202
   120
bob@3202
   121
   ret16 = SDL_AtomicFetchThenSubtract16(&val16, 10);
bob@3202
   122
   printf("FetchThenSubtract16  ret=%d val=%d\n", ret16, val16);
bob@3202
   123
bob@3202
   124
   ret16 = SDL_AtomicIncrementThenFetch16(&val16);
bob@3202
   125
   printf("IncrementThenFetch16 ret=%d val=%d\n", ret16, val16);
bob@3202
   126
bob@3202
   127
   ret16 = SDL_AtomicDecrementThenFetch16(&val16);
bob@3202
   128
   printf("DecrementThenFetch16 ret=%d val=%d\n", ret16, val16);
bob@3202
   129
bob@3202
   130
   ret16 = SDL_AtomicAddThenFetch16(&val16, 10);
bob@3202
   131
   printf("AddThenFetch16       ret=%d val=%d\n", ret16, val16);
bob@3202
   132
bob@3202
   133
   ret16 = SDL_AtomicSubtractThenFetch16(&val16, 10);
bob@3202
   134
   printf("SubtractThenFetch16  ret=%d val=%d\n", ret16, val16);
bob@3202
   135
bob@3202
   136
   printf("32 bit -----------------------------------------\n\n");
slouken@3186
   137
bob@3201
   138
   ret32 = SDL_AtomicExchange32(&val32, 10);
bob@3202
   139
   printf("Exchange32           ret=%d val=%d\n", ret32, val32);
bob@3202
   140
   ret32 = SDL_AtomicExchange32(&val32, 0);
bob@3202
   141
   printf("Exchange32           ret=%d val=%d\n", ret32, val32);
bob@3202
   142
bob@3202
   143
   val32 = 10;
bob@3202
   144
   tfret = SDL_AtomicCompareThenSet32(&val32, 10, 20);
bob@3202
   145
   printf("CompareThenSet32     tfret=%s val=%d\n", tf(tfret), val32);
bob@3202
   146
   val32 = 10;
bob@3202
   147
   tfret = SDL_AtomicCompareThenSet32(&val32, 0, 20);
bob@3202
   148
   printf("CompareThenSet32     tfret=%s val=%d\n", tf(tfret), val32);
bob@3202
   149
bob@3202
   150
   val32 = 0;
bob@3202
   151
   tfret = SDL_AtomicTestThenSet32(&val32);
bob@3202
   152
   printf("TestThenSet32        tfret=%s val=%d\n", tf(tfret), val32);
bob@3202
   153
   tfret = SDL_AtomicTestThenSet32(&val32);
bob@3202
   154
   printf("TestThenSet32        tfret=%s val=%d\n", tf(tfret), val32);
bob@3202
   155
bob@3201
   156
   SDL_AtomicClear32(&val32);
bob@3202
   157
   printf("Clear32              val=%d\n", val32);
bob@3202
   158
bob@3201
   159
   ret32 = SDL_AtomicFetchThenIncrement32(&val32);
bob@3202
   160
   printf("FetchThenIncrement32 ret=%d val=%d\n", ret32, val32);
bob@3202
   161
bob@3201
   162
   ret32 = SDL_AtomicFetchThenDecrement32(&val32);
bob@3202
   163
   printf("FetchThenDecrement32 ret=%d val=%d\n", ret32, val32);
bob@3202
   164
bob@3201
   165
   ret32 = SDL_AtomicFetchThenAdd32(&val32, 10);
bob@3202
   166
   printf("FetchThenAdd32       ret=%d val=%d\n", ret32, val32);
bob@3202
   167
bob@3201
   168
   ret32 = SDL_AtomicFetchThenSubtract32(&val32, 10);
bob@3202
   169
   printf("FetchThenSubtract32  ret=%d val=%d\n", ret32, val32);
bob@3202
   170
bob@3201
   171
   ret32 = SDL_AtomicIncrementThenFetch32(&val32);
bob@3202
   172
   printf("IncrementThenFetch32 ret=%d val=%d\n", ret32, val32);
bob@3202
   173
bob@3201
   174
   ret32 = SDL_AtomicDecrementThenFetch32(&val32);
bob@3202
   175
   printf("DecrementThenFetch32 ret=%d val=%d\n", ret32, val32);
bob@3202
   176
bob@3201
   177
   ret32 = SDL_AtomicAddThenFetch32(&val32, 10);
bob@3202
   178
   printf("AddThenFetch32       ret=%d val=%d\n", ret32, val32);
bob@3202
   179
bob@3201
   180
   ret32 = SDL_AtomicSubtractThenFetch32(&val32, 10);
bob@3202
   181
   printf("SubtractThenFetch32  ret=%d val=%d\n", ret32, val32);
slouken@3186
   182
bob@3202
   183
#ifdef SDL_HAS_64BIT_TYPE
bob@3202
   184
   printf("64 bit -----------------------------------------\n\n");
slouken@3186
   185
bob@3201
   186
   ret64 = SDL_AtomicExchange64(&val64, 10);
bob@3202
   187
   printf("Exchange64           ret=%lld val=%lld\n", ret64, val64);
bob@3202
   188
   ret64 = SDL_AtomicExchange64(&val64, 0);
bob@3202
   189
   printf("Exchange64           ret=%lld val=%lld\n", ret64, val64);
bob@3202
   190
bob@3202
   191
   val64 = 10;
bob@3202
   192
   tfret = SDL_AtomicCompareThenSet64(&val64, 10, 20);
bob@3202
   193
   printf("CompareThenSet64     tfret=%s val=%lld\n", tf(tfret), val64);
bob@3202
   194
   val64 = 10;
bob@3202
   195
   tfret = SDL_AtomicCompareThenSet64(&val64, 0, 20);
bob@3202
   196
   printf("CompareThenSet64     tfret=%s val=%lld\n", tf(tfret), val64);
bob@3202
   197
bob@3202
   198
   val64 = 0;
bob@3202
   199
   tfret = SDL_AtomicTestThenSet64(&val64);
bob@3202
   200
   printf("TestThenSet64        tfret=%s val=%lld\n", tf(tfret), val64);
bob@3202
   201
   tfret = SDL_AtomicTestThenSet64(&val64);
bob@3202
   202
   printf("TestThenSet64        tfret=%s val=%lld\n", tf(tfret), val64);
bob@3202
   203
bob@3201
   204
   SDL_AtomicClear64(&val64);
bob@3202
   205
   printf("Clear64              val=%lld\n", val64);
bob@3202
   206
bob@3201
   207
   ret64 = SDL_AtomicFetchThenIncrement64(&val64);
bob@3202
   208
   printf("FetchThenIncrement64 ret=%lld val=%lld\n", ret64, val64);
bob@3202
   209
bob@3201
   210
   ret64 = SDL_AtomicFetchThenDecrement64(&val64);
bob@3202
   211
   printf("FetchThenDecrement64 ret=%lld val=%lld\n", ret64, val64);
bob@3202
   212
bob@3201
   213
   ret64 = SDL_AtomicFetchThenAdd64(&val64, 10);
bob@3202
   214
   printf("FetchThenAdd64       ret=%lld val=%lld\n", ret64, val64);
bob@3202
   215
bob@3201
   216
   ret64 = SDL_AtomicFetchThenSubtract64(&val64, 10);
bob@3202
   217
   printf("FetchThenSubtract64  ret=%lld val=%lld\n", ret64, val64);
bob@3202
   218
bob@3201
   219
   ret64 = SDL_AtomicIncrementThenFetch64(&val64);
bob@3202
   220
   printf("IncrementThenFetch64 ret=%lld val=%lld\n", ret64, val64);
bob@3202
   221
bob@3201
   222
   ret64 = SDL_AtomicDecrementThenFetch64(&val64);
bob@3202
   223
   printf("DecrementThenFetch64 ret=%lld val=%lld\n", ret64, val64);
bob@3202
   224
bob@3201
   225
   ret64 = SDL_AtomicAddThenFetch64(&val64, 10);
bob@3202
   226
   printf("AddThenFetch64       ret=%lld val=%lld\n", ret64, val64);
bob@3202
   227
bob@3201
   228
   ret64 = SDL_AtomicSubtractThenFetch64(&val64, 10);
bob@3202
   229
   printf("SubtractThenFetch64  ret=%lld val=%lld\n", ret64, val64);
bob@3201
   230
#endif
slouken@3186
   231
bob@3201
   232
   return 0;
bob@3201
   233
   }