* sysdeps/s390/s390-64/dl-trampoline.S (_dl_runtime_profile): Use the
[glibc/pb-stable.git] / sysdeps / pthread / aio_suspend.c
blobb85b16d10e3868f010642a09647e497b21d3048a
1 /* Suspend until termination of a requests.
2 Copyright (C) 1997-2000,2002,2003,2005,2006 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
22 /* We use an UGLY hack to prevent gcc from finding us cheating. The
23 implementations of aio_suspend and aio_suspend64 are identical and so
24 we want to avoid code duplication by using aliases. But gcc sees
25 the different parameter lists and prints a warning. We define here
26 a function so that aio_suspend64 has no prototype. */
27 #define aio_suspend64 XXX
28 #include <aio.h>
29 /* And undo the hack. */
30 #undef aio_suspend64
32 #include <assert.h>
33 #include <errno.h>
34 #include <stdbool.h>
35 #include <stdlib.h>
36 #include <sys/time.h>
38 #include <bits/libc-lock.h>
39 #include <aio_misc.h>
42 struct clparam
44 const struct aiocb *const *list;
45 struct waitlist *waitlist;
46 struct requestlist **requestlist;
47 #ifndef DONT_NEED_AIO_MISC_COND
48 pthread_cond_t *cond;
49 #endif
50 int nent;
54 static void
55 cleanup (void *arg)
57 #ifdef DONT_NEED_AIO_MISC_COND
58 /* Acquire the mutex. If pthread_cond_*wait is used this would
59 happen implicitly. */
60 pthread_mutex_lock (&__aio_requests_mutex);
61 #endif
63 const struct clparam *param = (const struct clparam *) arg;
65 /* Now remove the entry in the waiting list for all requests
66 which didn't terminate. */
67 int cnt = param->nent;
68 while (cnt-- > 0)
69 if (param->list[cnt] != NULL
70 && param->list[cnt]->__error_code == EINPROGRESS)
72 struct waitlist **listp;
74 assert (param->requestlist[cnt] != NULL);
76 /* There is the chance that we cannot find our entry anymore. This
77 could happen if the request terminated and restarted again. */
78 listp = &param->requestlist[cnt]->waiting;
79 while (*listp != NULL && *listp != &param->waitlist[cnt])
80 listp = &(*listp)->next;
82 if (*listp != NULL)
83 *listp = (*listp)->next;
86 #ifndef DONT_NEED_AIO_MISC_COND
87 /* Release the conditional variable. */
88 (void) pthread_cond_destroy (param->cond);
89 #endif
91 /* Release the mutex. */
92 pthread_mutex_unlock (&__aio_requests_mutex);
96 int
97 aio_suspend (list, nent, timeout)
98 const struct aiocb *const list[];
99 int nent;
100 const struct timespec *timeout;
102 if (__builtin_expect (nent < 0, 0))
104 __set_errno (EINVAL);
105 return -1;
108 struct waitlist waitlist[nent];
109 struct requestlist *requestlist[nent];
110 #ifndef DONT_NEED_AIO_MISC_COND
111 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
112 #endif
113 int cnt;
114 bool any = false;
115 int result = 0;
116 int cntr = 1;
118 /* Request the mutex. */
119 pthread_mutex_lock (&__aio_requests_mutex);
121 /* There is not yet a finished request. Signal the request that
122 we are working for it. */
123 for (cnt = 0; cnt < nent; ++cnt)
124 if (list[cnt] != NULL)
126 if (list[cnt]->__error_code == EINPROGRESS)
128 requestlist[cnt] = __aio_find_req ((aiocb_union *) list[cnt]);
130 if (requestlist[cnt] != NULL)
132 #ifndef DONT_NEED_AIO_MISC_COND
133 waitlist[cnt].cond = &cond;
134 #endif
135 waitlist[cnt].result = NULL;
136 waitlist[cnt].next = requestlist[cnt]->waiting;
137 waitlist[cnt].counterp = &cntr;
138 waitlist[cnt].sigevp = NULL;
139 #ifdef BROKEN_THREAD_SIGNALS
140 waitlist[cnt].caller_pid = 0; /* Not needed. */
141 #endif
142 requestlist[cnt]->waiting = &waitlist[cnt];
143 any = true;
145 else
146 /* We will never suspend. */
147 break;
149 else
150 /* We will never suspend. */
151 break;
155 /* Only if none of the entries is NULL or finished to be wait. */
156 if (cnt == nent && any)
158 struct clparam clparam =
160 .list = list,
161 .waitlist = waitlist,
162 .requestlist = requestlist,
163 #ifndef DONT_NEED_AIO_MISC_COND
164 .cond = &cond,
165 #endif
166 .nent = nent
169 pthread_cleanup_push (cleanup, &clparam);
171 #ifdef DONT_NEED_AIO_MISC_COND
172 AIO_MISC_WAIT (result, cntr, timeout, 1);
173 #else
174 if (timeout == NULL)
175 result = pthread_cond_wait (&cond, &__aio_requests_mutex);
176 else
178 /* We have to convert the relative timeout value into an
179 absolute time value with pthread_cond_timedwait expects. */
180 struct timeval now;
181 struct timespec abstime;
183 __gettimeofday (&now, NULL);
184 abstime.tv_nsec = timeout->tv_nsec + now.tv_usec * 1000;
185 abstime.tv_sec = timeout->tv_sec + now.tv_sec;
186 if (abstime.tv_nsec >= 1000000000)
188 abstime.tv_nsec -= 1000000000;
189 abstime.tv_sec += 1;
192 result = pthread_cond_timedwait (&cond, &__aio_requests_mutex,
193 &abstime);
195 #endif
197 pthread_cleanup_pop (0);
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)
205 struct waitlist **listp;
207 assert (requestlist[cnt] != NULL);
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;
215 if (*listp != NULL)
216 *listp = (*listp)->next;
219 #ifndef DONT_NEED_AIO_MISC_COND
220 /* Release the conditional variable. */
221 if (__builtin_expect (pthread_cond_destroy (&cond) != 0, 0))
222 /* This must never happen. */
223 abort ();
224 #endif
226 if (result != 0)
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);
238 result = -1;
241 /* Release the mutex. */
242 pthread_mutex_unlock (&__aio_requests_mutex);
244 return result;
247 weak_alias (aio_suspend, aio_suspend64)