Atlas - torturethread.c
Home / ext / SDL / test Lines: 1 | Size: 3212 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]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 <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 22#ifdef SDL_PLATFORM_DOS 23#define NUMTHREADS 3 /* DOS cooperative scheduler has limited thread slots */ 24#else 25#define NUMTHREADS 10 26#endif 27 28static SDL_AtomicInt time_for_threads_to_die[NUMTHREADS]; 29 30/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ 31static void 32quit(int rc) 33{ 34 SDL_Quit(); 35 /* Let 'main()' return normally */ 36 if (rc != 0) { 37 exit(rc); 38 } 39} 40 41static int SDLCALL 42SubThreadFunc(void *data) 43{ 44 SDL_AtomicInt *flag = (SDL_AtomicInt *)data; 45 while (!SDL_GetAtomicInt(flag)) { 46 SDL_Delay(10); 47 } 48 return 0; 49} 50 51static int SDLCALL 52ThreadFunc(void *data) 53{ 54 SDL_Thread *sub_threads[NUMTHREADS]; 55 SDL_AtomicInt flags[NUMTHREADS]; 56 int i; 57 int tid = (int)(uintptr_t)data; 58 59 SDL_Log("Creating Thread %d", tid); 60 61 for (i = 0; i < NUMTHREADS; i++) { 62 char name[64]; 63 (void)SDL_snprintf(name, sizeof(name), "Child%d_%d", tid, i); 64 SDL_SetAtomicInt(&flags[i], 0); 65 sub_threads[i] = SDL_CreateThread(SubThreadFunc, name, &flags[i]); 66 } 67 68 SDL_Log("Thread '%d' waiting for signal", tid); 69 while (SDL_GetAtomicInt(&time_for_threads_to_die[tid]) != 1) { 70#ifdef SDL_PLATFORM_DOS 71 SDL_Delay(0); /* Yield for cooperative threading */ 72#endif 73 } 74 75 SDL_Log("Thread '%d' sending signals to subthreads", tid); 76 for (i = 0; i < NUMTHREADS; i++) { 77 SDL_SetAtomicInt(&flags[i], 1); 78 SDL_WaitThread(sub_threads[i], NULL); 79 } 80 81 SDL_Log("Thread '%d' exiting!", tid); 82 83 return 0; 84} 85 86int main(int argc, char *argv[]) 87{ 88 SDL_Thread *threads[NUMTHREADS]; 89 int i; 90 SDLTest_CommonState *state; 91 92 /* Initialize test framework */ 93 state = SDLTest_CommonCreateState(argv, 0); 94 if (!state) { 95 return 1; 96 } 97 98 if (!SDLTest_CommonDefaultArgs(state, argc, argv)) { 99 SDL_Quit(); 100 SDLTest_CommonDestroyState(state); 101 return 1; 102 } 103 104 (void)signal(SIGSEGV, SIG_DFL); 105 for (i = 0; i < NUMTHREADS; i++) { 106 char name[64]; 107 (void)SDL_snprintf(name, sizeof(name), "Parent%d", i); 108 SDL_SetAtomicInt(&time_for_threads_to_die[i], 0); 109 threads[i] = SDL_CreateThread(ThreadFunc, name, (void *)(uintptr_t)i); 110 111 if (threads[i] == NULL) { 112 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s", SDL_GetError()); 113 quit(1); 114 } 115 } 116 117 for (i = 0; i < NUMTHREADS; i++) { 118 SDL_SetAtomicInt(&time_for_threads_to_die[i], 1); 119 } 120 121 for (i = 0; i < NUMTHREADS; i++) { 122 SDL_WaitThread(threads[i], NULL); 123 } 124 SDL_Quit(); 125 SDLTest_CommonDestroyState(state); 126 return 0; 127} 128[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.