nptl: Add missing cancellation flags on futex_internal and pselect32
[glibc.git] / nptl / tst-rwlock9.c
blob408bbcdd5d47f93337c87cdb0be6e3cb20115efb
1 /* Test program for timedout read/write lock functions.
2 Copyright (C) 2000-2020 Free Software Foundation, Inc.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <https://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <error.h>
21 #include <pthread.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <time.h>
25 #include <unistd.h>
26 #include <sys/time.h>
27 #include <support/check.h>
28 #include <support/timespec.h>
29 #include <support/xthread.h>
32 #define NWRITERS 15
33 #define WRITETRIES 10
34 #define NREADERS 15
35 #define READTRIES 15
37 static const struct timespec timeout = { 0,1000000 };
38 static const struct timespec delay = { 0, 1000000 };
40 #ifndef KIND
41 # define KIND PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
42 #endif
44 /* A bogus clock value that tells the tests to use pthread_rwlock_timedrdlock
45 and pthread_rwlock_timedwrlock rather than pthread_rwlock_clockrdlock and
46 pthread_rwlock_clockwrlock. */
47 #define CLOCK_USE_TIMEDLOCK (-1)
49 static pthread_rwlock_t lock;
51 struct thread_args
53 int nr;
54 clockid_t clockid;
55 const char *fnname;
58 static void *
59 writer_thread (void *arg)
61 struct thread_args *args = arg;
62 const int nr = args->nr;
63 const clockid_t clockid = args->clockid;
64 const clockid_t clockid_for_get =
65 (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid;
66 const char *fnname = args->fnname;
68 struct timespec ts;
69 int n;
71 for (n = 0; n < WRITETRIES; ++n)
73 int e;
76 xclock_gettime (clockid_for_get, &ts);
78 ts = timespec_add (ts, timeout);
79 ts = timespec_add (ts, timeout);
81 printf ("writer thread %d tries again\n", nr);
83 e = (clockid == CLOCK_USE_TIMEDLOCK)
84 ? pthread_rwlock_timedwrlock (&lock, &ts)
85 : pthread_rwlock_clockwrlock (&lock, clockid, &ts);
86 if (e != 0 && e != ETIMEDOUT)
87 FAIL_EXIT1 ("%swrlock failed", fnname);
89 while (e == ETIMEDOUT);
91 printf ("writer thread %d succeeded\n", nr);
93 nanosleep (&delay, NULL);
95 if (pthread_rwlock_unlock (&lock) != 0)
96 FAIL_EXIT1 ("unlock for writer failed");
98 printf ("writer thread %d released\n", nr);
101 return NULL;
105 static void *
106 reader_thread (void *arg)
108 struct thread_args *args = arg;
109 const int nr = args->nr;
110 const clockid_t clockid = args->clockid;
111 const clockid_t clockid_for_get =
112 (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid;
113 const char *fnname = args->fnname;
115 struct timespec ts;
116 int n;
118 for (n = 0; n < READTRIES; ++n)
120 int e;
123 xclock_gettime (clockid_for_get, &ts);
125 ts = timespec_add (ts, timeout);
127 printf ("reader thread %d tries again\n", nr);
129 if (clockid == CLOCK_USE_TIMEDLOCK)
130 e = pthread_rwlock_timedrdlock (&lock, &ts);
131 else
132 e = pthread_rwlock_clockrdlock (&lock, clockid, &ts);
133 if (e != 0 && e != ETIMEDOUT)
134 FAIL_EXIT1 ("%srdlock failed", fnname);
136 while (e == ETIMEDOUT);
138 printf ("reader thread %d succeeded\n", nr);
140 nanosleep (&delay, NULL);
142 if (pthread_rwlock_unlock (&lock) != 0)
143 FAIL_EXIT1 ("unlock for reader failed");
145 printf ("reader thread %d released\n", nr);
148 return NULL;
152 static int
153 do_test_clock (clockid_t clockid, const char *fnname)
155 pthread_t thwr[NWRITERS];
156 pthread_t thrd[NREADERS];
157 int n;
158 pthread_rwlockattr_t a;
160 if (pthread_rwlockattr_init (&a) != 0)
161 FAIL_EXIT1 ("rwlockattr_t failed");
163 if (pthread_rwlockattr_setkind_np (&a, KIND) != 0)
164 FAIL_EXIT1 ("rwlockattr_setkind failed");
166 if (pthread_rwlock_init (&lock, &a) != 0)
167 FAIL_EXIT1 ("rwlock_init failed");
169 /* Make standard error the same as standard output. */
170 dup2 (1, 2);
172 /* Make sure we see all message, even those on stdout. */
173 setvbuf (stdout, NULL, _IONBF, 0);
175 struct thread_args wargs[NWRITERS];
176 for (n = 0; n < NWRITERS; ++n) {
177 wargs[n].nr = n;
178 wargs[n].clockid = clockid;
179 wargs[n].fnname = fnname;
180 thwr[n] = xpthread_create (NULL, writer_thread, &wargs[n]);
183 struct thread_args rargs[NREADERS];
184 for (n = 0; n < NREADERS; ++n) {
185 rargs[n].nr = n;
186 rargs[n].clockid = clockid;
187 rargs[n].fnname = fnname;
188 thrd[n] = xpthread_create (NULL, reader_thread, &rargs[n]);
191 /* Wait for all the threads. */
192 for (n = 0; n < NWRITERS; ++n)
193 xpthread_join (thwr[n]);
194 for (n = 0; n < NREADERS; ++n)
195 xpthread_join (thrd[n]);
197 return 0;
200 static int
201 do_test (void)
203 do_test_clock (CLOCK_USE_TIMEDLOCK, "timed");
204 do_test_clock (CLOCK_REALTIME, "clock(realtime)");
205 do_test_clock (CLOCK_MONOTONIC, "clock(monotonic)");
207 return 0;
210 #define TIMEOUT 30
211 #include <support/test-driver.c>