2
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
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.
8
Permission is granted to anyone to use this software for any purpose,
9
including commercial applications, and to alter it and redistribute it
13
/* Simple test of the SDL threading code */
19
#include <SDL3/SDL_main.h>
20
#include <SDL3/SDL_test.h>
24
static SDL_AtomicInt time_for_threads_to_die[NUMTHREADS];
26
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
31
/* Let 'main()' return normally */
38
SubThreadFunc(void *data)
40
while (!*(int volatile *)data) {
49
SDL_Thread *sub_threads[NUMTHREADS];
50
int flags[NUMTHREADS];
52
int tid = (int)(uintptr_t)data;
54
SDL_Log("Creating Thread %d\n", tid);
56
for (i = 0; i < NUMTHREADS; i++) {
58
(void)SDL_snprintf(name, sizeof(name), "Child%d_%d", tid, i);
60
sub_threads[i] = SDL_CreateThread(SubThreadFunc, name, &flags[i]);
63
SDL_Log("Thread '%d' waiting for signal\n", tid);
64
while (SDL_AtomicGet(&time_for_threads_to_die[tid]) != 1) {
68
SDL_Log("Thread '%d' sending signals to subthreads\n", tid);
69
for (i = 0; i < NUMTHREADS; i++) {
71
SDL_WaitThread(sub_threads[i], NULL);
74
SDL_Log("Thread '%d' exiting!\n", tid);
79
int main(int argc, char *argv[])
81
SDL_Thread *threads[NUMTHREADS];
83
SDLTest_CommonState *state;
85
/* Initialize test framework */
86
state = SDLTest_CommonCreateState(argv, 0);
91
/* Enable standard application logging */
92
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
94
/* Load the SDL library */
95
if (SDL_Init(0) < 0) {
96
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
100
if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
102
SDLTest_CommonDestroyState(state);
106
(void)signal(SIGSEGV, SIG_DFL);
107
for (i = 0; i < NUMTHREADS; i++) {
109
(void)SDL_snprintf(name, sizeof(name), "Parent%d", i);
110
SDL_AtomicSet(&time_for_threads_to_die[i], 0);
111
threads[i] = SDL_CreateThread(ThreadFunc, name, (void *)(uintptr_t)i);
113
if (threads[i] == NULL) {
114
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
119
for (i = 0; i < NUMTHREADS; i++) {
120
SDL_AtomicSet(&time_for_threads_to_die[i], 1);
123
for (i = 0; i < NUMTHREADS; i++) {
124
SDL_WaitThread(threads[i], NULL);
127
SDLTest_CommonDestroyState(state);