Atlas - testerror.c
Home / ext / SDL / test Lines: 1 | Size: 3048 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 and error handling */ 14 15#include <stdlib.h> 16 17#include <SDL3/SDL.h> 18#include <SDL3/SDL_main.h> 19#include <SDL3/SDL_test.h> 20 21static int alive = 0; 22 23/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ 24static void 25quit(int rc) 26{ 27 SDL_Quit(); 28 /* Let 'main()' return normally */ 29 if (rc != 0) { 30 exit(rc); 31 } 32} 33 34static int SDLCALL 35ThreadFunc(void *data) 36{ 37 /* Set the child thread error string */ 38 SDL_SetError("Thread %s (%" SDL_PRIu64 ") had a problem: %s", 39 (char *)data, SDL_GetCurrentThreadID(), "nevermind"); 40 while (alive) { 41 SDL_Log("Thread '%s' is alive!", (char *)data); 42 SDL_Delay(1 * 1000); 43 } 44 SDL_Log("Child thread error string: %s", SDL_GetError()); 45 return 0; 46} 47 48int main(int argc, char *argv[]) 49{ 50 SDL_Thread *thread; 51 SDLTest_CommonState *state; 52 int i; 53 bool enable_threads = true; 54 55 /* Initialize test framework */ 56 state = SDLTest_CommonCreateState(argv, 0); 57 if (!state) { 58 return 1; 59 } 60 61 /* Parse commandline */ 62 for (i = 1; i < argc;) { 63 int consumed; 64 65 consumed = SDLTest_CommonArg(state, i); 66 if (consumed == 0) { 67 consumed = -1; 68 if (SDL_strcasecmp(argv[i], "--no-threads") == 0) { 69 enable_threads = false; 70 consumed = 1; 71 } 72 } 73 if (consumed < 0) { 74 static const char *options[] = { 75 "[--no-threads]", 76 NULL 77 }; 78 SDLTest_CommonLogUsage(state, argv[0], options); 79 return 1; 80 } 81 i += consumed; 82 } 83 84 /* Load the SDL library */ 85 if (!SDL_Init(0)) { 86 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError()); 87 return 1; 88 } 89 90 /* Set the error value for the main thread */ 91 SDL_SetError("No worries"); 92 93 if (SDL_GetEnvironmentVariable(SDL_GetEnvironment(), "SDL_TESTS_QUICK") != NULL) { 94 SDL_Log("Not running slower tests"); 95 SDL_Quit(); 96 return 0; 97 } 98 99 if (enable_threads) { 100 alive = 1; 101 thread = SDL_CreateThread(ThreadFunc, NULL, "#1"); 102 if (!thread) { 103 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s", SDL_GetError()); 104 quit(1); 105 } 106 SDL_Delay(5 * 1000); 107 SDL_Log("Waiting for thread #1"); 108 alive = 0; 109 SDL_WaitThread(thread, NULL); 110 } 111 112 SDL_Log("Main thread error string: %s", SDL_GetError()); 113 114 SDL_Quit(); 115 SDLTest_CommonDestroyState(state); 116 return 0; 117} 118[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.