diff options
author | Ivan Maidanski <ivmai@mail.ru> | 2022-01-15 00:23:08 +0300 |
---|---|---|
committer | Ivan Maidanski <ivmai@mail.ru> | 2022-02-22 14:42:15 +0300 |
commit | 0e20a0d62e2d02bf0509fe3f58cb38159c5b274e (patch) | |
tree | 943fa37e0598791c0a2a33c05ce6da41d32806a6 | |
parent | cbf4e4d8ebfc337d4931697b349495ddd67e2f9a (diff) | |
download | bdwgc-0e20a0d62e2d02bf0509fe3f58cb38159c5b274e.tar.gz |
Workaround crash in FreeBSD rand() by avoiding its concurrent usage
(a cherry-pick of commit 121141be2 from 'release-7_6')
Issue #414 (bdwgc).
The standard specifies rand() as not a thread-safe API function. Thus,
the concurrent usage of libc rand() should be avoided.
Redefine the standard rand() in disclaim_test with a trivial
(yet sufficient for the test purpose) implementation to avoid crashes
inside rand() on some targets (e.g. FreeBSD 13.0).
* tests/disclaim_test.c [GC_PTHREADS] (GC_rand): New static function.
* tests/disclaim_test.c [GC_PTHREADS] (rand): Redefine to GC_rand();
add comment.
-rw-r--r-- | tests/disclaim_test.c | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/tests/disclaim_test.c b/tests/disclaim_test.c index a59c2361..7452041b 100644 --- a/tests/disclaim_test.c +++ b/tests/disclaim_test.c @@ -28,6 +28,23 @@ #undef GC_NO_THREAD_REDIRECTS #include "gc_disclaim.h" +#if defined(GC_PTHREADS) + static int GC_rand(void) + { + static unsigned seed; /* concurrent update does not hurt the test */ + + seed = (seed * 1103515245U + 12345) & (~0U >> 1); + return (int)seed; + } + + /* Redefine the standard rand() with a trivial (yet sufficient for */ + /* the test purpose) implementation to avoid crashes inside rand() */ + /* on some targets (e.g. FreeBSD 13.0) when used concurrently. */ + /* The standard specifies rand() as not a thread-safe API function. */ +# undef rand +# define rand() GC_rand() +#endif /* GC_PTHREADS */ + #define my_assert(e) \ if (!(e)) { \ fprintf(stderr, "Assertion failure, line %d: " #e "\n", __LINE__); \ |