2 Copyright (C) 1997-2018 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 /* Test the thread and mutex locking functions
14 Also exercises the system's signal/thread interaction
19 #include <stdlib.h> /* for atexit() */
23 static SDL_mutex *mutex = NULL;
24 static SDL_threadID mainthread;
25 static SDL_Thread *threads[6];
26 static SDL_atomic_t doterminate;
29 * SDL_Quit() shouldn't be used with atexit() directly because
30 * calling conventions may differ...
33 SDL_Quit_Wrapper(void)
41 SDL_Log("Process %lu: exiting\n", SDL_ThreadID());
47 signal(SIGINT, terminate);
48 SDL_AtomicSet(&doterminate, 1);
54 SDL_threadID id = SDL_ThreadID();
56 SDL_Log("Process %lu: Cleaning up...\n", id == mainthread ? 0 : id);
57 SDL_AtomicSet(&doterminate, 1);
58 for (i = 0; i < 6; ++i)
59 SDL_WaitThread(threads[i], NULL);
60 SDL_DestroyMutex(mutex);
67 if (SDL_ThreadID() == mainthread)
68 //signal(SIGTERM, closemutex);
69 signal(SIGTERM, closemutex);
70 while (!SDL_AtomicGet(&doterminate)) {
71 SDL_Log("Process %lu ready to work\n", SDL_ThreadID());
72 if (SDL_LockMutex(mutex) < 0) {
73 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock mutex: %s", SDL_GetError());
76 SDL_Log("Process %lu, working!\n", SDL_ThreadID());
78 SDL_Log("Process %lu, done!\n", SDL_ThreadID());
79 if (SDL_UnlockMutex(mutex) < 0) {
80 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't unlock mutex: %s", SDL_GetError());
83 /* If this sleep isn't done, then threads may starve */
86 if (SDL_ThreadID() == mainthread && SDL_AtomicGet(&doterminate)) {
87 SDL_Log("Process %lu: raising SIGTERM\n", SDL_ThreadID());
93 main(int argc, char *argv[])
98 /* Enable standard application logging */
99 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
101 /* Load the SDL library */
102 if (SDL_Init(0) < 0) {
103 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
106 atexit(SDL_Quit_Wrapper);
108 SDL_AtomicSet(&doterminate, 0);
110 if ((mutex = SDL_CreateMutex()) == NULL) {
111 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create mutex: %s\n", SDL_GetError());
115 mainthread = SDL_ThreadID();
116 SDL_Log("Main thread: %lu\n", mainthread);
118 for (i = 0; i < maxproc; ++i) {
120 SDL_snprintf(name, sizeof (name), "Worker%d", i);
121 if ((threads[i] = SDL_CreateThread(Run, name, NULL)) == NULL)
122 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread!\n");
124 signal(SIGINT, terminate);
127 return (0); /* Never reached */