]>
Commit | Line | Data |
---|---|---|
581c785b | 1 | /* Copyright (C) 2002-2022 Free Software Foundation, Inc. |
76a50749 | 2 | This file is part of the GNU C Library. |
76a50749 UD |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or | |
5 | modify it under the terms of the GNU Lesser General Public | |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
8 | ||
9 | The GNU C Library is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | Lesser General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
76a50749 UD |
17 | |
18 | #include <errno.h> | |
19 | #include <pthread.h> | |
20 | #include <stdlib.h> | |
21 | #include <stdio.h> | |
22 | #include <string.h> | |
23 | #include <sys/time.h> | |
495514ee | 24 | #include <support/check.h> |
e996fa72 | 25 | #include <support/test-driver.h> |
495514ee MC |
26 | #include <support/timespec.h> |
27 | #include <support/xthread.h> | |
28 | #include <support/xtime.h> | |
76a50749 UD |
29 | |
30 | ||
e996fa72 MC |
31 | /* A bogus clock value that tells run_test to use pthread_rwlock_timedrdlock |
32 | and pthread_rwlock_timedwrlock rather than pthread_rwlock_clockrdlock and | |
33 | pthread_rwlock_clockwrlock. */ | |
34 | #define CLOCK_USE_TIMEDLOCK (-1) | |
35 | ||
76a50749 UD |
36 | static int kind[] = |
37 | { | |
38 | PTHREAD_RWLOCK_PREFER_READER_NP, | |
39 | PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP, | |
40 | PTHREAD_RWLOCK_PREFER_WRITER_NP, | |
41 | }; | |
42 | ||
e996fa72 MC |
43 | struct thread_args |
44 | { | |
45 | pthread_rwlock_t *rwlock; | |
46 | clockid_t clockid; | |
47 | const char *fnname; | |
48 | }; | |
76a50749 UD |
49 | |
50 | static void * | |
51 | tf (void *arg) | |
52 | { | |
e996fa72 MC |
53 | struct thread_args *args = arg; |
54 | pthread_rwlock_t *r = args->rwlock; | |
55 | const clockid_t clockid = args->clockid; | |
56 | const clockid_t clockid_for_get = | |
57 | (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid; | |
58 | const char *fnname = args->fnname; | |
76a50749 UD |
59 | |
60 | /* Timeout: 0.3 secs. */ | |
495514ee | 61 | struct timespec ts_start; |
e996fa72 | 62 | xclock_gettime (clockid_for_get, &ts_start); |
76a50749 | 63 | |
495514ee MC |
64 | struct timespec ts_timeout = timespec_add (ts_start, |
65 | make_timespec (0, 300000000)); | |
76a50749 | 66 | |
e996fa72 | 67 | verbose_printf ("child calling %srdlock\n", fnname); |
51d0678c | 68 | |
e996fa72 MC |
69 | if (clockid == CLOCK_USE_TIMEDLOCK) |
70 | TEST_COMPARE (pthread_rwlock_timedrdlock (r, &ts_timeout), ETIMEDOUT); | |
71 | else | |
72 | TEST_COMPARE (pthread_rwlock_clockrdlock (r, clockid, &ts_timeout), | |
73 | ETIMEDOUT); | |
76a50749 | 74 | |
e996fa72 | 75 | verbose_printf ("1st child %srdlock done\n", fnname); |
51d0678c | 76 | |
495514ee | 77 | TEST_TIMESPEC_NOW_OR_AFTER (CLOCK_REALTIME, ts_timeout); |
76a50749 | 78 | |
e996fa72 | 79 | xclock_gettime (clockid_for_get, &ts_timeout); |
495514ee | 80 | ts_timeout.tv_sec += 10; |
0a37669a | 81 | /* Note that the following operation makes ts invalid. */ |
495514ee | 82 | ts_timeout.tv_nsec += 1000000000; |
0a37669a | 83 | |
e996fa72 MC |
84 | if (clockid == CLOCK_USE_TIMEDLOCK) |
85 | TEST_COMPARE (pthread_rwlock_timedrdlock (r, &ts_timeout), EINVAL); | |
86 | else | |
87 | TEST_COMPARE (pthread_rwlock_clockrdlock (r, clockid, &ts_timeout), EINVAL); | |
0a37669a | 88 | |
e996fa72 | 89 | verbose_printf ("2nd child %srdlock done\n", fnname); |
51d0678c | 90 | |
76a50749 UD |
91 | return NULL; |
92 | } | |
93 | ||
94 | ||
95 | static int | |
e996fa72 | 96 | do_test_clock (clockid_t clockid, const char *fnname) |
76a50749 | 97 | { |
e996fa72 MC |
98 | const clockid_t clockid_for_get = |
99 | (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid; | |
f9657e88 | 100 | size_t cnt; |
76a50749 UD |
101 | for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt) |
102 | { | |
103 | pthread_rwlock_t r; | |
104 | pthread_rwlockattr_t a; | |
105 | ||
106 | if (pthread_rwlockattr_init (&a) != 0) | |
de477abc | 107 | FAIL_EXIT1 ("round %zu: rwlockattr_t failed\n", cnt); |
76a50749 UD |
108 | |
109 | if (pthread_rwlockattr_setkind_np (&a, kind[cnt]) != 0) | |
de477abc | 110 | FAIL_EXIT1 ("round %zu: rwlockattr_setkind failed\n", cnt); |
76a50749 UD |
111 | |
112 | if (pthread_rwlock_init (&r, &a) != 0) | |
de477abc | 113 | FAIL_EXIT1 ("round %zu: rwlock_init failed\n", cnt); |
76a50749 UD |
114 | |
115 | if (pthread_rwlockattr_destroy (&a) != 0) | |
de477abc | 116 | FAIL_EXIT1 ("round %zu: rwlockattr_destroy failed\n", cnt); |
76a50749 UD |
117 | |
118 | struct timespec ts; | |
e996fa72 | 119 | xclock_gettime (clockid_for_get, &ts); |
76a50749 UD |
120 | ++ts.tv_sec; |
121 | ||
122 | /* Get a write lock. */ | |
e996fa72 MC |
123 | int e = (clockid == CLOCK_USE_TIMEDLOCK) |
124 | ? pthread_rwlock_timedwrlock (&r, &ts) | |
125 | : pthread_rwlock_clockwrlock (&r, clockid, &ts); | |
51d0678c | 126 | if (e != 0) |
de477abc | 127 | FAIL_EXIT1 ("round %zu: %swrlock failed (%d)\n", |
e996fa72 | 128 | cnt, fnname, e); |
76a50749 | 129 | |
e996fa72 | 130 | verbose_printf ("1st %swrlock done\n", fnname); |
51d0678c | 131 | |
e996fa72 | 132 | xclock_gettime (clockid_for_get, &ts); |
0a37669a | 133 | ++ts.tv_sec; |
e996fa72 MC |
134 | if (clockid == CLOCK_USE_TIMEDLOCK) |
135 | TEST_COMPARE (pthread_rwlock_timedrdlock (&r, &ts), EDEADLK); | |
136 | else | |
137 | TEST_COMPARE (pthread_rwlock_clockrdlock (&r, clockid, &ts), EDEADLK); | |
0a37669a | 138 | |
e996fa72 | 139 | verbose_printf ("1st %srdlock done\n", fnname); |
51d0678c | 140 | |
e996fa72 | 141 | xclock_gettime (clockid_for_get, &ts); |
0a37669a | 142 | ++ts.tv_sec; |
e996fa72 MC |
143 | if (clockid == CLOCK_USE_TIMEDLOCK) |
144 | TEST_COMPARE (pthread_rwlock_timedwrlock (&r, &ts), EDEADLK); | |
145 | else | |
146 | TEST_COMPARE (pthread_rwlock_clockwrlock (&r, clockid, &ts), EDEADLK); | |
0a37669a | 147 | |
e996fa72 | 148 | verbose_printf ("2nd %swrlock done\n", fnname); |
51d0678c | 149 | |
e996fa72 MC |
150 | struct thread_args args; |
151 | args.rwlock = &r; | |
152 | args.clockid = clockid; | |
153 | args.fnname = fnname; | |
154 | pthread_t th = xpthread_create (NULL, tf, &args); | |
76a50749 | 155 | |
51d0678c UD |
156 | puts ("started thread"); |
157 | ||
e996fa72 | 158 | (void) xpthread_join (th); |
76a50749 | 159 | |
51d0678c UD |
160 | puts ("joined thread"); |
161 | ||
76a50749 | 162 | if (pthread_rwlock_destroy (&r) != 0) |
de477abc | 163 | FAIL_EXIT1 ("round %zu: rwlock_destroy failed\n", cnt); |
76a50749 UD |
164 | } |
165 | ||
166 | return 0; | |
167 | } | |
168 | ||
e996fa72 MC |
169 | static int do_test (void) |
170 | { | |
171 | do_test_clock (CLOCK_USE_TIMEDLOCK, "timed"); | |
172 | do_test_clock (CLOCK_REALTIME, "clock(realtime)"); | |
173 | do_test_clock (CLOCK_MONOTONIC, "clock(monotonic)"); | |
174 | return 0; | |
175 | } | |
176 | ||
495514ee | 177 | #include <support/test-driver.c> |