Debian Codegraph @

test/testlock.c

raw
1 /*
2 Copyright (C) 1997-2026 Sam Lantinga <[email protected]>
3
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.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11 */
12
13 /* Test the thread and mutex locking functions
14 Also exercises the system's signal/thread interaction
15 */
16
17 #include <signal.h>
18 #include <stdio.h>
19 #include <stdlib.h> /* for atexit() */
20
21 #include "SDL.h"
22
23 static SDL_mutex *mutex = NULL;
24 static SDL_threadID mainthread;
25 static SDL_Thread *threads[6];
26 static SDL_atomic_t doterminate;
27
28 /*
29 * SDL_Quit() shouldn't be used with atexit() directly because
30 * calling conventions may differ...
31 */
32 static void
33 SDL_Quit_Wrapper(void)
34 {
35 SDL_Quit();
36 }
37
38 void printid(void)
39 {
40 SDL_Log("Process %lu: exiting\n", SDL_ThreadID());
41 }
42
43 void terminate(int sig)
44 {
45 (void)signal(SIGINT, terminate);
46 SDL_AtomicSet(&doterminate, 1);
47 }
48
49 void closemutex(int sig)
50 {
51 SDL_threadID id = SDL_ThreadID();
52 int i;
53 SDL_Log("Process %lu: Cleaning up...\n", id == mainthread ? 0 : id);
54 SDL_AtomicSet(&doterminate, 1);
55 for (i = 0; i < 6; ++i) {
56 SDL_WaitThread(threads[i], NULL);
57 }
58 SDL_DestroyMutex(mutex);
59 exit(sig);
60 }
61
62 int SDLCALL
63 Run(void *data)
64 {
65 if (SDL_ThreadID() == mainthread) {
66 (void)signal(SIGTERM, closemutex);
67 }
68 while (!SDL_AtomicGet(&doterminate)) {
69 SDL_Log("Process %lu ready to work\n", SDL_ThreadID());
70 if (SDL_LockMutex(mutex) < 0) {
71 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock mutex: %s", SDL_GetError());
72 exit(1);
73 }
74 SDL_Log("Process %lu, working!\n", SDL_ThreadID());
75 SDL_Delay(1 * 1000);
76 SDL_Log("Process %lu, done!\n", SDL_ThreadID());
77 if (SDL_UnlockMutex(mutex) < 0) {
78 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't unlock mutex: %s", SDL_GetError());
79 exit(1);
80 }
81 /* If this sleep isn't done, then threads may starve */
82 SDL_Delay(10);
83 }
84 if (SDL_ThreadID() == mainthread && SDL_AtomicGet(&doterminate)) {
85 SDL_Log("Process %lu: raising SIGTERM\n", SDL_ThreadID());
86 (void)raise(SIGTERM);
87 }
88 return 0;
89 }
90
91 int main(int argc, char *argv[])
92 {
93 int i;
94 int maxproc = 6;
95
96 /* Enable standard application logging */
97 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
98
99 /* Load the SDL library */
100 if (SDL_Init(0) < 0) {
101 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", SDL_GetError());
102 exit(1);
103 }
104 (void)atexit(SDL_Quit_Wrapper);
105
106 SDL_AtomicSet(&doterminate, 0);
107
108 mutex = SDL_CreateMutex();
109 if (!mutex) {
110 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create mutex: %s\n", SDL_GetError());
111 exit(1);
112 }
113
114 mainthread = SDL_ThreadID();
115 SDL_Log("Main thread: %lu\n", mainthread);
116 (void)atexit(printid);
117 for (i = 0; i < maxproc; ++i) {
118 char name[64];
119 (void)SDL_snprintf(name, sizeof(name), "Worker%d", i);
120 threads[i] = SDL_CreateThread(Run, name, NULL);
121 if (threads[i] == NULL) {
122 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread!\n");
123 }
124 }
125 (void)signal(SIGINT, terminate);
126 Run(NULL);
127
128 return 0; /* Never reached */
129 }