Renamed atomic functions to match SDL 3.0 naming convention
This will also allow us to cleanly add atomic operations for other types in the future.
This commit is contained in:
@@ -49,30 +49,30 @@ static void RunBasicTest(void)
|
||||
|
||||
SDL_Log("\natomic -----------------------------------------\n\n");
|
||||
|
||||
SDL_AtomicSet(&v, 0);
|
||||
tfret = SDL_AtomicSet(&v, 10) == 0;
|
||||
SDL_Log("AtomicSet(10) tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
|
||||
tfret = SDL_AtomicAdd(&v, 10) == 10;
|
||||
SDL_Log("AtomicAdd(10) tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
|
||||
SDL_SetAtomicInt(&v, 0);
|
||||
tfret = SDL_SetAtomicInt(&v, 10) == 0;
|
||||
SDL_Log("AtomicSet(10) tfret=%s val=%d\n", tf(tfret), SDL_GetAtomicInt(&v));
|
||||
tfret = SDL_AddAtomicInt(&v, 10) == 10;
|
||||
SDL_Log("AtomicAdd(10) tfret=%s val=%d\n", tf(tfret), SDL_GetAtomicInt(&v));
|
||||
|
||||
SDL_AtomicSet(&v, 0);
|
||||
SDL_SetAtomicInt(&v, 0);
|
||||
SDL_AtomicIncRef(&v);
|
||||
tfret = (SDL_AtomicGet(&v) == 1);
|
||||
SDL_Log("AtomicIncRef() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
|
||||
tfret = (SDL_GetAtomicInt(&v) == 1);
|
||||
SDL_Log("AtomicIncRef() tfret=%s val=%d\n", tf(tfret), SDL_GetAtomicInt(&v));
|
||||
SDL_AtomicIncRef(&v);
|
||||
tfret = (SDL_AtomicGet(&v) == 2);
|
||||
SDL_Log("AtomicIncRef() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
|
||||
tfret = (SDL_GetAtomicInt(&v) == 2);
|
||||
SDL_Log("AtomicIncRef() tfret=%s val=%d\n", tf(tfret), SDL_GetAtomicInt(&v));
|
||||
tfret = (SDL_AtomicDecRef(&v) == SDL_FALSE);
|
||||
SDL_Log("AtomicDecRef() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
|
||||
SDL_Log("AtomicDecRef() tfret=%s val=%d\n", tf(tfret), SDL_GetAtomicInt(&v));
|
||||
tfret = (SDL_AtomicDecRef(&v) == SDL_TRUE);
|
||||
SDL_Log("AtomicDecRef() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
|
||||
SDL_Log("AtomicDecRef() tfret=%s val=%d\n", tf(tfret), SDL_GetAtomicInt(&v));
|
||||
|
||||
SDL_AtomicSet(&v, 10);
|
||||
tfret = (SDL_AtomicCompareAndSwap(&v, 0, 20) == SDL_FALSE);
|
||||
SDL_Log("AtomicCAS() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
|
||||
value = SDL_AtomicGet(&v);
|
||||
tfret = (SDL_AtomicCompareAndSwap(&v, value, 20) == SDL_TRUE);
|
||||
SDL_Log("AtomicCAS() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v));
|
||||
SDL_SetAtomicInt(&v, 10);
|
||||
tfret = (SDL_CompareAndSwapAtomicInt(&v, 0, 20) == SDL_FALSE);
|
||||
SDL_Log("AtomicCAS() tfret=%s val=%d\n", tf(tfret), SDL_GetAtomicInt(&v));
|
||||
value = SDL_GetAtomicInt(&v);
|
||||
tfret = (SDL_CompareAndSwapAtomicInt(&v, value, 20) == SDL_TRUE);
|
||||
SDL_Log("AtomicCAS() tfret=%s val=%d\n", tf(tfret), SDL_GetAtomicInt(&v));
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
@@ -117,10 +117,10 @@ static int SDLCALL adder(void *junk)
|
||||
unsigned long N = NInter;
|
||||
SDL_Log("Thread subtracting %d %lu times\n", CountInc, N);
|
||||
while (N--) {
|
||||
SDL_AtomicAdd(&good, -CountInc);
|
||||
SDL_AddAtomicInt(&good, -CountInc);
|
||||
bad -= CountInc;
|
||||
}
|
||||
SDL_AtomicAdd(&threadsRunning, -1);
|
||||
SDL_AddAtomicInt(&threadsRunning, -1);
|
||||
SDL_SignalSemaphore(threadDone);
|
||||
return 0;
|
||||
}
|
||||
@@ -135,13 +135,13 @@ static void runAdder(void)
|
||||
|
||||
threadDone = SDL_CreateSemaphore(0);
|
||||
|
||||
SDL_AtomicSet(&threadsRunning, NThreads);
|
||||
SDL_SetAtomicInt(&threadsRunning, NThreads);
|
||||
|
||||
for (i = 0; i < NThreads; i++) {
|
||||
threads[i] = SDL_CreateThread(adder, "Adder", NULL);
|
||||
}
|
||||
|
||||
while (SDL_AtomicGet(&threadsRunning) > 0) {
|
||||
while (SDL_GetAtomicInt(&threadsRunning) > 0) {
|
||||
SDL_WaitSemaphore(threadDone);
|
||||
}
|
||||
|
||||
@@ -167,65 +167,65 @@ static void RunEpicTest(void)
|
||||
SDL_assert(sizeof(atomicValue) >= 4);
|
||||
|
||||
SDL_Log("Check static initializer\n");
|
||||
v = SDL_AtomicGet(&good);
|
||||
v = SDL_GetAtomicInt(&good);
|
||||
SDL_assert(v == 42);
|
||||
|
||||
SDL_assert(bad == 42);
|
||||
|
||||
SDL_Log("Test negative values\n");
|
||||
SDL_AtomicSet(&good, -5);
|
||||
v = SDL_AtomicGet(&good);
|
||||
SDL_SetAtomicInt(&good, -5);
|
||||
v = SDL_GetAtomicInt(&good);
|
||||
SDL_assert(v == -5);
|
||||
|
||||
SDL_Log("Verify maximum value\n");
|
||||
SDL_AtomicSet(&good, CountTo);
|
||||
v = SDL_AtomicGet(&good);
|
||||
SDL_SetAtomicInt(&good, CountTo);
|
||||
v = SDL_GetAtomicInt(&good);
|
||||
SDL_assert(v == CountTo);
|
||||
|
||||
SDL_Log("Test compare and exchange\n");
|
||||
|
||||
b = SDL_AtomicCompareAndSwap(&good, 500, 43);
|
||||
b = SDL_CompareAndSwapAtomicInt(&good, 500, 43);
|
||||
SDL_assert(!b); /* no swap since CountTo!=500 */
|
||||
v = SDL_AtomicGet(&good);
|
||||
v = SDL_GetAtomicInt(&good);
|
||||
SDL_assert(v == CountTo); /* ensure no swap */
|
||||
|
||||
b = SDL_AtomicCompareAndSwap(&good, CountTo, 44);
|
||||
b = SDL_CompareAndSwapAtomicInt(&good, CountTo, 44);
|
||||
SDL_assert(!!b); /* will swap */
|
||||
v = SDL_AtomicGet(&good);
|
||||
v = SDL_GetAtomicInt(&good);
|
||||
SDL_assert(v == 44);
|
||||
|
||||
SDL_Log("Test Add\n");
|
||||
|
||||
v = SDL_AtomicAdd(&good, 1);
|
||||
v = SDL_AddAtomicInt(&good, 1);
|
||||
SDL_assert(v == 44);
|
||||
v = SDL_AtomicGet(&good);
|
||||
v = SDL_GetAtomicInt(&good);
|
||||
SDL_assert(v == 45);
|
||||
|
||||
v = SDL_AtomicAdd(&good, 10);
|
||||
v = SDL_AddAtomicInt(&good, 10);
|
||||
SDL_assert(v == 45);
|
||||
v = SDL_AtomicGet(&good);
|
||||
v = SDL_GetAtomicInt(&good);
|
||||
SDL_assert(v == 55);
|
||||
|
||||
SDL_Log("Test Add (Negative values)\n");
|
||||
|
||||
v = SDL_AtomicAdd(&good, -20);
|
||||
v = SDL_AddAtomicInt(&good, -20);
|
||||
SDL_assert(v == 55);
|
||||
v = SDL_AtomicGet(&good);
|
||||
v = SDL_GetAtomicInt(&good);
|
||||
SDL_assert(v == 35);
|
||||
|
||||
v = SDL_AtomicAdd(&good, -50); /* crossing zero down */
|
||||
v = SDL_AddAtomicInt(&good, -50); /* crossing zero down */
|
||||
SDL_assert(v == 35);
|
||||
v = SDL_AtomicGet(&good);
|
||||
v = SDL_GetAtomicInt(&good);
|
||||
SDL_assert(v == -15);
|
||||
|
||||
v = SDL_AtomicAdd(&good, 30); /* crossing zero up */
|
||||
v = SDL_AddAtomicInt(&good, 30); /* crossing zero up */
|
||||
SDL_assert(v == -15);
|
||||
v = SDL_AtomicGet(&good);
|
||||
v = SDL_GetAtomicInt(&good);
|
||||
SDL_assert(v == 15);
|
||||
|
||||
SDL_Log("Reset before count down test\n");
|
||||
SDL_AtomicSet(&good, CountTo);
|
||||
v = SDL_AtomicGet(&good);
|
||||
SDL_SetAtomicInt(&good, CountTo);
|
||||
v = SDL_GetAtomicInt(&good);
|
||||
SDL_assert(v == CountTo);
|
||||
|
||||
bad = CountTo;
|
||||
@@ -234,7 +234,7 @@ static void RunEpicTest(void)
|
||||
SDL_Log("Counting down from %d, Expect %d remaining\n", CountTo, Expect);
|
||||
runAdder();
|
||||
|
||||
v = SDL_AtomicGet(&good);
|
||||
v = SDL_GetAtomicInt(&good);
|
||||
SDL_Log("Atomic %d Non-Atomic %d\n", v, bad);
|
||||
SDL_assert(v == Expect);
|
||||
/* We can't guarantee that bad != Expect, this would happen on a single core system, for example. */
|
||||
@@ -300,16 +300,16 @@ static void InitEventQueue(SDL_EventQueue *queue)
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_ENTRIES; ++i) {
|
||||
SDL_AtomicSet(&queue->entries[i].sequence, i);
|
||||
SDL_SetAtomicInt(&queue->entries[i].sequence, i);
|
||||
}
|
||||
SDL_AtomicSet(&queue->enqueue_pos, 0);
|
||||
SDL_AtomicSet(&queue->dequeue_pos, 0);
|
||||
SDL_SetAtomicInt(&queue->enqueue_pos, 0);
|
||||
SDL_SetAtomicInt(&queue->dequeue_pos, 0);
|
||||
#ifdef TEST_SPINLOCK_FIFO
|
||||
queue->lock = 0;
|
||||
SDL_AtomicSet(&queue->rwcount, 0);
|
||||
SDL_AtomicSet(&queue->watcher, 0);
|
||||
SDL_SetAtomicInt(&queue->rwcount, 0);
|
||||
SDL_SetAtomicInt(&queue->watcher, 0);
|
||||
#endif
|
||||
SDL_AtomicSet(&queue->active, 1);
|
||||
SDL_SetAtomicInt(&queue->active, 1);
|
||||
}
|
||||
|
||||
static SDL_bool EnqueueEvent_LockFree(SDL_EventQueue *queue, const SDL_Event *event)
|
||||
@@ -323,23 +323,23 @@ static SDL_bool EnqueueEvent_LockFree(SDL_EventQueue *queue, const SDL_Event *ev
|
||||
#ifdef TEST_SPINLOCK_FIFO
|
||||
/* This is a gate so an external thread can lock the queue */
|
||||
SDL_LockSpinlock(&queue->lock);
|
||||
SDL_assert(SDL_AtomicGet(&queue->watcher) == 0);
|
||||
SDL_assert(SDL_GetAtomicInt(&queue->watcher) == 0);
|
||||
SDL_AtomicIncRef(&queue->rwcount);
|
||||
SDL_UnlockSpinlock(&queue->lock);
|
||||
#endif
|
||||
|
||||
queue_pos = (unsigned)SDL_AtomicGet(&queue->enqueue_pos);
|
||||
queue_pos = (unsigned)SDL_GetAtomicInt(&queue->enqueue_pos);
|
||||
for (;;) {
|
||||
entry = &queue->entries[queue_pos & WRAP_MASK];
|
||||
entry_seq = (unsigned)SDL_AtomicGet(&entry->sequence);
|
||||
entry_seq = (unsigned)SDL_GetAtomicInt(&entry->sequence);
|
||||
|
||||
delta = (int)(entry_seq - queue_pos);
|
||||
if (delta == 0) {
|
||||
/* The entry and the queue position match, try to increment the queue position */
|
||||
if (SDL_AtomicCompareAndSwap(&queue->enqueue_pos, (int)queue_pos, (int)(queue_pos + 1))) {
|
||||
if (SDL_CompareAndSwapAtomicInt(&queue->enqueue_pos, (int)queue_pos, (int)(queue_pos + 1))) {
|
||||
/* We own the object, fill it! */
|
||||
entry->event = *event;
|
||||
SDL_AtomicSet(&entry->sequence, (int)(queue_pos + 1));
|
||||
SDL_SetAtomicInt(&entry->sequence, (int)(queue_pos + 1));
|
||||
status = SDL_TRUE;
|
||||
break;
|
||||
}
|
||||
@@ -349,7 +349,7 @@ static SDL_bool EnqueueEvent_LockFree(SDL_EventQueue *queue, const SDL_Event *ev
|
||||
break;
|
||||
} else {
|
||||
/* We ran into a new queue entry, get the new queue position */
|
||||
queue_pos = (unsigned)SDL_AtomicGet(&queue->enqueue_pos);
|
||||
queue_pos = (unsigned)SDL_GetAtomicInt(&queue->enqueue_pos);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -370,23 +370,23 @@ static SDL_bool DequeueEvent_LockFree(SDL_EventQueue *queue, SDL_Event *event)
|
||||
#ifdef TEST_SPINLOCK_FIFO
|
||||
/* This is a gate so an external thread can lock the queue */
|
||||
SDL_LockSpinlock(&queue->lock);
|
||||
SDL_assert(SDL_AtomicGet(&queue->watcher) == 0);
|
||||
SDL_assert(SDL_GetAtomicInt(&queue->watcher) == 0);
|
||||
SDL_AtomicIncRef(&queue->rwcount);
|
||||
SDL_UnlockSpinlock(&queue->lock);
|
||||
#endif
|
||||
|
||||
queue_pos = (unsigned)SDL_AtomicGet(&queue->dequeue_pos);
|
||||
queue_pos = (unsigned)SDL_GetAtomicInt(&queue->dequeue_pos);
|
||||
for (;;) {
|
||||
entry = &queue->entries[queue_pos & WRAP_MASK];
|
||||
entry_seq = (unsigned)SDL_AtomicGet(&entry->sequence);
|
||||
entry_seq = (unsigned)SDL_GetAtomicInt(&entry->sequence);
|
||||
|
||||
delta = (int)(entry_seq - (queue_pos + 1));
|
||||
if (delta == 0) {
|
||||
/* The entry and the queue position match, try to increment the queue position */
|
||||
if (SDL_AtomicCompareAndSwap(&queue->dequeue_pos, (int)queue_pos, (int)(queue_pos + 1))) {
|
||||
if (SDL_CompareAndSwapAtomicInt(&queue->dequeue_pos, (int)queue_pos, (int)(queue_pos + 1))) {
|
||||
/* We own the object, fill it! */
|
||||
*event = entry->event;
|
||||
SDL_AtomicSet(&entry->sequence, (int)(queue_pos + MAX_ENTRIES));
|
||||
SDL_SetAtomicInt(&entry->sequence, (int)(queue_pos + MAX_ENTRIES));
|
||||
status = SDL_TRUE;
|
||||
break;
|
||||
}
|
||||
@@ -396,7 +396,7 @@ static SDL_bool DequeueEvent_LockFree(SDL_EventQueue *queue, SDL_Event *event)
|
||||
break;
|
||||
} else {
|
||||
/* We ran into a new queue entry, get the new queue position */
|
||||
queue_pos = (unsigned)SDL_AtomicGet(&queue->dequeue_pos);
|
||||
queue_pos = (unsigned)SDL_GetAtomicInt(&queue->dequeue_pos);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -537,7 +537,7 @@ static int SDLCALL FIFO_Reader(void *_data)
|
||||
if (DequeueEvent_LockFree(queue, &event)) {
|
||||
WriterData *writer = (WriterData *)event.user.data1;
|
||||
++data->counters[writer->index];
|
||||
} else if (SDL_AtomicGet(&queue->active)) {
|
||||
} else if (SDL_GetAtomicInt(&queue->active)) {
|
||||
++data->waits;
|
||||
SDL_Delay(0);
|
||||
} else {
|
||||
@@ -550,7 +550,7 @@ static int SDLCALL FIFO_Reader(void *_data)
|
||||
if (DequeueEvent_Mutex(queue, &event)) {
|
||||
WriterData *writer = (WriterData *)event.user.data1;
|
||||
++data->counters[writer->index];
|
||||
} else if (SDL_AtomicGet(&queue->active)) {
|
||||
} else if (SDL_GetAtomicInt(&queue->active)) {
|
||||
++data->waits;
|
||||
SDL_Delay(0);
|
||||
} else {
|
||||
@@ -568,10 +568,10 @@ static int SDLCALL FIFO_Watcher(void *_data)
|
||||
{
|
||||
SDL_EventQueue *queue = (SDL_EventQueue *)_data;
|
||||
|
||||
while (SDL_AtomicGet(&queue->active)) {
|
||||
while (SDL_GetAtomicInt(&queue->active)) {
|
||||
SDL_LockSpinlock(&queue->lock);
|
||||
SDL_AtomicIncRef(&queue->watcher);
|
||||
while (SDL_AtomicGet(&queue->rwcount) > 0) {
|
||||
while (SDL_GetAtomicInt(&queue->rwcount) > 0) {
|
||||
SDL_Delay(0);
|
||||
}
|
||||
/* Do queue manipulation here... */
|
||||
@@ -645,7 +645,7 @@ static void RunFIFOTest(SDL_bool lock_free)
|
||||
}
|
||||
|
||||
/* Shut down the queue so readers exit */
|
||||
SDL_AtomicSet(&queue.active, 0);
|
||||
SDL_SetAtomicInt(&queue.active, 0);
|
||||
|
||||
/* Wait for the readers */
|
||||
for (i = 0; i < NUM_READERS; ++i) {
|
||||
|
||||
@@ -786,7 +786,7 @@ static void SDLCALL PostmixCallback(void *userdata, const SDL_AudioSpec *spec, f
|
||||
SDL_copyp(&thing->data.logdev.postmix_spec, spec);
|
||||
SDL_memcpy(thing->data.logdev.postmix_buffer, buffer, buflen);
|
||||
thing->data.logdev.postmix_buflen = buflen;
|
||||
SDL_AtomicSet(&thing->data.logdev.postmix_updated, 1);
|
||||
SDL_SetAtomicInt(&thing->data.logdev.postmix_updated, 1);
|
||||
|
||||
SDL_UnlockMutex(thing->data.logdev.postmix_lock);
|
||||
}
|
||||
@@ -857,7 +857,7 @@ static void LogicalDeviceThing_ontick(Thing *thing, Uint64 now)
|
||||
if (thing->data.logdev.postmix_buffer) {
|
||||
SDL_memset(thing->data.logdev.postmix_buffer, '\0', thing->data.logdev.postmix_buflen);
|
||||
}
|
||||
SDL_AtomicSet(&thing->data.logdev.postmix_updated, 1); /* so this will at least clear the texture later. */
|
||||
SDL_SetAtomicInt(&thing->data.logdev.postmix_updated, 1); /* so this will at least clear the texture later. */
|
||||
SDL_SetAudioPostmixCallback(thing->data.logdev.devid, PostmixCallback, thing);
|
||||
}
|
||||
}
|
||||
@@ -872,7 +872,7 @@ static void LogicalDeviceThing_ondraw(Thing *thing, SDL_Renderer *renderer)
|
||||
dst.x = thing->rect.x + ((thing->rect.w - dst.w) / 2);
|
||||
dst.y = thing->rect.y + ((thing->rect.h - dst.h) / 2);
|
||||
|
||||
if (SDL_AtomicGet(&thing->data.logdev.postmix_updated)) {
|
||||
if (SDL_GetAtomicInt(&thing->data.logdev.postmix_updated)) {
|
||||
float *buffer;
|
||||
int channels;
|
||||
int buflen;
|
||||
@@ -883,7 +883,7 @@ static void LogicalDeviceThing_ondraw(Thing *thing, SDL_Renderer *renderer)
|
||||
buffer = (float *) SDL_malloc(thing->data.logdev.postmix_buflen);
|
||||
if (buffer) {
|
||||
SDL_memcpy(buffer, thing->data.logdev.postmix_buffer, thing->data.logdev.postmix_buflen);
|
||||
SDL_AtomicSet(&thing->data.logdev.postmix_updated, 0);
|
||||
SDL_SetAtomicInt(&thing->data.logdev.postmix_updated, 0);
|
||||
}
|
||||
SDL_UnlockMutex(thing->data.logdev.postmix_lock);
|
||||
|
||||
|
||||
@@ -568,7 +568,7 @@ render_thread_fn(void *render_ctx)
|
||||
thread_data *thread = render_ctx;
|
||||
|
||||
while (!done && !thread->done && state->windows[thread->index]) {
|
||||
if (SDL_AtomicCompareAndSwap(&thread->suspended, WAIT_STATE_ENTER_SEM, WAIT_STATE_WAITING_ON_SEM)) {
|
||||
if (SDL_CompareAndSwapAtomicInt(&thread->suspended, WAIT_STATE_ENTER_SEM, WAIT_STATE_WAITING_ON_SEM)) {
|
||||
SDL_WaitSemaphore(thread->suspend_sem);
|
||||
}
|
||||
render_window(thread->index);
|
||||
@@ -603,12 +603,12 @@ loop_threaded(void)
|
||||
if (suspend_when_occluded && event.type == SDL_EVENT_WINDOW_OCCLUDED) {
|
||||
tdata = GetThreadDataForWindow(event.window.windowID);
|
||||
if (tdata) {
|
||||
SDL_AtomicCompareAndSwap(&tdata->suspended, WAIT_STATE_GO, WAIT_STATE_ENTER_SEM);
|
||||
SDL_CompareAndSwapAtomicInt(&tdata->suspended, WAIT_STATE_GO, WAIT_STATE_ENTER_SEM);
|
||||
}
|
||||
} else if (suspend_when_occluded && event.type == SDL_EVENT_WINDOW_EXPOSED) {
|
||||
tdata = GetThreadDataForWindow(event.window.windowID);
|
||||
if (tdata) {
|
||||
if (SDL_AtomicSet(&tdata->suspended, WAIT_STATE_GO) == WAIT_STATE_WAITING_ON_SEM) {
|
||||
if (SDL_SetAtomicInt(&tdata->suspended, WAIT_STATE_GO) == WAIT_STATE_WAITING_ON_SEM) {
|
||||
SDL_SignalSemaphore(tdata->suspend_sem);
|
||||
}
|
||||
}
|
||||
@@ -618,7 +618,7 @@ loop_threaded(void)
|
||||
/* Stop the render thread when the window is closed */
|
||||
tdata->done = 1;
|
||||
if (tdata->thread) {
|
||||
SDL_AtomicSet(&tdata->suspended, WAIT_STATE_GO);
|
||||
SDL_SetAtomicInt(&tdata->suspended, WAIT_STATE_GO);
|
||||
SDL_SignalSemaphore(tdata->suspend_sem);
|
||||
SDL_WaitThread(tdata->thread, NULL);
|
||||
tdata->thread = NULL;
|
||||
@@ -909,7 +909,7 @@ int main(int argc, char *argv[])
|
||||
/* Start a render thread for each window */
|
||||
for (i = 0; i < state->num_windows; ++i) {
|
||||
threads[i].index = i;
|
||||
SDL_AtomicSet(&threads[i].suspended, 0);
|
||||
SDL_SetAtomicInt(&threads[i].suspended, 0);
|
||||
threads[i].suspend_sem = SDL_CreateSemaphore(0);
|
||||
threads[i].thread = SDL_CreateThread(render_thread_fn, "RenderThread", &threads[i]);
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ static void printid(void)
|
||||
static void terminate(int sig)
|
||||
{
|
||||
(void)signal(SIGINT, terminate);
|
||||
SDL_AtomicSet(&doterminate, 1);
|
||||
SDL_SetAtomicInt(&doterminate, 1);
|
||||
}
|
||||
|
||||
static void closemutex(int sig)
|
||||
@@ -56,7 +56,7 @@ static void closemutex(int sig)
|
||||
SDL_ThreadID id = SDL_GetCurrentThreadID();
|
||||
int i;
|
||||
SDL_Log("Thread %" SDL_PRIu64 ": Cleaning up...\n", id == mainthread ? 0 : id);
|
||||
SDL_AtomicSet(&doterminate, 1);
|
||||
SDL_SetAtomicInt(&doterminate, 1);
|
||||
if (threads) {
|
||||
for (i = 0; i < nb_threads; ++i) {
|
||||
SDL_WaitThread(threads[i], NULL);
|
||||
@@ -80,7 +80,7 @@ Run(void *data)
|
||||
(void)signal(SIGTERM, closemutex);
|
||||
}
|
||||
SDL_Log("Thread %" SDL_PRIu64 ": starting up", current_thread);
|
||||
while (!SDL_AtomicGet(&doterminate)) {
|
||||
while (!SDL_GetAtomicInt(&doterminate)) {
|
||||
SDL_Log("Thread %" SDL_PRIu64 ": ready to work\n", current_thread);
|
||||
SDL_LockMutex(mutex);
|
||||
SDL_Log("Thread %" SDL_PRIu64 ": start work!\n", current_thread);
|
||||
@@ -91,7 +91,7 @@ Run(void *data)
|
||||
/* If this sleep isn't done, then threads may starve */
|
||||
SDL_Delay(10);
|
||||
}
|
||||
if (current_thread == mainthread && SDL_AtomicGet(&doterminate)) {
|
||||
if (current_thread == mainthread && SDL_GetAtomicInt(&doterminate)) {
|
||||
SDL_Log("Thread %" SDL_PRIu64 ": raising SIGTERM\n", current_thread);
|
||||
(void)raise(SIGTERM);
|
||||
}
|
||||
@@ -179,7 +179,7 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
(void)atexit(SDL_Quit_Wrapper);
|
||||
|
||||
SDL_AtomicSet(&doterminate, 0);
|
||||
SDL_SetAtomicInt(&doterminate, 0);
|
||||
|
||||
mutex = SDL_CreateMutex();
|
||||
if (!mutex) {
|
||||
|
||||
@@ -54,7 +54,7 @@ static int SDLCALL
|
||||
ReaderRun(void *data)
|
||||
{
|
||||
SDL_Log("Reader Thread %" SDL_PRIu64 ": starting up", SDL_GetCurrentThreadID());
|
||||
while (!SDL_AtomicGet(&doterminate)) {
|
||||
while (!SDL_GetAtomicInt(&doterminate)) {
|
||||
DoWork(worktime);
|
||||
}
|
||||
SDL_Log("Reader Thread %" SDL_PRIu64 ": exiting!\n", SDL_GetCurrentThreadID());
|
||||
@@ -71,7 +71,7 @@ int main(int argc, char *argv[])
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_AtomicSet(&doterminate, 0);
|
||||
SDL_SetAtomicInt(&doterminate, 0);
|
||||
|
||||
/* Parse commandline */
|
||||
for (i = 1; i < argc;) {
|
||||
@@ -136,7 +136,7 @@ int main(int argc, char *argv[])
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_AtomicSet(&doterminate, 0);
|
||||
SDL_SetAtomicInt(&doterminate, 0);
|
||||
|
||||
rwlock = SDL_CreateRWLock();
|
||||
if (!rwlock) {
|
||||
@@ -157,11 +157,11 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
}
|
||||
|
||||
while (!SDL_AtomicGet(&doterminate) && (SDL_GetTicks() < ((Uint64) timeout))) {
|
||||
while (!SDL_GetAtomicInt(&doterminate) && (SDL_GetTicks() < ((Uint64) timeout))) {
|
||||
DoWork(writerworktime);
|
||||
}
|
||||
|
||||
SDL_AtomicSet(&doterminate, 1);
|
||||
SDL_SetAtomicInt(&doterminate, 1);
|
||||
SDL_Log("Waiting on reader threads to terminate...");
|
||||
for (i = 0; i < nb_threads; ++i) {
|
||||
SDL_WaitThread(threads[i], NULL);
|
||||
|
||||
@@ -62,7 +62,7 @@ ThreadFunc(void *data)
|
||||
SDL_SetTLS(&tls, "baby thread", NULL);
|
||||
SDL_Log("Started thread %s: My thread id is %" SDL_PRIu64 ", thread data = %s\n",
|
||||
(char *)data, SDL_GetCurrentThreadID(), (const char *)SDL_GetTLS(&tls));
|
||||
while (SDL_AtomicGet(&alive)) {
|
||||
while (SDL_GetAtomicInt(&alive)) {
|
||||
SDL_Log("Thread '%s' is alive!\n", (char *)data);
|
||||
|
||||
if (testprio) {
|
||||
@@ -83,7 +83,7 @@ killed(int sig)
|
||||
{
|
||||
SDL_Log("Killed with SIGTERM, waiting 5 seconds to exit\n");
|
||||
SDL_Delay(5 * 1000);
|
||||
SDL_AtomicSet(&alive, 0);
|
||||
SDL_SetAtomicInt(&alive, 0);
|
||||
SDL_WaitThread(thread, NULL);
|
||||
quit(0);
|
||||
}
|
||||
@@ -133,7 +133,7 @@ int main(int argc, char *argv[])
|
||||
SDL_SetTLS(&tls, "main thread", NULL);
|
||||
SDL_Log("Main thread data initially: %s\n", (const char *)SDL_GetTLS(&tls));
|
||||
|
||||
SDL_AtomicSet(&alive, 1);
|
||||
SDL_SetAtomicInt(&alive, 1);
|
||||
thread = SDL_CreateThread(ThreadFunc, "One", "#1");
|
||||
if (!thread) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
|
||||
@@ -141,12 +141,12 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
SDL_Delay(5 * 1000);
|
||||
SDL_Log("Waiting for thread #1\n");
|
||||
SDL_AtomicSet(&alive, 0);
|
||||
SDL_SetAtomicInt(&alive, 0);
|
||||
SDL_WaitThread(thread, NULL);
|
||||
|
||||
SDL_Log("Main thread data finally: %s\n", (const char *)SDL_GetTLS(&tls));
|
||||
|
||||
SDL_AtomicSet(&alive, 1);
|
||||
SDL_SetAtomicInt(&alive, 1);
|
||||
(void)signal(SIGTERM, killed);
|
||||
thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
|
||||
if (!thread) {
|
||||
|
||||
@@ -38,7 +38,7 @@ static int SDLCALL
|
||||
SubThreadFunc(void *data)
|
||||
{
|
||||
SDL_AtomicInt *flag = (SDL_AtomicInt *)data;
|
||||
while (!SDL_AtomicGet(flag)) {
|
||||
while (!SDL_GetAtomicInt(flag)) {
|
||||
SDL_Delay(10);
|
||||
}
|
||||
return 0;
|
||||
@@ -57,18 +57,18 @@ ThreadFunc(void *data)
|
||||
for (i = 0; i < NUMTHREADS; i++) {
|
||||
char name[64];
|
||||
(void)SDL_snprintf(name, sizeof(name), "Child%d_%d", tid, i);
|
||||
SDL_AtomicSet(&flags[i], 0);
|
||||
SDL_SetAtomicInt(&flags[i], 0);
|
||||
sub_threads[i] = SDL_CreateThread(SubThreadFunc, name, &flags[i]);
|
||||
}
|
||||
|
||||
SDL_Log("Thread '%d' waiting for signal\n", tid);
|
||||
while (SDL_AtomicGet(&time_for_threads_to_die[tid]) != 1) {
|
||||
while (SDL_GetAtomicInt(&time_for_threads_to_die[tid]) != 1) {
|
||||
; /* do nothing */
|
||||
}
|
||||
|
||||
SDL_Log("Thread '%d' sending signals to subthreads\n", tid);
|
||||
for (i = 0; i < NUMTHREADS; i++) {
|
||||
SDL_AtomicSet(&flags[i], 1);
|
||||
SDL_SetAtomicInt(&flags[i], 1);
|
||||
SDL_WaitThread(sub_threads[i], NULL);
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ int main(int argc, char *argv[])
|
||||
for (i = 0; i < NUMTHREADS; i++) {
|
||||
char name[64];
|
||||
(void)SDL_snprintf(name, sizeof(name), "Parent%d", i);
|
||||
SDL_AtomicSet(&time_for_threads_to_die[i], 0);
|
||||
SDL_SetAtomicInt(&time_for_threads_to_die[i], 0);
|
||||
threads[i] = SDL_CreateThread(ThreadFunc, name, (void *)(uintptr_t)i);
|
||||
|
||||
if (threads[i] == NULL) {
|
||||
@@ -109,7 +109,7 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
for (i = 0; i < NUMTHREADS; i++) {
|
||||
SDL_AtomicSet(&time_for_threads_to_die[i], 1);
|
||||
SDL_SetAtomicInt(&time_for_threads_to_die[i], 1);
|
||||
}
|
||||
|
||||
for (i = 0; i < NUMTHREADS; i++) {
|
||||
|
||||
Reference in New Issue
Block a user