x86: Always include <dl-cet.h>/cet-tunables.h> for --enable-cet
[glibc.git] / nptl / tst-rwlock20.c
blobaeb41d29c40cfc249414b3b5703f2b7d0da3cf52
1 /* Test program for a read-phase / write-phase explicit hand-over.
2 Copyright (C) 2017-2018 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 <http://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <error.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <stdint.h>
25 #include <time.h>
26 #include <atomic.h>
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%. */
39 #define THREADS 3
41 #define KIND PTHREAD_RWLOCK_PREFER_READER_NP
43 static pthread_rwlock_t lock;
44 static int done = 0;
46 static void*
47 tf (void* arg)
49 while (atomic_load_relaxed (&done) == 0)
51 int rcnt = 0;
52 int wcnt = 100;
53 if ((uintptr_t) arg == 0)
55 rcnt = 1;
56 wcnt = 1;
61 if (wcnt)
63 xpthread_rwlock_wrlock (&lock);
64 xpthread_rwlock_unlock (&lock);
65 wcnt--;
67 if (rcnt)
69 xpthread_rwlock_rdlock (&lock);
70 xpthread_rwlock_unlock (&lock);
71 rcnt--;
74 while ((atomic_load_relaxed (&done) == 0) && (rcnt + wcnt > 0));
77 return NULL;
82 static int
83 do_test (void)
85 pthread_t thr[THREADS];
86 int n;
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. */
95 dup2 (1, 2);
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;
104 delay.tv_sec = 10;
105 delay.tv_nsec = 0;
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]);
113 return 0;
116 #include <support/test-driver.c>