]> git.draconx.ca Git - cdecl99.git/blob - t/errthread.c
libcdecl: Actually test threading support.
[cdecl99.git] / t / errthread.c
1 /*
2  * Sanity check of libcdecl multithread safety.
3  *
4  * Copyright © 2024 Nick Bowler
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <assert.h>
22 #include "cdecl-internal.h"
23 #include "errmsg.h"
24 #include "tap.h"
25
26 /*
27  * Function called from output.c but not needed for error messaging.
28  */
29 const char *cdecl__token_name(unsigned token)
30 {
31         tap_bail_out("stub cdecl__token_name called");
32 }
33
34 /*
35  * Prior returned value from cdecl_get_error in the main thread.
36  */
37 static const struct cdecl_error *thread1_err;
38
39 /*
40  * Check that the error code and message matches expectations (noting that
41  * this application does not call setlocale to enable translations).
42  */
43 static void check_simple_err(const struct cdecl_error *err, unsigned t,
44                              unsigned exp_code, unsigned msg_id)
45 {
46         static const char errmsgs[] = STRTAB_INITIALIZER;
47         const char *exp_msg;
48
49         if (!tap_result(err->code == exp_code, "thread[%u] err->code", t)) {
50                 tap_diag("Failed, unexpected result");
51                 tap_diag("   Received: %u", err->code);
52                 tap_diag("   Expected: %u", exp_code);
53         }
54
55         exp_msg = &errmsgs[msg_id];
56         if (!tap_result(!strcmp(err->str, exp_msg), "thread[%u] err->str", t)) {
57                 tap_diag("Failed, unexpected result");
58                 tap_diag("   Received: %.*s", (int)strlen(exp_msg), err->str);
59                 tap_diag("   Expected: %s", exp_msg);
60         }
61 }
62
63 static void thread2_func(void)
64 {
65         const struct cdecl_error *err;
66
67         cdecl__errmsg(CDECL__ENOTYPE);
68         err = cdecl_get_error();
69
70         /*
71          * Ensure that the error returned in this new thread is distinct from
72          * the error returned in the main thread.
73          */
74         tap_diag("thread[2] err: %p", (void *)err);
75         tap_result(thread1_err != err, "thread[2] new state");
76
77         check_simple_err(err, 2, CDECL_ENOPARSE, CDECL__ENOTYPE);
78
79         tap_diag("thread[2] exit");
80 }
81
82 #if USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS
83 #define THREAD_API "posix"
84 #include <pthread.h>
85
86 static void *thread2(void *p)
87 {
88         thread2_func();
89         return 0;
90 }
91
92 static void run_thread2(void)
93 {
94         pthread_t t;
95         int err;
96
97         if (!(err = pthread_create(&t, 0, thread2, 0)))
98                 if (!(err = pthread_join(t, 0)))
99                         return;
100
101         tap_bail_out("run_thread2 failed: %s", strerror(err));
102 }
103
104 #elif USE_ISOC_THREADS
105 #define THREAD_API "isoc"
106 #include <threads.h>
107
108 static int thread2(void *p)
109 {
110         thread2_func();
111         return 0;
112 }
113
114 static void run_thread2(void)
115 {
116         thrd_t t;
117         if (thrd_create(&t, thread2, 0) == thrd_success)
118                 if (thrd_join(t, 0) == thrd_success)
119                         return;
120
121         tap_bail_out("run_thread2 failed");
122 }
123
124 #elif USE_WINDOWS_THREADS
125 #define THREAD_API "windows"
126 #define WIN32_LEAN_AND_MEAN
127 #include <windows.h>
128
129 static DWORD WINAPI thread2(LPVOID p)
130 {
131         thread2_func();
132         return 0;
133 }
134
135 static void run_thread2(void)
136 {
137         HANDLE h;
138         DWORD rc;
139
140         if ((h = CreateThread(NULL, 0, thread2, NULL, 0, &rc))) {
141                 do {
142                         if (GetExitCodeThread(h, &rc) && rc != STILL_ACTIVE) {
143                                 CloseHandle(h);
144                                 return;
145                         }
146                 } while (WaitForSingleObject(h, INFINITE) != WAIT_FAILED);
147         }
148
149         tap_bail_out("run_thread2 failed (%lu)", GetLastError());
150 }
151 #else
152 #undef THREAD_API
153 int main(void)
154 {
155         tap_skip_all("multithreading support disabled");
156 }
157 #endif
158
159 #ifdef THREAD_API
160 int main(void)
161 {
162         size_t test_live_allocations(void);
163         const struct cdecl_error *err;
164
165         tap_diag("using thread API: " THREAD_API);
166         tap_plan(9);
167
168         /* Simulate an error in the main thread. */
169         cdecl__errmsg(CDECL__ENOMEM);
170         thread1_err = cdecl_get_error();
171         tap_diag("thread[1] err: %p", (void *)thread1_err);
172         check_simple_err(thread1_err, 1, CDECL_ENOMEM, CDECL__ENOMEM);
173
174         run_thread2();
175
176         /*
177          * Back in the main thread, the error previously returned by
178          * cdecl_get_error() should still be valid.
179          */
180         check_simple_err(thread1_err, 1, CDECL_ENOMEM, CDECL__ENOMEM);
181
182         /*
183          * Moreover, cdecl_get_error should return the same pointer it did
184          * last time (undocumented implementation detail).
185          */
186         if (!tap_result((err = cdecl_get_error()) == thread1_err,
187                         "thread[1] unchanged state"))
188         {
189                 tap_diag("Failed, unexpected result");
190                 tap_diag("   Received: %p", (void *)err);
191                 tap_diag("   Expected: %p", (void *)thread1_err);
192         }
193
194         /*
195          * Main thread allocation should be the only one left.
196          */
197         tap_result(test_live_allocations() == 1, "thread cleanup");
198         tap_done();
199 }
200 #endif