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. */
21 /* Whether to enable locking.
22 Uncomment this to get a test program without locking, to verify that
24 #define ENABLE_LOCKING 1
26 /* Which tests to perform.
27 Uncomment some of these, to verify that all tests crash if no locking
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
53 #include "glthread/lock.h"
61 #include "atomic-int-isoc.h"
64 # define dbgprintf printf
66 # define dbgprintf if (0) printf
70 # define yield() thrd_yield ()
75 /* Returns a reference to the current thread as a pointer, for debugging. */
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 ().__))
82 /* On Solaris, thrd_t is merely an 'unsigned int'. */
83 # define thrd_current_pointer() ((void *) (uintptr_t) thrd_current ())
85 # define thrd_current_pointer() ((void *) thrd_current ())
88 #define ACCOUNT_COUNT 4
90 static int account
[ACCOUNT_COUNT
];
95 return ((unsigned long) random () >> 3) % ACCOUNT_COUNT
;
104 for (i
= 0; i
< ACCOUNT_COUNT
; i
++)
106 if (sum
!= ACCOUNT_COUNT
* 1000)
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
;
120 lock_mutator_thread (void *arg
)
124 for (repeat
= REPEAT_COUNT
; repeat
> 0; repeat
--)
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
);
145 ASSERT (mtx_unlock (&my_lock
) == thrd_success
);
146 dbgprintf ("Mutator %p after check unlock\n", thrd_current_pointer ());
151 dbgprintf ("Mutator %p dying.\n", thrd_current_pointer ());
155 static struct atomic_int lock_checker_done
;
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
);
165 ASSERT (mtx_unlock (&my_lock
) == thrd_success
);
166 dbgprintf ("Checker %p after check unlock\n", thrd_current_pointer ());
171 dbgprintf ("Checker %p dying.\n", thrd_current_pointer ());
176 test_mtx_plain (void)
179 thrd_t checkerthread
;
180 thrd_t threads
[THREAD_COUNT
];
182 /* Initialization. */
183 for (i
= 0; i
< ACCOUNT_COUNT
; i
++)
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
)
191 for (i
= 0; i
< THREAD_COUNT
; i
++)
192 ASSERT (thrd_create (&threads
[i
], lock_mutator_thread
, NULL
)
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
);
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
;
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)
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 ());
237 reclock_mutator_thread (void *arg
)
241 for (repeat
= REPEAT_COUNT
; repeat
> 0; repeat
--)
245 dbgprintf ("Mutator %p before check lock\n", thrd_current_pointer ());
246 ASSERT (mtx_lock (&my_reclock
) == thrd_success
);
248 ASSERT (mtx_unlock (&my_reclock
) == thrd_success
);
249 dbgprintf ("Mutator %p after check unlock\n", thrd_current_pointer ());
254 dbgprintf ("Mutator %p dying.\n", thrd_current_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", thrd_current_pointer ());
266 ASSERT (mtx_lock (&my_reclock
) == thrd_success
);
268 ASSERT (mtx_unlock (&my_reclock
) == thrd_success
);
269 dbgprintf ("Checker %p after check unlock\n", thrd_current_pointer ());
274 dbgprintf ("Checker %p dying.\n", thrd_current_pointer ());
279 test_mtx_recursive (void)
282 thrd_t checkerthread
;
283 thrd_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 (thrd_create (&checkerthread
, reclock_checker_thread
, NULL
)
294 for (i
= 0; i
< THREAD_COUNT
; i
++)
295 ASSERT (thrd_create (&threads
[i
], reclock_mutator_thread
, NULL
)
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
);
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
);
320 ASSERT (mtx_init (&my_lock
, mtx_plain
) == thrd_success
);
321 ASSERT (mtx_init (&my_reclock
, mtx_plain
| mtx_recursive
) == thrd_success
);
324 printf ("Starting test_mtx_plain ..."); fflush (stdout
);
326 printf (" OK\n"); fflush (stdout
);
328 #if DO_TEST_RECURSIVE_LOCK
329 printf ("Starting test_mtx_recursive ..."); fflush (stdout
);
330 test_mtx_recursive ();
331 printf (" OK\n"); fflush (stdout
);
334 return test_exit_status
;