test/testatomic.c
changeset 7191 75360622e65f
parent 6977 b73d51026c68
child 7435 63e300e4bd26
     1.1 --- a/test/testatomic.c	Sat May 18 12:48:50 2013 -0700
     1.2 +++ b/test/testatomic.c	Sat May 18 14:17:52 2013 -0700
     1.3 @@ -53,7 +53,7 @@
     1.4      printf("AtomicUnlock                 lock=%d\n", lock);
     1.5  
     1.6      printf("\natomic -----------------------------------------\n\n");
     1.7 -     
     1.8 +
     1.9      SDL_AtomicSet(&v, 0);
    1.10      tfret = SDL_AtomicSet(&v, 10) == 0;
    1.11      printf("AtomicSet(10)        tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
    1.12 @@ -95,27 +95,27 @@
    1.13   * at the end the value is compared with the expected
    1.14   * and with a non-atomic counter.
    1.15   */
    1.16 - 
    1.17 +
    1.18  /* Number of concurrent incrementers */
    1.19  #define NThreads 2
    1.20  #define CountInc 100
    1.21  #define VALBITS (sizeof(atomicValue)*8)
    1.22 - 
    1.23 +
    1.24  #define atomicValue int
    1.25  #define CountTo ((atomicValue)((unsigned int)(1<<(VALBITS-1))-1))
    1.26  #define NInter (CountTo/CountInc/NThreads)
    1.27  #define Expect (CountTo-NInter*CountInc*NThreads)
    1.28 - 
    1.29 +
    1.30  SDL_COMPILE_TIME_ASSERT(size, CountTo>0); /* check for rollover */
    1.31 - 
    1.32 +
    1.33  static SDL_atomic_t good = { 42 };
    1.34 - 
    1.35 +
    1.36  static atomicValue bad = 42;
    1.37 - 
    1.38 +
    1.39  static SDL_atomic_t threadsRunning;
    1.40  
    1.41  static SDL_sem *threadDone;
    1.42 - 
    1.43 +
    1.44  static
    1.45  int adder(void* junk)
    1.46  {
    1.47 @@ -129,111 +129,111 @@
    1.48      SDL_SemPost(threadDone);
    1.49      return 0;
    1.50  }
    1.51 - 
    1.52 +
    1.53  static
    1.54  void runAdder(void)
    1.55  {
    1.56      Uint32 start, end;
    1.57      int T=NThreads;
    1.58 - 
    1.59 +
    1.60      start = SDL_GetTicks();
    1.61 - 
    1.62 +
    1.63      threadDone = SDL_CreateSemaphore(0);
    1.64  
    1.65      SDL_AtomicSet(&threadsRunning, NThreads);
    1.66  
    1.67      while (T--)
    1.68          SDL_CreateThread(adder, "Adder", NULL);
    1.69 - 
    1.70 +
    1.71      while (SDL_AtomicGet(&threadsRunning) > 0)
    1.72          SDL_SemWait(threadDone);
    1.73 - 
    1.74 +
    1.75      SDL_DestroySemaphore(threadDone);
    1.76  
    1.77      end = SDL_GetTicks();
    1.78 - 
    1.79 +
    1.80      printf("Finished in %f sec\n", (end - start) / 1000.f);
    1.81  }
    1.82 - 
    1.83 +
    1.84  static
    1.85  void RunEpicTest()
    1.86  {
    1.87      int b;
    1.88      atomicValue v;
    1.89 - 
    1.90 +
    1.91      printf("\nepic test---------------------------------------\n\n");
    1.92  
    1.93      printf("Size asserted to be >= 32-bit\n");
    1.94      SDL_assert(sizeof(atomicValue)>=4);
    1.95 - 
    1.96 +
    1.97      printf("Check static initializer\n");
    1.98      v=SDL_AtomicGet(&good);
    1.99      SDL_assert(v==42);
   1.100 - 
   1.101 +
   1.102      SDL_assert(bad==42);
   1.103 - 
   1.104 +
   1.105      printf("Test negative values\n");
   1.106      SDL_AtomicSet(&good, -5);
   1.107      v=SDL_AtomicGet(&good);
   1.108      SDL_assert(v==-5);
   1.109 - 
   1.110 +
   1.111      printf("Verify maximum value\n");
   1.112      SDL_AtomicSet(&good, CountTo);
   1.113      v=SDL_AtomicGet(&good);
   1.114      SDL_assert(v==CountTo);
   1.115 - 
   1.116 +
   1.117      printf("Test compare and exchange\n");
   1.118 - 
   1.119 +
   1.120      b=SDL_AtomicCAS(&good, 500, 43);
   1.121      SDL_assert(!b); /* no swap since CountTo!=500 */
   1.122      v=SDL_AtomicGet(&good);
   1.123      SDL_assert(v==CountTo); /* ensure no swap */
   1.124 - 
   1.125 +
   1.126      b=SDL_AtomicCAS(&good, CountTo, 44);
   1.127      SDL_assert(!!b); /* will swap */
   1.128      v=SDL_AtomicGet(&good);
   1.129      SDL_assert(v==44);
   1.130 - 
   1.131 +
   1.132      printf("Test Add\n");
   1.133 - 
   1.134 +
   1.135      v=SDL_AtomicAdd(&good, 1);
   1.136      SDL_assert(v==44);
   1.137      v=SDL_AtomicGet(&good);
   1.138      SDL_assert(v==45);
   1.139 - 
   1.140 +
   1.141      v=SDL_AtomicAdd(&good, 10);
   1.142      SDL_assert(v==45);
   1.143      v=SDL_AtomicGet(&good);
   1.144      SDL_assert(v==55);
   1.145 - 
   1.146 +
   1.147      printf("Test Add (Negative values)\n");
   1.148 - 
   1.149 +
   1.150      v=SDL_AtomicAdd(&good, -20);
   1.151      SDL_assert(v==55);
   1.152      v=SDL_AtomicGet(&good);
   1.153      SDL_assert(v==35);
   1.154 - 
   1.155 +
   1.156      v=SDL_AtomicAdd(&good, -50); /* crossing zero down */
   1.157      SDL_assert(v==35);
   1.158      v=SDL_AtomicGet(&good);
   1.159      SDL_assert(v==-15);
   1.160 - 
   1.161 +
   1.162      v=SDL_AtomicAdd(&good, 30); /* crossing zero up */
   1.163      SDL_assert(v==-15);
   1.164      v=SDL_AtomicGet(&good);
   1.165      SDL_assert(v==15);
   1.166 - 
   1.167 +
   1.168      printf("Reset before count down test\n");
   1.169      SDL_AtomicSet(&good, CountTo);
   1.170      v=SDL_AtomicGet(&good);
   1.171      SDL_assert(v==CountTo);
   1.172 - 
   1.173 +
   1.174      bad=CountTo;
   1.175      SDL_assert(bad==CountTo);
   1.176 - 
   1.177 +
   1.178      printf("Counting down from %d, Expect %d remaining\n",CountTo,Expect);
   1.179      runAdder();
   1.180 - 
   1.181 +
   1.182      v=SDL_AtomicGet(&good);
   1.183      printf("Atomic %d Non-Atomic %d\n",v,bad);
   1.184      SDL_assert(v==Expect);
   1.185 @@ -599,7 +599,7 @@
   1.186      Uint32 start, end;
   1.187      int i, j;
   1.188      int grand_total;
   1.189 - 
   1.190 +
   1.191      printf("\nFIFO test---------------------------------------\n\n");
   1.192      printf("Mode: %s\n", lock_free ? "LockFree" : "Mutex");
   1.193  
   1.194 @@ -614,7 +614,7 @@
   1.195      }
   1.196  
   1.197      start = SDL_GetTicks();
   1.198 - 
   1.199 +
   1.200  #ifdef TEST_SPINLOCK_FIFO
   1.201      /* Start a monitoring thread */
   1.202      if (lock_free) {
   1.203 @@ -646,12 +646,12 @@
   1.204          writerData[i].lock_free = lock_free;
   1.205          SDL_CreateThread(FIFO_Writer, name, &writerData[i]);
   1.206      }
   1.207 - 
   1.208 +
   1.209      /* Wait for the writers */
   1.210      while (SDL_AtomicGet(&writersRunning) > 0) {
   1.211          SDL_SemWait(writersDone);
   1.212      }
   1.213 - 
   1.214 +
   1.215      /* Shut down the queue so readers exit */
   1.216      queue.active = SDL_FALSE;
   1.217  
   1.218 @@ -661,14 +661,14 @@
   1.219      }
   1.220  
   1.221      end = SDL_GetTicks();
   1.222 - 
   1.223 +
   1.224      SDL_DestroySemaphore(readersDone);
   1.225      SDL_DestroySemaphore(writersDone);
   1.226  
   1.227      if (!lock_free) {
   1.228          SDL_DestroyMutex(queue.mutex);
   1.229      }
   1.230 - 
   1.231 +
   1.232      printf("Finished in %f sec\n", (end - start) / 1000.f);
   1.233  
   1.234      printf("\n");