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
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
29 /* And undo the hack. */
38 #include <bits/libc-lock.h>
44 const struct aiocb
*const *list
;
45 struct waitlist
*waitlist
;
46 struct requestlist
**requestlist
;
47 #ifndef DONT_NEED_AIO_MISC_COND
57 #ifdef DONT_NEED_AIO_MISC_COND
58 /* Acquire the mutex. If pthread_cond_*wait is used this would
60 pthread_mutex_lock (&__aio_requests_mutex
);
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
;
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
= ¶m
->requestlist
[cnt
]->waiting
;
79 while (*listp
!= NULL
&& *listp
!= ¶m
->waitlist
[cnt
])
80 listp
= &(*listp
)->next
;
83 *listp
= (*listp
)->next
;
86 #ifndef DONT_NEED_AIO_MISC_COND
87 /* Release the conditional variable. */
88 (void) pthread_cond_destroy (param
->cond
);
91 /* Release the mutex. */
92 pthread_mutex_unlock (&__aio_requests_mutex
);
97 aio_suspend (list
, nent
, timeout
)
98 const struct aiocb
*const list
[];
100 const struct timespec
*timeout
;
102 if (__builtin_expect (nent
< 0, 0))
104 __set_errno (EINVAL
);
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
;
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
;
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. */
142 requestlist
[cnt
]->waiting
= &waitlist
[cnt
];
146 /* We will never suspend. */
150 /* We will never suspend. */
155 /* Only if none of the entries is NULL or finished to be wait. */
156 if (cnt
== nent
&& any
)
158 struct clparam clparam
=
161 .waitlist
= waitlist
,
162 .requestlist
= requestlist
,
163 #ifndef DONT_NEED_AIO_MISC_COND
169 pthread_cleanup_push (cleanup
, &clparam
);
171 #ifdef DONT_NEED_AIO_MISC_COND
172 AIO_MISC_WAIT (result
, cntr
, timeout
, 1);
175 result
= pthread_cond_wait (&cond
, &__aio_requests_mutex
);
178 /* We have to convert the relative timeout value into an
179 absolute time value with pthread_cond_timedwait expects. */
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;
192 result
= pthread_cond_timedwait (&cond
, &__aio_requests_mutex
,
197 pthread_cleanup_pop (0);
200 /* Now remove the entry in the waiting list for all requests
201 which didn't terminate. */
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
;
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. */
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
);
236 __set_errno (result
);
241 /* Release the mutex. */
242 pthread_mutex_unlock (&__aio_requests_mutex
);
247 weak_alias (aio_suspend
, aio_suspend64
)