|
|
1 | #define _REENTRANT 1 |
|
|
2 | #include <errno.h> |
|
|
3 | |
1 | #include "EXTERN.h" |
4 | #include "EXTERN.h" |
2 | #include "perl.h" |
5 | #include "perl.h" |
3 | #include "XSUB.h" |
6 | #include "XSUB.h" |
4 | |
7 | |
|
|
8 | #include "autoconf/config.h" |
|
|
9 | |
5 | #include <sys/types.h> |
10 | #include <sys/types.h> |
6 | #include <sys/stat.h> |
11 | #include <sys/stat.h> |
|
|
12 | |
7 | #include <unistd.h> |
13 | #include <unistd.h> |
8 | #include <fcntl.h> |
14 | #include <fcntl.h> |
9 | #include <signal.h> |
15 | #include <signal.h> |
10 | #include <sched.h> |
16 | #include <sched.h> |
11 | #include <endian.h> |
|
|
12 | |
17 | |
13 | #include <pthread.h> |
18 | #include <pthread.h> |
14 | #include <sys/syscall.h> |
|
|
15 | |
19 | |
16 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
20 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
17 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
21 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
18 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
22 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
19 | |
23 | |
… | |
… | |
40 | off_t offset; |
44 | off_t offset; |
41 | size_t length; |
45 | size_t length; |
42 | ssize_t result; |
46 | ssize_t result; |
43 | mode_t mode; /* open */ |
47 | mode_t mode; /* open */ |
44 | int errorno; |
48 | int errorno; |
45 | SV *data, *callback; |
49 | SV *data, *callback, *fh; |
46 | void *dataptr; |
50 | void *dataptr; |
47 | STRLEN dataoffset; |
51 | STRLEN dataoffset; |
48 | |
52 | |
49 | Stat_t *statdata; |
53 | Stat_t *statdata; |
50 | } aio_cb; |
54 | } aio_cb; |
51 | |
55 | |
52 | typedef aio_cb *aio_req; |
56 | typedef aio_cb *aio_req; |
53 | |
57 | |
54 | static int started; |
58 | static int started; |
55 | static int nreqs; |
59 | static volatile int nreqs; |
56 | static int max_outstanding = 1<<30; |
60 | static int max_outstanding = 1<<30; |
57 | static int respipe [2]; |
61 | static int respipe [2]; |
58 | |
62 | |
59 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
63 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
60 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
64 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
… | |
… | |
64 | static volatile aio_req ress, rese; /* queue start, queue end */ |
68 | static volatile aio_req ress, rese; /* queue start, queue end */ |
65 | |
69 | |
66 | static void |
70 | static void |
67 | poll_wait () |
71 | poll_wait () |
68 | { |
72 | { |
69 | if (!nreqs) |
73 | if (nreqs && !ress) |
70 | return; |
74 | { |
71 | |
|
|
72 | fd_set rfd; |
75 | fd_set rfd; |
73 | FD_ZERO(&rfd); |
76 | FD_ZERO(&rfd); |
74 | FD_SET(respipe [0], &rfd); |
77 | FD_SET(respipe [0], &rfd); |
75 | |
78 | |
76 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
79 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
80 | } |
77 | } |
81 | } |
78 | |
82 | |
79 | static int |
83 | static int |
80 | poll_cb () |
84 | poll_cb () |
81 | { |
85 | { |
82 | dSP; |
86 | dSP; |
83 | int count = 0; |
87 | int count = 0; |
84 | aio_req req; |
88 | aio_req req, prv; |
85 | |
89 | |
|
|
90 | pthread_mutex_lock (&reslock); |
|
|
91 | |
86 | { |
92 | { |
87 | /* read and signals sent by the worker threads */ |
93 | /* read any signals sent by the worker threads */ |
88 | char buf [32]; |
94 | char buf [32]; |
89 | while (read (respipe [0], buf, 32) > 0) |
95 | while (read (respipe [0], buf, 32) > 0) |
90 | ; |
96 | ; |
91 | } |
97 | } |
92 | |
98 | |
93 | for (;;) |
99 | req = ress; |
|
|
100 | ress = rese = 0; |
|
|
101 | |
|
|
102 | pthread_mutex_unlock (&reslock); |
|
|
103 | |
|
|
104 | while (req) |
94 | { |
105 | { |
95 | pthread_mutex_lock (&reslock); |
|
|
96 | |
|
|
97 | req = ress; |
|
|
98 | |
|
|
99 | if (ress) |
|
|
100 | { |
|
|
101 | ress = ress->next; |
|
|
102 | if (!ress) rese = 0; |
|
|
103 | } |
|
|
104 | |
|
|
105 | pthread_mutex_unlock (&reslock); |
|
|
106 | |
|
|
107 | if (!req) |
|
|
108 | break; |
|
|
109 | |
|
|
110 | nreqs--; |
106 | nreqs--; |
111 | |
107 | |
112 | if (req->type == REQ_QUIT) |
108 | if (req->type == REQ_QUIT) |
113 | started--; |
109 | started--; |
114 | else |
110 | else |
… | |
… | |
121 | + req->result > 0 ? req->result : 0); |
117 | + req->result > 0 ? req->result : 0); |
122 | |
118 | |
123 | if (req->data) |
119 | if (req->data) |
124 | SvREFCNT_dec (req->data); |
120 | SvREFCNT_dec (req->data); |
125 | |
121 | |
|
|
122 | if (req->fh) |
|
|
123 | SvREFCNT_dec (req->fh); |
|
|
124 | |
126 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
125 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
127 | { |
126 | { |
128 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
127 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
129 | PL_laststatval = req->result; |
128 | PL_laststatval = req->result; |
130 | PL_statcache = *(req->statdata); |
129 | PL_statcache = *(req->statdata); |
131 | |
130 | |
132 | Safefree (req->statdata); |
131 | Safefree (req->statdata); |
133 | } |
132 | } |
134 | |
133 | |
|
|
134 | ENTER; |
135 | PUSHMARK (SP); |
135 | PUSHMARK (SP); |
136 | XPUSHs (sv_2mortal (newSViv (req->result))); |
136 | XPUSHs (sv_2mortal (newSViv (req->result))); |
137 | |
137 | |
138 | if (req->type == REQ_OPEN) |
138 | if (req->type == REQ_OPEN) |
139 | { |
139 | { |
… | |
… | |
142 | |
142 | |
143 | PUTBACK; |
143 | PUTBACK; |
144 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
144 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
145 | SPAGAIN; |
145 | SPAGAIN; |
146 | |
146 | |
147 | fh = POPs; |
147 | fh = SvREFCNT_inc (POPs); |
148 | |
148 | |
149 | PUSHMARK (SP); |
149 | PUSHMARK (SP); |
150 | XPUSHs (fh); |
150 | XPUSHs (sv_2mortal (fh)); |
151 | } |
151 | } |
152 | |
152 | |
|
|
153 | if (SvOK (req->callback)) |
|
|
154 | { |
153 | PUTBACK; |
155 | PUTBACK; |
154 | call_sv (req->callback, G_VOID | G_EVAL); |
156 | call_sv (req->callback, G_VOID | G_EVAL); |
155 | SPAGAIN; |
157 | SPAGAIN; |
|
|
158 | } |
|
|
159 | |
|
|
160 | LEAVE; |
156 | |
161 | |
157 | if (req->callback) |
162 | if (req->callback) |
158 | SvREFCNT_dec (req->callback); |
163 | SvREFCNT_dec (req->callback); |
159 | |
164 | |
160 | errno = errorno; |
165 | errno = errorno; |
161 | count++; |
166 | count++; |
162 | } |
167 | } |
163 | |
168 | |
|
|
169 | prv = req; |
|
|
170 | req = req->next; |
164 | Safefree (req); |
171 | Safefree (prv); |
|
|
172 | |
|
|
173 | /* TODO: croak on errors? */ |
165 | } |
174 | } |
166 | |
175 | |
167 | return count; |
176 | return count; |
168 | } |
177 | } |
169 | |
178 | |
… | |
… | |
224 | req->type = REQ_QUIT; |
233 | req->type = REQ_QUIT; |
225 | |
234 | |
226 | send_req (req); |
235 | send_req (req); |
227 | } |
236 | } |
228 | |
237 | |
229 | static void |
238 | /* work around various missing functions */ |
230 | read_write (int dowrite, int fd, off_t offset, size_t length, |
|
|
231 | SV *data, STRLEN dataoffset, SV *callback) |
|
|
232 | { |
|
|
233 | aio_req req; |
|
|
234 | STRLEN svlen; |
|
|
235 | char *svptr = SvPV (data, svlen); |
|
|
236 | |
239 | |
237 | SvUPGRADE (data, SVt_PV); |
240 | #if !HAVE_PREADWRITE |
238 | SvPOK_on (data); |
241 | # define pread aio_pread |
|
|
242 | # define pwrite aio_pwrite |
239 | |
243 | |
240 | if (dataoffset < 0) |
244 | /* |
241 | dataoffset += svlen; |
245 | * make our pread/pwrite safe against themselves, but not against |
|
|
246 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
247 | * but that's your problem, not mine. |
|
|
248 | */ |
|
|
249 | static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; |
242 | |
250 | |
243 | if (dataoffset < 0 || dataoffset > svlen) |
251 | static ssize_t |
244 | croak ("data offset outside of string"); |
252 | pread (int fd, void *buf, size_t count, off_t offset) |
|
|
253 | { |
|
|
254 | ssize_t res; |
|
|
255 | off_t ooffset; |
245 | |
256 | |
246 | if (dowrite) |
257 | pthread_mutex_lock (&iolock); |
|
|
258 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
259 | lseek (fd, offset, SEEK_SET); |
|
|
260 | res = read (fd, buf, count); |
|
|
261 | lseek (fd, ooffset, SEEK_SET); |
|
|
262 | pthread_mutex_unlock (&iolock); |
|
|
263 | |
|
|
264 | return res; |
|
|
265 | } |
|
|
266 | |
|
|
267 | static ssize_t |
|
|
268 | pwrite (int fd, void *buf, size_t count, off_t offset) |
|
|
269 | { |
|
|
270 | ssize_t res; |
|
|
271 | off_t ooffset; |
|
|
272 | |
|
|
273 | pthread_mutex_lock (&iolock); |
|
|
274 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
275 | lseek (fd, offset, SEEK_SET); |
|
|
276 | res = write (fd, buf, count); |
|
|
277 | lseek (fd, offset, SEEK_SET); |
|
|
278 | pthread_mutex_unlock (&iolock); |
|
|
279 | |
|
|
280 | return res; |
|
|
281 | } |
|
|
282 | #endif |
|
|
283 | |
|
|
284 | #if !HAVE_FDATASYNC |
|
|
285 | # define fdatasync fsync |
|
|
286 | #endif |
|
|
287 | |
|
|
288 | #if !HAVE_READAHEAD |
|
|
289 | # define readahead aio_readahead |
|
|
290 | |
|
|
291 | static char readahead_buf[4096]; |
|
|
292 | |
|
|
293 | static ssize_t |
|
|
294 | readahead (int fd, off_t offset, size_t count) |
|
|
295 | { |
|
|
296 | while (count > 0) |
247 | { |
297 | { |
248 | /* write: check length and adjust. */ |
298 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
249 | if (length < 0 || length + dataoffset > svlen) |
299 | |
250 | length = svlen - dataoffset; |
300 | pread (fd, readahead_buf, len, offset); |
|
|
301 | offset += len; |
|
|
302 | count -= len; |
251 | } |
303 | } |
252 | else |
|
|
253 | { |
|
|
254 | /* read: grow scalar as necessary */ |
|
|
255 | svptr = SvGROW (data, length + dataoffset); |
|
|
256 | } |
|
|
257 | |
304 | |
258 | if (length < 0) |
305 | errno = 0; |
259 | croak ("length must not be negative"); |
|
|
260 | |
|
|
261 | Newz (0, req, 1, aio_cb); |
|
|
262 | |
|
|
263 | if (!req) |
|
|
264 | croak ("out of memory during aio_req allocation"); |
|
|
265 | |
|
|
266 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
|
|
267 | req->fd = fd; |
|
|
268 | req->offset = offset; |
|
|
269 | req->length = length; |
|
|
270 | req->data = SvREFCNT_inc (data); |
|
|
271 | req->dataptr = (char *)svptr + dataoffset; |
|
|
272 | req->callback = SvREFCNT_inc (callback); |
|
|
273 | |
|
|
274 | send_req (req); |
|
|
275 | } |
306 | } |
|
|
307 | #endif |
276 | |
308 | |
277 | static void * |
309 | static void * |
278 | aio_proc (void *thr_arg) |
310 | aio_proc (void *thr_arg) |
279 | { |
311 | { |
280 | aio_req req; |
312 | aio_req req; |
… | |
… | |
306 | |
338 | |
307 | type = req->type; |
339 | type = req->type; |
308 | |
340 | |
309 | switch (type) |
341 | switch (type) |
310 | { |
342 | { |
311 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break; |
343 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
312 | case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, req->offset); break; |
344 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
313 | #if SYS_readahead |
345 | |
314 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
346 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
315 | #else |
|
|
316 | case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; |
|
|
317 | #endif |
|
|
318 | |
347 | |
319 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
348 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
320 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
349 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
321 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
350 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
322 | |
351 | |
323 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
352 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
324 | case REQ_CLOSE: req->result = close (req->fd); break; |
353 | case REQ_CLOSE: req->result = close (req->fd); break; |
325 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
354 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
326 | |
355 | |
|
|
356 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
327 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
357 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
328 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
|
|
329 | |
358 | |
330 | case REQ_QUIT: |
359 | case REQ_QUIT: |
331 | break; |
360 | break; |
332 | |
361 | |
333 | default: |
362 | default: |
… | |
… | |
361 | return 0; |
390 | return 0; |
362 | } |
391 | } |
363 | |
392 | |
364 | MODULE = IO::AIO PACKAGE = IO::AIO |
393 | MODULE = IO::AIO PACKAGE = IO::AIO |
365 | |
394 | |
|
|
395 | PROTOTYPES: ENABLE |
|
|
396 | |
366 | BOOT: |
397 | BOOT: |
367 | { |
398 | { |
368 | if (pipe (respipe)) |
399 | if (pipe (respipe)) |
369 | croak ("unable to initialize result pipe"); |
400 | croak ("unable to initialize result pipe"); |
370 | |
401 | |
… | |
… | |
410 | CODE: |
441 | CODE: |
411 | RETVAL = max_outstanding; |
442 | RETVAL = max_outstanding; |
412 | max_outstanding = nreqs; |
443 | max_outstanding = nreqs; |
413 | |
444 | |
414 | void |
445 | void |
415 | aio_open(pathname,flags,mode,callback) |
446 | aio_open(pathname,flags,mode,callback=&PL_sv_undef) |
416 | SV * pathname |
447 | SV * pathname |
417 | int flags |
448 | int flags |
418 | int mode |
449 | int mode |
419 | SV * callback |
450 | SV * callback |
420 | PROTOTYPE: $$$$ |
451 | PROTOTYPE: $$$;$ |
421 | CODE: |
452 | CODE: |
422 | { |
453 | { |
423 | aio_req req; |
454 | aio_req req; |
424 | |
455 | |
425 | Newz (0, req, 1, aio_cb); |
456 | Newz (0, req, 1, aio_cb); |
… | |
… | |
436 | |
467 | |
437 | send_req (req); |
468 | send_req (req); |
438 | } |
469 | } |
439 | |
470 | |
440 | void |
471 | void |
441 | aio_close(fh,callback) |
472 | aio_close(fh,callback=&PL_sv_undef) |
442 | InputStream fh |
473 | SV * fh |
443 | SV * callback |
474 | SV * callback |
444 | PROTOTYPE: $$ |
475 | PROTOTYPE: $;$ |
445 | ALIAS: |
476 | ALIAS: |
446 | aio_close = REQ_CLOSE |
477 | aio_close = REQ_CLOSE |
447 | aio_fsync = REQ_FSYNC |
478 | aio_fsync = REQ_FSYNC |
448 | aio_fdatasync = REQ_FDATASYNC |
479 | aio_fdatasync = REQ_FDATASYNC |
449 | CODE: |
480 | CODE: |
… | |
… | |
454 | |
485 | |
455 | if (!req) |
486 | if (!req) |
456 | croak ("out of memory during aio_req allocation"); |
487 | croak ("out of memory during aio_req allocation"); |
457 | |
488 | |
458 | req->type = ix; |
489 | req->type = ix; |
|
|
490 | req->fh = newSVsv (fh); |
459 | req->fd = PerlIO_fileno (fh); |
491 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
460 | req->callback = SvREFCNT_inc (callback); |
492 | req->callback = SvREFCNT_inc (callback); |
461 | |
493 | |
462 | send_req (req); |
494 | send_req (req); |
463 | } |
495 | } |
464 | |
496 | |
465 | void |
497 | void |
466 | aio_read(fh,offset,length,data,dataoffset,callback) |
498 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
467 | InputStream fh |
499 | SV * fh |
468 | UV offset |
500 | UV offset |
469 | IV length |
501 | IV length |
470 | SV * data |
502 | SV * data |
471 | IV dataoffset |
503 | IV dataoffset |
472 | SV * callback |
504 | SV * callback |
|
|
505 | ALIAS: |
|
|
506 | aio_read = REQ_READ |
|
|
507 | aio_write = REQ_WRITE |
473 | PROTOTYPE: $$$$$$ |
508 | PROTOTYPE: $$$$$;$ |
474 | CODE: |
509 | CODE: |
475 | read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
|
|
476 | |
|
|
477 | void |
|
|
478 | aio_write(fh,offset,length,data,dataoffset,callback) |
|
|
479 | OutputStream fh |
|
|
480 | UV offset |
|
|
481 | IV length |
|
|
482 | SV * data |
|
|
483 | IV dataoffset |
|
|
484 | SV * callback |
|
|
485 | PROTOTYPE: $$$$$$ |
|
|
486 | CODE: |
|
|
487 | read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
|
|
488 | |
|
|
489 | void |
|
|
490 | aio_readahead(fh,offset,length,callback) |
|
|
491 | InputStream fh |
|
|
492 | UV offset |
|
|
493 | IV length |
|
|
494 | SV * callback |
|
|
495 | PROTOTYPE: $$$$ |
|
|
496 | CODE: |
|
|
497 | { |
510 | { |
498 | aio_req req; |
511 | aio_req req; |
|
|
512 | STRLEN svlen; |
|
|
513 | char *svptr = SvPV (data, svlen); |
|
|
514 | |
|
|
515 | SvUPGRADE (data, SVt_PV); |
|
|
516 | SvPOK_on (data); |
|
|
517 | |
|
|
518 | if (dataoffset < 0) |
|
|
519 | dataoffset += svlen; |
|
|
520 | |
|
|
521 | if (dataoffset < 0 || dataoffset > svlen) |
|
|
522 | croak ("data offset outside of string"); |
|
|
523 | |
|
|
524 | if (ix == REQ_WRITE) |
|
|
525 | { |
|
|
526 | /* write: check length and adjust. */ |
|
|
527 | if (length < 0 || length + dataoffset > svlen) |
|
|
528 | length = svlen - dataoffset; |
|
|
529 | } |
|
|
530 | else |
|
|
531 | { |
|
|
532 | /* read: grow scalar as necessary */ |
|
|
533 | svptr = SvGROW (data, length + dataoffset); |
|
|
534 | } |
499 | |
535 | |
500 | if (length < 0) |
536 | if (length < 0) |
501 | croak ("length must not be negative"); |
537 | croak ("length must not be negative"); |
502 | |
538 | |
503 | Newz (0, req, 1, aio_cb); |
539 | Newz (0, req, 1, aio_cb); |
504 | |
540 | |
505 | if (!req) |
541 | if (!req) |
506 | croak ("out of memory during aio_req allocation"); |
542 | croak ("out of memory during aio_req allocation"); |
507 | |
543 | |
|
|
544 | req->type = ix; |
|
|
545 | req->fh = newSVsv (fh); |
|
|
546 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
|
|
547 | : IoOFP (sv_2io (fh))); |
|
|
548 | req->offset = offset; |
|
|
549 | req->length = length; |
|
|
550 | req->data = SvREFCNT_inc (data); |
|
|
551 | req->dataptr = (char *)svptr + dataoffset; |
|
|
552 | req->callback = SvREFCNT_inc (callback); |
|
|
553 | |
|
|
554 | send_req (req); |
|
|
555 | } |
|
|
556 | |
|
|
557 | void |
|
|
558 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
|
|
559 | SV * fh |
|
|
560 | UV offset |
|
|
561 | IV length |
|
|
562 | SV * callback |
|
|
563 | PROTOTYPE: $$$;$ |
|
|
564 | CODE: |
|
|
565 | { |
|
|
566 | aio_req req; |
|
|
567 | |
|
|
568 | if (length < 0) |
|
|
569 | croak ("length must not be negative"); |
|
|
570 | |
|
|
571 | Newz (0, req, 1, aio_cb); |
|
|
572 | |
|
|
573 | if (!req) |
|
|
574 | croak ("out of memory during aio_req allocation"); |
|
|
575 | |
508 | req->type = REQ_READAHEAD; |
576 | req->type = REQ_READAHEAD; |
|
|
577 | req->fh = newSVsv (fh); |
509 | req->fd = PerlIO_fileno (fh); |
578 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
510 | req->offset = offset; |
579 | req->offset = offset; |
511 | req->length = length; |
580 | req->length = length; |
512 | req->callback = SvREFCNT_inc (callback); |
581 | req->callback = SvREFCNT_inc (callback); |
513 | |
582 | |
514 | send_req (req); |
583 | send_req (req); |
515 | } |
584 | } |
516 | |
585 | |
517 | void |
586 | void |
518 | aio_stat(fh_or_path,callback) |
587 | aio_stat(fh_or_path,callback=&PL_sv_undef) |
519 | SV * fh_or_path |
588 | SV * fh_or_path |
520 | SV * callback |
589 | SV * callback |
521 | PROTOTYPE: $$ |
|
|
522 | ALIAS: |
590 | ALIAS: |
|
|
591 | aio_stat = REQ_STAT |
523 | aio_lstat = 1 |
592 | aio_lstat = REQ_LSTAT |
524 | CODE: |
593 | CODE: |
525 | { |
594 | { |
526 | aio_req req; |
595 | aio_req req; |
527 | |
596 | |
528 | Newz (0, req, 1, aio_cb); |
597 | Newz (0, req, 1, aio_cb); |
… | |
… | |
535 | if (!req->statdata) |
604 | if (!req->statdata) |
536 | croak ("out of memory during aio_req->statdata allocation"); |
605 | croak ("out of memory during aio_req->statdata allocation"); |
537 | |
606 | |
538 | if (SvPOK (fh_or_path)) |
607 | if (SvPOK (fh_or_path)) |
539 | { |
608 | { |
540 | req->type = ix ? REQ_LSTAT : REQ_STAT; |
609 | req->type = ix; |
541 | req->data = newSVsv (fh_or_path); |
610 | req->data = newSVsv (fh_or_path); |
542 | req->dataptr = SvPV_nolen (req->data); |
611 | req->dataptr = SvPV_nolen (req->data); |
543 | } |
612 | } |
544 | else |
613 | else |
545 | { |
614 | { |
546 | req->type = REQ_FSTAT; |
615 | req->type = REQ_FSTAT; |
|
|
616 | req->fh = newSVsv (fh_or_path); |
547 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
617 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
548 | } |
618 | } |
549 | |
619 | |
550 | req->callback = SvREFCNT_inc (callback); |
620 | req->callback = SvREFCNT_inc (callback); |
551 | |
621 | |
552 | send_req (req); |
622 | send_req (req); |
553 | } |
623 | } |
554 | |
624 | |
555 | void |
625 | void |
556 | aio_unlink(pathname,callback) |
626 | aio_unlink(pathname,callback=&PL_sv_undef) |
557 | SV * pathname |
627 | SV * pathname |
558 | SV * callback |
628 | SV * callback |
559 | PROTOTYPE: $$ |
|
|
560 | CODE: |
629 | CODE: |
561 | { |
630 | { |
562 | aio_req req; |
631 | aio_req req; |
563 | |
632 | |
564 | Newz (0, req, 1, aio_cb); |
633 | Newz (0, req, 1, aio_cb); |
… | |
… | |
572 | req->callback = SvREFCNT_inc (callback); |
641 | req->callback = SvREFCNT_inc (callback); |
573 | |
642 | |
574 | send_req (req); |
643 | send_req (req); |
575 | } |
644 | } |
576 | |
645 | |
|
|
646 | void |
|
|
647 | flush() |
|
|
648 | PROTOTYPE: |
|
|
649 | CODE: |
|
|
650 | while (nreqs) |
|
|
651 | { |
|
|
652 | poll_wait (); |
|
|
653 | poll_cb (); |
|
|
654 | } |
|
|
655 | |
|
|
656 | void |
|
|
657 | poll() |
|
|
658 | PROTOTYPE: |
|
|
659 | CODE: |
|
|
660 | if (nreqs) |
|
|
661 | { |
|
|
662 | poll_wait (); |
|
|
663 | poll_cb (); |
|
|
664 | } |
|
|
665 | |
577 | int |
666 | int |
578 | poll_fileno() |
667 | poll_fileno() |
579 | PROTOTYPE: |
668 | PROTOTYPE: |
580 | CODE: |
669 | CODE: |
581 | RETVAL = respipe [0]; |
670 | RETVAL = respipe [0]; |