… | |
… | |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include <sys/types.h> |
5 | #include <sys/types.h> |
6 | #include <unistd.h> |
6 | #include <unistd.h> |
7 | #include <fcntl.h> |
7 | #include <fcntl.h> |
|
|
8 | #include <signal.h> |
8 | #include <sched.h> |
9 | #include <sched.h> |
9 | |
10 | |
10 | #define STACKSIZE 4096 /* yeah */ |
11 | #define STACKSIZE 1024 /* yeah */ |
11 | |
12 | |
12 | #define REQ_QUIT 0 |
13 | enum { REQ_QUIT, REQ_READ, REQ_WRITE, REQ_OPEN }; |
13 | #define REQ_READ 1 |
|
|
14 | #define REQ_WRITE 2 |
|
|
15 | |
14 | |
16 | typedef struct { |
15 | typedef struct { |
17 | char stack[STACKSIZE]; |
16 | char stack[STACKSIZE]; |
18 | } aio_thread; |
17 | } aio_thread; |
19 | |
18 | |
… | |
… | |
24 | /* read/write */ |
23 | /* read/write */ |
25 | int fd; |
24 | int fd; |
26 | off_t offset; |
25 | off_t offset; |
27 | size_t length; |
26 | size_t length; |
28 | ssize_t result; |
27 | ssize_t result; |
|
|
28 | mode_t mode; /* open */ |
29 | int errorno; |
29 | int errorno; |
30 | |
|
|
31 | SV *data, *callback; |
30 | SV *data, *callback; |
32 | void *dataptr; |
31 | void *dataptr; |
33 | STRLEN dataoffset; |
32 | STRLEN dataoffset; |
34 | } aio_cb; |
33 | } aio_cb; |
35 | |
34 | |
… | |
… | |
61 | end_thread(void) |
60 | end_thread(void) |
62 | { |
61 | { |
63 | aio_req req; |
62 | aio_req req; |
64 | New (0, req, 1, aio_cb); |
63 | New (0, req, 1, aio_cb); |
65 | req->type = REQ_QUIT; |
64 | req->type = REQ_QUIT; |
|
|
65 | write (reqpipe[1], &req, sizeof (aio_req)); |
|
|
66 | } |
|
|
67 | |
|
|
68 | static void |
|
|
69 | send_req (aio_req req) |
|
|
70 | { |
|
|
71 | nreqs++; |
66 | write (reqpipe[1], &req, sizeof (aio_req)); |
72 | write (reqpipe[1], &req, sizeof (aio_req)); |
67 | } |
73 | } |
68 | |
74 | |
69 | static void |
75 | static void |
70 | read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, |
76 | read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, |
… | |
… | |
106 | req->length = length; |
112 | req->length = length; |
107 | req->data = SvREFCNT_inc (data); |
113 | req->data = SvREFCNT_inc (data); |
108 | req->dataptr = (char *)svptr + dataoffset; |
114 | req->dataptr = (char *)svptr + dataoffset; |
109 | req->callback = SvREFCNT_inc (callback); |
115 | req->callback = SvREFCNT_inc (callback); |
110 | |
116 | |
111 | nreqs++; |
117 | send_req (req); |
112 | write (reqpipe[1], &req, sizeof (aio_req)); |
|
|
113 | } |
118 | } |
114 | |
119 | |
115 | static int |
120 | static int |
116 | poll_cb (pTHX) |
121 | poll_cb (pTHX) |
117 | { |
122 | { |
… | |
… | |
153 | } |
158 | } |
154 | |
159 | |
155 | return count; |
160 | return count; |
156 | } |
161 | } |
157 | |
162 | |
|
|
163 | static sigset_t fullsigset; |
|
|
164 | |
158 | #undef errno |
165 | #undef errno |
159 | #include <asm/unistd.h> |
166 | #include <asm/unistd.h> |
160 | |
167 | |
161 | static int |
168 | static int |
162 | aio_proc(void *thr_arg) |
169 | aio_proc(void *thr_arg) |
… | |
… | |
164 | aio_thread *thr = thr_arg; |
171 | aio_thread *thr = thr_arg; |
165 | int sig; |
172 | int sig; |
166 | int errno; |
173 | int errno; |
167 | aio_req req; |
174 | aio_req req; |
168 | |
175 | |
|
|
176 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
|
|
177 | |
169 | /* we rely on gcc's ability to create closures. */ |
178 | /* we rely on gcc's ability to create closures. */ |
170 | _syscall3(int,lseek,int,fd,off_t,offset,int,whence); |
179 | _syscall3(int,lseek,int,fd,off_t,offset,int,whence); |
171 | _syscall3(int,read,int,fd,char *,buf,off_t,count); |
180 | _syscall3(int,read,int,fd,char *,buf,off_t,count); |
172 | _syscall3(int,write,int,fd,char *,buf,off_t,count); |
181 | _syscall3(int,write,int,fd,char *,buf,off_t,count); |
|
|
182 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode); |
173 | |
183 | |
174 | /* first get rid of any signals */ |
|
|
175 | for (sig = 1; sig < _NSIG; sig++) |
|
|
176 | signal (sig, SIG_DFL); |
|
|
177 | |
|
|
178 | signal (SIGPIPE, SIG_IGN); |
|
|
179 | |
|
|
180 | /* then loop */ |
184 | /* then loop */ |
181 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
185 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
182 | { |
186 | { |
183 | req->thread = thr; |
187 | req->thread = thr; |
|
|
188 | errno = 0; |
184 | |
189 | |
185 | if (req->type == REQ_READ || req->type == REQ_WRITE) |
190 | if (req->type == REQ_READ || req->type == REQ_WRITE) |
186 | { |
191 | { |
187 | errno = 0; |
|
|
188 | |
|
|
189 | if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) |
192 | if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) |
190 | { |
193 | { |
191 | if (req->type == REQ_READ) |
194 | if (req->type == REQ_READ) |
192 | req->result = read (req->fd, req->dataptr, req->length); |
195 | req->result = read (req->fd, req->dataptr, req->length); |
193 | else |
196 | else |
194 | req->result = write(req->fd, req->dataptr, req->length); |
197 | req->result = write(req->fd, req->dataptr, req->length); |
195 | } |
198 | } |
196 | |
199 | } |
197 | req->errorno = errno; |
200 | else if (req->type == REQ_OPEN) |
|
|
201 | { |
|
|
202 | req->result = open (req->dataptr, req->fd, req->mode); |
198 | } |
203 | } |
199 | else |
204 | else |
200 | { |
205 | { |
201 | write (respipe[1], (void *)&req, sizeof (req)); |
206 | write (respipe[1], (void *)&req, sizeof (req)); |
202 | break; |
207 | break; |
203 | } |
208 | } |
204 | |
209 | |
|
|
210 | req->errorno = errno; |
205 | write (respipe[1], (void *)&req, sizeof (req)); |
211 | write (respipe[1], (void *)&req, sizeof (req)); |
206 | } |
212 | } |
207 | |
213 | |
208 | return 0; |
214 | return 0; |
209 | } |
215 | } |
210 | |
216 | |
211 | MODULE = Linux::AIO PACKAGE = Linux::AIO |
217 | MODULE = Linux::AIO PACKAGE = Linux::AIO |
212 | |
218 | |
213 | BOOT: |
219 | BOOT: |
214 | { |
220 | { |
|
|
221 | sigfillset (&fullsigset); |
|
|
222 | |
215 | if (pipe (reqpipe) || pipe (respipe)) |
223 | if (pipe (reqpipe) || pipe (respipe)) |
216 | croak ("unable to initialize request or result pipe"); |
224 | croak ("unable to initialize request or result pipe"); |
217 | |
225 | |
218 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
226 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
219 | croak ("cannot set result pipe to nonblocking mode"); |
227 | croak ("cannot set result pipe to nonblocking mode"); |
… | |
… | |
260 | CODE: |
268 | CODE: |
261 | SvUPGRADE (data, SVt_PV); |
269 | SvUPGRADE (data, SVt_PV); |
262 | SvPOK_on (data); |
270 | SvPOK_on (data); |
263 | read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
271 | read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
264 | |
272 | |
|
|
273 | void |
|
|
274 | aio_open(pathname,flags,mode,callback) |
|
|
275 | char * pathname |
|
|
276 | int flags |
|
|
277 | int mode |
|
|
278 | SV * callback |
|
|
279 | PROTOTYPE: $$$$ |
|
|
280 | CODE: |
|
|
281 | aio_req req; |
|
|
282 | |
|
|
283 | New (0, req, 1, aio_cb); |
|
|
284 | |
|
|
285 | if (!req) |
|
|
286 | croak ("out of memory during aio_req allocation"); |
|
|
287 | |
|
|
288 | req->type = REQ_OPEN; |
|
|
289 | req->dataptr = pathname; |
|
|
290 | req->fd = flags; |
|
|
291 | req->mode = mode; |
|
|
292 | req->callback = SvREFCNT_inc (callback); |
|
|
293 | |
|
|
294 | send_req (req); |
|
|
295 | |
265 | int |
296 | int |
266 | poll_fileno() |
297 | poll_fileno() |
267 | PROTOTYPE: |
298 | PROTOTYPE: |
268 | CODE: |
299 | CODE: |
269 | RETVAL = respipe[0]; |
300 | RETVAL = respipe[0]; |