diff options
author | 3gg <3gg@shellblade.net> | 2025-08-30 16:53:58 -0700 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2025-08-30 16:53:58 -0700 |
commit | 6aaedb813fa11ba0679c3051bc2eb28646b9506c (patch) | |
tree | 34acbfc9840e02cb4753e6306ea7ce978bf8b58e /src/contrib/SDL-3.2.20/test/testerror.c | |
parent | 8f228ade99dd3d4c8da9b78ade1815c9adf85c8f (diff) |
Update to SDL3
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testerror.c')
-rw-r--r-- | src/contrib/SDL-3.2.20/test/testerror.c | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testerror.c b/src/contrib/SDL-3.2.20/test/testerror.c new file mode 100644 index 0000000..687cb43 --- /dev/null +++ b/src/contrib/SDL-3.2.20/test/testerror.c | |||
@@ -0,0 +1,117 @@ | |||
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 and error handling */ | ||
14 | |||
15 | #include <stdlib.h> | ||
16 | |||
17 | #include <SDL3/SDL.h> | ||
18 | #include <SDL3/SDL_main.h> | ||
19 | #include <SDL3/SDL_test.h> | ||
20 | |||
21 | static int alive = 0; | ||
22 | |||
23 | /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ | ||
24 | static void | ||
25 | quit(int rc) | ||
26 | { | ||
27 | SDL_Quit(); | ||
28 | /* Let 'main()' return normally */ | ||
29 | if (rc != 0) { | ||
30 | exit(rc); | ||
31 | } | ||
32 | } | ||
33 | |||
34 | static int SDLCALL | ||
35 | ThreadFunc(void *data) | ||
36 | { | ||
37 | /* Set the child thread error string */ | ||
38 | SDL_SetError("Thread %s (%" SDL_PRIu64 ") had a problem: %s", | ||
39 | (char *)data, SDL_GetCurrentThreadID(), "nevermind"); | ||
40 | while (alive) { | ||
41 | SDL_Log("Thread '%s' is alive!", (char *)data); | ||
42 | SDL_Delay(1 * 1000); | ||
43 | } | ||
44 | SDL_Log("Child thread error string: %s", SDL_GetError()); | ||
45 | return 0; | ||
46 | } | ||
47 | |||
48 | int main(int argc, char *argv[]) | ||
49 | { | ||
50 | SDL_Thread *thread; | ||
51 | SDLTest_CommonState *state; | ||
52 | int i; | ||
53 | bool enable_threads = true; | ||
54 | |||
55 | /* Initialize test framework */ | ||
56 | state = SDLTest_CommonCreateState(argv, 0); | ||
57 | if (!state) { | ||
58 | return 1; | ||
59 | } | ||
60 | |||
61 | /* Parse commandline */ | ||
62 | for (i = 1; i < argc;) { | ||
63 | int consumed; | ||
64 | |||
65 | consumed = SDLTest_CommonArg(state, i); | ||
66 | if (consumed == 0) { | ||
67 | consumed = -1; | ||
68 | if (SDL_strcasecmp(argv[i], "--no-threads") == 0) { | ||
69 | enable_threads = false; | ||
70 | consumed = 1; | ||
71 | } | ||
72 | } | ||
73 | if (consumed < 0) { | ||
74 | static const char *options[] = { | ||
75 | "[--no-threads]", | ||
76 | NULL | ||
77 | }; | ||
78 | SDLTest_CommonLogUsage(state, argv[0], options); | ||
79 | return 1; | ||
80 | } | ||
81 | i += consumed; | ||
82 | } | ||
83 | |||
84 | /* Load the SDL library */ | ||
85 | if (!SDL_Init(0)) { | ||
86 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError()); | ||
87 | return 1; | ||
88 | } | ||
89 | |||
90 | /* Set the error value for the main thread */ | ||
91 | SDL_SetError("No worries"); | ||
92 | |||
93 | if (SDL_GetEnvironmentVariable(SDL_GetEnvironment(), "SDL_TESTS_QUICK") != NULL) { | ||
94 | SDL_Log("Not running slower tests"); | ||
95 | SDL_Quit(); | ||
96 | return 0; | ||
97 | } | ||
98 | |||
99 | if (enable_threads) { | ||
100 | alive = 1; | ||
101 | thread = SDL_CreateThread(ThreadFunc, NULL, "#1"); | ||
102 | if (!thread) { | ||
103 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create thread: %s", SDL_GetError()); | ||
104 | quit(1); | ||
105 | } | ||
106 | SDL_Delay(5 * 1000); | ||
107 | SDL_Log("Waiting for thread #1"); | ||
108 | alive = 0; | ||
109 | SDL_WaitThread(thread, NULL); | ||
110 | } | ||
111 | |||
112 | SDL_Log("Main thread error string: %s", SDL_GetError()); | ||
113 | |||
114 | SDL_Quit(); | ||
115 | SDLTest_CommonDestroyState(state); | ||
116 | return 0; | ||
117 | } | ||