1 /* Suspend until termination of a requests.
2 Copyright (C) 1997-2018 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, see
18 <http://www.gnu.org/licenses/>. */
21 /* We use an UGLY hack to prevent gcc from finding us cheating. The
22 implementations of aio_suspend and aio_suspend64 are identical and so
23 we want to avoid code duplication by using aliases. But gcc sees
24 the different parameter lists and prints a warning. We define here
25 a function so that aio_suspend64 has no prototype. */
26 #define aio_suspend64 XXX
28 /* And undo the hack. */
37 #include <libc-lock.h>
43 const struct aiocb
*const *list
;
44 struct waitlist
*waitlist
;
45 struct requestlist
**requestlist
;
46 #ifndef DONT_NEED_AIO_MISC_COND
56 #ifdef DONT_NEED_AIO_MISC_COND
57 /* Acquire the mutex. If pthread_cond_*wait is used this would
59 pthread_mutex_lock (&__aio_requests_mutex
);
62 const struct clparam
*param
= (const struct clparam
*) arg
;
64 /* Now remove the entry in the waiting list for all requests
65 which didn't terminate. */
66 int cnt
= param
->nent
;
68 if (param
->list
[cnt
] != NULL
69 && param
->list
[cnt
]->__error_code
== EINPROGRESS
)
71 struct waitlist
**listp
;
73 assert (param
->requestlist
[cnt
] != NULL
);
75 /* There is the chance that we cannot find our entry anymore. This
76 could happen if the request terminated and restarted again. */
77 listp
= ¶m
->requestlist
[cnt
]->waiting
;
78 while (*listp
!= NULL
&& *listp
!= ¶m
->waitlist
[cnt
])
79 listp
= &(*listp
)->next
;
82 *listp
= (*listp
)->next
;
85 #ifndef DONT_NEED_AIO_MISC_COND
86 /* Release the conditional variable. */
87 (void) pthread_cond_destroy (param
->cond
);
90 /* Release the mutex. */
91 pthread_mutex_unlock (&__aio_requests_mutex
);
94 #ifdef DONT_NEED_AIO_MISC_COND
96 __attribute__ ((noinline
))
97 do_aio_misc_wait (unsigned int *cntr
, const struct timespec
*timeout
)
101 AIO_MISC_WAIT (result
, *cntr
, timeout
, 1);
108 aio_suspend (const struct aiocb
*const list
[], int nent
,
109 const struct timespec
*timeout
)
111 if (__glibc_unlikely (nent
< 0))
113 __set_errno (EINVAL
);
117 struct waitlist waitlist
[nent
];
118 struct requestlist
*requestlist
[nent
];
119 #ifndef DONT_NEED_AIO_MISC_COND
120 pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
125 unsigned int cntr
= 1;
127 /* Request the mutex. */
128 pthread_mutex_lock (&__aio_requests_mutex
);
130 /* There is not yet a finished request. Signal the request that
131 we are working for it. */
132 for (cnt
= 0; cnt
< nent
; ++cnt
)
133 if (list
[cnt
] != NULL
)
135 if (list
[cnt
]->__error_code
== EINPROGRESS
)
137 requestlist
[cnt
] = __aio_find_req ((aiocb_union
*) list
[cnt
]);
139 if (requestlist
[cnt
] != NULL
)
141 #ifndef DONT_NEED_AIO_MISC_COND
142 waitlist
[cnt
].cond
= &cond
;
144 waitlist
[cnt
].result
= NULL
;
145 waitlist
[cnt
].next
= requestlist
[cnt
]->waiting
;
146 waitlist
[cnt
].counterp
= &cntr
;
147 waitlist
[cnt
].sigevp
= NULL
;
148 requestlist
[cnt
]->waiting
= &waitlist
[cnt
];
152 /* We will never suspend. */
156 /* We will never suspend. */
161 /* Only if none of the entries is NULL or finished to be wait. */
162 if (cnt
== nent
&& any
)
164 struct clparam clparam
=
167 .waitlist
= waitlist
,
168 .requestlist
= requestlist
,
169 #ifndef DONT_NEED_AIO_MISC_COND
175 pthread_cleanup_push (cleanup
, &clparam
);
177 #ifdef DONT_NEED_AIO_MISC_COND
178 result
= do_aio_misc_wait (&cntr
, timeout
);
181 result
= pthread_cond_wait (&cond
, &__aio_requests_mutex
);
184 /* We have to convert the relative timeout value into an
185 absolute time value with pthread_cond_timedwait expects. */
187 struct timespec abstime
;
189 __gettimeofday (&now
, NULL
);
190 abstime
.tv_nsec
= timeout
->tv_nsec
+ now
.tv_usec
* 1000;
191 abstime
.tv_sec
= timeout
->tv_sec
+ now
.tv_sec
;
192 if (abstime
.tv_nsec
>= 1000000000)
194 abstime
.tv_nsec
-= 1000000000;
198 result
= pthread_cond_timedwait (&cond
, &__aio_requests_mutex
,
203 pthread_cleanup_pop (0);
206 /* Now remove the entry in the waiting list for all requests
207 which didn't terminate. */
209 if (list
[cnt
] != NULL
&& list
[cnt
]->__error_code
== EINPROGRESS
)
211 struct waitlist
**listp
;
213 assert (requestlist
[cnt
] != NULL
);
215 /* There is the chance that we cannot find our entry anymore. This
216 could happen if the request terminated and restarted again. */
217 listp
= &requestlist
[cnt
]->waiting
;
218 while (*listp
!= NULL
&& *listp
!= &waitlist
[cnt
])
219 listp
= &(*listp
)->next
;
222 *listp
= (*listp
)->next
;
225 #ifndef DONT_NEED_AIO_MISC_COND
226 /* Release the conditional variable. */
227 if (__glibc_unlikely (pthread_cond_destroy (&cond
) != 0))
228 /* This must never happen. */
234 #ifndef DONT_NEED_AIO_MISC_COND
235 /* An error occurred. Possibly it's ETIMEDOUT. We have to translate
236 the timeout error report of `pthread_cond_timedwait' to the
237 form expected from `aio_suspend'. */
238 if (result
== ETIMEDOUT
)
239 __set_errno (EAGAIN
);
242 __set_errno (result
);
247 /* Release the mutex. */
248 pthread_mutex_unlock (&__aio_requests_mutex
);
253 weak_alias (aio_suspend
, aio_suspend64
)