| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 3 |
|
| 4 |
This software is provided 'as-is', without any express or implied |
| 5 |
warranty. In no event will the authors be held liable for any damages |
| 6 |
arising from the use of this software. |
| 7 |
|
| 8 |
Permission is granted to anyone to use this software for any purpose, |
| 9 |
including commercial applications, and to alter it and redistribute it |
| 10 |
freely. |
| 11 |
*/ |
| 12 |
|
| 13 |
/* Simple test of the SDL threading code */ |
| 14 |
|
| 15 |
#include <stdio.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <signal.h> |
| 18 |
|
| 19 |
#include "SDL.h" |
| 20 |
#include "SDL_test.h" |
| 21 |
|
| 22 |
static SDL_TLSID tls; |
| 23 |
static SDL_Thread *thread = NULL; |
| 24 |
static SDL_atomic_t alive; |
| 25 |
static int testprio = 0; |
| 26 |
static SDLTest_CommonState *state; |
| 27 |
|
| 28 |
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
| 29 |
static void |
| 30 |
quit(int rc) |
| 31 |
{ |
| 32 |
SDLTest_CommonQuit(state); |
| 33 |
SDL_Quit(); |
| 34 |
exit(rc); |
| 35 |
} |
| 36 |
|
| 37 |
static const char * |
| 38 |
getprioritystr(SDL_ThreadPriority priority) |
| 39 |
{ |
| 40 |
switch (priority) { |
| 41 |
case SDL_THREAD_PRIORITY_LOW: |
| 42 |
return "SDL_THREAD_PRIORITY_LOW"; |
| 43 |
case SDL_THREAD_PRIORITY_NORMAL: |
| 44 |
return "SDL_THREAD_PRIORITY_NORMAL"; |
| 45 |
case SDL_THREAD_PRIORITY_HIGH: |
| 46 |
return "SDL_THREAD_PRIORITY_HIGH"; |
| 47 |
case SDL_THREAD_PRIORITY_TIME_CRITICAL: |
| 48 |
return "SDL_THREAD_PRIORITY_TIME_CRITICAL"; |
| 49 |
} |
| 50 |
|
| 51 |
return "???"; |
| 52 |
} |
| 53 |
|
| 54 |
int SDLCALL |
| 55 |
ThreadFunc(void *data) |
| 56 |
{ |
| 57 |
SDL_ThreadPriority prio = SDL_THREAD_PRIORITY_NORMAL; |
| 58 |
|
| 59 |
SDL_TLSSet(tls, "baby thread", NULL); |
| 60 |
SDL_Log("Started thread %s: My thread id is %lu, thread data = %s\n", |
| 61 |
(char *)data, SDL_ThreadID(), (const char *)SDL_TLSGet(tls)); |
| 62 |
while (SDL_AtomicGet(&alive)) { |
| 63 |
SDL_Log("Thread '%s' is alive!\n", (char *)data); |
| 64 |
|
| 65 |
if (testprio) { |
| 66 |
SDL_Log("SDL_SetThreadPriority(%s):%d\n", getprioritystr(prio), SDL_SetThreadPriority(prio)); |
| 67 |
if (++prio > SDL_THREAD_PRIORITY_TIME_CRITICAL) { |
| 68 |
prio = SDL_THREAD_PRIORITY_LOW; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
SDL_Delay(1 * 1000); |
| 73 |
} |
| 74 |
SDL_Log("Thread '%s' exiting!\n", (char *)data); |
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
static void |
| 79 |
killed(int sig) |
| 80 |
{ |
| 81 |
SDL_Log("Killed with SIGTERM, waiting 5 seconds to exit\n"); |
| 82 |
SDL_Delay(5 * 1000); |
| 83 |
SDL_AtomicSet(&alive, 0); |
| 84 |
SDL_WaitThread(thread, NULL); |
| 85 |
quit(0); |
| 86 |
} |
| 87 |
|
| 88 |
int main(int argc, char *argv[]) |
| 89 |
{ |
| 90 |
int i; |
| 91 |
|
| 92 |
state = SDLTest_CommonCreateState(argv, 0); |
| 93 |
if (!state) { |
| 94 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError()); |
| 95 |
return 1; |
| 96 |
} |
| 97 |
|
| 98 |
/* Enable standard application logging */ |
| 99 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 100 |
|
| 101 |
/* Parse commandline */ |
| 102 |
for (i = 1; i < argc;) { |
| 103 |
int consumed; |
| 104 |
|
| 105 |
consumed = SDLTest_CommonArg(state, i); |
| 106 |
if (!consumed) { |
| 107 |
if (SDL_strcmp("--prio", argv[i]) == 0) { |
| 108 |
testprio = 1; |
| 109 |
consumed = 1; |
| 110 |
} |
| 111 |
} |
| 112 |
if (consumed <= 0) { |
| 113 |
static const char *options[] = { "[--prio]", NULL }; |
| 114 |
SDLTest_CommonLogUsage(state, argv[0], options); |
| 115 |
exit(1); |
| 116 |
} |
| 117 |
|
| 118 |
i += consumed; |
| 119 |
} |
| 120 |
|
| 121 |
if (!SDLTest_CommonInit(state)) { |
| 122 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
| 123 |
return 1; |
| 124 |
} |
| 125 |
|
| 126 |
if (SDL_getenv("SDL_TESTS_QUICK") != NULL) { |
| 127 |
SDL_Log("Not running slower tests"); |
| 128 |
SDL_Quit(); |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
tls = SDL_TLSCreate(); |
| 133 |
SDL_assert(tls); |
| 134 |
SDL_TLSSet(tls, "main thread", NULL); |
| 135 |
SDL_Log("Main thread data initially: %s\n", (const char *)SDL_TLSGet(tls)); |
| 136 |
|
| 137 |
SDL_AtomicSet(&alive, 1); |
| 138 |
thread = SDL_CreateThread(ThreadFunc, "One", "#1"); |
| 139 |
if (!thread) { |
| 140 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError()); |
| 141 |
quit(1); |
| 142 |
} |
| 143 |
SDL_Delay(5 * 1000); |
| 144 |
SDL_Log("Waiting for thread #1\n"); |
| 145 |
SDL_AtomicSet(&alive, 0); |
| 146 |
SDL_WaitThread(thread, NULL); |
| 147 |
|
| 148 |
SDL_Log("Main thread data finally: %s\n", (const char *)SDL_TLSGet(tls)); |
| 149 |
|
| 150 |
SDL_AtomicSet(&alive, 1); |
| 151 |
(void)signal(SIGTERM, killed); |
| 152 |
thread = SDL_CreateThread(ThreadFunc, "Two", "#2"); |
| 153 |
if (!thread) { |
| 154 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError()); |
| 155 |
quit(1); |
| 156 |
} |
| 157 |
(void)raise(SIGTERM); |
| 158 |
|
| 159 |
SDLTest_CommonQuit(state); /* Never reached */ |
| 160 |
return 0; /* Never reached */ |
| 161 |
} |