diff options
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testthread.c')
-rw-r--r-- | src/contrib/SDL-3.2.20/test/testthread.c | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testthread.c b/src/contrib/SDL-3.2.20/test/testthread.c new file mode 100644 index 0000000..70710ea --- /dev/null +++ b/src/contrib/SDL-3.2.20/test/testthread.c | |||
@@ -0,0 +1,160 @@ | |||
1 | /* | ||
2 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
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 | /* Simple test of the SDL threading code */ | ||
14 | |||
15 | #include <stdlib.h> | ||
16 | #include <signal.h> | ||
17 | |||
18 | #include <SDL3/SDL.h> | ||
19 | #include <SDL3/SDL_main.h> | ||
20 | #include <SDL3/SDL_test.h> | ||
21 | |||
22 | static SDL_TLSID tls; | ||
23 | static SDL_Thread *thread = NULL; | ||
24 | static SDL_AtomicInt alive; | ||
25 | static int testprio = 0; | ||
26 | static SDLTest_CommonState *state; | ||
27 | |||
28 | /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ | ||
29 | static void | ||
30 | quit(int rc) | ||
31 | { | ||
32 | SDL_Quit(); | ||
33 | SDLTest_CommonDestroyState(state); | ||
34 | /* Let 'main()' return normally */ | ||
35 | if (rc != 0) { | ||
36 | exit(rc); | ||
37 | } | ||
38 | } | ||
39 | |||
40 | static const char * | ||
41 | getprioritystr(SDL_ThreadPriority priority) | ||
42 | { | ||
43 | switch (priority) { | ||
44 | case SDL_THREAD_PRIORITY_LOW: | ||
45 | return "SDL_THREAD_PRIORITY_LOW"; | ||
46 | case SDL_THREAD_PRIORITY_NORMAL: | ||
47 | return "SDL_THREAD_PRIORITY_NORMAL"; | ||
48 | case SDL_THREAD_PRIORITY_HIGH: | ||
49 | return "SDL_THREAD_PRIORITY_HIGH"; | ||
50 | case SDL_THREAD_PRIORITY_TIME_CRITICAL: | ||
51 | return "SDL_THREAD_PRIORITY_TIME_CRITICAL"; | ||
52 | } | ||
53 | |||
54 | return "???"; | ||
55 | } | ||
56 | |||
57 | static int SDLCALL | ||
58 | ThreadFunc(void *data) | ||
59 | { | ||
60 | SDL_ThreadPriority prio = SDL_THREAD_PRIORITY_NORMAL; | ||
61 | |||
62 | SDL_SetTLS(&tls, "baby thread", NULL); | ||
63 | SDL_Log("Started thread %s: My thread id is %" SDL_PRIu64 ", thread data = %s", | ||
64 | (char *)data, SDL_GetCurrentThreadID(), (const char *)SDL_GetTLS(&tls)); | ||
65 | while (SDL_GetAtomicInt(&alive)) { | ||
66 | SDL_Log("Thread '%s' is alive!", (char *)data); | ||
67 | |||
68 | if (testprio) { | ||
69 | SDL_Log("SDL_SetCurrentThreadPriority(%s):%d", getprioritystr(prio), SDL_SetCurrentThreadPriority(prio)); | ||
70 | if (++prio > SDL_THREAD_PRIORITY_TIME_CRITICAL) { | ||
71 | prio = SDL_THREAD_PRIORITY_LOW; | ||
72 | } | ||
73 | } | ||
74 | |||
75 | SDL_Delay(1 * 1000); | ||
76 | } | ||
77 | SDL_Log("Thread '%s' exiting!", (char *)data); | ||
78 | return 0; | ||
79 | } | ||
80 | |||
81 | static void | ||
82 | killed(int sig) | ||
83 | { | ||
84 | SDL_Log("Killed with SIGTERM, waiting 5 seconds to exit"); | ||
85 | SDL_Delay(5 * 1000); | ||
86 | SDL_SetAtomicInt(&alive, 0); | ||
87 | SDL_WaitThread(thread, NULL); | ||
88 | quit(0); | ||
89 | } | ||
90 | |||
91 | int main(int argc, char *argv[]) | ||
92 | { | ||
93 | int i; | ||
94 | |||
95 | /* Initialize test framework */ | ||
96 | state = SDLTest_CommonCreateState(argv, 0); | ||
97 | if (!state) { | ||
98 | return 1; | ||
99 | } | ||
100 | |||
101 | /* Parse commandline */ | ||
102 | for (i = 1; i < argc;) { | ||
103 | int consumed; | ||
104 | |||
105 | consumed = SDLTest_CommonArg(state, i); | ||
106 | if (!consumed) { | ||
107 | if (SDL_strcmp("--prio", argv[i]) == 0) { | ||
108 | testprio = 1; | ||
109 | consumed = 1; | ||
110 | } | ||
111 | } | ||
112 | if (consumed <= 0) { | ||
113 | static const char *options[] = { "[--prio]", NULL }; | ||
114 | SDLTest_CommonLogUsage(state, argv[0], options); | ||
115 | exit(1); | ||
116 | } | ||
117 | |||
118 | i += consumed; | ||
119 | } | ||
120 | |||
121 | /* Load the SDL library */ | ||
122 | if (!SDL_Init(0)) { | ||
123 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError()); | ||
124 | return 1; | ||
125 | } | ||
126 | |||
127 | if (SDL_GetEnvironmentVariable(SDL_GetEnvironment(), "SDL_TESTS_QUICK") != NULL) { | ||
128 | SDL_Log("Not running slower tests"); | ||
129 | SDL_Quit(); | ||
130 | return 0; | ||
131 | } | ||
132 | |||
133 | SDL_SetTLS(&tls, "main thread", NULL); | ||
134 | SDL_Log("Main thread data initially: %s", (const char *)SDL_GetTLS(&tls)); | ||
135 | |||
136 | SDL_SetAtomicInt(&alive, 1); | ||
137 | thread = SDL_CreateThread(ThreadFunc, "One", "#1"); | ||
138 | if (!thread) { | ||
139 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s", SDL_GetError()); | ||
140 | quit(1); | ||
141 | } | ||
142 | SDL_Delay(5 * 1000); | ||
143 | SDL_Log("Waiting for thread #1"); | ||
144 | SDL_SetAtomicInt(&alive, 0); | ||
145 | SDL_WaitThread(thread, NULL); | ||
146 | |||
147 | SDL_Log("Main thread data finally: %s", (const char *)SDL_GetTLS(&tls)); | ||
148 | |||
149 | SDL_SetAtomicInt(&alive, 1); | ||
150 | (void)signal(SIGTERM, killed); | ||
151 | thread = SDL_CreateThread(ThreadFunc, "Two", "#2"); | ||
152 | if (!thread) { | ||
153 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s", SDL_GetError()); | ||
154 | quit(1); | ||
155 | } | ||
156 | (void)raise(SIGTERM); | ||
157 | |||
158 | SDL_Quit(); /* Never reached */ | ||
159 | return 0; /* Never reached */ | ||
160 | } | ||