From ea1514ab63f19d9f5d5a62935cd0e58352072efa Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 30 Nov 2025 10:51:19 -0800
Subject: [PATCH] testthread: verify that child threads aren't
SDL_IsMainThread()
---
test/testthread.c | 37 +++++++++++++++++++++++++++++++------
1 file changed, 31 insertions(+), 6 deletions(-)
diff --git a/test/testthread.c b/test/testthread.c
index ea56af95b9580..48f39ee1c7cbe 100644
--- a/test/testthread.c
+++ b/test/testthread.c
@@ -54,8 +54,14 @@ getprioritystr(SDL_ThreadPriority priority)
return "???";
}
-static int SDLCALL
-ThreadFunc(void *data)
+static int SDLCALL CheckMainThread(void *data)
+{
+ bool *thread_is_main = (bool *)data;
+ *thread_is_main = SDL_IsMainThread();
+ return 0;
+}
+
+static int SDLCALL ThreadFunc(void *data)
{
SDL_ThreadPriority prio = SDL_THREAD_PRIORITY_NORMAL;
@@ -91,6 +97,7 @@ killed(int sig)
int main(int argc, char *argv[])
{
int i;
+ bool child_is_main = true;
/* Initialize test framework */
state = SDLTest_CommonCreateState(argv, 0);
@@ -112,15 +119,33 @@ int main(int argc, char *argv[])
if (consumed <= 0) {
static const char *options[] = { "[--prio]", NULL };
SDLTest_CommonLogUsage(state, argv[0], options);
- exit(1);
+ quit(1);
}
i += consumed;
}
+ /* Check main thread */
+ if (!SDL_IsMainThread()) {
+ SDL_Log("SDL_IsMainThread() returned false for the main thread");
+ quit(1);
+ }
+
+ thread = SDL_CreateThread(CheckMainThread, "CheckMainThread", &child_is_main);
+ if (!thread) {
+ SDL_Log("Couldn't create thread: %s", SDL_GetError());
+ quit(1);
+ }
+ SDL_WaitThread(thread, NULL);
+
+ if (child_is_main) {
+ SDL_Log("SDL_IsMainThread() returned true for a child thread");
+ quit(1);
+ }
+
if (SDL_GetEnvironmentVariable(SDL_GetEnvironment(), "SDL_TESTS_QUICK") != NULL) {
SDL_Log("Not running slower tests");
- SDL_Quit();
+ quit(0);
return 0;
}
@@ -130,7 +155,7 @@ int main(int argc, char *argv[])
SDL_SetAtomicInt(&alive, 1);
thread = SDL_CreateThread(ThreadFunc, "One", "#1");
if (!thread) {
- SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s", SDL_GetError());
+ SDL_Log("Couldn't create thread: %s", SDL_GetError());
quit(1);
}
SDL_Delay(5 * 1000);
@@ -144,7 +169,7 @@ int main(int argc, char *argv[])
(void)signal(SIGTERM, killed);
thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
if (!thread) {
- SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s", SDL_GetError());
+ SDL_Log("Couldn't create thread: %s", SDL_GetError());
quit(1);
}
(void)raise(SIGTERM);