]>
Commit | Line | Data |
---|---|---|
8980796b | 1 | /* Copyright (C) 2002, 2003, 2006 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 | ||
54 | int err = pthread_rwlock_timedwrlock (r, &ts); | |
55 | if (err == 0) | |
56 | { | |
57 | puts ("rwlock_timedwrlock returned"); | |
58 | pthread_exit ((void *) 1l); | |
59 | } | |
60 | ||
61 | if (err != ETIMEDOUT) | |
62 | { | |
63 | printf ("err = %s (%d), expected %s (%d)\n", | |
64 | strerror (err), err, strerror (ETIMEDOUT), ETIMEDOUT); | |
65 | pthread_exit ((void *) 1l); | |
66 | } | |
8980796b | 67 | puts ("child: timedwrlock failed with ETIMEDOUT"); |
76a50749 UD |
68 | |
69 | struct timeval tv2; | |
70 | (void) gettimeofday (&tv2, NULL); | |
71 | ||
72 | timersub (&tv2, &tv, &tv); | |
73 | ||
74 | if (tv.tv_usec < 200000) | |
75 | { | |
76 | puts ("timeout too short"); | |
77 | pthread_exit ((void *) 1l); | |
78 | } | |
79 | ||
0a37669a UD |
80 | (void) gettimeofday (&tv, NULL); |
81 | TIMEVAL_TO_TIMESPEC (&tv, &ts); | |
82 | ts.tv_sec += 10; | |
83 | /* Note that the following operation makes ts invalid. */ | |
84 | ts.tv_nsec += 1000000000; | |
85 | ||
86 | err = pthread_rwlock_timedwrlock (r, &ts); | |
87 | if (err == 0) | |
88 | { | |
89 | puts ("2nd timedwrlock succeeded"); | |
90 | pthread_exit ((void *) 1l); | |
91 | } | |
92 | if (err != EINVAL) | |
93 | { | |
94 | puts ("2nd timedwrlock did not return EINVAL"); | |
95 | pthread_exit ((void *) 1l); | |
96 | } | |
8980796b | 97 | puts ("child: timedwrlock failed with EINVAL"); |
0a37669a | 98 | |
76a50749 UD |
99 | return NULL; |
100 | } | |
101 | ||
102 | ||
103 | static int | |
104 | do_test (void) | |
105 | { | |
f9657e88 | 106 | size_t cnt; |
76a50749 UD |
107 | for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt) |
108 | { | |
109 | pthread_rwlock_t r; | |
110 | pthread_rwlockattr_t a; | |
111 | ||
112 | if (pthread_rwlockattr_init (&a) != 0) | |
113 | { | |
1472a752 | 114 | printf ("round %Zu: rwlockattr_t failed\n", cnt); |
76a50749 UD |
115 | exit (1); |
116 | } | |
117 | ||
118 | if (pthread_rwlockattr_setkind_np (&a, kind[cnt]) != 0) | |
119 | { | |
1472a752 | 120 | printf ("round %Zu: rwlockattr_setkind failed\n", cnt); |
76a50749 UD |
121 | exit (1); |
122 | } | |
123 | ||
124 | if (pthread_rwlock_init (&r, &a) != 0) | |
125 | { | |
1472a752 | 126 | printf ("round %Zu: rwlock_init failed\n", cnt); |
76a50749 UD |
127 | exit (1); |
128 | } | |
129 | ||
130 | if (pthread_rwlockattr_destroy (&a) != 0) | |
131 | { | |
1472a752 | 132 | printf ("round %Zu: rwlockattr_destroy failed\n", cnt); |
76a50749 UD |
133 | exit (1); |
134 | } | |
135 | ||
136 | struct timeval tv; | |
137 | (void) gettimeofday (&tv, NULL); | |
138 | ||
139 | struct timespec ts; | |
140 | TIMEVAL_TO_TIMESPEC (&tv, &ts); | |
141 | ||
142 | ++ts.tv_sec; | |
143 | ||
144 | /* Get a read lock. */ | |
145 | if (pthread_rwlock_timedrdlock (&r, &ts) != 0) | |
146 | { | |
1472a752 | 147 | printf ("round %Zu: rwlock_timedrdlock failed\n", cnt); |
76a50749 UD |
148 | exit (1); |
149 | } | |
8980796b | 150 | printf ("%zu: got timedrdlock\n", cnt); |
76a50749 UD |
151 | |
152 | pthread_t th; | |
153 | if (pthread_create (&th, NULL, tf, &r) != 0) | |
154 | { | |
1472a752 | 155 | printf ("round %Zu: create failed\n", cnt); |
76a50749 UD |
156 | exit (1); |
157 | } | |
158 | ||
159 | void *status; | |
160 | if (pthread_join (th, &status) != 0) | |
161 | { | |
1472a752 | 162 | printf ("round %Zu: join failed\n", cnt); |
76a50749 UD |
163 | exit (1); |
164 | } | |
165 | if (status != NULL) | |
166 | { | |
65d46efe | 167 | printf ("failure in round %Zu\n", cnt); |
76a50749 UD |
168 | exit (1); |
169 | } | |
170 | ||
171 | if (pthread_rwlock_destroy (&r) != 0) | |
172 | { | |
1472a752 | 173 | printf ("round %Zu: rwlock_destroy failed\n", cnt); |
76a50749 UD |
174 | exit (1); |
175 | } | |
176 | } | |
177 | ||
178 | return 0; | |
179 | } | |
180 | ||
181 | #define TEST_FUNCTION do_test () | |
182 | #include "../test-skeleton.c" |