Update.
[glibc.git] / rt / aio_suspend.c
blob6736857ba15782d90f4838db0d0cd48f374e853b
1 /* Suspend until termination of a requests.
2 Copyright (C) 1997, 1998, 1999 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* We use an UGLY hack to prevent gcc from finding us cheating. The
23 implementation 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 <errno.h>
34 #include "aio_misc.h"
37 int
38 aio_suspend (list, nent, timeout)
39 const struct aiocb *const list[];
40 int nent;
41 const struct timespec *timeout;
43 struct waitlist waitlist[nent];
44 struct requestlist *requestlist[nent];
45 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
46 int cnt;
47 int result = 0;
48 int dummy;
49 int none = 1;
51 /* Request the mutex. */
52 pthread_mutex_lock (&__aio_requests_mutex);
54 /* There is not yet a finished request. Signal the request that
55 we are working for it. */
56 for (cnt = 0; cnt < nent; ++cnt)
57 if (list[cnt] != NULL && list[cnt]->__error_code == EINPROGRESS)
59 requestlist[cnt] = __aio_find_req ((aiocb_union *) list[cnt]);
61 if (requestlist[cnt] != NULL)
63 waitlist[cnt].cond = &cond;
64 waitlist[cnt].next = requestlist[cnt]->waiting;
65 waitlist[cnt].counterp = &dummy;
66 waitlist[cnt].sigevp = NULL;
67 waitlist[cnt].caller_pid = 0; /* Not needed. */
68 requestlist[cnt]->waiting = &waitlist[cnt];
69 none = 0;
73 /* If there is a not finished request wait for it. */
74 if (!none)
76 int oldstate;
78 /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancelation
79 points we must be careful. We added entries to the waiting lists
80 which we must remove. So defer cancelation for now. */
81 pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
83 if (timeout == NULL)
84 result = pthread_cond_wait (&cond, &__aio_requests_mutex);
85 else
86 result = pthread_cond_timedwait (&cond, &__aio_requests_mutex,
87 timeout);
89 /* Now remove the entry in the waiting list for all requests
90 which didn't terminate. */
91 for (cnt = 0; cnt < nent; ++cnt)
92 if (list[cnt] != NULL && list[cnt]->__error_code == EINPROGRESS
93 && requestlist[cnt] != NULL)
95 struct waitlist **listp = &requestlist[cnt]->waiting;
97 /* There is the chance that we cannot find our entry anymore.
98 This could happen if the request terminated and restarted
99 again. */
100 while (*listp != NULL && *listp != &waitlist[cnt])
101 listp = &(*listp)->next;
103 if (*listp != NULL)
104 *listp = (*listp)->next;
107 /* Now it's time to restore the cancelation state. */
108 pthread_setcancelstate (oldstate, NULL);
110 /* Release the conditional variable. */
111 if (pthread_cond_destroy (&cond) != 0)
112 /* This must never happen. */
113 abort ();
115 if (result != 0)
117 /* An error occurred. Possibly it's EINTR. We have to translate
118 the timeout error report of `pthread_cond_timedwait' to the
119 form expected from `aio_suspend'. */
120 if (result == ETIMEDOUT)
121 __set_errno (EAGAIN);
123 result = -1;
127 /* Release the mutex. */
128 pthread_mutex_unlock (&__aio_requests_mutex);
130 return result;
133 weak_alias (aio_suspend, aio_suspend64)