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/cdefs.h>
31 #include <sys/types.h>
32 #include <sys/syscall.h>
35 #include "namespace.h"
39 #include "sigev_thread.h"
40 #include "un-namespace.h"
42 __weak_reference(__aio_read, aio_read);
43 __weak_reference(__aio_readv, aio_readv);
44 __weak_reference(__aio_write, aio_write);
45 __weak_reference(__aio_writev, aio_writev);
46 __weak_reference(__aio_return, aio_return);
47 __weak_reference(__aio_waitcomplete, aio_waitcomplete);
48 __weak_reference(__aio_fsync, aio_fsync);
49 __weak_reference(__lio_listio, lio_listio);
51 typedef void (*aio_func)(union sigval val, struct aiocb *iocb);
53 extern int __sys_aio_read(struct aiocb *iocb);
54 extern int __sys_aio_readv(struct aiocb *iocb);
55 extern int __sys_aio_write(struct aiocb *iocb);
56 extern int __sys_aio_writev(struct aiocb *iocb);
57 extern ssize_t __sys_aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout);
58 extern ssize_t __sys_aio_return(struct aiocb *iocb);
59 extern int __sys_aio_error(struct aiocb *iocb);
60 extern int __sys_aio_fsync(int op, struct aiocb *iocb);
61 extern int __sys_lio_listio(int mode, struct aiocb * const list[], int nent,
62 struct sigevent *sig);
65 aio_dispatch(struct sigev_node *sn)
67 aio_func f = sn->sn_func;
69 f(sn->sn_value, (struct aiocb *)sn->sn_id);
73 aio_sigev_alloc(sigev_id_t id, struct sigevent *sigevent,
74 struct sigev_node **sn, struct sigevent *saved_ev)
76 if (__sigev_check_init()) {
77 /* This might be that thread library is not enabled. */
82 *sn = __sigev_alloc(SI_ASYNCIO, sigevent, NULL, 1);
88 *saved_ev = *sigevent;
90 __sigev_get_sigevent(*sn, sigevent, (*sn)->sn_id);
91 (*sn)->sn_dispatch = aio_dispatch;
94 __sigev_register(*sn);
95 __sigev_list_unlock();
101 aio_io(struct aiocb *iocb, int (*sysfunc)(struct aiocb *iocb))
103 struct sigev_node *sn;
104 struct sigevent saved_ev;
107 if (iocb->aio_sigevent.sigev_notify != SIGEV_THREAD) {
112 ret = aio_sigev_alloc((sigev_id_t)iocb, &iocb->aio_sigevent, &sn,
117 iocb->aio_sigevent = saved_ev;
121 __sigev_delete_node(sn);
122 __sigev_list_unlock();
129 __aio_read(struct aiocb *iocb)
132 return aio_io(iocb, &__sys_aio_read);
136 __aio_readv(struct aiocb *iocb)
139 return aio_io(iocb, &__sys_aio_readv);
143 __aio_write(struct aiocb *iocb)
146 return aio_io(iocb, &__sys_aio_write);
150 __aio_writev(struct aiocb *iocb)
153 return aio_io(iocb, &__sys_aio_writev);
157 __aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout)
162 ret = __sys_aio_waitcomplete(iocbp, timeout);
164 if ((*iocbp)->aio_sigevent.sigev_notify == SIGEV_THREAD) {
167 __sigev_delete(SI_ASYNCIO, (sigev_id_t)(*iocbp));
168 __sigev_list_unlock();
177 __aio_return(struct aiocb *iocb)
180 if (iocb->aio_sigevent.sigev_notify == SIGEV_THREAD) {
181 if (__sys_aio_error(iocb) == EINPROGRESS) {
183 * Fail with EINVAL to match the semantics of
184 * __sys_aio_return() for an in-progress
191 __sigev_delete(SI_ASYNCIO, (sigev_id_t)iocb);
192 __sigev_list_unlock();
195 return __sys_aio_return(iocb);
199 __aio_fsync(int op, struct aiocb *iocb)
201 struct sigev_node *sn;
202 struct sigevent saved_ev;
205 if (iocb->aio_sigevent.sigev_notify != SIGEV_THREAD)
206 return __sys_aio_fsync(op, iocb);
208 ret = aio_sigev_alloc((sigev_id_t)iocb, &iocb->aio_sigevent, &sn,
212 ret = __sys_aio_fsync(op, iocb);
213 iocb->aio_sigevent = saved_ev;
217 __sigev_delete_node(sn);
218 __sigev_list_unlock();
225 __lio_listio(int mode, struct aiocb * const list[], int nent,
226 struct sigevent *sig)
228 struct sigev_node *sn;
229 struct sigevent saved_ev;
232 if (sig == NULL || sig->sigev_notify != SIGEV_THREAD)
233 return (__sys_lio_listio(mode, list, nent, sig));
235 ret = aio_sigev_alloc((sigev_id_t)list, sig, &sn, &saved_ev);
238 ret = __sys_lio_listio(mode, list, nent, sig);
243 __sigev_delete_node(sn);
244 __sigev_list_unlock();