]>
Commit | Line | Data |
---|---|---|
0a37669a | 1 | /* Copyright (C) 2002, 2003 Free Software Foundation, Inc. |
76a50749 UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. | |
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 | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the 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; if not, write to the Free | |
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
18 | 02111-1307 USA. */ | |
19 | ||
20 | #include <errno.h> | |
21 | #include <pthread.h> | |
22 | #include <stdlib.h> | |
23 | #include <stdio.h> | |
24 | #include <string.h> | |
25 | #include <sys/time.h> | |
26 | ||
27 | ||
28 | static int kind[] = | |
29 | { | |
30 | PTHREAD_RWLOCK_PREFER_READER_NP, | |
31 | PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP, | |
32 | PTHREAD_RWLOCK_PREFER_WRITER_NP, | |
33 | }; | |
34 | ||
35 | ||
36 | static void * | |
37 | tf (void *arg) | |
38 | { | |
39 | pthread_rwlock_t *r = arg; | |
40 | ||
41 | /* Timeout: 0.3 secs. */ | |
42 | struct timeval tv; | |
43 | (void) gettimeofday (&tv, NULL); | |
44 | ||
45 | struct timespec ts; | |
46 | TIMEVAL_TO_TIMESPEC (&tv, &ts); | |
47 | ts.tv_nsec += 300000000; | |
48 | if (ts.tv_nsec >= 1000000000) | |
49 | { | |
50 | ts.tv_nsec -= 1000000000; | |
51 | ++ts.tv_sec; | |
52 | } | |
53 | ||
51d0678c UD |
54 | puts ("child calling timedrdlock"); |
55 | ||
76a50749 UD |
56 | int err = pthread_rwlock_timedrdlock (r, &ts); |
57 | if (err == 0) | |
58 | { | |
59 | puts ("rwlock_timedrdlock returned"); | |
60 | pthread_exit ((void *) 1l); | |
61 | } | |
62 | ||
63 | if (err != ETIMEDOUT) | |
64 | { | |
65 | printf ("err = %s (%d), expected %s (%d)\n", | |
66 | strerror (err), err, strerror (ETIMEDOUT), ETIMEDOUT); | |
67 | pthread_exit ((void *) 1l); | |
68 | } | |
69 | ||
51d0678c UD |
70 | puts ("1st child timedrdlock done"); |
71 | ||
76a50749 UD |
72 | struct timeval tv2; |
73 | (void) gettimeofday (&tv2, NULL); | |
74 | ||
75 | timersub (&tv2, &tv, &tv); | |
76 | ||
77 | if (tv.tv_usec < 200000) | |
78 | { | |
79 | puts ("timeout too short"); | |
80 | pthread_exit ((void *) 1l); | |
81 | } | |
82 | ||
0a37669a UD |
83 | (void) gettimeofday (&tv, NULL); |
84 | TIMEVAL_TO_TIMESPEC (&tv, &ts); | |
85 | ts.tv_sec += 10; | |
86 | /* Note that the following operation makes ts invalid. */ | |
87 | ts.tv_nsec += 1000000000; | |
88 | ||
89 | err = pthread_rwlock_timedrdlock (r, &ts); | |
90 | if (err == 0) | |
91 | { | |
92 | puts ("2nd timedrdlock succeeded"); | |
93 | pthread_exit ((void *) 1l); | |
94 | } | |
95 | if (err != EINVAL) | |
96 | { | |
97 | puts ("2nd timedrdlock did not return EINVAL"); | |
98 | pthread_exit ((void *) 1l); | |
99 | } | |
100 | ||
51d0678c UD |
101 | puts ("2nd child timedrdlock done"); |
102 | ||
76a50749 UD |
103 | return NULL; |
104 | } | |
105 | ||
106 | ||
107 | static int | |
108 | do_test (void) | |
109 | { | |
f9657e88 | 110 | size_t cnt; |
76a50749 UD |
111 | for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt) |
112 | { | |
113 | pthread_rwlock_t r; | |
114 | pthread_rwlockattr_t a; | |
115 | ||
116 | if (pthread_rwlockattr_init (&a) != 0) | |
117 | { | |
1472a752 | 118 | printf ("round %Zu: rwlockattr_t failed\n", cnt); |
76a50749 UD |
119 | exit (1); |
120 | } | |
121 | ||
122 | if (pthread_rwlockattr_setkind_np (&a, kind[cnt]) != 0) | |
123 | { | |
1472a752 | 124 | printf ("round %Zu: rwlockattr_setkind failed\n", cnt); |
76a50749 UD |
125 | exit (1); |
126 | } | |
127 | ||
128 | if (pthread_rwlock_init (&r, &a) != 0) | |
129 | { | |
1472a752 | 130 | printf ("round %Zu: rwlock_init failed\n", cnt); |
76a50749 UD |
131 | exit (1); |
132 | } | |
133 | ||
134 | if (pthread_rwlockattr_destroy (&a) != 0) | |
135 | { | |
1472a752 | 136 | printf ("round %Zu: rwlockattr_destroy failed\n", cnt); |
76a50749 UD |
137 | exit (1); |
138 | } | |
139 | ||
140 | struct timeval tv; | |
141 | (void) gettimeofday (&tv, NULL); | |
142 | ||
143 | struct timespec ts; | |
144 | TIMEVAL_TO_TIMESPEC (&tv, &ts); | |
145 | ||
146 | ++ts.tv_sec; | |
147 | ||
148 | /* Get a write lock. */ | |
51d0678c UD |
149 | int e = pthread_rwlock_timedwrlock (&r, &ts); |
150 | if (e != 0) | |
76a50749 | 151 | { |
1472a752 | 152 | printf ("round %Zu: rwlock_timedwrlock failed (%d)\n", cnt, e); |
76a50749 UD |
153 | exit (1); |
154 | } | |
155 | ||
51d0678c UD |
156 | puts ("1st timedwrlock done"); |
157 | ||
0a37669a UD |
158 | (void) gettimeofday (&tv, NULL); |
159 | TIMEVAL_TO_TIMESPEC (&tv, &ts); | |
160 | ++ts.tv_sec; | |
51d0678c | 161 | e = pthread_rwlock_timedrdlock (&r, &ts); |
0a37669a UD |
162 | if (e == 0) |
163 | { | |
164 | puts ("timedrdlock succeeded"); | |
165 | exit (1); | |
166 | } | |
167 | if (e != EDEADLK) | |
168 | { | |
169 | puts ("timedrdlock did not return EDEADLK"); | |
170 | exit (1); | |
171 | } | |
172 | ||
51d0678c UD |
173 | puts ("1st timedrdlock done"); |
174 | ||
0a37669a UD |
175 | (void) gettimeofday (&tv, NULL); |
176 | TIMEVAL_TO_TIMESPEC (&tv, &ts); | |
177 | ++ts.tv_sec; | |
178 | e = pthread_rwlock_timedwrlock (&r, &ts); | |
179 | if (e == 0) | |
180 | { | |
181 | puts ("2nd timedwrlock succeeded"); | |
182 | exit (1); | |
183 | } | |
184 | if (e != EDEADLK) | |
185 | { | |
186 | puts ("2nd timedwrlock did not return EDEADLK"); | |
187 | exit (1); | |
188 | } | |
189 | ||
51d0678c UD |
190 | puts ("2nd timedwrlock done"); |
191 | ||
76a50749 UD |
192 | pthread_t th; |
193 | if (pthread_create (&th, NULL, tf, &r) != 0) | |
194 | { | |
1472a752 | 195 | printf ("round %Zu: create failed\n", cnt); |
76a50749 UD |
196 | exit (1); |
197 | } | |
198 | ||
51d0678c UD |
199 | puts ("started thread"); |
200 | ||
76a50749 UD |
201 | void *status; |
202 | if (pthread_join (th, &status) != 0) | |
203 | { | |
1472a752 | 204 | printf ("round %Zu: join failed\n", cnt); |
76a50749 UD |
205 | exit (1); |
206 | } | |
207 | if (status != NULL) | |
208 | { | |
7ab7ea33 | 209 | printf ("failure in round %Zu\n", cnt); |
76a50749 UD |
210 | exit (1); |
211 | } | |
212 | ||
51d0678c UD |
213 | puts ("joined thread"); |
214 | ||
76a50749 UD |
215 | if (pthread_rwlock_destroy (&r) != 0) |
216 | { | |
1472a752 | 217 | printf ("round %Zu: rwlock_destroy failed\n", cnt); |
76a50749 UD |
218 | exit (1); |
219 | } | |
220 | } | |
221 | ||
222 | return 0; | |
223 | } | |
224 | ||
225 | #define TEST_FUNCTION do_test () | |
226 | #include "../test-skeleton.c" |