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 | |
40 | |
41 | struct clparam |
42 | { |
43 | const struct aiocb *const *list; |
44 | struct waitlist *waitlist; |
45 | struct requestlist **requestlist; |
46 | #ifndef DONT_NEED_AIO_MISC_COND |
47 | pthread_cond_t *cond; |
48 | #endif |
49 | int nent; |
50 | }; |
51 | |
52 | |
53 | static void |
54 | cleanup (void *arg) |
55 | { |
56 | #ifdef DONT_NEED_AIO_MISC_COND |
57 | /* Acquire the mutex. If pthread_cond_*wait is used this would |
58 | happen implicitly. */ |
59 | pthread_mutex_lock (&__aio_requests_mutex); |
60 | #endif |
61 | |
62 | const struct clparam *param = (const struct clparam *) arg; |
63 | |
64 | /* Now remove the entry in the waiting list for all requests |
65 | which didn't terminate. */ |
66 | int cnt = param->nent; |
67 | while (cnt-- > 0) |
68 | if (param->list[cnt] != NULL |
69 | && param->list[cnt]->__error_code == EINPROGRESS) |
70 | { |
71 | struct waitlist **listp; |
72 | |
73 | assert (param->requestlist[cnt] != NULL); |
74 | |
75 | /* There is the chance that we cannot find our entry anymore. This |
76 | could happen if the request terminated and restarted again. */ |
77 | listp = ¶m->requestlist[cnt]->waiting; |
78 | while (*listp != NULL && *listp != ¶m->waitlist[cnt]) |
79 | listp = &(*listp)->next; |
80 | |
81 | if (*listp != NULL) |
82 | *listp = (*listp)->next; |
83 | } |
84 | |
85 | #ifndef DONT_NEED_AIO_MISC_COND |
86 | /* Release the conditional variable. */ |
87 | (void) pthread_cond_destroy (param->cond); |
88 | #endif |
89 | |
90 | /* Release the mutex. */ |
91 | pthread_mutex_unlock (&__aio_requests_mutex); |
92 | } |
93 | |
94 | #ifdef DONT_NEED_AIO_MISC_COND |
95 | static int |
96 | __attribute__ ((noinline)) |
97 | do_aio_misc_wait (unsigned int *cntr, const struct __timespec64 *timeout) |
98 | { |
99 | int result = 0; |
100 | |
101 | AIO_MISC_WAIT (result, *cntr, timeout, 1); |
102 | |
103 | return result; |
104 | } |
105 | #endif |
106 | |
107 | int |
108 | __aio_suspend_time64 (const struct aiocb *const list[], int nent, |
109 | const struct __timespec64 *timeout) |
110 | { |
111 | if (__glibc_unlikely (nent < 0)) |
112 | { |
113 | __set_errno (EINVAL); |
114 | return -1; |
115 | } |
116 | |
117 | struct waitlist waitlist[nent]; |
118 | struct requestlist *requestlist[nent]; |
119 | #ifndef DONT_NEED_AIO_MISC_COND |
120 | pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
121 | #endif |
122 | int cnt; |
123 | bool any = false; |
124 | int result = 0; |
125 | unsigned int cntr = 1; |
126 | |
127 | /* Request the mutex. */ |
128 | pthread_mutex_lock (&__aio_requests_mutex); |
129 | |
130 | /* There is not yet a finished request. Signal the request that |
131 | we are working for it. */ |
132 | for (cnt = 0; cnt < nent; ++cnt) |
133 | if (list[cnt] != NULL) |
134 | { |
135 | if (list[cnt]->__error_code == EINPROGRESS) |
136 | { |
137 | requestlist[cnt] = __aio_find_req ((aiocb_union *) list[cnt]); |
138 | |
139 | if (requestlist[cnt] != NULL) |
140 | { |
141 | #ifndef DONT_NEED_AIO_MISC_COND |
142 | waitlist[cnt].cond = &cond; |
143 | #endif |
144 | waitlist[cnt].result = NULL; |
145 | waitlist[cnt].next = requestlist[cnt]->waiting; |
146 | waitlist[cnt].counterp = &cntr; |
147 | waitlist[cnt].sigevp = NULL; |
148 | requestlist[cnt]->waiting = &waitlist[cnt]; |
149 | any = true; |
150 | } |
151 | else |
152 | /* We will never suspend. */ |
153 | break; |
154 | } |
155 | else |
156 | /* We will never suspend. */ |
157 | break; |
158 | } |
159 | |
160 | struct __timespec64 ts; |
161 | if (timeout != NULL) |
162 | { |
163 | __clock_gettime64 (CLOCK_MONOTONIC, &ts); |
164 | ts.tv_sec += timeout->tv_sec; |
165 | ts.tv_nsec += timeout->tv_nsec; |
166 | if (ts.tv_nsec >= 1000000000) |
167 | { |
168 | ts.tv_nsec -= 1000000000; |
169 | ts.tv_sec++; |
170 | } |
171 | } |
172 | |
173 | /* Only if none of the entries is NULL or finished to be wait. */ |
174 | if (cnt == nent && any) |
175 | { |
176 | struct clparam clparam = |
177 | { |
178 | .list = list, |
179 | .waitlist = waitlist, |
180 | .requestlist = requestlist, |
181 | #ifndef DONT_NEED_AIO_MISC_COND |
182 | .cond = &cond, |
183 | #endif |
184 | .nent = nent |
185 | }; |
186 | |
187 | pthread_cleanup_push (cleanup, &clparam); |
188 | |
189 | #ifdef DONT_NEED_AIO_MISC_COND |
190 | result = do_aio_misc_wait (&cntr, timeout == NULL ? NULL : &ts); |
191 | #else |
192 | struct timespec ts32 = valid_timespec64_to_timespec (ts); |
193 | result = pthread_cond_timedwait (&cond, &__aio_requests_mutex, |
194 | timeout == NULL ? NULL : &ts32); |
195 | #endif |
196 | |
197 | pthread_cleanup_pop (0); |
198 | } |
199 | |
200 | /* Now remove the entry in the waiting list for all requests |
201 | which didn't terminate. */ |
202 | while (cnt-- > 0) |
203 | if (list[cnt] != NULL && list[cnt]->__error_code == EINPROGRESS) |
204 | { |
205 | struct waitlist **listp; |
206 | |
207 | assert (requestlist[cnt] != NULL); |
208 | |
209 | /* There is the chance that we cannot find our entry anymore. This |
210 | could happen if the request terminated and restarted again. */ |
211 | listp = &requestlist[cnt]->waiting; |
212 | while (*listp != NULL && *listp != &waitlist[cnt]) |
213 | listp = &(*listp)->next; |
214 | |
215 | if (*listp != NULL) |
216 | *listp = (*listp)->next; |
217 | } |
218 | |
219 | #ifndef DONT_NEED_AIO_MISC_COND |
220 | /* Release the conditional variable. */ |
221 | if (__glibc_unlikely (pthread_cond_destroy (&cond) != 0)) |
222 | /* This must never happen. */ |
223 | abort (); |
224 | #endif |
225 | |
226 | if (result != 0) |
227 | { |
228 | #ifndef DONT_NEED_AIO_MISC_COND |
229 | /* An error occurred. Possibly it's ETIMEDOUT. We have to translate |
230 | the timeout error report of `pthread_cond_timedwait' to the |
231 | form expected from `aio_suspend'. */ |
232 | if (result == ETIMEDOUT) |
233 | __set_errno (EAGAIN); |
234 | else |
235 | #endif |
236 | __set_errno (result); |
237 | |
238 | result = -1; |
239 | } |
240 | |
241 | /* Release the mutex. */ |
242 | pthread_mutex_unlock (&__aio_requests_mutex); |
243 | |
244 | return result; |
245 | } |
246 | |
247 | #if __TIMESIZE != 64 |
248 | librt_hidden_def (__aio_suspend_time64) |
249 | |
250 | int |
251 | __aio_suspend (const struct aiocb *const list[], int nent, |
252 | const struct timespec *timeout) |
253 | { |
254 | struct __timespec64 ts64; |
255 | |
256 | if (timeout != NULL) |
257 | ts64 = valid_timespec_to_timespec64 (*timeout); |
258 | |
259 | return __aio_suspend_time64 (list, nent, timeout != NULL ? &ts64 : NULL); |
260 | } |
261 | #endif |
262 | weak_alias (__aio_suspend, aio_suspend) |
263 | weak_alias (aio_suspend, aio_suspend64) |
264 | |