2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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.
32 #include <sys/cdefs.h>
33 #include <sys/types.h>
34 #include <sys/syscall.h>
37 #include "namespace.h"
41 #include "sigev_thread.h"
42 #include "un-namespace.h"
44 __weak_reference(__aio_read, aio_read);
45 __weak_reference(__aio_write, aio_write);
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_write(struct aiocb *iocb);
55 extern ssize_t __sys_aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout);
56 extern ssize_t __sys_aio_return(struct aiocb *iocb);
57 extern int __sys_aio_error(struct aiocb *iocb);
58 extern int __sys_aio_fsync(int op, struct aiocb *iocb);
59 extern int __sys_lio_listio(int mode, struct aiocb * const list[], int nent,
60 struct sigevent *sig);
63 aio_dispatch(struct sigev_node *sn)
65 aio_func f = sn->sn_func;
67 f(sn->sn_value, (struct aiocb *)sn->sn_id);
71 aio_sigev_alloc(sigev_id_t id, struct sigevent *sigevent,
72 struct sigev_node **sn, struct sigevent *saved_ev)
74 if (__sigev_check_init()) {
75 /* This might be that thread library is not enabled. */
80 *sn = __sigev_alloc(SI_ASYNCIO, sigevent, NULL, 1);
86 *saved_ev = *sigevent;
88 __sigev_get_sigevent(*sn, sigevent, (*sn)->sn_id);
89 (*sn)->sn_dispatch = aio_dispatch;
92 __sigev_register(*sn);
93 __sigev_list_unlock();
99 aio_io(struct aiocb *iocb, int (*sysfunc)(struct aiocb *iocb))
101 struct sigev_node *sn;
102 struct sigevent saved_ev;
105 if (iocb->aio_sigevent.sigev_notify != SIGEV_THREAD) {
110 ret = aio_sigev_alloc((sigev_id_t)iocb, &iocb->aio_sigevent, &sn,
115 iocb->aio_sigevent = saved_ev;
119 __sigev_delete_node(sn);
120 __sigev_list_unlock();
127 __aio_read(struct aiocb *iocb)
130 return aio_io(iocb, &__sys_aio_read);
134 __aio_write(struct aiocb *iocb)
137 return aio_io(iocb, &__sys_aio_write);
141 __aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout)
146 ret = __sys_aio_waitcomplete(iocbp, timeout);
148 if ((*iocbp)->aio_sigevent.sigev_notify == SIGEV_THREAD) {
151 __sigev_delete(SI_ASYNCIO, (sigev_id_t)(*iocbp));
152 __sigev_list_unlock();
161 __aio_return(struct aiocb *iocb)
164 if (iocb->aio_sigevent.sigev_notify == SIGEV_THREAD) {
165 if (__sys_aio_error(iocb) == EINPROGRESS) {
167 * Fail with EINVAL to match the semantics of
168 * __sys_aio_return() for an in-progress
175 __sigev_delete(SI_ASYNCIO, (sigev_id_t)iocb);
176 __sigev_list_unlock();
179 return __sys_aio_return(iocb);
183 __aio_fsync(int op, struct aiocb *iocb)
185 struct sigev_node *sn;
186 struct sigevent saved_ev;
189 if (iocb->aio_sigevent.sigev_notify != SIGEV_THREAD)
190 return __sys_aio_fsync(op, iocb);
192 ret = aio_sigev_alloc((sigev_id_t)iocb, &iocb->aio_sigevent, &sn,
196 ret = __sys_aio_fsync(op, iocb);
197 iocb->aio_sigevent = saved_ev;
201 __sigev_delete_node(sn);
202 __sigev_list_unlock();
209 __lio_listio(int mode, struct aiocb * const list[], int nent,
210 struct sigevent *sig)
212 struct sigev_node *sn;
213 struct sigevent saved_ev;
216 if (sig == NULL || sig->sigev_notify != SIGEV_THREAD)
217 return (__sys_lio_listio(mode, list, nent, sig));
219 ret = aio_sigev_alloc((sigev_id_t)list, sig, &sn, &saved_ev);
222 ret = __sys_lio_listio(mode, list, nent, sig);
227 __sigev_delete_node(sn);
228 __sigev_list_unlock();