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