Atlas - testthread.c

Home / ext / SDL / test Lines: 1 | Size: 4793 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Copyright (C) 1997-2025 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 <stdlib.h> 16#include <signal.h> 17 18#include <SDL3/SDL.h> 19#include <SDL3/SDL_main.h> 20#include <SDL3/SDL_test.h> 21 22static SDL_TLSID tls; 23static SDL_Thread *thread = NULL; 24static SDL_AtomicInt alive; 25static int testprio = 0; 26static SDLTest_CommonState *state; 27 28/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ 29static void 30quit(int rc) 31{ 32 SDL_Quit(); 33 SDLTest_CommonDestroyState(state); 34 /* Let 'main()' return normally */ 35 if (rc != 0) { 36 exit(rc); 37 } 38} 39 40static const char * 41getprioritystr(SDL_ThreadPriority priority) 42{ 43 switch (priority) { 44 case SDL_THREAD_PRIORITY_LOW: 45 return "SDL_THREAD_PRIORITY_LOW"; 46 case SDL_THREAD_PRIORITY_NORMAL: 47 return "SDL_THREAD_PRIORITY_NORMAL"; 48 case SDL_THREAD_PRIORITY_HIGH: 49 return "SDL_THREAD_PRIORITY_HIGH"; 50 case SDL_THREAD_PRIORITY_TIME_CRITICAL: 51 return "SDL_THREAD_PRIORITY_TIME_CRITICAL"; 52 } 53 54 return "???"; 55} 56 57static int SDLCALL CheckMainThread(void *data) 58{ 59 bool *thread_is_main = (bool *)data; 60 *thread_is_main = SDL_IsMainThread(); 61 return 0; 62} 63 64static int SDLCALL ThreadFunc(void *data) 65{ 66 SDL_ThreadPriority prio = SDL_THREAD_PRIORITY_NORMAL; 67 68 SDL_SetTLS(&tls, "baby thread", NULL); 69 SDL_Log("Started thread %s: My thread id is %" SDL_PRIu64 ", thread data = %s", 70 (char *)data, SDL_GetCurrentThreadID(), (const char *)SDL_GetTLS(&tls)); 71 while (SDL_GetAtomicInt(&alive)) { 72 SDL_Log("Thread '%s' is alive!", (char *)data); 73 74 if (testprio) { 75 SDL_Log("SDL_SetCurrentThreadPriority(%s):%d", getprioritystr(prio), SDL_SetCurrentThreadPriority(prio)); 76 if (++prio > SDL_THREAD_PRIORITY_TIME_CRITICAL) { 77 prio = SDL_THREAD_PRIORITY_LOW; 78 } 79 } 80 81 SDL_Delay(1 * 1000); 82 } 83 SDL_Log("Thread '%s' exiting!", (char *)data); 84 return 0; 85} 86 87static void 88killed(int sig) 89{ 90 SDL_Log("Killed with SIGTERM, waiting 5 seconds to exit"); 91 SDL_Delay(5 * 1000); 92 SDL_SetAtomicInt(&alive, 0); 93 SDL_WaitThread(thread, NULL); 94 quit(0); 95} 96 97int main(int argc, char *argv[]) 98{ 99 int i; 100 bool child_is_main = true; 101 102 /* Initialize test framework */ 103 state = SDLTest_CommonCreateState(argv, 0); 104 if (!state) { 105 return 1; 106 } 107 108 /* Parse commandline */ 109 for (i = 1; i < argc;) { 110 int consumed; 111 112 consumed = SDLTest_CommonArg(state, i); 113 if (!consumed) { 114 if (SDL_strcmp("--prio", argv[i]) == 0) { 115 testprio = 1; 116 consumed = 1; 117 } 118 } 119 if (consumed <= 0) { 120 static const char *options[] = { "[--prio]", NULL }; 121 SDLTest_CommonLogUsage(state, argv[0], options); 122 quit(1); 123 } 124 125 i += consumed; 126 } 127 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 146 if (SDL_GetEnvironmentVariable(SDL_GetEnvironment(), "SDL_TESTS_QUICK") != NULL) { 147 SDL_Log("Not running slower tests"); 148 quit(0); 149 return 0; 150 } 151 152 SDL_SetTLS(&tls, "main thread", NULL); 153 SDL_Log("Main thread data initially: %s", (const char *)SDL_GetTLS(&tls)); 154 155 SDL_SetAtomicInt(&alive, 1); 156 thread = SDL_CreateThread(ThreadFunc, "One", "#1"); 157 if (!thread) { 158 SDL_Log("Couldn't create thread: %s", SDL_GetError()); 159 quit(1); 160 } 161 SDL_Delay(5 * 1000); 162 SDL_Log("Waiting for thread #1"); 163 SDL_SetAtomicInt(&alive, 0); 164 SDL_WaitThread(thread, NULL); 165 166 SDL_Log("Main thread data finally: %s", (const char *)SDL_GetTLS(&tls)); 167 168 SDL_SetAtomicInt(&alive, 1); 169 (void)signal(SIGTERM, killed); 170 thread = SDL_CreateThread(ThreadFunc, "Two", "#2"); 171 if (!thread) { 172 SDL_Log("Couldn't create thread: %s", SDL_GetError()); 173 quit(1); 174 } 175 (void)raise(SIGTERM); 176 177 SDL_Quit(); /* Never reached */ 178 return 0; /* Never reached */ 179} 180
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.