]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/tst-rwlock9.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nptl / tst-rwlock9.c
CommitLineData
26a526fa 1/* Test program for timedout read/write lock functions.
04277e02 2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
26a526fa
UD
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; see the file COPYING.LIB. If
5a82c748 17 not, see <https://www.gnu.org/licenses/>. */
26a526fa
UD
18
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>
495514ee
MC
27#include <support/check.h>
28#include <support/timespec.h>
e996fa72 29#include <support/xthread.h>
26a526fa
UD
30
31
32#define NWRITERS 15
33#define WRITETRIES 10
34#define NREADERS 15
35#define READTRIES 15
36
495514ee
MC
37static const struct timespec timeout = { 0,1000000 };
38static const struct timespec delay = { 0, 1000000 };
26a526fa 39
cc25c8b4
TR
40#ifndef KIND
41# define KIND PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
d148ed25
UD
42#endif
43
e996fa72
MC
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)
48
cc25c8b4 49static pthread_rwlock_t lock;
26a526fa 50
e996fa72
MC
51struct thread_args
52{
53 int nr;
54 clockid_t clockid;
55 const char *fnname;
56};
26a526fa
UD
57
58static void *
e996fa72 59writer_thread (void *arg)
26a526fa 60{
e996fa72
MC
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;
67
26a526fa 68 struct timespec ts;
26a526fa
UD
69 int n;
70
26a526fa
UD
71 for (n = 0; n < WRITETRIES; ++n)
72 {
73 int e;
74 do
75 {
e996fa72 76 xclock_gettime (clockid_for_get, &ts);
26a526fa 77
495514ee
MC
78 ts = timespec_add (ts, timeout);
79 ts = timespec_add (ts, timeout);
26a526fa 80
e996fa72 81 printf ("writer thread %d tries again\n", nr);
26a526fa 82
e996fa72
MC
83 e = (clockid == CLOCK_USE_TIMEDLOCK)
84 ? pthread_rwlock_timedwrlock (&lock, &ts)
85 : pthread_rwlock_clockwrlock (&lock, clockid, &ts);
26a526fa 86 if (e != 0 && e != ETIMEDOUT)
e996fa72 87 FAIL_EXIT1 ("%swrlock failed", fnname);
26a526fa
UD
88 }
89 while (e == ETIMEDOUT);
90
e996fa72 91 printf ("writer thread %d succeeded\n", nr);
26a526fa
UD
92
93 nanosleep (&delay, NULL);
94
95 if (pthread_rwlock_unlock (&lock) != 0)
495514ee 96 FAIL_EXIT1 ("unlock for writer failed");
26a526fa 97
e996fa72 98 printf ("writer thread %d released\n", nr);
26a526fa
UD
99 }
100
101 return NULL;
102}
103
104
105static void *
e996fa72 106reader_thread (void *arg)
26a526fa 107{
e996fa72
MC
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;
114
26a526fa 115 struct timespec ts;
26a526fa
UD
116 int n;
117
26a526fa
UD
118 for (n = 0; n < READTRIES; ++n)
119 {
120 int e;
121 do
122 {
e996fa72 123 xclock_gettime (clockid_for_get, &ts);
26a526fa 124
495514ee 125 ts = timespec_add (ts, timeout);
26a526fa 126
e996fa72 127 printf ("reader thread %d tries again\n", nr);
26a526fa 128
e996fa72
MC
129 if (clockid == CLOCK_USE_TIMEDLOCK)
130 e = pthread_rwlock_timedrdlock (&lock, &ts);
131 else
132 e = pthread_rwlock_clockrdlock (&lock, clockid, &ts);
26a526fa 133 if (e != 0 && e != ETIMEDOUT)
e996fa72 134 FAIL_EXIT1 ("%srdlock failed", fnname);
26a526fa
UD
135 }
136 while (e == ETIMEDOUT);
137
e996fa72 138 printf ("reader thread %d succeeded\n", nr);
26a526fa
UD
139
140 nanosleep (&delay, NULL);
141
142 if (pthread_rwlock_unlock (&lock) != 0)
495514ee 143 FAIL_EXIT1 ("unlock for reader failed");
26a526fa 144
e996fa72 145 printf ("reader thread %d released\n", nr);
26a526fa
UD
146 }
147
148 return NULL;
149}
150
151
152static int
e996fa72 153do_test_clock (clockid_t clockid, const char *fnname)
26a526fa
UD
154{
155 pthread_t thwr[NWRITERS];
156 pthread_t thrd[NREADERS];
157 int n;
cc25c8b4
TR
158 pthread_rwlockattr_t a;
159
160 if (pthread_rwlockattr_init (&a) != 0)
495514ee 161 FAIL_EXIT1 ("rwlockattr_t failed");
cc25c8b4
TR
162
163 if (pthread_rwlockattr_setkind_np (&a, KIND) != 0)
495514ee 164 FAIL_EXIT1 ("rwlockattr_setkind failed");
cc25c8b4
TR
165
166 if (pthread_rwlock_init (&lock, &a) != 0)
495514ee 167 FAIL_EXIT1 ("rwlock_init failed");
26a526fa
UD
168
169 /* Make standard error the same as standard output. */
170 dup2 (1, 2);
171
172 /* Make sure we see all message, even those on stdout. */
173 setvbuf (stdout, NULL, _IONBF, 0);
174
e996fa72
MC
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]);
181 }
182
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]);
189 }
26a526fa
UD
190
191 /* Wait for all the threads. */
192 for (n = 0; n < NWRITERS; ++n)
e996fa72 193 xpthread_join (thwr[n]);
26a526fa 194 for (n = 0; n < NREADERS; ++n)
e996fa72
MC
195 xpthread_join (thrd[n]);
196
197 return 0;
198}
199
200static int
201do_test (void)
202{
203 do_test_clock (CLOCK_USE_TIMEDLOCK, "timed");
204 do_test_clock (CLOCK_REALTIME, "clock(realtime)");
205 do_test_clock (CLOCK_MONOTONIC, "clock(monotonic)");
26a526fa
UD
206
207 return 0;
208}
209
26a526fa 210#define TIMEOUT 30
495514ee 211#include <support/test-driver.c>