/*
- 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
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));
+
}
/**************************************************************************/
#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 };
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;
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
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 */
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);
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);
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);
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);
}
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;
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)
} 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);
} 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);
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;
return 0;
}
-static int FIFO_Reader(void* _data)
+static int SDLCALL FIFO_Reader(void* _data)
{
ReaderData *data = (ReaderData *)_data;
SDL_EventQueue *queue = data->queue;
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 {
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 {
#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) {
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);
#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) {
}
/* 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) {
}
/* 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) {
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;
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) {
}
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);