]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/tst-rwlock9.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nptl / tst-rwlock9.c
1 /* Test program for timedout read/write lock functions.
2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
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
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <https://www.gnu.org/licenses/>. */
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>
27 #include <support/check.h>
28 #include <support/timespec.h>
29 #include <support/xthread.h>
30
31
32 #define NWRITERS 15
33 #define WRITETRIES 10
34 #define NREADERS 15
35 #define READTRIES 15
36
37 static const struct timespec timeout = { 0,1000000 };
38 static const struct timespec delay = { 0, 1000000 };
39
40 #ifndef KIND
41 # define KIND PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
42 #endif
43
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
49 static pthread_rwlock_t lock;
50
51 struct thread_args
52 {
53 int nr;
54 clockid_t clockid;
55 const char *fnname;
56 };
57
58 static void *
59 writer_thread (void *arg)
60 {
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
68 struct timespec ts;
69 int n;
70
71 for (n = 0; n < WRITETRIES; ++n)
72 {
73 int e;
74 do
75 {
76 xclock_gettime (clockid_for_get, &ts);
77
78 ts = timespec_add (ts, timeout);
79 ts = timespec_add (ts, timeout);
80
81 printf ("writer thread %d tries again\n", nr);
82
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);
88 }
89 while (e == ETIMEDOUT);
90
91 printf ("writer thread %d succeeded\n", nr);
92
93 nanosleep (&delay, NULL);
94
95 if (pthread_rwlock_unlock (&lock) != 0)
96 FAIL_EXIT1 ("unlock for writer failed");
97
98 printf ("writer thread %d released\n", nr);
99 }
100
101 return NULL;
102 }
103
104
105 static void *
106 reader_thread (void *arg)
107 {
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
115 struct timespec ts;
116 int n;
117
118 for (n = 0; n < READTRIES; ++n)
119 {
120 int e;
121 do
122 {
123 xclock_gettime (clockid_for_get, &ts);
124
125 ts = timespec_add (ts, timeout);
126
127 printf ("reader thread %d tries again\n", nr);
128
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);
135 }
136 while (e == ETIMEDOUT);
137
138 printf ("reader thread %d succeeded\n", nr);
139
140 nanosleep (&delay, NULL);
141
142 if (pthread_rwlock_unlock (&lock) != 0)
143 FAIL_EXIT1 ("unlock for reader failed");
144
145 printf ("reader thread %d released\n", nr);
146 }
147
148 return NULL;
149 }
150
151
152 static int
153 do_test_clock (clockid_t clockid, const char *fnname)
154 {
155 pthread_t thwr[NWRITERS];
156 pthread_t thrd[NREADERS];
157 int n;
158 pthread_rwlockattr_t a;
159
160 if (pthread_rwlockattr_init (&a) != 0)
161 FAIL_EXIT1 ("rwlockattr_t failed");
162
163 if (pthread_rwlockattr_setkind_np (&a, KIND) != 0)
164 FAIL_EXIT1 ("rwlockattr_setkind failed");
165
166 if (pthread_rwlock_init (&lock, &a) != 0)
167 FAIL_EXIT1 ("rwlock_init failed");
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
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 }
190
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]);
196
197 return 0;
198 }
199
200 static int
201 do_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)");
206
207 return 0;
208 }
209
210 #define TIMEOUT 30
211 #include <support/test-driver.c>