1 | /* Test program for timedout read/write lock functions.
|
---|
2 | Copyright (C) 2000, 2003 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 not,
|
---|
17 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
---|
18 | Boston, MA 02111-1307, USA. */
|
---|
19 |
|
---|
20 | #include <errno.h>
|
---|
21 | #include <error.h>
|
---|
22 | #include <pthread.h>
|
---|
23 | #include <stdio.h>
|
---|
24 | #include <stdlib.h>
|
---|
25 | #include <time.h>
|
---|
26 | #include <unistd.h>
|
---|
27 | #include <sys/time.h>
|
---|
28 |
|
---|
29 |
|
---|
30 | #define NWRITERS 15
|
---|
31 | #define WRITETRIES 10
|
---|
32 | #define NREADERS 15
|
---|
33 | #define READTRIES 15
|
---|
34 |
|
---|
35 | #define TIMEOUT 1000000
|
---|
36 | #define DELAY 1000000
|
---|
37 |
|
---|
38 | #ifndef INIT
|
---|
39 | # define INIT PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | static pthread_rwlock_t lock = INIT;
|
---|
43 |
|
---|
44 |
|
---|
45 | static void *
|
---|
46 | writer_thread (void *nr)
|
---|
47 | {
|
---|
48 | struct timespec ts;
|
---|
49 | struct timespec delay;
|
---|
50 | int n;
|
---|
51 |
|
---|
52 | delay.tv_sec = 0;
|
---|
53 | delay.tv_nsec = DELAY;
|
---|
54 |
|
---|
55 | for (n = 0; n < WRITETRIES; ++n)
|
---|
56 | {
|
---|
57 | int e;
|
---|
58 | do
|
---|
59 | {
|
---|
60 | struct timeval tv;
|
---|
61 | (void) gettimeofday (&tv, NULL);
|
---|
62 | TIMEVAL_TO_TIMESPEC (&tv, &ts);
|
---|
63 |
|
---|
64 | ts.tv_nsec += 2 * TIMEOUT;
|
---|
65 | if (ts.tv_nsec >= 1000000000)
|
---|
66 | {
|
---|
67 | ts.tv_nsec -= 1000000000;
|
---|
68 | ++ts.tv_sec;
|
---|
69 | }
|
---|
70 |
|
---|
71 | printf ("writer thread %ld tries again\n", (long int) nr);
|
---|
72 |
|
---|
73 | e = pthread_rwlock_timedwrlock (&lock, &ts);
|
---|
74 | if (e != 0 && e != ETIMEDOUT)
|
---|
75 | {
|
---|
76 | puts ("timedwrlock failed");
|
---|
77 | exit (1);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | while (e == ETIMEDOUT);
|
---|
81 |
|
---|
82 | printf ("writer thread %ld succeeded\n", (long int) nr);
|
---|
83 |
|
---|
84 | nanosleep (&delay, NULL);
|
---|
85 |
|
---|
86 | if (pthread_rwlock_unlock (&lock) != 0)
|
---|
87 | {
|
---|
88 | puts ("unlock for writer failed");
|
---|
89 | exit (1);
|
---|
90 | }
|
---|
91 |
|
---|
92 | printf ("writer thread %ld released\n", (long int) nr);
|
---|
93 | }
|
---|
94 |
|
---|
95 | return NULL;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | static void *
|
---|
100 | reader_thread (void *nr)
|
---|
101 | {
|
---|
102 | struct timespec ts;
|
---|
103 | struct timespec delay;
|
---|
104 | int n;
|
---|
105 |
|
---|
106 | delay.tv_sec = 0;
|
---|
107 | delay.tv_nsec = DELAY;
|
---|
108 |
|
---|
109 | for (n = 0; n < READTRIES; ++n)
|
---|
110 | {
|
---|
111 | int e;
|
---|
112 | do
|
---|
113 | {
|
---|
114 | struct timeval tv;
|
---|
115 | (void) gettimeofday (&tv, NULL);
|
---|
116 | TIMEVAL_TO_TIMESPEC (&tv, &ts);
|
---|
117 |
|
---|
118 | ts.tv_nsec += TIMEOUT;
|
---|
119 | if (ts.tv_nsec >= 1000000000)
|
---|
120 | {
|
---|
121 | ts.tv_nsec -= 1000000000;
|
---|
122 | ++ts.tv_sec;
|
---|
123 | }
|
---|
124 |
|
---|
125 | printf ("reader thread %ld tries again\n", (long int) nr);
|
---|
126 |
|
---|
127 | e = pthread_rwlock_timedrdlock (&lock, &ts);
|
---|
128 | if (e != 0 && e != ETIMEDOUT)
|
---|
129 | {
|
---|
130 | puts ("timedrdlock failed");
|
---|
131 | exit (1);
|
---|
132 | }
|
---|
133 | }
|
---|
134 | while (e == ETIMEDOUT);
|
---|
135 |
|
---|
136 | printf ("reader thread %ld succeeded\n", (long int) nr);
|
---|
137 |
|
---|
138 | nanosleep (&delay, NULL);
|
---|
139 |
|
---|
140 | if (pthread_rwlock_unlock (&lock) != 0)
|
---|
141 | {
|
---|
142 | puts ("unlock for reader failed");
|
---|
143 | exit (1);
|
---|
144 | }
|
---|
145 |
|
---|
146 | printf ("reader thread %ld released\n", (long int) nr);
|
---|
147 | }
|
---|
148 |
|
---|
149 | return NULL;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | static int
|
---|
154 | do_test (void)
|
---|
155 | {
|
---|
156 | pthread_t thwr[NWRITERS];
|
---|
157 | pthread_t thrd[NREADERS];
|
---|
158 | int n;
|
---|
159 | void *res;
|
---|
160 |
|
---|
161 | /* Make standard error the same as standard output. */
|
---|
162 | dup2 (1, 2);
|
---|
163 |
|
---|
164 | /* Make sure we see all message, even those on stdout. */
|
---|
165 | setvbuf (stdout, NULL, _IONBF, 0);
|
---|
166 |
|
---|
167 | for (n = 0; n < NWRITERS; ++n)
|
---|
168 | if (pthread_create (&thwr[n], NULL, writer_thread,
|
---|
169 | (void *) (long int) n) != 0)
|
---|
170 | {
|
---|
171 | puts ("writer create failed");
|
---|
172 | exit (1);
|
---|
173 | }
|
---|
174 |
|
---|
175 | for (n = 0; n < NREADERS; ++n)
|
---|
176 | if (pthread_create (&thrd[n], NULL, reader_thread,
|
---|
177 | (void *) (long int) n) != 0)
|
---|
178 | {
|
---|
179 | puts ("reader create failed");
|
---|
180 | exit (1);
|
---|
181 | }
|
---|
182 |
|
---|
183 | /* Wait for all the threads. */
|
---|
184 | for (n = 0; n < NWRITERS; ++n)
|
---|
185 | if (pthread_join (thwr[n], &res) != 0)
|
---|
186 | {
|
---|
187 | puts ("writer join failed");
|
---|
188 | exit (1);
|
---|
189 | }
|
---|
190 | for (n = 0; n < NREADERS; ++n)
|
---|
191 | if (pthread_join (thrd[n], &res) != 0)
|
---|
192 | {
|
---|
193 | puts ("reader join failed");
|
---|
194 | exit (1);
|
---|
195 | }
|
---|
196 |
|
---|
197 | return 0;
|
---|
198 | }
|
---|
199 |
|
---|
200 | #undef TIMEOUT
|
---|
201 | #define TIMEOUT 30
|
---|
202 | #define TEST_FUNCTION do_test ()
|
---|
203 | #include "../test-skeleton.c"
|
---|