Warning: That file was not part of the compilation database. It may have many parsing errors.

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 <http://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
28
29#define NWRITERS 15
30#define WRITETRIES 10
31#define NREADERS 15
32#define READTRIES 15
33
34#define TIMEOUT 1000000
35#define DELAY 1000000
36
37#ifndef KIND
38# define KIND PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
39#endif
40
41static pthread_rwlock_t lock;
42
43
44static void *
45writer_thread (void *nr)
46{
47 struct timespec ts;
48 struct timespec delay;
49 int n;
50
51 delay.tv_sec = 0;
52 delay.tv_nsec = DELAY;
53
54 for (n = 0; n < WRITETRIES; ++n)
55 {
56 int e;
57 do
58 {
59 struct timeval tv;
60 (void) gettimeofday (&tv, NULL);
61 TIMEVAL_TO_TIMESPEC (&tv, &ts);
62
63 ts.tv_nsec += 2 * TIMEOUT;
64 if (ts.tv_nsec >= 1000000000)
65 {
66 ts.tv_nsec -= 1000000000;
67 ++ts.tv_sec;
68 }
69
70 printf ("writer thread %ld tries again\n", (long int) nr);
71
72 e = pthread_rwlock_timedwrlock (&lock, &ts);
73 if (e != 0 && e != ETIMEDOUT)
74 {
75 puts ("timedwrlock failed");
76 exit (1);
77 }
78 }
79 while (e == ETIMEDOUT);
80
81 printf ("writer thread %ld succeeded\n", (long int) nr);
82
83 nanosleep (&delay, NULL);
84
85 if (pthread_rwlock_unlock (&lock) != 0)
86 {
87 puts ("unlock for writer failed");
88 exit (1);
89 }
90
91 printf ("writer thread %ld released\n", (long int) nr);
92 }
93
94 return NULL;
95}
96
97
98static void *
99reader_thread (void *nr)
100{
101 struct timespec ts;
102 struct timespec delay;
103 int n;
104
105 delay.tv_sec = 0;
106 delay.tv_nsec = DELAY;
107
108 for (n = 0; n < READTRIES; ++n)
109 {
110 int e;
111 do
112 {
113 struct timeval tv;
114 (void) gettimeofday (&tv, NULL);
115 TIMEVAL_TO_TIMESPEC (&tv, &ts);
116
117 ts.tv_nsec += TIMEOUT;
118 if (ts.tv_nsec >= 1000000000)
119 {
120 ts.tv_nsec -= 1000000000;
121 ++ts.tv_sec;
122 }
123
124 printf ("reader thread %ld tries again\n", (long int) nr);
125
126 e = pthread_rwlock_timedrdlock (&lock, &ts);
127 if (e != 0 && e != ETIMEDOUT)
128 {
129 puts ("timedrdlock failed");
130 exit (1);
131 }
132 }
133 while (e == ETIMEDOUT);
134
135 printf ("reader thread %ld succeeded\n", (long int) nr);
136
137 nanosleep (&delay, NULL);
138
139 if (pthread_rwlock_unlock (&lock) != 0)
140 {
141 puts ("unlock for reader failed");
142 exit (1);
143 }
144
145 printf ("reader thread %ld released\n", (long int) nr);
146 }
147
148 return NULL;
149}
150
151
152static int
153do_test (void)
154{
155 pthread_t thwr[NWRITERS];
156 pthread_t thrd[NREADERS];
157 int n;
158 void *res;
159 pthread_rwlockattr_t a;
160
161 if (pthread_rwlockattr_init (&a) != 0)
162 {
163 puts ("rwlockattr_t failed");
164 exit (1);
165 }
166
167 if (pthread_rwlockattr_setkind_np (&a, KIND) != 0)
168 {
169 puts ("rwlockattr_setkind failed");
170 exit (1);
171 }
172
173 if (pthread_rwlock_init (&lock, &a) != 0)
174 {
175 puts ("rwlock_init failed");
176 exit (1);
177 }
178
179 /* Make standard error the same as standard output. */
180 dup2 (1, 2);
181
182 /* Make sure we see all message, even those on stdout. */
183 setvbuf (stdout, NULL, _IONBF, 0);
184
185 for (n = 0; n < NWRITERS; ++n)
186 if (pthread_create (&thwr[n], NULL, writer_thread,
187 (void *) (long int) n) != 0)
188 {
189 puts ("writer create failed");
190 exit (1);
191 }
192
193 for (n = 0; n < NREADERS; ++n)
194 if (pthread_create (&thrd[n], NULL, reader_thread,
195 (void *) (long int) n) != 0)
196 {
197 puts ("reader create failed");
198 exit (1);
199 }
200
201 /* Wait for all the threads. */
202 for (n = 0; n < NWRITERS; ++n)
203 if (pthread_join (thwr[n], &res) != 0)
204 {
205 puts ("writer join failed");
206 exit (1);
207 }
208 for (n = 0; n < NREADERS; ++n)
209 if (pthread_join (thrd[n], &res) != 0)
210 {
211 puts ("reader join failed");
212 exit (1);
213 }
214
215 return 0;
216}
217
218#undef TIMEOUT
219#define TIMEOUT 30
220#define TEST_FUNCTION do_test ()
221#include "../test-skeleton.c"
222

Warning: That file was not part of the compilation database. It may have many parsing errors.