usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-pthread-mutex.c
blobed1d64b7fcf7e58fdc1fac2713d818fde3580629
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 #if USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS
23 /* Whether to enable locking.
24 Uncomment this to get a test program without locking, to verify that
25 it crashes. */
26 #define ENABLE_LOCKING 1
28 /* Which tests to perform.
29 Uncomment some of these, to verify that all tests crash if no locking
30 is enabled. */
31 #define DO_TEST_LOCK 1
32 #define DO_TEST_RECURSIVE_LOCK 1
34 /* Whether to help the scheduler through explicit sched_yield().
35 Uncomment this to see if the operating system has a fair scheduler. */
36 #define EXPLICIT_YIELD 1
38 /* Whether to print debugging messages. */
39 #define ENABLE_DEBUGGING 0
41 /* Number of simultaneous threads. */
42 #define THREAD_COUNT 10
44 /* Number of operations performed in each thread.
45 This is quite high, because with a smaller count, say 5000, we often get
46 an "OK" result even without ENABLE_LOCKING (on Linux/x86). */
47 #define REPEAT_COUNT 50000
49 #include <pthread.h>
50 #include <stdint.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
55 #if EXPLICIT_YIELD
56 # include <sched.h>
57 #endif
59 #if HAVE_DECL_ALARM
60 # include <signal.h>
61 # include <unistd.h>
62 #endif
64 #include "macros.h"
65 #include "atomic-int-posix.h"
67 #if ENABLE_DEBUGGING
68 # define dbgprintf printf
69 #else
70 # define dbgprintf if (0) printf
71 #endif
73 #if EXPLICIT_YIELD
74 # define yield() sched_yield ()
75 #else
76 # define yield()
77 #endif
79 /* Returns a reference to the current thread as a pointer, for debugging. */
80 #if defined __MVS__
81 /* On IBM z/OS, pthread_t is a struct with an 8-byte '__' field.
82 The first three bytes of this field appear to uniquely identify a
83 pthread_t, though not necessarily representing a pointer. */
84 # define pthread_self_pointer() (*((void **) pthread_self ().__))
85 #else
86 # define pthread_self_pointer() ((void *) (uintptr_t) pthread_self ())
87 #endif
89 #define ACCOUNT_COUNT 4
91 static int account[ACCOUNT_COUNT];
93 static int
94 random_account (void)
96 return ((unsigned long) random () >> 3) % ACCOUNT_COUNT;
99 static void
100 check_accounts (void)
102 int i, sum;
104 sum = 0;
105 for (i = 0; i < ACCOUNT_COUNT; i++)
106 sum += account[i];
107 if (sum != ACCOUNT_COUNT * 1000)
108 abort ();
112 /* ------------------- Test normal (non-recursive) locks ------------------- */
114 /* Test normal locks by having several bank accounts and several threads
115 which shuffle around money between the accounts and another thread
116 checking that all the money is still there. */
118 static pthread_mutex_t my_lock;
120 static void *
121 lock_mutator_thread (void *arg)
123 int repeat;
125 for (repeat = REPEAT_COUNT; repeat > 0; repeat--)
127 int i1, i2, value;
129 dbgprintf ("Mutator %p before lock\n", pthread_self_pointer ());
130 ASSERT (pthread_mutex_lock (&my_lock) == 0);
131 dbgprintf ("Mutator %p after lock\n", pthread_self_pointer ());
133 i1 = random_account ();
134 i2 = random_account ();
135 value = ((unsigned long) random () >> 3) % 10;
136 account[i1] += value;
137 account[i2] -= value;
139 dbgprintf ("Mutator %p before unlock\n", pthread_self_pointer ());
140 ASSERT (pthread_mutex_unlock (&my_lock) == 0);
141 dbgprintf ("Mutator %p after unlock\n", pthread_self_pointer ());
143 dbgprintf ("Mutator %p before check lock\n", pthread_self_pointer ());
144 ASSERT (pthread_mutex_lock (&my_lock) == 0);
145 check_accounts ();
146 ASSERT (pthread_mutex_unlock (&my_lock) == 0);
147 dbgprintf ("Mutator %p after check unlock\n", pthread_self_pointer ());
149 yield ();
152 dbgprintf ("Mutator %p dying.\n", pthread_self_pointer ());
153 return NULL;
156 static struct atomic_int lock_checker_done;
158 static void *
159 lock_checker_thread (void *arg)
161 while (get_atomic_int_value (&lock_checker_done) == 0)
163 dbgprintf ("Checker %p before check lock\n", pthread_self_pointer ());
164 ASSERT (pthread_mutex_lock (&my_lock) == 0);
165 check_accounts ();
166 ASSERT (pthread_mutex_unlock (&my_lock) == 0);
167 dbgprintf ("Checker %p after check unlock\n", pthread_self_pointer ());
169 yield ();
172 dbgprintf ("Checker %p dying.\n", pthread_self_pointer ());
173 return NULL;
176 static void
177 test_pthread_mutex_normal (void)
179 int i;
180 pthread_t checkerthread;
181 pthread_t threads[THREAD_COUNT];
183 /* Initialization. */
184 for (i = 0; i < ACCOUNT_COUNT; i++)
185 account[i] = 1000;
186 init_atomic_int (&lock_checker_done);
187 set_atomic_int_value (&lock_checker_done, 0);
189 /* Spawn the threads. */
190 ASSERT (pthread_create (&checkerthread, NULL, lock_checker_thread, NULL)
191 == 0);
192 for (i = 0; i < THREAD_COUNT; i++)
193 ASSERT (pthread_create (&threads[i], NULL, lock_mutator_thread, NULL) == 0);
195 /* Wait for the threads to terminate. */
196 for (i = 0; i < THREAD_COUNT; i++)
197 ASSERT (pthread_join (threads[i], NULL) == 0);
198 set_atomic_int_value (&lock_checker_done, 1);
199 ASSERT (pthread_join (checkerthread, NULL) == 0);
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 pthread_mutex_t my_reclock;
212 static void
213 recshuffle (void)
215 int i1, i2, value;
217 dbgprintf ("Mutator %p before lock\n", pthread_self_pointer ());
218 ASSERT (pthread_mutex_lock (&my_reclock) == 0);
219 dbgprintf ("Mutator %p after lock\n", pthread_self_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", pthread_self_pointer ());
232 ASSERT (pthread_mutex_unlock (&my_reclock) == 0);
233 dbgprintf ("Mutator %p after unlock\n", pthread_self_pointer ());
236 static void *
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", pthread_self_pointer ());
246 ASSERT (pthread_mutex_lock (&my_reclock) == 0);
247 check_accounts ();
248 ASSERT (pthread_mutex_unlock (&my_reclock) == 0);
249 dbgprintf ("Mutator %p after check unlock\n", pthread_self_pointer ());
251 yield ();
254 dbgprintf ("Mutator %p dying.\n", pthread_self_pointer ());
255 return NULL;
258 static struct atomic_int reclock_checker_done;
260 static void *
261 reclock_checker_thread (void *arg)
263 while (get_atomic_int_value (&reclock_checker_done) == 0)
265 dbgprintf ("Checker %p before check lock\n", pthread_self_pointer ());
266 ASSERT (pthread_mutex_lock (&my_reclock) == 0);
267 check_accounts ();
268 ASSERT (pthread_mutex_unlock (&my_reclock) == 0);
269 dbgprintf ("Checker %p after check unlock\n", pthread_self_pointer ());
271 yield ();
274 dbgprintf ("Checker %p dying.\n", pthread_self_pointer ());
275 return NULL;
278 static void
279 test_pthread_mutex_recursive (void)
281 int i;
282 pthread_t checkerthread;
283 pthread_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 (pthread_create (&checkerthread, NULL, reclock_checker_thread, NULL)
293 == 0);
294 for (i = 0; i < THREAD_COUNT; i++)
295 ASSERT (pthread_create (&threads[i], NULL, reclock_mutator_thread, NULL)
296 == 0);
298 /* Wait for the threads to terminate. */
299 for (i = 0; i < THREAD_COUNT; i++)
300 ASSERT (pthread_join (threads[i], NULL) == 0);
301 set_atomic_int_value (&reclock_checker_done, 1);
302 ASSERT (pthread_join (checkerthread, NULL) == 0);
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
321 pthread_mutexattr_t attr;
323 ASSERT (pthread_mutexattr_init (&attr) == 0);
324 ASSERT (pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_NORMAL) == 0);
325 ASSERT (pthread_mutex_init (&my_lock, &attr) == 0);
326 ASSERT (pthread_mutexattr_destroy (&attr) == 0);
330 pthread_mutexattr_t attr;
332 ASSERT (pthread_mutexattr_init (&attr) == 0);
333 ASSERT (pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE) == 0);
334 ASSERT (pthread_mutex_init (&my_reclock, &attr) == 0);
335 ASSERT (pthread_mutexattr_destroy (&attr) == 0);
338 #if DO_TEST_LOCK
339 printf ("Starting test_pthread_mutex_normal ..."); fflush (stdout);
340 test_pthread_mutex_normal ();
341 printf (" OK\n"); fflush (stdout);
342 #endif
343 #if DO_TEST_RECURSIVE_LOCK
344 printf ("Starting test_pthread_mutex_recursive ..."); fflush (stdout);
345 test_pthread_mutex_recursive ();
346 printf (" OK\n"); fflush (stdout);
347 #endif
349 return test_exit_status;
352 #else
354 /* No multithreading available. */
356 #include <stdio.h>
359 main ()
361 fputs ("Skipping test: multithreading not enabled\n", stderr);
362 return 77;
365 #endif