]>
Commit | Line | Data |
---|---|---|
cbdee279 | 1 | /* Suspend until termination of a requests. |
f7a9f785 | 2 | Copyright (C) 1997-2016 Free Software Foundation, Inc. |
cbdee279 UD |
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 | |
41bdb6e2 AJ |
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. | |
cbdee279 UD |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
cbdee279 | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
cbdee279 UD |
19 | |
20 | ||
21 | /* We use an UGLY hack to prevent gcc from finding us cheating. The | |
f14811c7 | 22 | implementations of aio_suspend and aio_suspend64 are identical and so |
cbdee279 UD |
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 | ||
8b8cc76f | 31 | #include <assert.h> |
cbdee279 | 32 | #include <errno.h> |
c547b58a | 33 | #include <stdbool.h> |
3d8e9510 | 34 | #include <stdlib.h> |
f14811c7 | 35 | #include <sys/time.h> |
cbdee279 | 36 | |
ec999b8e | 37 | #include <libc-lock.h> |
8f480b4b | 38 | #include <aio_misc.h> |
cbdee279 UD |
39 | |
40 | ||
60d73a7a UD |
41 | struct clparam |
42 | { | |
43 | const struct aiocb *const *list; | |
44 | struct waitlist *waitlist; | |
45 | struct requestlist **requestlist; | |
679d83ba | 46 | #ifndef DONT_NEED_AIO_MISC_COND |
60d73a7a | 47 | pthread_cond_t *cond; |
679d83ba | 48 | #endif |
60d73a7a UD |
49 | int nent; |
50 | }; | |
51 | ||
52 | ||
53 | static void | |
54 | cleanup (void *arg) | |
55 | { | |
679d83ba UD |
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 | ||
60d73a7a UD |
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 | ||
679d83ba | 85 | #ifndef DONT_NEED_AIO_MISC_COND |
60d73a7a UD |
86 | /* Release the conditional variable. */ |
87 | (void) pthread_cond_destroy (param->cond); | |
679d83ba | 88 | #endif |
60d73a7a UD |
89 | |
90 | /* Release the mutex. */ | |
91 | pthread_mutex_unlock (&__aio_requests_mutex); | |
92 | } | |
93 | ||
22044b48 DM |
94 | #ifdef DONT_NEED_AIO_MISC_COND |
95 | static int | |
96 | __attribute__ ((noinline)) | |
86edd44f | 97 | do_aio_misc_wait (unsigned int *cntr, const struct timespec *timeout) |
22044b48 | 98 | { |
86edd44f | 99 | int result = 0; |
22044b48 | 100 | |
86edd44f | 101 | AIO_MISC_WAIT (result, *cntr, timeout, 1); |
22044b48 | 102 | |
86edd44f | 103 | return result; |
22044b48 DM |
104 | } |
105 | #endif | |
60d73a7a | 106 | |
cbdee279 | 107 | int |
9dd346ff JM |
108 | aio_suspend (const struct aiocb *const list[], int nent, |
109 | const struct timespec *timeout) | |
cbdee279 | 110 | { |
a1ffb40e | 111 | if (__glibc_unlikely (nent < 0)) |
679d83ba UD |
112 | { |
113 | __set_errno (EINVAL); | |
114 | return -1; | |
115 | } | |
116 | ||
d71b808a UD |
117 | struct waitlist waitlist[nent]; |
118 | struct requestlist *requestlist[nent]; | |
679d83ba | 119 | #ifndef DONT_NEED_AIO_MISC_COND |
18de8c73 | 120 | pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
679d83ba | 121 | #endif |
cbdee279 | 122 | int cnt; |
c547b58a | 123 | bool any = false; |
d71b808a | 124 | int result = 0; |
86edd44f | 125 | unsigned int cntr = 1; |
d71b808a UD |
126 | |
127 | /* Request the mutex. */ | |
128 | pthread_mutex_lock (&__aio_requests_mutex); | |
cbdee279 | 129 | |
b9337b6a UD |
130 | /* There is not yet a finished request. Signal the request that |
131 | we are working for it. */ | |
cbdee279 | 132 | for (cnt = 0; cnt < nent; ++cnt) |
8b8cc76f | 133 | if (list[cnt] != NULL) |
b9337b6a | 134 | { |
8b8cc76f | 135 | if (list[cnt]->__error_code == EINPROGRESS) |
b9337b6a | 136 | { |
8b8cc76f UD |
137 | requestlist[cnt] = __aio_find_req ((aiocb_union *) list[cnt]); |
138 | ||
139 | if (requestlist[cnt] != NULL) | |
140 | { | |
679d83ba | 141 | #ifndef DONT_NEED_AIO_MISC_COND |
8b8cc76f | 142 | waitlist[cnt].cond = &cond; |
679d83ba | 143 | #endif |
9759bbf1 | 144 | waitlist[cnt].result = NULL; |
8b8cc76f | 145 | waitlist[cnt].next = requestlist[cnt]->waiting; |
679d83ba | 146 | waitlist[cnt].counterp = &cntr; |
8b8cc76f | 147 | waitlist[cnt].sigevp = NULL; |
b61c8aba | 148 | #ifdef BROKEN_THREAD_SIGNALS |
8b8cc76f | 149 | waitlist[cnt].caller_pid = 0; /* Not needed. */ |
b61c8aba | 150 | #endif |
8b8cc76f | 151 | requestlist[cnt]->waiting = &waitlist[cnt]; |
c547b58a | 152 | any = true; |
8b8cc76f UD |
153 | } |
154 | else | |
155 | /* We will never suspend. */ | |
156 | break; | |
b9337b6a | 157 | } |
8b8cc76f UD |
158 | else |
159 | /* We will never suspend. */ | |
160 | break; | |
b9337b6a | 161 | } |
d71b808a | 162 | |
8ee87680 UD |
163 | |
164 | /* Only if none of the entries is NULL or finished to be wait. */ | |
c547b58a | 165 | if (cnt == nent && any) |
d71b808a | 166 | { |
60d73a7a UD |
167 | struct clparam clparam = |
168 | { | |
169 | .list = list, | |
170 | .waitlist = waitlist, | |
171 | .requestlist = requestlist, | |
679d83ba | 172 | #ifndef DONT_NEED_AIO_MISC_COND |
60d73a7a | 173 | .cond = &cond, |
679d83ba | 174 | #endif |
60d73a7a UD |
175 | .nent = nent |
176 | }; | |
d71b808a | 177 | |
67b78ef9 | 178 | pthread_cleanup_push (cleanup, &clparam); |
d71b808a | 179 | |
679d83ba | 180 | #ifdef DONT_NEED_AIO_MISC_COND |
86edd44f | 181 | result = do_aio_misc_wait (&cntr, timeout); |
679d83ba | 182 | #else |
8ee87680 UD |
183 | if (timeout == NULL) |
184 | result = pthread_cond_wait (&cond, &__aio_requests_mutex); | |
185 | else | |
f14811c7 | 186 | { |
8ee87680 UD |
187 | /* We have to convert the relative timeout value into an |
188 | absolute time value with pthread_cond_timedwait expects. */ | |
189 | struct timeval now; | |
190 | struct timespec abstime; | |
191 | ||
192 | __gettimeofday (&now, NULL); | |
193 | abstime.tv_nsec = timeout->tv_nsec + now.tv_usec * 1000; | |
194 | abstime.tv_sec = timeout->tv_sec + now.tv_sec; | |
195 | if (abstime.tv_nsec >= 1000000000) | |
f14811c7 | 196 | { |
8ee87680 UD |
197 | abstime.tv_nsec -= 1000000000; |
198 | abstime.tv_sec += 1; | |
f14811c7 | 199 | } |
8ee87680 UD |
200 | |
201 | result = pthread_cond_timedwait (&cond, &__aio_requests_mutex, | |
202 | &abstime); | |
f14811c7 | 203 | } |
679d83ba | 204 | #endif |
d71b808a | 205 | |
67b78ef9 | 206 | pthread_cleanup_pop (0); |
8ee87680 | 207 | } |
8b8cc76f | 208 | |
8ee87680 UD |
209 | /* Now remove the entry in the waiting list for all requests |
210 | which didn't terminate. */ | |
211 | while (cnt-- > 0) | |
212 | if (list[cnt] != NULL && list[cnt]->__error_code == EINPROGRESS) | |
213 | { | |
214 | struct waitlist **listp; | |
d71b808a | 215 | |
8ee87680 | 216 | assert (requestlist[cnt] != NULL); |
d71b808a | 217 | |
8ee87680 UD |
218 | /* There is the chance that we cannot find our entry anymore. This |
219 | could happen if the request terminated and restarted again. */ | |
220 | listp = &requestlist[cnt]->waiting; | |
221 | while (*listp != NULL && *listp != &waitlist[cnt]) | |
222 | listp = &(*listp)->next; | |
d71b808a | 223 | |
8ee87680 UD |
224 | if (*listp != NULL) |
225 | *listp = (*listp)->next; | |
226 | } | |
d71b808a | 227 | |
679d83ba | 228 | #ifndef DONT_NEED_AIO_MISC_COND |
8ee87680 | 229 | /* Release the conditional variable. */ |
a1ffb40e | 230 | if (__glibc_unlikely (pthread_cond_destroy (&cond) != 0)) |
8ee87680 UD |
231 | /* This must never happen. */ |
232 | abort (); | |
679d83ba | 233 | #endif |
b9337b6a | 234 | |
8ee87680 UD |
235 | if (result != 0) |
236 | { | |
679d83ba UD |
237 | #ifndef DONT_NEED_AIO_MISC_COND |
238 | /* An error occurred. Possibly it's ETIMEDOUT. We have to translate | |
8ee87680 UD |
239 | the timeout error report of `pthread_cond_timedwait' to the |
240 | form expected from `aio_suspend'. */ | |
241 | if (result == ETIMEDOUT) | |
242 | __set_errno (EAGAIN); | |
67b78ef9 | 243 | else |
679d83ba | 244 | #endif |
67b78ef9 | 245 | __set_errno (result); |
d71b808a | 246 | |
8ee87680 | 247 | result = -1; |
d71b808a | 248 | } |
cbdee279 | 249 | |
d71b808a UD |
250 | /* Release the mutex. */ |
251 | pthread_mutex_unlock (&__aio_requests_mutex); | |
cbdee279 | 252 | |
d71b808a | 253 | return result; |
cbdee279 UD |
254 | } |
255 | ||
256 | weak_alias (aio_suspend, aio_suspend64) |