2015-06-21 09:33:46 -06:00
|
|
|
/*
|
2024-01-01 14:15:26 -07:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Simple test of the SDL threading code */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2022-11-26 21:43:38 -07:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-14 21:58:20 -07:00
|
|
|
#include <SDL3/SDL_main.h>
|
2023-03-16 17:25:39 -06:00
|
|
|
#include <SDL3/SDL_test.h>
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
#define NUMTHREADS 10
|
|
|
|
|
2023-02-23 12:33:51 -07:00
|
|
|
static SDL_AtomicInt time_for_threads_to_die[NUMTHREADS];
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
|
|
|
static void
|
|
|
|
quit(int rc)
|
|
|
|
{
|
|
|
|
SDL_Quit();
|
2023-04-12 03:07:28 -06:00
|
|
|
/* Let 'main()' return normally */
|
|
|
|
if (rc != 0) {
|
|
|
|
exit(rc);
|
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
2023-03-08 03:40:07 -07:00
|
|
|
static int SDLCALL
|
2015-06-21 09:33:46 -06:00
|
|
|
SubThreadFunc(void *data)
|
|
|
|
{
|
2022-11-30 13:51:59 -07:00
|
|
|
while (!*(int volatile *)data) {
|
2023-03-17 13:58:01 -06:00
|
|
|
SDL_Delay(10);
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-08 03:40:07 -07:00
|
|
|
static int SDLCALL
|
2015-06-21 09:33:46 -06:00
|
|
|
ThreadFunc(void *data)
|
|
|
|
{
|
|
|
|
SDL_Thread *sub_threads[NUMTHREADS];
|
|
|
|
int flags[NUMTHREADS];
|
|
|
|
int i;
|
2022-11-30 13:51:59 -07:00
|
|
|
int tid = (int)(uintptr_t)data;
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
SDL_Log("Creating Thread %d\n", tid);
|
|
|
|
|
|
|
|
for (i = 0; i < NUMTHREADS; i++) {
|
|
|
|
char name[64];
|
2023-03-09 16:10:00 -07:00
|
|
|
(void)SDL_snprintf(name, sizeof(name), "Child%d_%d", tid, i);
|
2015-06-21 09:33:46 -06:00
|
|
|
flags[i] = 0;
|
|
|
|
sub_threads[i] = SDL_CreateThread(SubThreadFunc, name, &flags[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Log("Thread '%d' waiting for signal\n", tid);
|
2016-01-03 04:50:50 -07:00
|
|
|
while (SDL_AtomicGet(&time_for_threads_to_die[tid]) != 1) {
|
2022-11-30 13:51:59 -07:00
|
|
|
; /* do nothing */
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Log("Thread '%d' sending signals to subthreads\n", tid);
|
|
|
|
for (i = 0; i < NUMTHREADS; i++) {
|
|
|
|
flags[i] = 1;
|
|
|
|
SDL_WaitThread(sub_threads[i], NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Log("Thread '%d' exiting!\n", tid);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
int main(int argc, char *argv[])
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
|
|
|
SDL_Thread *threads[NUMTHREADS];
|
|
|
|
int i;
|
2023-03-16 17:25:39 -06:00
|
|
|
SDLTest_CommonState *state;
|
|
|
|
|
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, 0);
|
2023-11-09 14:29:15 -07:00
|
|
|
if (!state) {
|
2023-03-16 17:25:39 -06:00
|
|
|
return 1;
|
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2015-11-25 13:39:28 -07:00
|
|
|
/* Enable standard application logging */
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
|
|
|
|
|
|
|
/* Load the SDL library */
|
|
|
|
if (SDL_Init(0) < 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
2022-11-27 09:38:43 -07:00
|
|
|
return 1;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
2023-03-16 17:25:39 -06:00
|
|
|
if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
|
|
|
|
SDLTest_CommonDestroyState(state);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-12-01 14:07:03 -07:00
|
|
|
(void)signal(SIGSEGV, SIG_DFL);
|
2015-06-21 09:33:46 -06:00
|
|
|
for (i = 0; i < NUMTHREADS; i++) {
|
|
|
|
char name[64];
|
2023-03-09 16:10:00 -07:00
|
|
|
(void)SDL_snprintf(name, sizeof(name), "Parent%d", i);
|
2016-01-03 04:50:50 -07:00
|
|
|
SDL_AtomicSet(&time_for_threads_to_die[i], 0);
|
2022-11-30 13:51:59 -07:00
|
|
|
threads[i] = SDL_CreateThread(ThreadFunc, name, (void *)(uintptr_t)i);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
if (threads[i] == NULL) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s\n", SDL_GetError());
|
|
|
|
quit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < NUMTHREADS; i++) {
|
2016-01-03 04:50:50 -07:00
|
|
|
SDL_AtomicSet(&time_for_threads_to_die[i], 1);
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < NUMTHREADS; i++) {
|
|
|
|
SDL_WaitThread(threads[i], NULL);
|
|
|
|
}
|
|
|
|
SDL_Quit();
|
2023-03-16 17:25:39 -06:00
|
|
|
SDLTest_CommonDestroyState(state);
|
2022-11-27 09:38:43 -07:00
|
|
|
return 0;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|