* sysdeps/i386/__longjmp.S [PTR_DEMANGLE]: Also demangle stack
[glibc.git] / sysdeps / pthread / aio_suspend.c
blob8f8e33a7dc29345264a498291f1a20e58a1ee44a
1 /* Suspend until termination of a requests.
2 Copyright (C) 1997,1998,1999,2000,2002,2003,2005
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
23 /* We use an UGLY hack to prevent gcc from finding us cheating. The
24 implementations of aio_suspend and aio_suspend64 are identical and so
25 we want to avoid code duplication by using aliases. But gcc sees
26 the different parameter lists and prints a warning. We define here
27 a function so that aio_suspend64 has no prototype. */
28 #define aio_suspend64 XXX
29 #include <aio.h>
30 /* And undo the hack. */
31 #undef aio_suspend64
33 #include <assert.h>
34 #include <errno.h>
35 #include <stdbool.h>
36 #include <stdlib.h>
37 #include <sys/time.h>
39 #include <bits/libc-lock.h>
40 #include <aio_misc.h>
43 struct clparam
45 const struct aiocb *const *list;
46 struct waitlist *waitlist;
47 struct requestlist **requestlist;
48 pthread_cond_t *cond;
49 int nent;
53 static void
54 cleanup (void *arg)
56 const struct clparam *param = (const struct clparam *) arg;
58 /* Now remove the entry in the waiting list for all requests
59 which didn't terminate. */
60 int cnt = param->nent;
61 while (cnt-- > 0)
62 if (param->list[cnt] != NULL
63 && param->list[cnt]->__error_code == EINPROGRESS)
65 struct waitlist **listp;
67 assert (param->requestlist[cnt] != NULL);
69 /* There is the chance that we cannot find our entry anymore. This
70 could happen if the request terminated and restarted again. */
71 listp = &param->requestlist[cnt]->waiting;
72 while (*listp != NULL && *listp != &param->waitlist[cnt])
73 listp = &(*listp)->next;
75 if (*listp != NULL)
76 *listp = (*listp)->next;
79 /* Release the conditional variable. */
80 (void) pthread_cond_destroy (param->cond);
82 /* Release the mutex. */
83 pthread_mutex_unlock (&__aio_requests_mutex);
87 int
88 aio_suspend (list, nent, timeout)
89 const struct aiocb *const list[];
90 int nent;
91 const struct timespec *timeout;
93 struct waitlist waitlist[nent];
94 struct requestlist *requestlist[nent];
95 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
96 int cnt;
97 bool any = false;
98 int result = 0;
99 int dummy;
101 /* Request the mutex. */
102 pthread_mutex_lock (&__aio_requests_mutex);
104 /* There is not yet a finished request. Signal the request that
105 we are working for it. */
106 for (cnt = 0; cnt < nent; ++cnt)
107 if (list[cnt] != NULL)
109 if (list[cnt]->__error_code == EINPROGRESS)
111 requestlist[cnt] = __aio_find_req ((aiocb_union *) list[cnt]);
113 if (requestlist[cnt] != NULL)
115 waitlist[cnt].cond = &cond;
116 waitlist[cnt].next = requestlist[cnt]->waiting;
117 waitlist[cnt].counterp = &dummy;
118 waitlist[cnt].sigevp = NULL;
119 #ifdef BROKEN_THREAD_SIGNALS
120 waitlist[cnt].caller_pid = 0; /* Not needed. */
121 #endif
122 requestlist[cnt]->waiting = &waitlist[cnt];
123 any = true;
125 else
126 /* We will never suspend. */
127 break;
129 else
130 /* We will never suspend. */
131 break;
135 /* Only if none of the entries is NULL or finished to be wait. */
136 if (cnt == nent && any)
138 struct clparam clparam =
140 .list = list,
141 .waitlist = waitlist,
142 .requestlist = requestlist,
143 .cond = &cond,
144 .nent = nent
147 pthread_cleanup_push (cleanup, &clparam);
149 if (timeout == NULL)
150 result = pthread_cond_wait (&cond, &__aio_requests_mutex);
151 else
153 /* We have to convert the relative timeout value into an
154 absolute time value with pthread_cond_timedwait expects. */
155 struct timeval now;
156 struct timespec abstime;
158 __gettimeofday (&now, NULL);
159 abstime.tv_nsec = timeout->tv_nsec + now.tv_usec * 1000;
160 abstime.tv_sec = timeout->tv_sec + now.tv_sec;
161 if (abstime.tv_nsec >= 1000000000)
163 abstime.tv_nsec -= 1000000000;
164 abstime.tv_sec += 1;
167 result = pthread_cond_timedwait (&cond, &__aio_requests_mutex,
168 &abstime);
171 pthread_cleanup_pop (0);
174 /* Now remove the entry in the waiting list for all requests
175 which didn't terminate. */
176 while (cnt-- > 0)
177 if (list[cnt] != NULL && list[cnt]->__error_code == EINPROGRESS)
179 struct waitlist **listp;
181 assert (requestlist[cnt] != NULL);
183 /* There is the chance that we cannot find our entry anymore. This
184 could happen if the request terminated and restarted again. */
185 listp = &requestlist[cnt]->waiting;
186 while (*listp != NULL && *listp != &waitlist[cnt])
187 listp = &(*listp)->next;
189 if (*listp != NULL)
190 *listp = (*listp)->next;
193 /* Release the conditional variable. */
194 if (__builtin_expect (pthread_cond_destroy (&cond) != 0, 0))
195 /* This must never happen. */
196 abort ();
198 if (result != 0)
200 /* An error occurred. Possibly it's EINTR. We have to translate
201 the timeout error report of `pthread_cond_timedwait' to the
202 form expected from `aio_suspend'. */
203 if (result == ETIMEDOUT)
204 __set_errno (EAGAIN);
205 else
206 __set_errno (result);
208 result = -1;
211 /* Release the mutex. */
212 pthread_mutex_unlock (&__aio_requests_mutex);
214 return result;
217 weak_alias (aio_suspend, aio_suspend64)