1 /* Test program for a read-phase / write-phase explicit hand-over.
2 Copyright (C) 2017-2023 Free Software Foundation, Inc.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 The GNU C Library 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 GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, see <https://www.gnu.org/licenses/>. */
27 #include <support/xthread.h>
29 /* We realy want to set threads to 2 to reproduce this issue. The goal
30 is to have one primary writer and a single reader, and to hit the
31 bug that happens in the interleaving of those two phase transitions.
32 However, on most hardware, adding a second writer seems to help the
33 interleaving happen slightly more often, say 20% of the time. On a
34 16 core ppc64 machine this fails 100% of the time with an unpatched
35 glibc. On a 8 core x86_64 machine this fails ~93% of the time, but
36 it doesn't fail at all on a 4 core system, so having available
37 unloaded cores makes a big difference in reproducibility. On an 8
38 core qemu/kvm guest the reproducer reliability drops to ~10%. */
41 #define KIND PTHREAD_RWLOCK_PREFER_READER_NP
43 static pthread_rwlock_t lock
;
49 while (atomic_load_relaxed (&done
) == 0)
53 if ((uintptr_t) arg
== 0)
63 xpthread_rwlock_wrlock (&lock
);
64 xpthread_rwlock_unlock (&lock
);
69 xpthread_rwlock_rdlock (&lock
);
70 xpthread_rwlock_unlock (&lock
);
74 while ((atomic_load_relaxed (&done
) == 0) && (rcnt
+ wcnt
> 0));
85 pthread_t thr
[THREADS
];
87 pthread_rwlockattr_t attr
;
89 xpthread_rwlockattr_init (&attr
);
90 xpthread_rwlockattr_setkind_np (&attr
, KIND
);
92 xpthread_rwlock_init (&lock
, &attr
);
94 /* Make standard error the same as standard output. */
97 /* Make sure we see all message, even those on stdout. */
98 setvbuf (stdout
, NULL
, _IONBF
, 0);
100 for (n
= 0; n
< THREADS
; ++n
)
101 thr
[n
] = xpthread_create (NULL
, tf
, (void *) (uintptr_t) n
);
103 struct timespec delay
;
106 nanosleep (&delay
, NULL
);
107 atomic_store_relaxed (&done
, 1);
109 /* Wait for all the threads. */
110 for (n
= 0; n
< THREADS
; ++n
)
111 xpthread_join (thr
[n
]);
116 #include <support/test-driver.c>