2 * Sanity check of libcdecl multithread safety.
4 * Copyright © 2024 Nick Bowler
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.
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.
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/>.
22 #include "cdecl-internal.h"
27 * Function called from output.c but not needed for error messaging.
29 const char *cdecl__token_name(unsigned token)
31 tap_bail_out("stub cdecl__token_name called");
35 * Prior returned value from cdecl_get_error in the main thread.
37 static const struct cdecl_error *thread1_err;
40 * Check that the error code and message matches expectations (noting that
41 * this application does not call setlocale to enable translations).
43 static void check_simple_err(const struct cdecl_error *err, unsigned t,
44 unsigned exp_code, unsigned msg_id)
46 static const char errmsgs[] = STRTAB_INITIALIZER;
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);
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);
63 static void thread2_func(void)
65 const struct cdecl_error *err;
67 cdecl__errmsg(CDECL__ENOTYPE);
68 err = cdecl_get_error();
71 * Ensure that the error returned in this new thread is distinct from
72 * the error returned in the main thread.
74 tap_diag("thread[2] err: %p", (void *)err);
75 tap_result(thread1_err != err, "thread[2] new state");
77 check_simple_err(err, 2, CDECL_ENOPARSE, CDECL__ENOTYPE);
79 tap_diag("thread[2] exit");
82 #if USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS
83 #define THREAD_API "posix"
86 static void *thread2(void *p)
92 static void run_thread2(void)
97 if (!(err = pthread_create(&t, 0, thread2, 0)))
98 if (!(err = pthread_join(t, 0)))
101 tap_bail_out("run_thread2 failed: %s", strerror(err));
104 #elif USE_ISOC_THREADS
105 #define THREAD_API "isoc"
108 static int thread2(void *p)
114 static void run_thread2(void)
117 if (thrd_create(&t, thread2, 0) == thrd_success)
118 if (thrd_join(t, 0) == thrd_success)
121 tap_bail_out("run_thread2 failed");
124 #elif USE_WINDOWS_THREADS
125 #define THREAD_API "windows"
126 #define WIN32_LEAN_AND_MEAN
129 static DWORD WINAPI thread2(LPVOID p)
135 static void run_thread2(void)
140 if ((h = CreateThread(NULL, 0, thread2, NULL, 0, &rc))) {
142 if (GetExitCodeThread(h, &rc) && rc != STILL_ACTIVE) {
146 } while (WaitForSingleObject(h, INFINITE) != WAIT_FAILED);
149 tap_bail_out("run_thread2 failed (%lu)", GetLastError());
155 tap_skip_all("multithreading support disabled");
162 size_t test_live_allocations(void);
163 const struct cdecl_error *err;
165 tap_diag("using thread API: " THREAD_API);
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);
177 * Back in the main thread, the error previously returned by
178 * cdecl_get_error() should still be valid.
180 check_simple_err(thread1_err, 1, CDECL_ENOMEM, CDECL__ENOMEM);
183 * Moreover, cdecl_get_error should return the same pointer it did
184 * last time (undocumented implementation detail).
186 if (!tap_result((err = cdecl_get_error()) == thread1_err,
187 "thread[1] unchanged state"))
189 tap_diag("Failed, unexpected result");
190 tap_diag(" Received: %p", (void *)err);
191 tap_diag(" Expected: %p", (void *)thread1_err);
195 * Main thread allocation should be the only one left.
197 tap_result(test_live_allocations() == 1, "thread cleanup");