1 | /* Suspend until termination of a requests. |
2 | Copyright (C) 1997-2021 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997. |
5 | |
6 | The GNU C Library is free software; you can redistribute it and/or |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either |
9 | version 2.1 of the License, or (at your option) any later version. |
10 | |
11 | The GNU C Library is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | Lesser General Public License for more details. |
15 | |
16 | You should have received a copy of the GNU Lesser General Public |
17 | License along with the GNU C Library; if not, see |
18 | <https://www.gnu.org/licenses/>. */ |
19 | |
20 | |
21 | /* We use an UGLY hack to prevent gcc from finding us cheating. The |
22 | implementations of aio_suspend and aio_suspend64 are identical and so |
23 | we want to avoid code duplication by using aliases. But gcc sees |
24 | the different parameter lists and prints a warning. We define here |
25 | a function so that aio_suspend64 has no prototype. */ |
26 | #define aio_suspend64 XXX |
27 | #include <aio.h> |
28 | /* And undo the hack. */ |
29 | #undef aio_suspend64 |
30 | |
31 | #include <assert.h> |
32 | #include <errno.h> |
33 | #include <stdbool.h> |
34 | #include <stdlib.h> |
35 | #include <sys/time.h> |
36 | |
37 | #include <libc-lock.h> |
38 | #include <aio_misc.h> |
39 | #include <pthreadP.h> |
40 | #include <shlib-compat.h> |
41 | |
42 | |
43 | struct clparam |
44 | { |
45 | const struct aiocb *const *list; |
46 | struct waitlist *waitlist; |
47 | struct requestlist **requestlist; |
48 | #ifndef DONT_NEED_AIO_MISC_COND |
49 | pthread_cond_t *cond; |
50 | #endif |
51 | int nent; |
52 | }; |
53 | |
54 | |
55 | static void |
56 | cleanup (void *arg) |
57 | { |
58 | #ifdef DONT_NEED_AIO_MISC_COND |
59 | /* Acquire the mutex. If pthread_cond_*wait is used this would |
60 | happen implicitly. */ |
61 | __pthread_mutex_lock (&__aio_requests_mutex); |
62 | #endif |
63 | |
64 | const struct clparam *param = (const struct clparam *) arg; |
65 | |
66 | /* Now remove the entry in the waiting list for all requests |
67 | which didn't terminate. */ |
68 | int cnt = param->nent; |
69 | while (cnt-- > 0) |
70 | if (param->list[cnt] != NULL |
71 | && param->list[cnt]->__error_code == EINPROGRESS) |
72 | { |
73 | struct waitlist **listp; |
74 | |
75 | assert (param->requestlist[cnt] != NULL); |
76 | |
77 | /* There is the chance that we cannot find our entry anymore. This |
78 | could happen if the request terminated and restarted again. */ |
79 | listp = ¶m->requestlist[cnt]->waiting; |
80 | while (*listp != NULL && *listp != ¶m->waitlist[cnt]) |
81 | listp = &(*listp)->next; |
82 | |
83 | if (*listp != NULL) |
84 | *listp = (*listp)->next; |
85 | } |
86 | |
87 | #ifndef DONT_NEED_AIO_MISC_COND |
88 | /* Release the conditional variable. */ |
89 | (void) pthread_cond_destroy (param->cond); |
90 | #endif |
91 | |
92 | /* Release the mutex. */ |
93 | __pthread_mutex_unlock (&__aio_requests_mutex); |
94 | } |
95 | |
96 | #ifdef DONT_NEED_AIO_MISC_COND |
97 | static int |
98 | __attribute__ ((noinline)) |
99 | do_aio_misc_wait (unsigned int *cntr, const struct __timespec64 *timeout) |
100 | { |
101 | int result = 0; |
102 | |
103 | AIO_MISC_WAIT (result, *cntr, timeout, 1); |
104 | |
105 | return result; |
106 | } |
107 | #endif |
108 | |
109 | int |
110 | ___aio_suspend_time64 (const struct aiocb *const list[], int nent, |
111 | const struct __timespec64 *timeout) |
112 | { |
113 | if (__glibc_unlikely (nent < 0)) |
114 | { |
115 | __set_errno (EINVAL); |
116 | return -1; |
117 | } |
118 | |
119 | struct waitlist waitlist[nent]; |
120 | struct requestlist *requestlist[nent]; |
121 | #ifndef DONT_NEED_AIO_MISC_COND |
122 | pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
123 | #endif |
124 | int cnt; |
125 | bool any = false; |
126 | int result = 0; |
127 | unsigned int cntr = 1; |
128 | |
129 | /* Request the mutex. */ |
130 | __pthread_mutex_lock (&__aio_requests_mutex); |
131 | |
132 | /* There is not yet a finished request. Signal the request that |
133 | we are working for it. */ |
134 | for (cnt = 0; cnt < nent; ++cnt) |
135 | if (list[cnt] != NULL) |
136 | { |
137 | if (list[cnt]->__error_code == EINPROGRESS) |
138 | { |
139 | requestlist[cnt] = __aio_find_req ((aiocb_union *) list[cnt]); |
140 | |
141 | if (requestlist[cnt] != NULL) |
142 | { |
143 | #ifndef DONT_NEED_AIO_MISC_COND |
144 | waitlist[cnt].cond = &cond; |
145 | #endif |
146 | waitlist[cnt].result = NULL; |
147 | waitlist[cnt].next = requestlist[cnt]->waiting; |
148 | waitlist[cnt].counterp = &cntr; |
149 | waitlist[cnt].sigevp = NULL; |
150 | requestlist[cnt]->waiting = &waitlist[cnt]; |
151 | any = true; |
152 | } |
153 | else |
154 | /* We will never suspend. */ |
155 | break; |
156 | } |
157 | else |
158 | /* We will never suspend. */ |
159 | break; |
160 | } |
161 | |
162 | struct __timespec64 ts; |
163 | if (timeout != NULL) |
164 | { |
165 | __clock_gettime64 (CLOCK_MONOTONIC, &ts); |
166 | ts.tv_sec += timeout->tv_sec; |
167 | ts.tv_nsec += timeout->tv_nsec; |
168 | if (ts.tv_nsec >= 1000000000) |
169 | { |
170 | ts.tv_nsec -= 1000000000; |
171 | ts.tv_sec++; |
172 | } |
173 | } |
174 | |
175 | /* Only if none of the entries is NULL or finished to be wait. */ |
176 | if (cnt == nent && any) |
177 | { |
178 | struct clparam clparam = |
179 | { |
180 | .list = list, |
181 | .waitlist = waitlist, |
182 | .requestlist = requestlist, |
183 | #ifndef DONT_NEED_AIO_MISC_COND |
184 | .cond = &cond, |
185 | #endif |
186 | .nent = nent |
187 | }; |
188 | |
189 | #if PTHREAD_IN_LIBC |
190 | __libc_cleanup_region_start (1, cleanup, &clparam); |
191 | #else |
192 | __pthread_cleanup_push (cleanup, &clparam); |
193 | #endif |
194 | |
195 | #ifdef DONT_NEED_AIO_MISC_COND |
196 | result = do_aio_misc_wait (&cntr, timeout == NULL ? NULL : &ts); |
197 | #else |
198 | struct timespec ts32 = valid_timespec64_to_timespec (ts); |
199 | result = pthread_cond_timedwait (&cond, &__aio_requests_mutex, |
200 | timeout == NULL ? NULL : &ts32); |
201 | #endif |
202 | |
203 | #if PTHREAD_IN_LIBC |
204 | __libc_cleanup_region_end (0); |
205 | #else |
206 | pthread_cleanup_pop (0); |
207 | #endif |
208 | } |
209 | |
210 | /* Now remove the entry in the waiting list for all requests |
211 | which didn't terminate. */ |
212 | while (cnt-- > 0) |
213 | if (list[cnt] != NULL && list[cnt]->__error_code == EINPROGRESS) |
214 | { |
215 | struct waitlist **listp; |
216 | |
217 | assert (requestlist[cnt] != NULL); |
218 | |
219 | /* There is the chance that we cannot find our entry anymore. This |
220 | could happen if the request terminated and restarted again. */ |
221 | listp = &requestlist[cnt]->waiting; |
222 | while (*listp != NULL && *listp != &waitlist[cnt]) |
223 | listp = &(*listp)->next; |
224 | |
225 | if (*listp != NULL) |
226 | *listp = (*listp)->next; |
227 | } |
228 | |
229 | #ifndef DONT_NEED_AIO_MISC_COND |
230 | /* Release the conditional variable. */ |
231 | if (__glibc_unlikely (pthread_cond_destroy (&cond) != 0)) |
232 | /* This must never happen. */ |
233 | abort (); |
234 | #endif |
235 | |
236 | if (result != 0) |
237 | { |
238 | #ifndef DONT_NEED_AIO_MISC_COND |
239 | /* An error occurred. Possibly it's ETIMEDOUT. We have to translate |
240 | the timeout error report of `pthread_cond_timedwait' to the |
241 | form expected from `aio_suspend'. */ |
242 | if (result == ETIMEDOUT) |
243 | __set_errno (EAGAIN); |
244 | else |
245 | #endif |
246 | __set_errno (result); |
247 | |
248 | result = -1; |
249 | } |
250 | |
251 | /* Release the mutex. */ |
252 | __pthread_mutex_unlock (&__aio_requests_mutex); |
253 | |
254 | return result; |
255 | } |
256 | |
257 | #if __TIMESIZE == 64 |
258 | strong_alias (___aio_suspend_time64, __aio_suspend) |
259 | #else /* __TIMESIZE != 64 */ |
260 | # if PTHREAD_IN_LIBC |
261 | libc_hidden_ver (___aio_suspend_time64, __aio_suspend_time64) |
262 | /* The conditional is slightly wrong: PTHREAD_IN_LIBC is a stand-in |
263 | for whether time64 support is needed. */ |
264 | versioned_symbol (libc, ___aio_suspend_time64, __aio_suspend_time64, GLIBC_2_34); |
265 | # else |
266 | librt_hidden_ver (___aio_suspend_time64, __aio_suspend_time64) |
267 | # endif |
268 | |
269 | int |
270 | __aio_suspend (const struct aiocb *const list[], int nent, |
271 | const struct timespec *timeout) |
272 | { |
273 | struct __timespec64 ts64; |
274 | |
275 | if (timeout != NULL) |
276 | ts64 = valid_timespec_to_timespec64 (*timeout); |
277 | |
278 | return __aio_suspend_time64 (list, nent, timeout != NULL ? &ts64 : NULL); |
279 | } |
280 | #endif /* __TIMESPEC64 != 64 */ |
281 | |
282 | #if PTHREAD_IN_LIBC |
283 | versioned_symbol (libc, __aio_suspend, aio_suspend, GLIBC_2_34); |
284 | versioned_symbol (libc, __aio_suspend, aio_suspend64, GLIBC_2_34); |
285 | # if OTHER_SHLIB_COMPAT (librt, GLIBC_2_1, GLIBC_2_34) |
286 | compat_symbol (librt, __aio_suspend, aio_suspend, GLIBC_2_1); |
287 | compat_symbol (librt, __aio_suspend, aio_suspend64, GLIBC_2_1); |
288 | # endif |
289 | #else /* !PTHREAD_IN_LIBC */ |
290 | weak_alias (__aio_suspend, aio_suspend) |
291 | weak_alias (__aio_suspend, aio_suspend64) |
292 | #endif /* !PTHREAD_IN_LIBC */ |
293 | |