2015-06-21 23:33:46 +08:00
|
|
|
/*
|
2023-01-10 01:41:41 +08:00
|
|
|
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 23:33:46 +08: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-27 12:43:38 +08:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-15 12:58:20 +08:00
|
|
|
#include <SDL3/SDL_main.h>
|
2023-03-17 07:25:39 +08:00
|
|
|
#include <SDL3/SDL_test.h>
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
#define NUMTHREADS 10
|
|
|
|
|
2023-02-24 03:33:51 +08:00
|
|
|
static SDL_AtomicInt time_for_threads_to_die[NUMTHREADS];
|
2015-06-21 23:33:46 +08: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 17:07:28 +08:00
|
|
|
/* Let 'main()' return normally */
|
|
|
|
if (rc != 0) {
|
|
|
|
exit(rc);
|
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
|
2023-03-08 18:40:07 +08:00
|
|
|
static int SDLCALL
|
2015-06-21 23:33:46 +08:00
|
|
|
SubThreadFunc(void *data)
|
|
|
|
{
|
2022-12-01 04:51:59 +08:00
|
|
|
while (!*(int volatile *)data) {
|
2023-03-18 03:58:01 +08:00
|
|
|
SDL_Delay(10);
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-08 18:40:07 +08:00
|
|
|
static int SDLCALL
|
2015-06-21 23:33:46 +08:00
|
|
|
ThreadFunc(void *data)
|
|
|
|
{
|
|
|
|
SDL_Thread *sub_threads[NUMTHREADS];
|
|
|
|
int flags[NUMTHREADS];
|
|
|
|
int i;
|
2022-12-01 04:51:59 +08:00
|
|
|
int tid = (int)(uintptr_t)data;
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
SDL_Log("Creating Thread %d\n", tid);
|
|
|
|
|
|
|
|
for (i = 0; i < NUMTHREADS; i++) {
|
|
|
|
char name[64];
|
2023-03-10 07:10:00 +08:00
|
|
|
(void)SDL_snprintf(name, sizeof(name), "Child%d_%d", tid, i);
|
2015-06-21 23:33:46 +08: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 19:50:50 +08:00
|
|
|
while (SDL_AtomicGet(&time_for_threads_to_die[tid]) != 1) {
|
2022-12-01 04:51:59 +08:00
|
|
|
; /* do nothing */
|
2015-06-21 23:33:46 +08: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-12-01 04:51:59 +08:00
|
|
|
int main(int argc, char *argv[])
|
2015-06-21 23:33:46 +08:00
|
|
|
{
|
|
|
|
SDL_Thread *threads[NUMTHREADS];
|
|
|
|
int i;
|
2023-03-17 07:25:39 +08:00
|
|
|
SDLTest_CommonState *state;
|
|
|
|
|
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, 0);
|
|
|
|
if (state == NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
|
2015-11-26 04:39:28 +08:00
|
|
|
/* Enable standard application logging */
|
2015-06-21 23:33:46 +08: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-28 00:38:43 +08:00
|
|
|
return 1;
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
|
2023-03-17 07:25:39 +08:00
|
|
|
if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
|
|
|
|
SDLTest_CommonDestroyState(state);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-12-02 05:07:03 +08:00
|
|
|
(void)signal(SIGSEGV, SIG_DFL);
|
2015-06-21 23:33:46 +08:00
|
|
|
for (i = 0; i < NUMTHREADS; i++) {
|
|
|
|
char name[64];
|
2023-03-10 07:10:00 +08:00
|
|
|
(void)SDL_snprintf(name, sizeof(name), "Parent%d", i);
|
2016-01-03 19:50:50 +08:00
|
|
|
SDL_AtomicSet(&time_for_threads_to_die[i], 0);
|
2022-12-01 04:51:59 +08:00
|
|
|
threads[i] = SDL_CreateThread(ThreadFunc, name, (void *)(uintptr_t)i);
|
2015-06-21 23:33:46 +08: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 19:50:50 +08:00
|
|
|
SDL_AtomicSet(&time_for_threads_to_die[i], 1);
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < NUMTHREADS; i++) {
|
|
|
|
SDL_WaitThread(threads[i], NULL);
|
|
|
|
}
|
|
|
|
SDL_Quit();
|
2023-03-17 07:25:39 +08:00
|
|
|
SDLTest_CommonDestroyState(state);
|
2022-11-28 00:38:43 +08:00
|
|
|
return 0;
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|