2 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * 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"
41 #include "sigev_thread.h"
42 #include "un-namespace.h"
44 __weak_reference(__aio_read, _aio_read);
45 __weak_reference(__aio_read, aio_read);
46 __weak_reference(__aio_write, _aio_write);
47 __weak_reference(__aio_write, aio_write);
48 __weak_reference(__aio_return, _aio_return);
49 __weak_reference(__aio_return, aio_return);
50 __weak_reference(__aio_waitcomplete, _aio_waitcomplete);
51 __weak_reference(__aio_waitcomplete, aio_waitcomplete);
53 typedef void (*aio_func)(union sigval val, struct aiocb *iocb);
55 extern int __sys_aio_read(struct aiocb *iocb);
56 extern int __sys_aio_write(struct aiocb *iocb);
57 extern int __sys_aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout);
58 extern int __sys_aio_return(struct aiocb *iocb);
59 extern int __sys_aio_error(struct aiocb *iocb);
62 aio_dispatch(struct sigev_node *sn, siginfo_t *si)
64 aio_func f = sn->sn_func;
66 f(sn->sn_value, (struct aiocb *)sn->sn_id);
70 aio_io(struct aiocb *iocb, int (*sysfunc)(struct aiocb *iocb))
72 struct sigev_node *sn;
73 struct sigevent saved_ev;
76 if (iocb->aio_sigevent.sigev_notify != SIGEV_THREAD) {
81 if (__sigev_check_init()) {
82 /* This might be that thread library is not enabled. */
87 sn = __sigev_alloc(SI_ASYNCIO, &iocb->aio_sigevent);
93 saved_ev = iocb->aio_sigevent;
94 sn->sn_id = (sigev_id_t)iocb;
95 sn->sn_flags |= SNF_ONESHOT;
96 __sigev_get_sigevent(sn, &iocb->aio_sigevent, sn->sn_id);
100 __sigev_list_unlock();
102 sn->sn_dispatch = aio_dispatch;
104 iocb->aio_sigevent = saved_ev;
109 __sigev_delete_node(sn);
110 __sigev_list_unlock();
117 __aio_read(struct aiocb *iocb)
120 return aio_io(iocb, &__sys_aio_read);
124 __aio_write(struct aiocb *iocb)
127 return aio_io(iocb, &__sys_aio_write);
131 __aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout)
134 int ret = __sys_aio_waitcomplete(iocbp, timeout);
137 if ((*iocbp)->aio_sigevent.sigev_notify == SIGEV_THREAD) {
140 __sigev_delete(SI_ASYNCIO, (sigev_id_t)(*iocbp));
141 __sigev_list_unlock();
150 __aio_return(struct aiocb *iocb)
153 if (iocb->aio_sigevent.sigev_notify == SIGEV_THREAD) {
154 if (__sys_aio_error(iocb) == EINPROGRESS)
155 return (EINPROGRESS);
157 __sigev_delete(SI_ASYNCIO, (sigev_id_t)iocb);
158 __sigev_list_unlock();
161 return __sys_aio_return(iocb);