maint.mk: Update system header list for #include syntax checks.
[gnulib.git] / tests / test-mtx.c
blob4c9f22a3c4917a0ae6660415b5635853c323b68c
1 /* Test of locking in multithreaded situations.
2 Copyright (C) 2005, 2008-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2005. */
19 #include <config.h>
21 /* Whether to enable locking.
22 Uncomment this to get a test program without locking, to verify that
23 it crashes. */
24 #define ENABLE_LOCKING 1
26 /* Which tests to perform.
27 Uncomment some of these, to verify that all tests crash if no locking
28 is enabled. */
29 #define DO_TEST_LOCK 1
30 #define DO_TEST_RECURSIVE_LOCK 1
32 /* Whether to help the scheduler through explicit thrd_yield().
33 Uncomment this to see if the operating system has a fair scheduler. */
34 #define EXPLICIT_YIELD 1
36 /* Whether to print debugging messages. */
37 #define ENABLE_DEBUGGING 0
39 /* Number of simultaneous threads. */
40 #define THREAD_COUNT 10
42 /* Number of operations performed in each thread.
43 This is quite high, because with a smaller count, say 5000, we often get
44 an "OK" result even without ENABLE_LOCKING (on Linux/x86). */
45 #define REPEAT_COUNT 50000
47 #include <threads.h>
48 #include <stdint.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
53 #include "glthread/lock.h"
55 #if HAVE_DECL_ALARM
56 # include <signal.h>
57 # include <unistd.h>
58 #endif
60 #include "macros.h"
61 #include "atomic-int-isoc.h"
63 #if ENABLE_DEBUGGING
64 # define dbgprintf printf
65 #else
66 # define dbgprintf if (0) printf
67 #endif
69 #if EXPLICIT_YIELD
70 # define yield() thrd_yield ()
71 #else
72 # define yield()
73 #endif
75 /* Returns a reference to the current thread as a pointer, for debugging. */
76 #if defined __MVS__
77 /* On IBM z/OS, pthread_t is a struct with an 8-byte '__' field.
78 The first three bytes of this field appear to uniquely identify a
79 pthread_t, though not necessarily representing a pointer. */
80 # define thrd_current_pointer() (*((void **) thrd_current ().__))
81 #elif defined __sun
82 /* On Solaris, thrd_t is merely an 'unsigned int'. */
83 # define thrd_current_pointer() ((void *) (uintptr_t) thrd_current ())
84 #else
85 # define thrd_current_pointer() ((void *) thrd_current ())
86 #endif
88 #define ACCOUNT_COUNT 4
90 static int account[ACCOUNT_COUNT];
92 static int
93 random_account (void)
95 return ((unsigned long) random () >> 3) % ACCOUNT_COUNT;
98 static void
99 check_accounts (void)
101 int i, sum;
103 sum = 0;
104 for (i = 0; i < ACCOUNT_COUNT; i++)
105 sum += account[i];
106 if (sum != ACCOUNT_COUNT * 1000)
107 abort ();
111 /* ------------------- Test normal (non-recursive) locks ------------------- */
113 /* Test normal locks by having several bank accounts and several threads
114 which shuffle around money between the accounts and another thread
115 checking that all the money is still there. */
117 static mtx_t my_lock;
119 static int
120 lock_mutator_thread (void *arg)
122 int repeat;
124 for (repeat = REPEAT_COUNT; repeat > 0; repeat--)
126 int i1, i2, value;
128 dbgprintf ("Mutator %p before lock\n", thrd_current_pointer ());
129 ASSERT (mtx_lock (&my_lock) == thrd_success);
130 dbgprintf ("Mutator %p after lock\n", thrd_current_pointer ());
132 i1 = random_account ();
133 i2 = random_account ();
134 value = ((unsigned long) random () >> 3) % 10;
135 account[i1] += value;
136 account[i2] -= value;
138 dbgprintf ("Mutator %p before unlock\n", thrd_current_pointer ());
139 ASSERT (mtx_unlock (&my_lock) == thrd_success);
140 dbgprintf ("Mutator %p after unlock\n", thrd_current_pointer ());
142 dbgprintf ("Mutator %p before check lock\n", thrd_current_pointer ());
143 ASSERT (mtx_lock (&my_lock) == thrd_success);
144 check_accounts ();
145 ASSERT (mtx_unlock (&my_lock) == thrd_success);
146 dbgprintf ("Mutator %p after check unlock\n", thrd_current_pointer ());
148 yield ();
151 dbgprintf ("Mutator %p dying.\n", thrd_current_pointer ());
152 return 0;
155 static struct atomic_int lock_checker_done;
157 static int
158 lock_checker_thread (void *arg)
160 while (get_atomic_int_value (&lock_checker_done) == 0)
162 dbgprintf ("Checker %p before check lock\n", thrd_current_pointer ());
163 ASSERT (mtx_lock (&my_lock) == thrd_success);
164 check_accounts ();
165 ASSERT (mtx_unlock (&my_lock) == thrd_success);
166 dbgprintf ("Checker %p after check unlock\n", thrd_current_pointer ());
168 yield ();
171 dbgprintf ("Checker %p dying.\n", thrd_current_pointer ());
172 return 0;
175 static void
176 test_mtx_plain (void)
178 int i;
179 thrd_t checkerthread;
180 thrd_t threads[THREAD_COUNT];
182 /* Initialization. */
183 for (i = 0; i < ACCOUNT_COUNT; i++)
184 account[i] = 1000;
185 init_atomic_int (&lock_checker_done);
186 set_atomic_int_value (&lock_checker_done, 0);
188 /* Spawn the threads. */
189 ASSERT (thrd_create (&checkerthread, lock_checker_thread, NULL)
190 == thrd_success);
191 for (i = 0; i < THREAD_COUNT; i++)
192 ASSERT (thrd_create (&threads[i], lock_mutator_thread, NULL)
193 == thrd_success);
195 /* Wait for the threads to terminate. */
196 for (i = 0; i < THREAD_COUNT; i++)
197 ASSERT (thrd_join (threads[i], NULL) == thrd_success);
198 set_atomic_int_value (&lock_checker_done, 1);
199 ASSERT (thrd_join (checkerthread, NULL) == thrd_success);
200 check_accounts ();
204 /* -------------------------- Test recursive locks -------------------------- */
206 /* Test recursive locks by having several bank accounts and several threads
207 which shuffle around money between the accounts (recursively) and another
208 thread checking that all the money is still there. */
210 static mtx_t my_reclock;
212 static void
213 recshuffle (void)
215 int i1, i2, value;
217 dbgprintf ("Mutator %p before lock\n", thrd_current_pointer ());
218 ASSERT (mtx_lock (&my_reclock) == thrd_success);
219 dbgprintf ("Mutator %p after lock\n", thrd_current_pointer ());
221 i1 = random_account ();
222 i2 = random_account ();
223 value = ((unsigned long) random () >> 3) % 10;
224 account[i1] += value;
225 account[i2] -= value;
227 /* Recursive with probability 0.5. */
228 if (((unsigned long) random () >> 3) % 2)
229 recshuffle ();
231 dbgprintf ("Mutator %p before unlock\n", thrd_current_pointer ());
232 ASSERT (mtx_unlock (&my_reclock) == thrd_success);
233 dbgprintf ("Mutator %p after unlock\n", thrd_current_pointer ());
236 static int
237 reclock_mutator_thread (void *arg)
239 int repeat;
241 for (repeat = REPEAT_COUNT; repeat > 0; repeat--)
243 recshuffle ();
245 dbgprintf ("Mutator %p before check lock\n", thrd_current_pointer ());
246 ASSERT (mtx_lock (&my_reclock) == thrd_success);
247 check_accounts ();
248 ASSERT (mtx_unlock (&my_reclock) == thrd_success);
249 dbgprintf ("Mutator %p after check unlock\n", thrd_current_pointer ());
251 yield ();
254 dbgprintf ("Mutator %p dying.\n", thrd_current_pointer ());
255 return 0;
258 static struct atomic_int reclock_checker_done;
260 static int
261 reclock_checker_thread (void *arg)
263 while (get_atomic_int_value (&reclock_checker_done) == 0)
265 dbgprintf ("Checker %p before check lock\n", thrd_current_pointer ());
266 ASSERT (mtx_lock (&my_reclock) == thrd_success);
267 check_accounts ();
268 ASSERT (mtx_unlock (&my_reclock) == thrd_success);
269 dbgprintf ("Checker %p after check unlock\n", thrd_current_pointer ());
271 yield ();
274 dbgprintf ("Checker %p dying.\n", thrd_current_pointer ());
275 return 0;
278 static void
279 test_mtx_recursive (void)
281 int i;
282 thrd_t checkerthread;
283 thrd_t threads[THREAD_COUNT];
285 /* Initialization. */
286 for (i = 0; i < ACCOUNT_COUNT; i++)
287 account[i] = 1000;
288 init_atomic_int (&reclock_checker_done);
289 set_atomic_int_value (&reclock_checker_done, 0);
291 /* Spawn the threads. */
292 ASSERT (thrd_create (&checkerthread, reclock_checker_thread, NULL)
293 == thrd_success);
294 for (i = 0; i < THREAD_COUNT; i++)
295 ASSERT (thrd_create (&threads[i], reclock_mutator_thread, NULL)
296 == thrd_success);
298 /* Wait for the threads to terminate. */
299 for (i = 0; i < THREAD_COUNT; i++)
300 ASSERT (thrd_join (threads[i], NULL) == thrd_success);
301 set_atomic_int_value (&reclock_checker_done, 1);
302 ASSERT (thrd_join (checkerthread, NULL) == thrd_success);
303 check_accounts ();
307 /* -------------------------------------------------------------------------- */
310 main ()
312 #if HAVE_DECL_ALARM
313 /* Declare failure if test takes too long, by using default abort
314 caused by SIGALRM. */
315 int alarm_value = 600;
316 signal (SIGALRM, SIG_DFL);
317 alarm (alarm_value);
318 #endif
320 ASSERT (mtx_init (&my_lock, mtx_plain) == thrd_success);
321 ASSERT (mtx_init (&my_reclock, mtx_plain | mtx_recursive) == thrd_success);
323 #if DO_TEST_LOCK
324 printf ("Starting test_mtx_plain ..."); fflush (stdout);
325 test_mtx_plain ();
326 printf (" OK\n"); fflush (stdout);
327 #endif
328 #if DO_TEST_RECURSIVE_LOCK
329 printf ("Starting test_mtx_recursive ..."); fflush (stdout);
330 test_mtx_recursive ();
331 printf (" OK\n"); fflush (stdout);
332 #endif
334 return test_exit_status;