2015-06-21 15:33:46 +00:00
|
|
|
/*
|
2024-01-01 21:15:26 +00:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 15:33:46 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Test the thread and mutex locking functions
|
|
|
|
Also exercises the system's signal/thread interaction
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdlib.h> /* for atexit() */
|
|
|
|
|
2022-11-27 04:43:38 +00:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-15 04:58:20 +00:00
|
|
|
#include <SDL3/SDL_main.h>
|
2023-03-16 15:56:16 +00:00
|
|
|
#include <SDL3/SDL_test.h>
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2023-04-28 14:31:12 +00:00
|
|
|
static SDL_Mutex *mutex = NULL;
|
2024-01-18 12:57:12 +00:00
|
|
|
static SDL_ThreadID mainthread;
|
2023-02-23 19:33:51 +00:00
|
|
|
static SDL_AtomicInt doterminate;
|
2023-03-16 15:56:16 +00:00
|
|
|
static int nb_threads = 6;
|
|
|
|
static SDL_Thread **threads;
|
|
|
|
static int worktime = 1000;
|
|
|
|
static SDLTest_CommonState *state;
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2023-02-01 23:21:53 +00:00
|
|
|
/**
|
2015-06-21 15:33:46 +00:00
|
|
|
* SDL_Quit() shouldn't be used with atexit() directly because
|
2023-03-09 23:10:00 +00:00
|
|
|
* calling conventions may differ...
|
2015-06-21 15:33:46 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
SDL_Quit_Wrapper(void)
|
|
|
|
{
|
|
|
|
SDL_Quit();
|
2023-03-16 15:56:16 +00:00
|
|
|
SDLTest_CommonDestroyState(state);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 15:12:45 +00:00
|
|
|
static void printid(void)
|
2015-06-21 15:33:46 +00:00
|
|
|
{
|
2024-01-18 12:57:12 +00:00
|
|
|
SDL_Log("Thread %" SDL_PRIu64 ": exiting\n", SDL_GetCurrentThreadID());
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 15:12:45 +00:00
|
|
|
static void terminate(int sig)
|
2015-06-21 15:33:46 +00:00
|
|
|
{
|
2022-12-01 21:07:03 +00:00
|
|
|
(void)signal(SIGINT, terminate);
|
2024-09-17 06:21:31 +00:00
|
|
|
SDL_SetAtomicInt(&doterminate, 1);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 15:12:45 +00:00
|
|
|
static void closemutex(int sig)
|
2015-06-21 15:33:46 +00:00
|
|
|
{
|
2024-01-18 12:57:12 +00:00
|
|
|
SDL_ThreadID id = SDL_GetCurrentThreadID();
|
2015-06-21 15:33:46 +00:00
|
|
|
int i;
|
2024-01-18 12:57:12 +00:00
|
|
|
SDL_Log("Thread %" SDL_PRIu64 ": Cleaning up...\n", id == mainthread ? 0 : id);
|
2024-09-17 06:21:31 +00:00
|
|
|
SDL_SetAtomicInt(&doterminate, 1);
|
2023-03-16 15:56:16 +00:00
|
|
|
if (threads) {
|
|
|
|
for (i = 0; i < nb_threads; ++i) {
|
|
|
|
SDL_WaitThread(threads[i], NULL);
|
|
|
|
}
|
|
|
|
SDL_free(threads);
|
|
|
|
threads = NULL;
|
2022-11-27 16:38:43 +00:00
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
SDL_DestroyMutex(mutex);
|
2023-04-12 09:07:28 +00:00
|
|
|
/* Let 'main()' return normally */
|
|
|
|
if (sig != 0) {
|
|
|
|
exit(sig);
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 15:12:45 +00:00
|
|
|
static int SDLCALL
|
2023-03-16 22:47:35 +00:00
|
|
|
Run(void *data)
|
2015-06-21 15:33:46 +00:00
|
|
|
{
|
2024-01-18 12:57:12 +00:00
|
|
|
SDL_ThreadID current_thread = SDL_GetCurrentThreadID();
|
|
|
|
|
|
|
|
if (current_thread == mainthread) {
|
2022-12-01 21:07:03 +00:00
|
|
|
(void)signal(SIGTERM, closemutex);
|
2022-11-27 16:38:43 +00:00
|
|
|
}
|
2024-01-18 12:57:12 +00:00
|
|
|
SDL_Log("Thread %" SDL_PRIu64 ": starting up", current_thread);
|
2024-09-17 06:21:31 +00:00
|
|
|
while (!SDL_GetAtomicInt(&doterminate)) {
|
2024-01-18 12:57:12 +00:00
|
|
|
SDL_Log("Thread %" SDL_PRIu64 ": ready to work\n", current_thread);
|
2023-10-25 14:00:26 +00:00
|
|
|
SDL_LockMutex(mutex);
|
2024-01-18 12:57:12 +00:00
|
|
|
SDL_Log("Thread %" SDL_PRIu64 ": start work!\n", current_thread);
|
2023-03-16 15:56:16 +00:00
|
|
|
SDL_Delay(1 * worktime);
|
2024-01-18 12:57:12 +00:00
|
|
|
SDL_Log("Thread %" SDL_PRIu64 ": work done!\n", current_thread);
|
2023-10-25 14:00:26 +00:00
|
|
|
SDL_UnlockMutex(mutex);
|
|
|
|
|
2015-06-21 15:33:46 +00:00
|
|
|
/* If this sleep isn't done, then threads may starve */
|
|
|
|
SDL_Delay(10);
|
|
|
|
}
|
2024-09-17 06:21:31 +00:00
|
|
|
if (current_thread == mainthread && SDL_GetAtomicInt(&doterminate)) {
|
2024-01-18 12:57:12 +00:00
|
|
|
SDL_Log("Thread %" SDL_PRIu64 ": raising SIGTERM\n", current_thread);
|
2022-12-01 21:07:03 +00:00
|
|
|
(void)raise(SIGTERM);
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
2024-01-18 12:57:12 +00:00
|
|
|
SDL_Log("Thread %" SDL_PRIu64 ": exiting!\n", current_thread);
|
2023-03-16 15:56:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-30 18:26:31 +00:00
|
|
|
#ifndef _WIN32
|
2024-05-27 00:56:29 +00:00
|
|
|
static Uint32 hit_timeout(void *param, SDL_TimerID timerID, Uint32 interval) {
|
2023-03-16 15:56:16 +00:00
|
|
|
SDL_Log("Hit timeout! Sending SIGINT!");
|
2024-04-25 01:01:33 +00:00
|
|
|
(void)raise(SIGINT);
|
2022-11-27 16:38:43 +00:00
|
|
|
return 0;
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
2023-03-16 15:56:16 +00:00
|
|
|
#endif
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2022-11-30 20:51:59 +00:00
|
|
|
int main(int argc, char *argv[])
|
2015-06-21 15:33:46 +00:00
|
|
|
{
|
|
|
|
int i;
|
2023-03-30 18:26:31 +00:00
|
|
|
#ifndef _WIN32
|
2023-03-16 15:56:16 +00:00
|
|
|
int timeout = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, 0);
|
2023-11-09 21:29:15 +00:00
|
|
|
if (!state) {
|
2023-03-16 15:56:16 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2023-03-16 15:56:16 +00:00
|
|
|
/* Parse commandline */
|
|
|
|
for (i = 1; i < argc;) {
|
|
|
|
int consumed;
|
|
|
|
|
|
|
|
consumed = SDLTest_CommonArg(state, i);
|
|
|
|
if (!consumed) {
|
|
|
|
if (SDL_strcmp(argv[i], "--nbthreads") == 0) {
|
|
|
|
if (argv[i + 1]) {
|
|
|
|
char *endptr;
|
|
|
|
nb_threads = SDL_strtol(argv[i + 1], &endptr, 0);
|
|
|
|
if (endptr != argv[i + 1] && *endptr == '\0' && nb_threads > 0) {
|
|
|
|
consumed = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (SDL_strcmp(argv[i], "--worktime") == 0) {
|
|
|
|
if (argv[i + 1]) {
|
|
|
|
char *endptr;
|
|
|
|
nb_threads = SDL_strtol(argv[i + 1], &endptr, 0);
|
|
|
|
if (endptr != argv[i + 1] && *endptr == '\0' && nb_threads > 0) {
|
|
|
|
consumed = 2;
|
|
|
|
}
|
|
|
|
}
|
2023-03-30 18:26:31 +00:00
|
|
|
#ifndef _WIN32
|
2023-03-16 15:56:16 +00:00
|
|
|
} else if (SDL_strcmp(argv[i], "--timeout") == 0) {
|
|
|
|
if (argv[i + 1]) {
|
|
|
|
char *endptr;
|
|
|
|
timeout = SDL_strtol(argv[i + 1], &endptr, 0);
|
|
|
|
if (endptr != argv[i + 1] && *endptr == '\0' && timeout > 0) {
|
|
|
|
consumed = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (consumed <= 0) {
|
|
|
|
static const char *options[] = {
|
|
|
|
"[--nbthreads NB]",
|
|
|
|
"[--worktime ms]",
|
2023-03-30 18:26:31 +00:00
|
|
|
#ifndef _WIN32
|
2023-03-16 15:56:16 +00:00
|
|
|
"[--timeout ms]",
|
|
|
|
#endif
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
SDLTest_CommonLogUsage(state, argv[0], options);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
i += consumed;
|
|
|
|
}
|
|
|
|
|
|
|
|
threads = SDL_malloc(nb_threads * sizeof(SDL_Thread*));
|
2015-06-21 15:33:46 +00:00
|
|
|
|
|
|
|
/* Load the SDL library */
|
2024-08-23 00:33:49 +00:00
|
|
|
if (!SDL_Init(0)) {
|
2015-06-21 15:33:46 +00:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-12-01 21:07:03 +00:00
|
|
|
(void)atexit(SDL_Quit_Wrapper);
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2024-09-17 06:21:31 +00:00
|
|
|
SDL_SetAtomicInt(&doterminate, 0);
|
2016-01-03 11:50:50 +00:00
|
|
|
|
2022-12-01 21:07:03 +00:00
|
|
|
mutex = SDL_CreateMutex();
|
2023-11-09 21:29:15 +00:00
|
|
|
if (!mutex) {
|
2015-06-21 15:33:46 +00:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create mutex: %s\n", SDL_GetError());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2024-01-18 12:57:12 +00:00
|
|
|
mainthread = SDL_GetCurrentThreadID();
|
|
|
|
SDL_Log("Main thread: %" SDL_PRIu64 "\n", mainthread);
|
2022-12-01 21:07:03 +00:00
|
|
|
(void)atexit(printid);
|
2023-03-16 15:56:16 +00:00
|
|
|
for (i = 0; i < nb_threads; ++i) {
|
2015-06-21 15:33:46 +00:00
|
|
|
char name[64];
|
2023-03-09 23:10:00 +00:00
|
|
|
(void)SDL_snprintf(name, sizeof(name), "Worker%d", i);
|
2023-03-16 22:47:35 +00:00
|
|
|
threads[i] = SDL_CreateThread(Run, name, NULL);
|
2022-12-01 21:07:03 +00:00
|
|
|
if (threads[i] == NULL) {
|
2015-06-21 15:33:46 +00:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread!\n");
|
2022-11-27 16:38:43 +00:00
|
|
|
}
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|
2023-03-16 15:56:16 +00:00
|
|
|
|
2023-03-30 18:26:31 +00:00
|
|
|
#ifndef _WIN32
|
2023-03-16 15:56:16 +00:00
|
|
|
if (timeout) {
|
|
|
|
SDL_AddTimer(timeout, hit_timeout, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-12-01 21:07:03 +00:00
|
|
|
(void)signal(SIGINT, terminate);
|
2023-03-16 22:47:35 +00:00
|
|
|
Run(NULL);
|
2015-06-21 15:33:46 +00:00
|
|
|
|
2022-11-30 20:51:59 +00:00
|
|
|
return 0; /* Never reached */
|
2015-06-21 15:33:46 +00:00
|
|
|
}
|