Skip to content

Commit ea1514a

Browse files
committed
testthread: verify that child threads aren't SDL_IsMainThread()
1 parent 955698c commit ea1514a

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

test/testthread.c

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,14 @@ getprioritystr(SDL_ThreadPriority priority)
5454
return "???";
5555
}
5656

57-
static int SDLCALL
58-
ThreadFunc(void *data)
57+
static int SDLCALL CheckMainThread(void *data)
58+
{
59+
bool *thread_is_main = (bool *)data;
60+
*thread_is_main = SDL_IsMainThread();
61+
return 0;
62+
}
63+
64+
static int SDLCALL ThreadFunc(void *data)
5965
{
6066
SDL_ThreadPriority prio = SDL_THREAD_PRIORITY_NORMAL;
6167

@@ -91,6 +97,7 @@ killed(int sig)
9197
int main(int argc, char *argv[])
9298
{
9399
int i;
100+
bool child_is_main = true;
94101

95102
/* Initialize test framework */
96103
state = SDLTest_CommonCreateState(argv, 0);
@@ -112,15 +119,33 @@ int main(int argc, char *argv[])
112119
if (consumed <= 0) {
113120
static const char *options[] = { "[--prio]", NULL };
114121
SDLTest_CommonLogUsage(state, argv[0], options);
115-
exit(1);
122+
quit(1);
116123
}
117124

118125
i += consumed;
119126
}
120127

128+
/* Check main thread */
129+
if (!SDL_IsMainThread()) {
130+
SDL_Log("SDL_IsMainThread() returned false for the main thread");
131+
quit(1);
132+
}
133+
134+
thread = SDL_CreateThread(CheckMainThread, "CheckMainThread", &child_is_main);
135+
if (!thread) {
136+
SDL_Log("Couldn't create thread: %s", SDL_GetError());
137+
quit(1);
138+
}
139+
SDL_WaitThread(thread, NULL);
140+
141+
if (child_is_main) {
142+
SDL_Log("SDL_IsMainThread() returned true for a child thread");
143+
quit(1);
144+
}
145+
121146
if (SDL_GetEnvironmentVariable(SDL_GetEnvironment(), "SDL_TESTS_QUICK") != NULL) {
122147
SDL_Log("Not running slower tests");
123-
SDL_Quit();
148+
quit(0);
124149
return 0;
125150
}
126151

@@ -130,7 +155,7 @@ int main(int argc, char *argv[])
130155
SDL_SetAtomicInt(&alive, 1);
131156
thread = SDL_CreateThread(ThreadFunc, "One", "#1");
132157
if (!thread) {
133-
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s", SDL_GetError());
158+
SDL_Log("Couldn't create thread: %s", SDL_GetError());
134159
quit(1);
135160
}
136161
SDL_Delay(5 * 1000);
@@ -144,7 +169,7 @@ int main(int argc, char *argv[])
144169
(void)signal(SIGTERM, killed);
145170
thread = SDL_CreateThread(ThreadFunc, "Two", "#2");
146171
if (!thread) {
147-
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s", SDL_GetError());
172+
SDL_Log("Couldn't create thread: %s", SDL_GetError());
148173
quit(1);
149174
}
150175
(void)raise(SIGTERM);

0 commit comments

Comments
 (0)