2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
31 #include <sys/syscall.h>
34 #include "namespace.h"
38 #include "sigev_thread.h"
39 #include "un-namespace.h"
41 __weak_reference(__aio_read, aio_read);
42 __weak_reference(__aio_readv, aio_readv);
43 __weak_reference(__aio_write, aio_write);
44 __weak_reference(__aio_writev, aio_writev);
45 __weak_reference(__aio_return, aio_return);
46 __weak_reference(__aio_waitcomplete, aio_waitcomplete);
47 __weak_reference(__aio_fsync, aio_fsync);
48 __weak_reference(__lio_listio, lio_listio);
50 typedef void (*aio_func)(union sigval val, struct aiocb *iocb);
52 extern int __sys_aio_read(struct aiocb *iocb);
53 extern int __sys_aio_readv(struct aiocb *iocb);
54 extern int __sys_aio_write(struct aiocb *iocb);
55 extern int __sys_aio_writev(struct aiocb *iocb);
56 extern ssize_t __sys_aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout);
57 extern ssize_t __sys_aio_return(struct aiocb *iocb);
58 extern int __sys_aio_error(struct aiocb *iocb);
59 extern int __sys_aio_fsync(int op, struct aiocb *iocb);
60 extern int __sys_lio_listio(int mode, struct aiocb * const list[], int nent,
61 struct sigevent *sig);
64 aio_dispatch(struct sigev_node *sn)
66 aio_func f = sn->sn_func;
68 f(sn->sn_value, (struct aiocb *)sn->sn_id);
72 aio_sigev_alloc(sigev_id_t id, struct sigevent *sigevent,
73 struct sigev_node **sn, struct sigevent *saved_ev)
75 if (__sigev_check_init()) {
76 /* This might be that thread library is not enabled. */
81 *sn = __sigev_alloc(SI_ASYNCIO, sigevent, NULL, 1);
87 *saved_ev = *sigevent;
89 __sigev_get_sigevent(*sn, sigevent, (*sn)->sn_id);
90 (*sn)->sn_dispatch = aio_dispatch;
93 __sigev_register(*sn);
94 __sigev_list_unlock();
100 aio_io(struct aiocb *iocb, int (*sysfunc)(struct aiocb *iocb))
102 struct sigev_node *sn;
103 struct sigevent saved_ev;
106 if (iocb->aio_sigevent.sigev_notify != SIGEV_THREAD) {
111 ret = aio_sigev_alloc((sigev_id_t)iocb, &iocb->aio_sigevent, &sn,
116 iocb->aio_sigevent = saved_ev;
120 __sigev_delete_node(sn);
121 __sigev_list_unlock();
128 __aio_read(struct aiocb *iocb)
131 return aio_io(iocb, &__sys_aio_read);
135 __aio_readv(struct aiocb *iocb)
138 return aio_io(iocb, &__sys_aio_readv);
142 __aio_write(struct aiocb *iocb)
145 return aio_io(iocb, &__sys_aio_write);
149 __aio_writev(struct aiocb *iocb)
152 return aio_io(iocb, &__sys_aio_writev);
156 __aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout)
161 ret = __sys_aio_waitcomplete(iocbp, timeout);
163 if ((*iocbp)->aio_sigevent.sigev_notify == SIGEV_THREAD) {
166 __sigev_delete(SI_ASYNCIO, (sigev_id_t)(*iocbp));
167 __sigev_list_unlock();
176 __aio_return(struct aiocb *iocb)
179 if (iocb->aio_sigevent.sigev_notify == SIGEV_THREAD) {
180 if (__sys_aio_error(iocb) == EINPROGRESS) {
182 * Fail with EINVAL to match the semantics of
183 * __sys_aio_return() for an in-progress
190 __sigev_delete(SI_ASYNCIO, (sigev_id_t)iocb);
191 __sigev_list_unlock();
194 return __sys_aio_return(iocb);
198 __aio_fsync(int op, struct aiocb *iocb)
200 struct sigev_node *sn;
201 struct sigevent saved_ev;
204 if (iocb->aio_sigevent.sigev_notify != SIGEV_THREAD)
205 return __sys_aio_fsync(op, iocb);
207 ret = aio_sigev_alloc((sigev_id_t)iocb, &iocb->aio_sigevent, &sn,
211 ret = __sys_aio_fsync(op, iocb);
212 iocb->aio_sigevent = saved_ev;
216 __sigev_delete_node(sn);
217 __sigev_list_unlock();
224 __lio_listio(int mode, struct aiocb * const list[], int nent,
225 struct sigevent *sig)
227 struct sigev_node *sn;
228 struct sigevent saved_ev;
231 if (sig == NULL || sig->sigev_notify != SIGEV_THREAD)
232 return (__sys_lio_listio(mode, list, nent, sig));
234 ret = aio_sigev_alloc((sigev_id_t)list, sig, &sn, &saved_ev);
237 ret = __sys_lio_listio(mode, list, nent, sig);
242 __sigev_delete_node(sn);
243 __sigev_list_unlock();