1 | /* Copyright (C) 2002-2022 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
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 |
15 | License along with the GNU C Library; if not, see |
16 | <https://www.gnu.org/licenses/>. */ |
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> |
24 | #include <support/check.h> |
25 | #include <support/test-driver.h> |
26 | #include <support/timespec.h> |
27 | #include <support/xthread.h> |
28 | #include <support/xtime.h> |
29 | |
30 | |
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 | |
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 | |
43 | struct thread_args |
44 | { |
45 | pthread_rwlock_t *rwlock; |
46 | clockid_t clockid; |
47 | const char *fnname; |
48 | }; |
49 | |
50 | static void * |
51 | tf (void *arg) |
52 | { |
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; |
59 | |
60 | /* Timeout: 0.3 secs. */ |
61 | struct timespec ts_start; |
62 | xclock_gettime (clock: clockid_for_get, ts: &ts_start); |
63 | |
64 | struct timespec ts_timeout = timespec_add (ts_start, |
65 | make_timespec (s: 0, ns: 300000000)); |
66 | |
67 | verbose_printf ("child calling %srdlock\n" , fnname); |
68 | |
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); |
74 | |
75 | verbose_printf ("1st child %srdlock done\n" , fnname); |
76 | |
77 | TEST_TIMESPEC_NOW_OR_AFTER (CLOCK_REALTIME, ts_timeout); |
78 | |
79 | xclock_gettime (clock: clockid_for_get, ts: &ts_timeout); |
80 | ts_timeout.tv_sec += 10; |
81 | /* Note that the following operation makes ts invalid. */ |
82 | ts_timeout.tv_nsec += 1000000000; |
83 | |
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); |
88 | |
89 | verbose_printf ("2nd child %srdlock done\n" , fnname); |
90 | |
91 | return NULL; |
92 | } |
93 | |
94 | |
95 | static int |
96 | do_test_clock (clockid_t clockid, const char *fnname) |
97 | { |
98 | const clockid_t clockid_for_get = |
99 | (clockid == CLOCK_USE_TIMEDLOCK) ? CLOCK_REALTIME : clockid; |
100 | size_t cnt; |
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 (attr: &a) != 0) |
107 | FAIL_EXIT1 ("round %Zu: rwlockattr_t failed\n" , cnt); |
108 | |
109 | if (pthread_rwlockattr_setkind_np (attr: &a, pref: kind[cnt]) != 0) |
110 | FAIL_EXIT1 ("round %Zu: rwlockattr_setkind failed\n" , cnt); |
111 | |
112 | if (pthread_rwlock_init (rwlock: &r, attr: &a) != 0) |
113 | FAIL_EXIT1 ("round %Zu: rwlock_init failed\n" , cnt); |
114 | |
115 | if (pthread_rwlockattr_destroy (attr: &a) != 0) |
116 | FAIL_EXIT1 ("round %Zu: rwlockattr_destroy failed\n" , cnt); |
117 | |
118 | struct timespec ts; |
119 | xclock_gettime (clock: clockid_for_get, ts: &ts); |
120 | ++ts.tv_sec; |
121 | |
122 | /* Get a write lock. */ |
123 | int e = (clockid == CLOCK_USE_TIMEDLOCK) |
124 | ? pthread_rwlock_timedwrlock (rwlock: &r, abstime: &ts) |
125 | : pthread_rwlock_clockwrlock (rwlock: &r, clockid: clockid, abstime: &ts); |
126 | if (e != 0) |
127 | FAIL_EXIT1 ("round %Zu: %swrlock failed (%d)\n" , |
128 | cnt, fnname, e); |
129 | |
130 | verbose_printf ("1st %swrlock done\n" , fnname); |
131 | |
132 | xclock_gettime (clock: clockid_for_get, ts: &ts); |
133 | ++ts.tv_sec; |
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); |
138 | |
139 | verbose_printf ("1st %srdlock done\n" , fnname); |
140 | |
141 | xclock_gettime (clock: clockid_for_get, ts: &ts); |
142 | ++ts.tv_sec; |
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); |
147 | |
148 | verbose_printf ("2nd %swrlock done\n" , fnname); |
149 | |
150 | struct thread_args args; |
151 | args.rwlock = &r; |
152 | args.clockid = clockid; |
153 | args.fnname = fnname; |
154 | pthread_t th = xpthread_create (NULL, thread_func: tf, closure: &args); |
155 | |
156 | puts (s: "started thread" ); |
157 | |
158 | (void) xpthread_join (thr: th); |
159 | |
160 | puts (s: "joined thread" ); |
161 | |
162 | if (pthread_rwlock_destroy (rwlock: &r) != 0) |
163 | FAIL_EXIT1 ("round %Zu: rwlock_destroy failed\n" , cnt); |
164 | } |
165 | |
166 | return 0; |
167 | } |
168 | |
169 | static int do_test (void) |
170 | { |
171 | do_test_clock (CLOCK_USE_TIMEDLOCK, fnname: "timed" ); |
172 | do_test_clock (CLOCK_REALTIME, fnname: "clock(realtime)" ); |
173 | do_test_clock (CLOCK_MONOTONIC, fnname: "clock(monotonic)" ); |
174 | return 0; |
175 | } |
176 | |
177 | #include <support/test-driver.c> |
178 | |