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