Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / pthread / aio_suspend.c
blobb8f8a5b22a156b6ef9312e8ee037f2fad3cd8e96
1 /* Suspend until termination of a requests.
2 Copyright (C) 1997-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
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.
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.
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 <http://www.gnu.org/licenses/>. */
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
31 #include <assert.h>
32 #include <errno.h>
33 #include <stdbool.h>
34 #include <stdlib.h>
35 #include <sys/time.h>
37 #include <bits/libc-lock.h>
38 #include <aio_misc.h>
41 struct clparam
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;
53 static void
54 cleanup (void *arg)
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
62 const struct clparam *param = (const struct clparam *) arg;
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)
71 struct waitlist **listp;
73 assert (param->requestlist[cnt] != NULL);
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 = &param->requestlist[cnt]->waiting;
78 while (*listp != NULL && *listp != &param->waitlist[cnt])
79 listp = &(*listp)->next;
81 if (*listp != NULL)
82 *listp = (*listp)->next;
85 #ifndef DONT_NEED_AIO_MISC_COND
86 /* Release the conditional variable. */
87 (void) pthread_cond_destroy (param->cond);
88 #endif
90 /* Release the mutex. */
91 pthread_mutex_unlock (&__aio_requests_mutex);
94 #ifdef DONT_NEED_AIO_MISC_COND
95 static int
96 __attribute__ ((noinline))
97 do_aio_misc_wait(int *cntr, const struct timespec *timeout)
99 int result = 0;
101 AIO_MISC_WAIT(result, *cntr, timeout, 1);
103 return result;
105 #endif
108 aio_suspend (list, nent, timeout)
109 const struct aiocb *const list[];
110 int nent;
111 const struct timespec *timeout;
113 if (__builtin_expect (nent < 0, 0))
115 __set_errno (EINVAL);
116 return -1;
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 int cntr = 1;
129 /* Request the mutex. */
130 pthread_mutex_lock (&__aio_requests_mutex);
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)
137 if (list[cnt]->__error_code == EINPROGRESS)
139 requestlist[cnt] = __aio_find_req ((aiocb_union *) list[cnt]);
141 if (requestlist[cnt] != NULL)
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 #ifdef BROKEN_THREAD_SIGNALS
151 waitlist[cnt].caller_pid = 0; /* Not needed. */
152 #endif
153 requestlist[cnt]->waiting = &waitlist[cnt];
154 any = true;
156 else
157 /* We will never suspend. */
158 break;
160 else
161 /* We will never suspend. */
162 break;
166 /* Only if none of the entries is NULL or finished to be wait. */
167 if (cnt == nent && any)
169 struct clparam clparam =
171 .list = list,
172 .waitlist = waitlist,
173 .requestlist = requestlist,
174 #ifndef DONT_NEED_AIO_MISC_COND
175 .cond = &cond,
176 #endif
177 .nent = nent
180 pthread_cleanup_push (cleanup, &clparam);
182 #ifdef DONT_NEED_AIO_MISC_COND
183 result = do_aio_misc_wait(&cntr, timeout);
184 #else
185 if (timeout == NULL)
186 result = pthread_cond_wait (&cond, &__aio_requests_mutex);
187 else
189 /* We have to convert the relative timeout value into an
190 absolute time value with pthread_cond_timedwait expects. */
191 struct timeval now;
192 struct timespec abstime;
194 __gettimeofday (&now, NULL);
195 abstime.tv_nsec = timeout->tv_nsec + now.tv_usec * 1000;
196 abstime.tv_sec = timeout->tv_sec + now.tv_sec;
197 if (abstime.tv_nsec >= 1000000000)
199 abstime.tv_nsec -= 1000000000;
200 abstime.tv_sec += 1;
203 result = pthread_cond_timedwait (&cond, &__aio_requests_mutex,
204 &abstime);
206 #endif
208 pthread_cleanup_pop (0);
211 /* Now remove the entry in the waiting list for all requests
212 which didn't terminate. */
213 while (cnt-- > 0)
214 if (list[cnt] != NULL && list[cnt]->__error_code == EINPROGRESS)
216 struct waitlist **listp;
218 assert (requestlist[cnt] != NULL);
220 /* There is the chance that we cannot find our entry anymore. This
221 could happen if the request terminated and restarted again. */
222 listp = &requestlist[cnt]->waiting;
223 while (*listp != NULL && *listp != &waitlist[cnt])
224 listp = &(*listp)->next;
226 if (*listp != NULL)
227 *listp = (*listp)->next;
230 #ifndef DONT_NEED_AIO_MISC_COND
231 /* Release the conditional variable. */
232 if (__builtin_expect (pthread_cond_destroy (&cond) != 0, 0))
233 /* This must never happen. */
234 abort ();
235 #endif
237 if (result != 0)
239 #ifndef DONT_NEED_AIO_MISC_COND
240 /* An error occurred. Possibly it's ETIMEDOUT. We have to translate
241 the timeout error report of `pthread_cond_timedwait' to the
242 form expected from `aio_suspend'. */
243 if (result == ETIMEDOUT)
244 __set_errno (EAGAIN);
245 else
246 #endif
247 __set_errno (result);
249 result = -1;
252 /* Release the mutex. */
253 pthread_mutex_unlock (&__aio_requests_mutex);
255 return result;
258 weak_alias (aio_suspend, aio_suspend64)