upgrade SDL to version 2.0.8
[platform/upstream/SDL.git] / test / testatomic.c
index 978815c..bc2cd83 100755 (executable)
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
+  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
 
   This software is provided 'as-is', without any express or implied
   warranty.  In no event will the authors be held liable for any damages
@@ -42,42 +42,40 @@ void RunBasicTest()
     SDL_atomic_t v;
     SDL_bool tfret = SDL_FALSE;
 
-    SDLTest_Log("\nspin lock---------------------------------------\n\n");
+    SDL_Log("\nspin lock---------------------------------------\n\n");
 
     SDL_AtomicLock(&lock);
-    SDLTest_Log("AtomicLock                   lock=%d\n", lock);
+    SDL_Log("AtomicLock                   lock=%d\n", lock);
     SDL_AtomicUnlock(&lock);
-    SDLTest_Log("AtomicUnlock                 lock=%d\n", lock);
+    SDL_Log("AtomicUnlock                 lock=%d\n", lock);
 
-    SDLTest_Log("\natomic -----------------------------------------\n\n");
+    SDL_Log("\natomic -----------------------------------------\n\n");
 
     SDL_AtomicSet(&v, 0);
     tfret = SDL_AtomicSet(&v, 10) == 0 ? SDL_TRUE : SDL_FALSE;
-    SDLTest_Log("AtomicSet(10)        tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
+    SDL_Log("AtomicSet(10)        tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
     tfret = SDL_AtomicAdd(&v, 10) == 10 ? SDL_TRUE : SDL_FALSE;
-    SDLTest_Log("AtomicAdd(10)        tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
+    SDL_Log("AtomicAdd(10)        tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
 
     SDL_AtomicSet(&v, 0);
     SDL_AtomicIncRef(&v);
     tfret = (SDL_AtomicGet(&v) == 1) ? SDL_TRUE : SDL_FALSE;
-    SDLTest_Log("AtomicIncRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
+    SDL_Log("AtomicIncRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
     SDL_AtomicIncRef(&v);
     tfret = (SDL_AtomicGet(&v) == 2) ? SDL_TRUE : SDL_FALSE;
-    SDLTest_Log("AtomicIncRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
+    SDL_Log("AtomicIncRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
     tfret = (SDL_AtomicDecRef(&v) == SDL_FALSE) ? SDL_TRUE : SDL_FALSE;
-    SDLTest_Log("AtomicDecRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
+    SDL_Log("AtomicDecRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
     tfret = (SDL_AtomicDecRef(&v) == SDL_TRUE) ? SDL_TRUE : SDL_FALSE;
-    SDLTest_Log("AtomicDecRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
+    SDL_Log("AtomicDecRef()       tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
 
     SDL_AtomicSet(&v, 10);
     tfret = (SDL_AtomicCAS(&v, 0, 20) == SDL_FALSE) ? SDL_TRUE : SDL_FALSE;
-    SDLTest_Log("AtomicCAS()          tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
+    SDL_Log("AtomicCAS()          tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
     value = SDL_AtomicGet(&v);
     tfret = (SDL_AtomicCAS(&v, value, 20) == SDL_TRUE) ? SDL_TRUE : SDL_FALSE;
-    SDLTest_Log("AtomicCAS()          tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
-       
-       SDL_MemoryBarrierAcquire();
-       SDL_MemoryBarrierRelease();
+    SDL_Log("AtomicCAS()          tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
+
 }
 
 /**************************************************************************/
@@ -106,7 +104,10 @@ void RunBasicTest()
 #define NInter (CountTo/CountInc/NThreads)
 #define Expect (CountTo-NInter*CountInc*NThreads)
 
-SDL_COMPILE_TIME_ASSERT(size, CountTo>0); /* check for rollover */
+enum {
+   CountTo_GreaterThanZero = CountTo > 0,
+};
+SDL_COMPILE_TIME_ASSERT(size, CountTo_GreaterThanZero); /* check for rollover */
 
 static SDL_atomic_t good = { 42 };
 
@@ -117,10 +118,10 @@ static SDL_atomic_t threadsRunning;
 static SDL_sem *threadDone;
 
 static
-int adder(void* junk)
+int SDLCALL adder(void* junk)
 {
     unsigned long N=NInter;
-    SDLTest_Log("Thread subtracting %d %lu times\n",CountInc,N);
+    SDL_Log("Thread subtracting %d %lu times\n",CountInc,N);
     while (N--) {
         SDL_AtomicAdd(&good, -CountInc);
         bad-=CountInc;
@@ -152,7 +153,7 @@ void runAdder(void)
 
     end = SDL_GetTicks();
 
-    SDLTest_Log("Finished in %f sec\n", (end - start) / 1000.f);
+    SDL_Log("Finished in %f sec\n", (end - start) / 1000.f);
 }
 
 static
@@ -161,28 +162,28 @@ void RunEpicTest()
     int b;
     atomicValue v;
 
-    SDLTest_Log("\nepic test---------------------------------------\n\n");
+    SDL_Log("\nepic test---------------------------------------\n\n");
 
-    SDLTest_Log("Size asserted to be >= 32-bit\n");
+    SDL_Log("Size asserted to be >= 32-bit\n");
     SDL_assert(sizeof(atomicValue)>=4);
 
-    SDLTest_Log("Check static initializer\n");
+    SDL_Log("Check static initializer\n");
     v=SDL_AtomicGet(&good);
     SDL_assert(v==42);
 
     SDL_assert(bad==42);
 
-    SDLTest_Log("Test negative values\n");
+    SDL_Log("Test negative values\n");
     SDL_AtomicSet(&good, -5);
     v=SDL_AtomicGet(&good);
     SDL_assert(v==-5);
 
-    SDLTest_Log("Verify maximum value\n");
+    SDL_Log("Verify maximum value\n");
     SDL_AtomicSet(&good, CountTo);
     v=SDL_AtomicGet(&good);
     SDL_assert(v==CountTo);
 
-    SDLTest_Log("Test compare and exchange\n");
+    SDL_Log("Test compare and exchange\n");
 
     b=SDL_AtomicCAS(&good, 500, 43);
     SDL_assert(!b); /* no swap since CountTo!=500 */
@@ -194,7 +195,7 @@ void RunEpicTest()
     v=SDL_AtomicGet(&good);
     SDL_assert(v==44);
 
-    SDLTest_Log("Test Add\n");
+    SDL_Log("Test Add\n");
 
     v=SDL_AtomicAdd(&good, 1);
     SDL_assert(v==44);
@@ -206,7 +207,7 @@ void RunEpicTest()
     v=SDL_AtomicGet(&good);
     SDL_assert(v==55);
 
-    SDLTest_Log("Test Add (Negative values)\n");
+    SDL_Log("Test Add (Negative values)\n");
 
     v=SDL_AtomicAdd(&good, -20);
     SDL_assert(v==55);
@@ -223,7 +224,7 @@ void RunEpicTest()
     v=SDL_AtomicGet(&good);
     SDL_assert(v==15);
 
-    SDLTest_Log("Reset before count down test\n");
+    SDL_Log("Reset before count down test\n");
     SDL_AtomicSet(&good, CountTo);
     v=SDL_AtomicGet(&good);
     SDL_assert(v==CountTo);
@@ -231,11 +232,11 @@ void RunEpicTest()
     bad=CountTo;
     SDL_assert(bad==CountTo);
 
-    SDLTest_Log("Counting down from %d, Expect %d remaining\n",CountTo,Expect);
+    SDL_Log("Counting down from %d, Expect %d remaining\n",CountTo,Expect);
     runAdder();
 
     v=SDL_AtomicGet(&good);
-    SDLTest_Log("Atomic %d Non-Atomic %d\n",v,bad);
+    SDL_Log("Atomic %d Non-Atomic %d\n",v,bad);
     SDL_assert(v==Expect);
     SDL_assert(bad!=Expect);
 }
@@ -287,7 +288,7 @@ typedef struct
     char cache_pad4[SDL_CACHELINE_SIZE-sizeof(SDL_SpinLock)-2*sizeof(SDL_atomic_t)];
 #endif
 
-    volatile SDL_bool active;
+    SDL_atomic_t active;
 
     /* Only needed for the mutex test */
     SDL_mutex *mutex;
@@ -308,7 +309,7 @@ static void InitEventQueue(SDL_EventQueue *queue)
     SDL_AtomicSet(&queue->rwcount, 0);
     SDL_AtomicSet(&queue->watcher, 0);
 #endif
-    queue->active = SDL_TRUE;
+    SDL_AtomicSet(&queue->active, 1);
 }
 
 static SDL_bool EnqueueEvent_LockFree(SDL_EventQueue *queue, const SDL_Event *event)
@@ -430,7 +431,7 @@ static SDL_bool EnqueueEvent_Mutex(SDL_EventQueue *queue, const SDL_Event *event
     } else if (delta < 0) {
         /* We ran into an old queue entry, which means it still needs to be dequeued */
     } else {
-        SDLTest_Log("ERROR: mutex failed!\n");
+        SDL_Log("ERROR: mutex failed!\n");
     }
 
     SDL_UnlockMutex(queue->mutex);
@@ -463,7 +464,7 @@ static SDL_bool DequeueEvent_Mutex(SDL_EventQueue *queue, SDL_Event *event)
     } else if (delta < 0) {
         /* We ran into an old queue entry, which means we've hit empty */
     } else {
-        SDLTest_Log("ERROR: mutex failed!\n");
+        SDL_Log("ERROR: mutex failed!\n");
     }
 
     SDL_UnlockMutex(queue->mutex);
@@ -495,7 +496,7 @@ typedef struct
     char padding[SDL_CACHELINE_SIZE-(sizeof(SDL_EventQueue*)+sizeof(int)*NUM_WRITERS+sizeof(int)+sizeof(SDL_bool))%SDL_CACHELINE_SIZE];
 } ReaderData;
 
-static int FIFO_Writer(void* _data)
+static int SDLCALL FIFO_Writer(void* _data)
 {
     WriterData *data = (WriterData *)_data;
     SDL_EventQueue *queue = data->queue;
@@ -530,7 +531,7 @@ static int FIFO_Writer(void* _data)
     return 0;
 }
 
-static int FIFO_Reader(void* _data)
+static int SDLCALL FIFO_Reader(void* _data)
 {
     ReaderData *data = (ReaderData *)_data;
     SDL_EventQueue *queue = data->queue;
@@ -541,7 +542,7 @@ static int FIFO_Reader(void* _data)
             if (DequeueEvent_LockFree(queue, &event)) {
                 WriterData *writer = (WriterData*)event.user.data1;
                 ++data->counters[writer->index];
-            } else if (queue->active) {
+            } else if (SDL_AtomicGet(&queue->active)) {
                 ++data->waits;
                 SDL_Delay(0);
             } else {
@@ -554,7 +555,7 @@ static int FIFO_Reader(void* _data)
             if (DequeueEvent_Mutex(queue, &event)) {
                 WriterData *writer = (WriterData*)event.user.data1;
                 ++data->counters[writer->index];
-            } else if (queue->active) {
+            } else if (SDL_AtomicGet(&queue->active)) {
                 ++data->waits;
                 SDL_Delay(0);
             } else {
@@ -570,11 +571,11 @@ static int FIFO_Reader(void* _data)
 
 #ifdef TEST_SPINLOCK_FIFO
 /* This thread periodically locks the queue for no particular reason */
-static int FIFO_Watcher(void* _data)
+static int SDLCALL FIFO_Watcher(void* _data)
 {
     SDL_EventQueue *queue = (SDL_EventQueue *)_data;
 
-    while (queue->active) {
+    while (SDL_AtomicGet(&queue->active)) {
         SDL_AtomicLock(&queue->lock);
         SDL_AtomicIncRef(&queue->watcher);
         while (SDL_AtomicGet(&queue->rwcount) > 0) {
@@ -600,10 +601,10 @@ static void RunFIFOTest(SDL_bool lock_free)
     int i, j;
     int grand_total;
     char textBuffer[1024];
-    int len;
+    size_t len;
 
-    SDLTest_Log("\nFIFO test---------------------------------------\n\n");
-    SDLTest_Log("Mode: %s\n", lock_free ? "LockFree" : "Mutex");
+    SDL_Log("\nFIFO test---------------------------------------\n\n");
+    SDL_Log("Mode: %s\n", lock_free ? "LockFree" : "Mutex");
 
     readersDone = SDL_CreateSemaphore(0);
     writersDone = SDL_CreateSemaphore(0);
@@ -625,7 +626,7 @@ static void RunFIFOTest(SDL_bool lock_free)
 #endif
 
     /* Start the readers first */
-    SDLTest_Log("Starting %d readers\n", NUM_READERS);
+    SDL_Log("Starting %d readers\n", NUM_READERS);
     SDL_zero(readerData);
     SDL_AtomicSet(&readersRunning, NUM_READERS);
     for (i = 0; i < NUM_READERS; ++i) {
@@ -637,7 +638,7 @@ static void RunFIFOTest(SDL_bool lock_free)
     }
 
     /* Start up the writers */
-    SDLTest_Log("Starting %d writers\n", NUM_WRITERS);
+    SDL_Log("Starting %d writers\n", NUM_WRITERS);
     SDL_zero(writerData);
     SDL_AtomicSet(&writersRunning, NUM_WRITERS);
     for (i = 0; i < NUM_WRITERS; ++i) {
@@ -655,7 +656,7 @@ static void RunFIFOTest(SDL_bool lock_free)
     }
 
     /* Shut down the queue so readers exit */
-    queue.active = SDL_FALSE;
+    SDL_AtomicSet(&queue.active, 0);
 
     /* Wait for the readers */
     while (SDL_AtomicGet(&readersRunning) > 0) {
@@ -671,16 +672,16 @@ static void RunFIFOTest(SDL_bool lock_free)
         SDL_DestroyMutex(queue.mutex);
     }
 
-    SDLTest_Log("Finished in %f sec\n", (end - start) / 1000.f);
+    SDL_Log("Finished in %f sec\n", (end - start) / 1000.f);
 
-    SDLTest_Log("\n");
+    SDL_Log("\n");
     for (i = 0; i < NUM_WRITERS; ++i) {
-        SDLTest_Log("Writer %d wrote %d events, had %d waits\n", i, EVENTS_PER_WRITER, writerData[i].waits);
+        SDL_Log("Writer %d wrote %d events, had %d waits\n", i, EVENTS_PER_WRITER, writerData[i].waits);
     }
-    SDLTest_Log("Writers wrote %d total events\n", NUM_WRITERS*EVENTS_PER_WRITER);
+    SDL_Log("Writers wrote %d total events\n", NUM_WRITERS*EVENTS_PER_WRITER);
 
     /* Print a breakdown of which readers read messages from which writer */
-    SDLTest_Log("\n");
+    SDL_Log("\n");
     grand_total = 0;
     for (i = 0; i < NUM_READERS; ++i) {
         int total = 0;
@@ -688,7 +689,7 @@ static void RunFIFOTest(SDL_bool lock_free)
             total += readerData[i].counters[j];
         }
         grand_total += total;
-        SDLTest_Log("Reader %d read %d events, had %d waits\n", i, total, readerData[i].waits);
+        SDL_Log("Reader %d read %d events, had %d waits\n", i, total, readerData[i].waits);
         SDL_snprintf(textBuffer, sizeof(textBuffer), "  { ");
         for (j = 0; j < NUM_WRITERS; ++j) {
             if (j > 0) {
@@ -700,15 +701,15 @@ static void RunFIFOTest(SDL_bool lock_free)
         }
         len = SDL_strlen(textBuffer);
         SDL_snprintf(textBuffer + len, sizeof(textBuffer) - len, " }\n");
-        SDLTest_Log("%s", textBuffer);
+        SDL_Log("%s", textBuffer);
     }
-    SDLTest_Log("Readers read %d total events\n", grand_total);
+    SDL_Log("Readers read %d total events\n", grand_total);
 }
 
 /* End FIFO test */
 /**************************************************************************/
 int
-SDL_main(int argc, char *argv[])
+main(int argc, char *argv[])
 {
     /* Enable standard application logging */
     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);