SDL: Use atomic variables for thread communication

From a0f36fb85b40338fb24e1f1479bdcc432c06aba6 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 5 Sep 2024 05:35:03 -0700
Subject: [PATCH] Use atomic variables for thread communication

Fixes https://github.com/libsdl-org/SDL/issues/10711
---
 test/testthread.c    | 12 ++++++------
 test/torturethread.c |  9 +++++----
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/test/testthread.c b/test/testthread.c
index 06b0871253452..9eeea4104e57e 100644
--- a/test/testthread.c
+++ b/test/testthread.c
@@ -21,7 +21,7 @@
 
 static SDL_TLSID tls;
 static SDL_Thread *thread = NULL;
-static int alive = 0;
+static SDL_AtomicInt alive;
 static int testprio = 0;
 static SDLTest_CommonState *state;
 
@@ -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 (alive) {
+    while (SDL_AtomicGet(&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);
-    alive = 0;
+    SDL_AtomicSet(&alive, 0);
     SDL_WaitThread(thread, NULL);
     quit(0);
 }
@@ -136,7 +136,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));
 
-    alive = 1;
+    SDL_AtomicSet(&alive, 1);
     thread = SDL_CreateThread(ThreadFunc, "One", "#1");
     if (!thread) {
         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
@@ -144,12 +144,12 @@ int main(int argc, char *argv[])
     }
     SDL_Delay(5 * 1000);
     SDL_Log("Waiting for thread #1\n");
-    alive = 0;
+    SDL_AtomicSet(&alive, 0);
     SDL_WaitThread(thread, NULL);
 
     SDL_Log("Main thread data finally: %s\n", (const char *)SDL_GetTLS(&tls));
 
-    alive = 1;
+    SDL_AtomicSet(&alive, 1);
     (void)signal(SIGTERM, killed);
     thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
     if (!thread) {
diff --git a/test/torturethread.c b/test/torturethread.c
index 2c6e562b0c9c4..deea79cea2d61 100644
--- a/test/torturethread.c
+++ b/test/torturethread.c
@@ -37,7 +37,8 @@ quit(int rc)
 static int SDLCALL
 SubThreadFunc(void *data)
 {
-    while (!*(int volatile *)data) {
+    SDL_AtomicInt *flag = (SDL_AtomicInt *)data;
+    while (!SDL_AtomicGet(flag)) {
         SDL_Delay(10);
     }
     return 0;
@@ -47,7 +48,7 @@ static int SDLCALL
 ThreadFunc(void *data)
 {
     SDL_Thread *sub_threads[NUMTHREADS];
-    int flags[NUMTHREADS];
+    SDL_AtomicInt flags[NUMTHREADS];
     int i;
     int tid = (int)(uintptr_t)data;
 
@@ -56,7 +57,7 @@ ThreadFunc(void *data)
     for (i = 0; i < NUMTHREADS; i++) {
         char name[64];
         (void)SDL_snprintf(name, sizeof(name), "Child%d_%d", tid, i);
-        flags[i] = 0;
+        SDL_AtomicSet(&flags[i], 0);
         sub_threads[i] = SDL_CreateThread(SubThreadFunc, name, &flags[i]);
     }
 
@@ -67,7 +68,7 @@ ThreadFunc(void *data)
 
     SDL_Log("Thread '%d' sending signals to subthreads\n", tid);
     for (i = 0; i < NUMTHREADS; i++) {
-        flags[i] = 1;
+        SDL_AtomicSet(&flags[i], 1);
         SDL_WaitThread(sub_threads[i], NULL);
     }