1 /* Test of locking in multithreaded situations.
2 Copyright (C) 2005, 2008-2020 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. */
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
26 #define ENABLE_LOCKING 1
28 /* Which tests to perform.
29 Uncomment some of these, to verify that all tests crash if no locking
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
65 #include "atomic-int-posix.h"
68 # define dbgprintf printf
70 # define dbgprintf if (0) printf
74 # define yield() sched_yield ()
79 /* Returns a reference to the current thread as a pointer, for debugging. */
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 ().__))
86 # define pthread_self_pointer() ((void *) (uintptr_t) pthread_self ())
89 #define ACCOUNT_COUNT 4
91 static int account
[ACCOUNT_COUNT
];
96 return ((unsigned int) rand () >> 3) % ACCOUNT_COUNT
;
100 check_accounts (void)
105 for (i
= 0; i
< ACCOUNT_COUNT
; i
++)
107 if (sum
!= ACCOUNT_COUNT
* 1000)
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
;
121 lock_mutator_thread (void *arg
)
125 for (repeat
= REPEAT_COUNT
; repeat
> 0; repeat
--)
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 int) rand () >> 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);
146 ASSERT (pthread_mutex_unlock (&my_lock
) == 0);
147 dbgprintf ("Mutator %p after check unlock\n", pthread_self_pointer ());
152 dbgprintf ("Mutator %p dying.\n", pthread_self_pointer ());
156 static struct atomic_int lock_checker_done
;
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);
166 ASSERT (pthread_mutex_unlock (&my_lock
) == 0);
167 dbgprintf ("Checker %p after check unlock\n", pthread_self_pointer ());
172 dbgprintf ("Checker %p dying.\n", pthread_self_pointer ());
177 test_pthread_mutex_normal (void)
180 pthread_t checkerthread
;
181 pthread_t threads
[THREAD_COUNT
];
183 /* Initialization. */
184 for (i
= 0; i
< ACCOUNT_COUNT
; i
++)
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
)
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);
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
;
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 int) rand () >> 3) % 10;
224 account
[i1
] += value
;
225 account
[i2
] -= value
;
227 /* Recursive with probability 0.5. */
228 if (((unsigned int) rand () >> 3) % 2)
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 ());
237 reclock_mutator_thread (void *arg
)
241 for (repeat
= REPEAT_COUNT
; repeat
> 0; repeat
--)
245 dbgprintf ("Mutator %p before check lock\n", pthread_self_pointer ());
246 ASSERT (pthread_mutex_lock (&my_reclock
) == 0);
248 ASSERT (pthread_mutex_unlock (&my_reclock
) == 0);
249 dbgprintf ("Mutator %p after check unlock\n", pthread_self_pointer ());
254 dbgprintf ("Mutator %p dying.\n", pthread_self_pointer ());
258 static struct atomic_int reclock_checker_done
;
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);
268 ASSERT (pthread_mutex_unlock (&my_reclock
) == 0);
269 dbgprintf ("Checker %p after check unlock\n", pthread_self_pointer ());
274 dbgprintf ("Checker %p dying.\n", pthread_self_pointer ());
279 test_pthread_mutex_recursive (void)
282 pthread_t checkerthread
;
283 pthread_t threads
[THREAD_COUNT
];
285 /* Initialization. */
286 for (i
= 0; i
< ACCOUNT_COUNT
; i
++)
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
)
294 for (i
= 0; i
< THREAD_COUNT
; i
++)
295 ASSERT (pthread_create (&threads
[i
], NULL
, reclock_mutator_thread
, NULL
)
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);
307 /* -------------------------------------------------------------------------- */
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
);
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);
339 printf ("Starting test_pthread_mutex_normal ..."); fflush (stdout
);
340 test_pthread_mutex_normal ();
341 printf (" OK\n"); fflush (stdout
);
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
);
354 /* No multithreading available. */
361 fputs ("Skipping test: multithreading not enabled\n", stderr
);