]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/pthread/aio_suspend.c
99f3a80c522190ffcb21b41530bdc09269f69599
[thirdparty/glibc.git] / sysdeps / pthread / aio_suspend.c
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.
5
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.
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
14 Lesser General Public License for more details.
15
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. */
20
21
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
31
32 #include <assert.h>
33 #include <errno.h>
34 #include <stdbool.h>
35 #include <stdlib.h>
36 #include <sys/time.h>
37
38 #include <bits/libc-lock.h>
39 #include <aio_misc.h>
40
41
42 struct clparam
43 {
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;
51 };
52
53
54 static void
55 cleanup (void *arg)
56 {
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
62
63 const struct clparam *param = (const struct clparam *) arg;
64
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)
71 {
72 struct waitlist **listp;
73
74 assert (param->requestlist[cnt] != NULL);
75
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;
81
82 if (*listp != NULL)
83 *listp = (*listp)->next;
84 }
85
86 #ifndef DONT_NEED_AIO_MISC_COND
87 /* Release the conditional variable. */
88 (void) pthread_cond_destroy (param->cond);
89 #endif
90
91 /* Release the mutex. */
92 pthread_mutex_unlock (&__aio_requests_mutex);
93 }
94
95 #ifdef DONT_NEED_AIO_MISC_COND
96 static int
97 __attribute__ ((noinline))
98 do_aio_misc_wait(int *cntr, const struct timespec *timeout)
99 {
100 int result = 0;
101
102 AIO_MISC_WAIT(result, *cntr, timeout, 1);
103
104 return result;
105 }
106 #endif
107
108 int
109 aio_suspend (list, nent, timeout)
110 const struct aiocb *const list[];
111 int nent;
112 const struct timespec *timeout;
113 {
114 if (__builtin_expect (nent < 0, 0))
115 {
116 __set_errno (EINVAL);
117 return -1;
118 }
119
120 struct waitlist waitlist[nent];
121 struct requestlist *requestlist[nent];
122 #ifndef DONT_NEED_AIO_MISC_COND
123 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
124 #endif
125 int cnt;
126 bool any = false;
127 int result = 0;
128 int cntr = 1;
129
130 /* Request the mutex. */
131 pthread_mutex_lock (&__aio_requests_mutex);
132
133 /* There is not yet a finished request. Signal the request that
134 we are working for it. */
135 for (cnt = 0; cnt < nent; ++cnt)
136 if (list[cnt] != NULL)
137 {
138 if (list[cnt]->__error_code == EINPROGRESS)
139 {
140 requestlist[cnt] = __aio_find_req ((aiocb_union *) list[cnt]);
141
142 if (requestlist[cnt] != NULL)
143 {
144 #ifndef DONT_NEED_AIO_MISC_COND
145 waitlist[cnt].cond = &cond;
146 #endif
147 waitlist[cnt].result = NULL;
148 waitlist[cnt].next = requestlist[cnt]->waiting;
149 waitlist[cnt].counterp = &cntr;
150 waitlist[cnt].sigevp = NULL;
151 #ifdef BROKEN_THREAD_SIGNALS
152 waitlist[cnt].caller_pid = 0; /* Not needed. */
153 #endif
154 requestlist[cnt]->waiting = &waitlist[cnt];
155 any = true;
156 }
157 else
158 /* We will never suspend. */
159 break;
160 }
161 else
162 /* We will never suspend. */
163 break;
164 }
165
166
167 /* Only if none of the entries is NULL or finished to be wait. */
168 if (cnt == nent && any)
169 {
170 struct clparam clparam =
171 {
172 .list = list,
173 .waitlist = waitlist,
174 .requestlist = requestlist,
175 #ifndef DONT_NEED_AIO_MISC_COND
176 .cond = &cond,
177 #endif
178 .nent = nent
179 };
180
181 pthread_cleanup_push (cleanup, &clparam);
182
183 #ifdef DONT_NEED_AIO_MISC_COND
184 result = do_aio_misc_wait(&cntr, timeout);
185 #else
186 if (timeout == NULL)
187 result = pthread_cond_wait (&cond, &__aio_requests_mutex);
188 else
189 {
190 /* We have to convert the relative timeout value into an
191 absolute time value with pthread_cond_timedwait expects. */
192 struct timeval now;
193 struct timespec abstime;
194
195 __gettimeofday (&now, NULL);
196 abstime.tv_nsec = timeout->tv_nsec + now.tv_usec * 1000;
197 abstime.tv_sec = timeout->tv_sec + now.tv_sec;
198 if (abstime.tv_nsec >= 1000000000)
199 {
200 abstime.tv_nsec -= 1000000000;
201 abstime.tv_sec += 1;
202 }
203
204 result = pthread_cond_timedwait (&cond, &__aio_requests_mutex,
205 &abstime);
206 }
207 #endif
208
209 pthread_cleanup_pop (0);
210 }
211
212 /* Now remove the entry in the waiting list for all requests
213 which didn't terminate. */
214 while (cnt-- > 0)
215 if (list[cnt] != NULL && list[cnt]->__error_code == EINPROGRESS)
216 {
217 struct waitlist **listp;
218
219 assert (requestlist[cnt] != NULL);
220
221 /* There is the chance that we cannot find our entry anymore. This
222 could happen if the request terminated and restarted again. */
223 listp = &requestlist[cnt]->waiting;
224 while (*listp != NULL && *listp != &waitlist[cnt])
225 listp = &(*listp)->next;
226
227 if (*listp != NULL)
228 *listp = (*listp)->next;
229 }
230
231 #ifndef DONT_NEED_AIO_MISC_COND
232 /* Release the conditional variable. */
233 if (__builtin_expect (pthread_cond_destroy (&cond) != 0, 0))
234 /* This must never happen. */
235 abort ();
236 #endif
237
238 if (result != 0)
239 {
240 #ifndef DONT_NEED_AIO_MISC_COND
241 /* An error occurred. Possibly it's ETIMEDOUT. We have to translate
242 the timeout error report of `pthread_cond_timedwait' to the
243 form expected from `aio_suspend'. */
244 if (result == ETIMEDOUT)
245 __set_errno (EAGAIN);
246 else
247 #endif
248 __set_errno (result);
249
250 result = -1;
251 }
252
253 /* Release the mutex. */
254 pthread_mutex_unlock (&__aio_requests_mutex);
255
256 return result;
257 }
258
259 weak_alias (aio_suspend, aio_suspend64)