Deleted Added
full compact
aio.c (156267) aio.c (156383)
1/*
2 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
10 * disclaimer.
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.
14 *
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.
25 *
1/*
2 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
10 * disclaimer.
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.
14 *
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.
25 *
26 * $FreeBSD: head/lib/librt/aio.c 156267 2006-03-04 00:18:19Z davidxu $
26 * $FreeBSD: head/lib/librt/aio.c 156383 2006-03-07 08:28:07Z davidxu $
27 *
28 */
29
30#include <sys/cdefs.h>
31#include <sys/types.h>
32#include <sys/syscall.h>
33#include <sys/aio.h>
34
35#include "namespace.h"
36#include <errno.h>
27 *
28 */
29
30#include <sys/cdefs.h>
31#include <sys/types.h>
32#include <sys/syscall.h>
33#include <sys/aio.h>
34
35#include "namespace.h"
36#include <errno.h>
37#include <pthread.h>
38#include <stddef.h>
39#include <signal.h>
37#include <stddef.h>
38#include <signal.h>
40#include <unistd.h>
41#include "sigev_thread.h"
42#include "un-namespace.h"
43
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);
52
53typedef void (*aio_func)(union sigval val, struct aiocb *iocb);
54
55extern int __sys_aio_read(struct aiocb *iocb);
56extern int __sys_aio_write(struct aiocb *iocb);
57extern int __sys_aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout);
58extern int __sys_aio_return(struct aiocb *iocb);
59extern int __sys_aio_error(struct aiocb *iocb);
60
61static void
62aio_dispatch(struct sigev_node *sn)
63{
64 aio_func f = sn->sn_func;
65
66 f(sn->sn_value, (struct aiocb *)sn->sn_id);
67}
68
69static int
70aio_io(struct aiocb *iocb, int (*sysfunc)(struct aiocb *iocb))
71{
72 struct sigev_node *sn;
73 struct sigevent saved_ev;
74 int ret, err;
75
76 if (iocb->aio_sigevent.sigev_notify != SIGEV_THREAD) {
77 ret = sysfunc(iocb);
78 return (ret);
79 }
80
81 if (__sigev_check_init()) {
82 /* This might be that thread library is not enabled. */
83 errno = EINVAL;
84 return (-1);
85 }
86
87 sn = __sigev_alloc(SI_ASYNCIO, &iocb->aio_sigevent, NULL, 1);
88 if (sn == NULL) {
89 errno = EAGAIN;
90 return (-1);
91 }
92
93 saved_ev = iocb->aio_sigevent;
94 sn->sn_id = (sigev_id_t)iocb;
39#include "sigev_thread.h"
40#include "un-namespace.h"
41
42__weak_reference(__aio_read, _aio_read);
43__weak_reference(__aio_read, aio_read);
44__weak_reference(__aio_write, _aio_write);
45__weak_reference(__aio_write, aio_write);
46__weak_reference(__aio_return, _aio_return);
47__weak_reference(__aio_return, aio_return);
48__weak_reference(__aio_waitcomplete, _aio_waitcomplete);
49__weak_reference(__aio_waitcomplete, aio_waitcomplete);
50
51typedef void (*aio_func)(union sigval val, struct aiocb *iocb);
52
53extern int __sys_aio_read(struct aiocb *iocb);
54extern int __sys_aio_write(struct aiocb *iocb);
55extern int __sys_aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout);
56extern int __sys_aio_return(struct aiocb *iocb);
57extern int __sys_aio_error(struct aiocb *iocb);
58
59static void
60aio_dispatch(struct sigev_node *sn)
61{
62 aio_func f = sn->sn_func;
63
64 f(sn->sn_value, (struct aiocb *)sn->sn_id);
65}
66
67static int
68aio_io(struct aiocb *iocb, int (*sysfunc)(struct aiocb *iocb))
69{
70 struct sigev_node *sn;
71 struct sigevent saved_ev;
72 int ret, err;
73
74 if (iocb->aio_sigevent.sigev_notify != SIGEV_THREAD) {
75 ret = sysfunc(iocb);
76 return (ret);
77 }
78
79 if (__sigev_check_init()) {
80 /* This might be that thread library is not enabled. */
81 errno = EINVAL;
82 return (-1);
83 }
84
85 sn = __sigev_alloc(SI_ASYNCIO, &iocb->aio_sigevent, NULL, 1);
86 if (sn == NULL) {
87 errno = EAGAIN;
88 return (-1);
89 }
90
91 saved_ev = iocb->aio_sigevent;
92 sn->sn_id = (sigev_id_t)iocb;
95 sn->sn_flags |= SNF_ONESHOT;
96 __sigev_get_sigevent(sn, &iocb->aio_sigevent, sn->sn_id);
97
98 __sigev_list_lock();
99 __sigev_register(sn);
100 __sigev_list_unlock();
101
102 sn->sn_dispatch = aio_dispatch;
103 ret = sysfunc(iocb);
104 iocb->aio_sigevent = saved_ev;
105
106 if (ret != 0) {
107 err = errno;
108 __sigev_list_lock();
109 __sigev_delete_node(sn);
110 __sigev_list_unlock();
111 errno = err;
112 }
113 return (ret);
114}
115
116int
117__aio_read(struct aiocb *iocb)
118{
119
120 return aio_io(iocb, &__sys_aio_read);
121}
122
123int
124__aio_write(struct aiocb *iocb)
125{
126
127 return aio_io(iocb, &__sys_aio_write);
128}
129
130int
131__aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout)
132{
133 int err;
134 int ret = __sys_aio_waitcomplete(iocbp, timeout);
135
136 if (*iocbp) {
137 if ((*iocbp)->aio_sigevent.sigev_notify == SIGEV_THREAD) {
138 err = errno;
139 __sigev_list_lock();
140 __sigev_delete(SI_ASYNCIO, (sigev_id_t)(*iocbp));
141 __sigev_list_unlock();
142 errno = err;
143 }
144 }
145
146 return (ret);
147}
148
149int
150__aio_return(struct aiocb *iocb)
151{
152
153 if (iocb->aio_sigevent.sigev_notify == SIGEV_THREAD) {
154 if (__sys_aio_error(iocb) == EINPROGRESS)
155 return (EINPROGRESS);
156 __sigev_list_lock();
157 __sigev_delete(SI_ASYNCIO, (sigev_id_t)iocb);
158 __sigev_list_unlock();
159 }
160
161 return __sys_aio_return(iocb);
162}
93 __sigev_get_sigevent(sn, &iocb->aio_sigevent, sn->sn_id);
94
95 __sigev_list_lock();
96 __sigev_register(sn);
97 __sigev_list_unlock();
98
99 sn->sn_dispatch = aio_dispatch;
100 ret = sysfunc(iocb);
101 iocb->aio_sigevent = saved_ev;
102
103 if (ret != 0) {
104 err = errno;
105 __sigev_list_lock();
106 __sigev_delete_node(sn);
107 __sigev_list_unlock();
108 errno = err;
109 }
110 return (ret);
111}
112
113int
114__aio_read(struct aiocb *iocb)
115{
116
117 return aio_io(iocb, &__sys_aio_read);
118}
119
120int
121__aio_write(struct aiocb *iocb)
122{
123
124 return aio_io(iocb, &__sys_aio_write);
125}
126
127int
128__aio_waitcomplete(struct aiocb **iocbp, struct timespec *timeout)
129{
130 int err;
131 int ret = __sys_aio_waitcomplete(iocbp, timeout);
132
133 if (*iocbp) {
134 if ((*iocbp)->aio_sigevent.sigev_notify == SIGEV_THREAD) {
135 err = errno;
136 __sigev_list_lock();
137 __sigev_delete(SI_ASYNCIO, (sigev_id_t)(*iocbp));
138 __sigev_list_unlock();
139 errno = err;
140 }
141 }
142
143 return (ret);
144}
145
146int
147__aio_return(struct aiocb *iocb)
148{
149
150 if (iocb->aio_sigevent.sigev_notify == SIGEV_THREAD) {
151 if (__sys_aio_error(iocb) == EINPROGRESS)
152 return (EINPROGRESS);
153 __sigev_list_lock();
154 __sigev_delete(SI_ASYNCIO, (sigev_id_t)iocb);
155 __sigev_list_unlock();
156 }
157
158 return __sys_aio_return(iocb);
159}