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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* 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 12:43:38 +08:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-15 12:58:20 +08:00
|
|
|
#include <SDL3/SDL_main.h>
|
2015-06-21 23:33:46 +08:00
|
|
|
|
|
|
|
static SDL_mutex *mutex = NULL;
|
|
|
|
static SDL_threadID mainthread;
|
|
|
|
static SDL_Thread *threads[6];
|
2023-02-24 03:33:51 +08:00
|
|
|
static SDL_AtomicInt doterminate;
|
2015-06-21 23:33:46 +08:00
|
|
|
|
2023-02-02 07:21:53 +08:00
|
|
|
/**
|
2015-06-21 23:33:46 +08:00
|
|
|
* SDL_Quit() shouldn't be used with atexit() directly because
|
2023-03-10 07:10:00 +08:00
|
|
|
* calling conventions may differ...
|
2015-06-21 23:33:46 +08:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
SDL_Quit_Wrapper(void)
|
|
|
|
{
|
|
|
|
SDL_Quit();
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:12:45 +08:00
|
|
|
static void printid(void)
|
2015-06-21 23:33:46 +08:00
|
|
|
{
|
|
|
|
SDL_Log("Process %lu: exiting\n", SDL_ThreadID());
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:12:45 +08:00
|
|
|
static void terminate(int sig)
|
2015-06-21 23:33:46 +08:00
|
|
|
{
|
2022-12-02 05:07:03 +08:00
|
|
|
(void)signal(SIGINT, terminate);
|
2016-01-03 19:50:50 +08:00
|
|
|
SDL_AtomicSet(&doterminate, 1);
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
|
2023-03-08 23:12:45 +08:00
|
|
|
static void closemutex(int sig)
|
2015-06-21 23:33:46 +08:00
|
|
|
{
|
|
|
|
SDL_threadID id = SDL_ThreadID();
|
|
|
|
int i;
|
|
|
|
SDL_Log("Process %lu: Cleaning up...\n", id == mainthread ? 0 : id);
|
2016-01-03 19:50:50 +08:00
|
|
|
SDL_AtomicSet(&doterminate, 1);
|
2022-11-28 00:38:43 +08:00
|
|
|
for (i = 0; i < 6; ++i) {
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_WaitThread(threads[i], NULL);
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_DestroyMutex(mutex);
|
|
|
|
exit(sig);
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:12:45 +08:00
|
|
|
static int SDLCALL
|
2015-06-21 23:33:46 +08:00
|
|
|
Run(void *data)
|
|
|
|
{
|
2022-11-28 00:38:43 +08:00
|
|
|
if (SDL_ThreadID() == mainthread) {
|
2022-12-02 05:07:03 +08:00
|
|
|
(void)signal(SIGTERM, closemutex);
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2016-01-03 19:50:50 +08:00
|
|
|
while (!SDL_AtomicGet(&doterminate)) {
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_Log("Process %lu ready to work\n", SDL_ThreadID());
|
|
|
|
if (SDL_LockMutex(mutex) < 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock mutex: %s", SDL_GetError());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
SDL_Log("Process %lu, working!\n", SDL_ThreadID());
|
|
|
|
SDL_Delay(1 * 1000);
|
|
|
|
SDL_Log("Process %lu, done!\n", SDL_ThreadID());
|
|
|
|
if (SDL_UnlockMutex(mutex) < 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't unlock mutex: %s", SDL_GetError());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/* If this sleep isn't done, then threads may starve */
|
|
|
|
SDL_Delay(10);
|
|
|
|
}
|
2016-01-03 19:50:50 +08:00
|
|
|
if (SDL_ThreadID() == mainthread && SDL_AtomicGet(&doterminate)) {
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_Log("Process %lu: raising SIGTERM\n", SDL_ThreadID());
|
2022-12-02 05:07:03 +08:00
|
|
|
(void)raise(SIGTERM);
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
2022-11-28 00:38:43 +08:00
|
|
|
return 0;
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
|
|
|
|
2022-12-01 04:51:59 +08:00
|
|
|
int main(int argc, char *argv[])
|
2015-06-21 23:33:46 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int maxproc = 6;
|
|
|
|
|
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, "%s\n", SDL_GetError());
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-12-02 05:07:03 +08:00
|
|
|
(void)atexit(SDL_Quit_Wrapper);
|
2015-06-21 23:33:46 +08:00
|
|
|
|
2016-01-03 19:50:50 +08:00
|
|
|
SDL_AtomicSet(&doterminate, 0);
|
|
|
|
|
2022-12-02 05:07:03 +08:00
|
|
|
mutex = SDL_CreateMutex();
|
|
|
|
if (mutex == NULL) {
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create mutex: %s\n", SDL_GetError());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
mainthread = SDL_ThreadID();
|
|
|
|
SDL_Log("Main thread: %lu\n", mainthread);
|
2022-12-02 05:07:03 +08:00
|
|
|
(void)atexit(printid);
|
2015-06-21 23:33:46 +08:00
|
|
|
for (i = 0; i < maxproc; ++i) {
|
|
|
|
char name[64];
|
2023-03-10 07:10:00 +08:00
|
|
|
(void)SDL_snprintf(name, sizeof(name), "Worker%d", i);
|
2022-12-02 05:07:03 +08:00
|
|
|
threads[i] = SDL_CreateThread(Run, name, NULL);
|
|
|
|
if (threads[i] == NULL) {
|
2015-06-21 23:33:46 +08:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread!\n");
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|
2022-12-02 05:07:03 +08:00
|
|
|
(void)signal(SIGINT, terminate);
|
2015-06-21 23:33:46 +08:00
|
|
|
Run(NULL);
|
|
|
|
|
2022-12-01 04:51:59 +08:00
|
|
|
return 0; /* Never reached */
|
2015-06-21 23:33:46 +08:00
|
|
|
}
|