ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
(Generate patch)

Comparing IO-AIO/AIO.xs (file contents):
Revision 1.6 by root, Mon Jul 11 01:03:17 2005 UTC vs.
Revision 1.26 by root, Wed Aug 17 03:52:20 2005 UTC

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
16typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 20typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
17typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 21typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
18typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 22typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
19 23
25 29
26enum { 30enum {
27 REQ_QUIT, 31 REQ_QUIT,
28 REQ_OPEN, REQ_CLOSE, 32 REQ_OPEN, REQ_CLOSE,
29 REQ_READ, REQ_WRITE, REQ_READAHEAD, 33 REQ_READ, REQ_WRITE, REQ_READAHEAD,
30 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, 34 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
31 REQ_FSYNC, REQ_FDATASYNC, 35 REQ_FSYNC, REQ_FDATASYNC,
36 REQ_UNLINK, REQ_RMDIR,
37 REQ_SYMLINK,
32}; 38};
33 39
34typedef struct aio_cb { 40typedef struct aio_cb {
35 struct aio_cb *volatile next; 41 struct aio_cb *volatile next;
36 42
40 off_t offset; 46 off_t offset;
41 size_t length; 47 size_t length;
42 ssize_t result; 48 ssize_t result;
43 mode_t mode; /* open */ 49 mode_t mode; /* open */
44 int errorno; 50 int errorno;
45 SV *data, *callback; 51 SV *data, *callback, *fh;
46 void *dataptr; 52 void *dataptr, *data2ptr;
47 STRLEN dataoffset; 53 STRLEN dataoffset;
48 54
49 Stat_t *statdata; 55 Stat_t *statdata;
50} aio_cb; 56} aio_cb;
51 57
52typedef aio_cb *aio_req; 58typedef aio_cb *aio_req;
53 59
54static int started; 60static int started;
55static int nreqs; 61static volatile int nreqs;
56static int max_outstanding = 1<<30; 62static int max_outstanding = 1<<30;
57static int respipe [2]; 63static int respipe [2];
58 64
59static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 65static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER;
60static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 66static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
67static pthread_mutex_t frklock = PTHREAD_MUTEX_INITIALIZER;
61static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 68static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
62 69
63static volatile aio_req reqs, reqe; /* queue start, queue end */ 70static volatile aio_req reqs, reqe; /* queue start, queue end */
64static volatile aio_req ress, rese; /* queue start, queue end */ 71static volatile aio_req ress, rese; /* queue start, queue end */
65 72
73static void free_req (aio_req req)
74{
75 if (req->data)
76 SvREFCNT_dec (req->data);
77
78 if (req->fh)
79 SvREFCNT_dec (req->fh);
80
81 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
82 Safefree (req->statdata);
83
84 if (req->callback)
85 SvREFCNT_dec (req->callback);
86
87 Safefree (req);
88}
89
66static void 90static void
67poll_wait () 91poll_wait ()
68{ 92{
69 if (!nreqs) 93 if (nreqs && !ress)
70 return; 94 {
71
72 fd_set rfd; 95 fd_set rfd;
73 FD_ZERO(&rfd); 96 FD_ZERO(&rfd);
74 FD_SET(respipe [0], &rfd); 97 FD_SET(respipe [0], &rfd);
75 98
76 select (respipe [0] + 1, &rfd, 0, 0, 0); 99 select (respipe [0] + 1, &rfd, 0, 0, 0);
100 }
77} 101}
78 102
79static int 103static int
80poll_cb () 104poll_cb ()
81{ 105{
82 dSP; 106 dSP;
83 int count = 0; 107 int count = 0;
108 int do_croak = 0;
84 aio_req req; 109 aio_req req, prv;
85 110
111 pthread_mutex_lock (&reslock);
112
86 { 113 {
87 /* read and signals sent by the worker threads */ 114 /* read any signals sent by the worker threads */
88 char buf [32]; 115 char buf [32];
89 while (read (respipe [0], buf, 32) > 0) 116 while (read (respipe [0], buf, 32) == 32)
90 ; 117 ;
91 } 118 }
92 119
93 for (;;)
94 {
95 pthread_mutex_lock (&reslock);
96
97 req = ress; 120 req = ress;
121 ress = rese = 0;
98 122
99 if (ress)
100 {
101 ress = ress->next;
102 if (!ress) rese = 0;
103 }
104
105 pthread_mutex_unlock (&reslock); 123 pthread_mutex_unlock (&reslock);
106 124
107 if (!req) 125 while (req)
108 break; 126 {
109
110 nreqs--; 127 nreqs--;
111 128
112 if (req->type == REQ_QUIT) 129 if (req->type == REQ_QUIT)
113 started--; 130 started--;
114 else 131 else
118 135
119 if (req->type == REQ_READ) 136 if (req->type == REQ_READ)
120 SvCUR_set (req->data, req->dataoffset 137 SvCUR_set (req->data, req->dataoffset
121 + req->result > 0 ? req->result : 0); 138 + req->result > 0 ? req->result : 0);
122 139
123 if (req->data)
124 SvREFCNT_dec (req->data);
125
126 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 140 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
127 { 141 {
128 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 142 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
129 PL_laststatval = req->result; 143 PL_laststatval = req->result;
130 PL_statcache = *(req->statdata); 144 PL_statcache = *(req->statdata);
131
132 Safefree (req->statdata);
133 } 145 }
134 146
147 ENTER;
135 PUSHMARK (SP); 148 PUSHMARK (SP);
136 XPUSHs (sv_2mortal (newSViv (req->result))); 149 XPUSHs (sv_2mortal (newSViv (req->result)));
137 150
138 if (req->type == REQ_OPEN) 151 if (req->type == REQ_OPEN)
139 { 152 {
142 155
143 PUTBACK; 156 PUTBACK;
144 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 157 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
145 SPAGAIN; 158 SPAGAIN;
146 159
147 fh = POPs; 160 fh = SvREFCNT_inc (POPs);
148 161
149 PUSHMARK (SP); 162 PUSHMARK (SP);
150 XPUSHs (fh); 163 XPUSHs (sv_2mortal (fh));
151 } 164 }
152 165
166 if (SvOK (req->callback))
167 {
153 PUTBACK; 168 PUTBACK;
154 call_sv (req->callback, G_VOID | G_EVAL); 169 call_sv (req->callback, G_VOID | G_EVAL);
155 SPAGAIN; 170 SPAGAIN;
171 }
172
173 LEAVE;
174
175 do_croak = SvTRUE (ERRSV);
156 176
157 if (req->callback)
158 SvREFCNT_dec (req->callback);
159
160 errno = errorno; 177 errno = errorno;
161 count++; 178 count++;
162 } 179 }
163 180
164 Safefree (req); 181 prv = req;
182 req = req->next;
183 free_req (prv);
184
185 if (do_croak)
186 croak (0);
165 } 187 }
166 188
167 return count; 189 return count;
168} 190}
169 191
218 240
219static void 241static void
220end_thread (void) 242end_thread (void)
221{ 243{
222 aio_req req; 244 aio_req req;
223 New (0, req, 1, aio_cb); 245 Newz (0, req, 1, aio_cb);
224 req->type = REQ_QUIT; 246 req->type = REQ_QUIT;
225 247
226 send_req (req); 248 send_req (req);
227} 249}
228 250
229static void
230read_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 251
237 SvUPGRADE (data, SVt_PV); 252static void min_parallel (int nthreads)
238 SvPOK_on (data); 253{
254 while (nthreads > started)
255 start_thread ();
256}
239 257
240 if (dataoffset < 0) 258static void max_parallel (int nthreads)
259{
260 int cur = started;
261 while (cur > nthreads)
262 {
263 end_thread ();
264 cur--;
265 }
266
267 while (started > nthreads)
268 {
269 poll_wait ();
270 poll_cb ();
271 }
272}
273
274static void create_pipe ()
275{
276 if (pipe (respipe))
277 croak ("unable to initialize result pipe");
278
279 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
280 croak ("cannot set result pipe to nonblocking mode");
281
282 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
283 croak ("cannot set result pipe to nonblocking mode");
284}
285
286static void atfork_prepare (void)
287{
288 pthread_mutex_lock (&frklock);
289 pthread_mutex_lock (&reqlock);
290 pthread_mutex_lock (&reslock);
291}
292
293static void atfork_parent (void)
294{
295 pthread_mutex_unlock (&reslock);
296 pthread_mutex_unlock (&reqlock);
297 pthread_mutex_unlock (&frklock);
298}
299
300static void atfork_child (void)
301{
302 int restart = started;
303 started = 0;
304
305 while (reqs)
306 {
307 free_req (reqs);
308 reqs = reqs->next;
309 }
310
311 reqs = reqe = 0;
312
313 while (ress)
314 {
315 free_req (ress);
316 ress = ress->next;
317 }
318
319 ress = rese = 0;
320
321 close (respipe [0]);
322 close (respipe [1]);
323
324 create_pipe ();
325
326 atfork_parent ();
327
328 min_parallel (restart);
329}
330
331/*****************************************************************************/
332/* work around various missing functions */
333
334#if !HAVE_PREADWRITE
335# define pread aio_pread
336# define pwrite aio_pwrite
337
338/*
339 * make our pread/pwrite safe against themselves, but not against
340 * normal read/write by using a mutex. slows down execution a lot,
341 * but that's your problem, not mine.
342 */
343static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER;
344
345static ssize_t
346pread (int fd, void *buf, size_t count, off_t offset)
347{
348 ssize_t res;
349 off_t ooffset;
350
351 pthread_mutex_lock (&iolock);
352 ooffset = lseek (fd, 0, SEEK_CUR);
353 lseek (fd, offset, SEEK_SET);
354 res = read (fd, buf, count);
355 lseek (fd, ooffset, SEEK_SET);
356 pthread_mutex_unlock (&iolock);
357
358 return res;
359}
360
361static ssize_t
362pwrite (int fd, void *buf, size_t count, off_t offset)
363{
364 ssize_t res;
365 off_t ooffset;
366
367 pthread_mutex_lock (&iolock);
368 ooffset = lseek (fd, 0, SEEK_CUR);
369 lseek (fd, offset, SEEK_SET);
370 res = write (fd, buf, count);
371 lseek (fd, offset, SEEK_SET);
372 pthread_mutex_unlock (&iolock);
373
374 return res;
375}
376#endif
377
378#if !HAVE_FDATASYNC
379# define fdatasync fsync
380#endif
381
382#if !HAVE_READAHEAD
383# define readahead aio_readahead
384
385static char readahead_buf[4096];
386
387static ssize_t
388readahead (int fd, off_t offset, size_t count)
389{
390 while (count > 0)
391 {
392 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
393
394 pread (fd, readahead_buf, len, offset);
241 dataoffset += svlen; 395 offset += len;
242 396 count -= len;
243 if (dataoffset < 0 || dataoffset > svlen)
244 croak ("data offset outside of string");
245
246 if (dowrite)
247 { 397 }
248 /* write: check length and adjust. */
249 if (length < 0 || length + dataoffset > svlen)
250 length = svlen - dataoffset;
251 }
252 else
253 {
254 /* read: grow scalar as necessary */
255 svptr = SvGROW (data, length + dataoffset);
256 }
257 398
258 if (length < 0) 399 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} 400}
401#endif
402
403/*****************************************************************************/
276 404
277static void * 405static void *
278aio_proc (void *thr_arg) 406aio_proc (void *thr_arg)
279{ 407{
280 aio_req req; 408 aio_req req;
306 434
307 type = req->type; 435 type = req->type;
308 436
309 switch (type) 437 switch (type)
310 { 438 {
311 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break; 439 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; 440 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
313#if SYS_readahead 441
314 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 442 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 443
319 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 444 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
320 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 445 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
321 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 446 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
322 447
323 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; 448 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
324 case REQ_CLOSE: req->result = close (req->fd); break; 449 case REQ_CLOSE: req->result = close (req->fd); break;
325 case REQ_UNLINK: req->result = unlink (req->dataptr); break; 450 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
451 case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
452 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
326 453
454 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
327 case REQ_FSYNC: req->result = fsync (req->fd); break; 455 case REQ_FSYNC: req->result = fsync (req->fd); break;
328 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
329 456
330 case REQ_QUIT: 457 case REQ_QUIT:
331 break; 458 break;
332 459
333 default: 460 default:
359 while (type != REQ_QUIT); 486 while (type != REQ_QUIT);
360 487
361 return 0; 488 return 0;
362} 489}
363 490
491#define dREQ \
492 aio_req req; \
493 \
494 if (SvOK (callback) && !SvROK (callback)) \
495 croak ("clalback must be undef or of reference type"); \
496 \
497 Newz (0, req, 1, aio_cb); \
498 if (!req) \
499 croak ("out of memory during aio_req allocation"); \
500 \
501 req->callback = SvREFCNT_inc (callback);
502
364MODULE = IO::AIO PACKAGE = IO::AIO 503MODULE = IO::AIO PACKAGE = IO::AIO
365 504
505PROTOTYPES: ENABLE
506
366BOOT: 507BOOT:
367{ 508{
368 if (pipe (respipe)) 509 create_pipe ();
369 croak ("unable to initialize result pipe"); 510 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
370
371 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
372 croak ("cannot set result pipe to nonblocking mode");
373
374 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
375 croak ("cannot set result pipe to nonblocking mode");
376} 511}
377 512
378void 513void
379min_parallel(nthreads) 514min_parallel(nthreads)
380 int nthreads 515 int nthreads
381 PROTOTYPE: $ 516 PROTOTYPE: $
382 CODE:
383 while (nthreads > started)
384 start_thread ();
385 517
386void 518void
387max_parallel(nthreads) 519max_parallel(nthreads)
388 int nthreads 520 int nthreads
389 PROTOTYPE: $ 521 PROTOTYPE: $
390 CODE:
391{
392 int cur = started;
393 while (cur > nthreads)
394 {
395 end_thread ();
396 cur--;
397 }
398
399 while (started > nthreads)
400 {
401 poll_wait ();
402 poll_cb ();
403 }
404}
405 522
406int 523int
407max_outstanding(nreqs) 524max_outstanding(nreqs)
408 int nreqs 525 int nreqs
409 PROTOTYPE: $ 526 PROTOTYPE: $
410 CODE: 527 CODE:
411 RETVAL = max_outstanding; 528 RETVAL = max_outstanding;
412 max_outstanding = nreqs; 529 max_outstanding = nreqs;
413 530
414void 531void
415aio_open(pathname,flags,mode,callback) 532aio_open(pathname,flags,mode,callback=&PL_sv_undef)
416 SV * pathname 533 SV * pathname
417 int flags 534 int flags
418 int mode 535 int mode
419 SV * callback 536 SV * callback
420 PROTOTYPE: $$$$ 537 PROTOTYPE: $$$;$
421 CODE: 538 CODE:
422{ 539{
423 aio_req req; 540 dREQ;
424
425 Newz (0, req, 1, aio_cb);
426
427 if (!req)
428 croak ("out of memory during aio_req allocation");
429 541
430 req->type = REQ_OPEN; 542 req->type = REQ_OPEN;
431 req->data = newSVsv (pathname); 543 req->data = newSVsv (pathname);
432 req->dataptr = SvPV_nolen (req->data); 544 req->dataptr = SvPVbyte_nolen (req->data);
433 req->fd = flags; 545 req->fd = flags;
434 req->mode = mode; 546 req->mode = mode;
435 req->callback = SvREFCNT_inc (callback);
436 547
437 send_req (req); 548 send_req (req);
438} 549}
439 550
440void 551void
441aio_close(fh,callback) 552aio_close(fh,callback=&PL_sv_undef)
442 InputStream fh 553 SV * fh
443 SV * callback 554 SV * callback
444 PROTOTYPE: $$ 555 PROTOTYPE: $;$
445 ALIAS: 556 ALIAS:
446 aio_close = REQ_CLOSE 557 aio_close = REQ_CLOSE
447 aio_fsync = REQ_FSYNC 558 aio_fsync = REQ_FSYNC
448 aio_fdatasync = REQ_FDATASYNC 559 aio_fdatasync = REQ_FDATASYNC
449 CODE: 560 CODE:
450{ 561{
562 dREQ;
563
564 req->type = ix;
565 req->fh = newSVsv (fh);
566 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
567
568 send_req (req);
569}
570
571void
572aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
573 SV * fh
574 UV offset
575 IV length
576 SV * data
577 IV dataoffset
578 SV * callback
579 ALIAS:
580 aio_read = REQ_READ
581 aio_write = REQ_WRITE
582 PROTOTYPE: $$$$$;$
583 CODE:
584{
451 aio_req req; 585 aio_req req;
586 STRLEN svlen;
587 char *svptr = SvPVbyte (data, svlen);
452 588
453 Newz (0, req, 1, aio_cb); 589 SvUPGRADE (data, SVt_PV);
590 SvPOK_on (data);
454 591
455 if (!req) 592 if (dataoffset < 0)
456 croak ("out of memory during aio_req allocation"); 593 dataoffset += svlen;
457 594
458 req->type = ix; 595 if (dataoffset < 0 || dataoffset > svlen)
459 req->fd = PerlIO_fileno (fh); 596 croak ("data offset outside of string");
460 req->callback = SvREFCNT_inc (callback);
461 597
462 send_req (req); 598 if (ix == REQ_WRITE)
463} 599 {
464 600 /* write: check length and adjust. */
465void 601 if (length < 0 || length + dataoffset > svlen)
466aio_read(fh,offset,length,data,dataoffset,callback) 602 length = svlen - dataoffset;
467 InputStream fh 603 }
468 UV offset 604 else
469 IV length 605 {
470 SV * data 606 /* read: grow scalar as necessary */
471 IV dataoffset 607 svptr = SvGROW (data, length + dataoffset);
472 SV * callback 608 }
473 PROTOTYPE: $$$$$$
474 CODE:
475 read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
476
477void
478aio_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
489void
490aio_readahead(fh,offset,length,callback)
491 InputStream fh
492 UV offset
493 IV length
494 SV * callback
495 PROTOTYPE: $$$$
496 CODE:
497{
498 aio_req req;
499 609
500 if (length < 0) 610 if (length < 0)
501 croak ("length must not be negative"); 611 croak ("length must not be negative");
502 612
503 Newz (0, req, 1, aio_cb); 613 {
614 dREQ;
504 615
505 if (!req) 616 req->type = ix;
506 croak ("out of memory during aio_req allocation"); 617 req->fh = newSVsv (fh);
618 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
619 : IoOFP (sv_2io (fh)));
620 req->offset = offset;
621 req->length = length;
622 req->data = SvREFCNT_inc (data);
623 req->dataptr = (char *)svptr + dataoffset;
624 req->callback = SvREFCNT_inc (callback);
625
626 send_req (req);
627 }
628}
629
630void
631aio_readahead(fh,offset,length,callback=&PL_sv_undef)
632 SV * fh
633 UV offset
634 IV length
635 SV * callback
636 PROTOTYPE: $$$;$
637 CODE:
638{
639 dREQ;
507 640
508 req->type = REQ_READAHEAD; 641 req->type = REQ_READAHEAD;
642 req->fh = newSVsv (fh);
509 req->fd = PerlIO_fileno (fh); 643 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
510 req->offset = offset; 644 req->offset = offset;
511 req->length = length; 645 req->length = length;
512 req->callback = SvREFCNT_inc (callback);
513 646
514 send_req (req); 647 send_req (req);
515} 648}
516 649
517void 650void
518aio_stat(fh_or_path,callback) 651aio_stat(fh_or_path,callback=&PL_sv_undef)
519 SV * fh_or_path 652 SV * fh_or_path
520 SV * callback 653 SV * callback
521 PROTOTYPE: $$
522 ALIAS: 654 ALIAS:
655 aio_stat = REQ_STAT
523 aio_lstat = 1 656 aio_lstat = REQ_LSTAT
524 CODE: 657 CODE:
525{ 658{
526 aio_req req; 659 dREQ;
527
528 Newz (0, req, 1, aio_cb);
529
530 if (!req)
531 croak ("out of memory during aio_req allocation");
532 660
533 New (0, req->statdata, 1, Stat_t); 661 New (0, req->statdata, 1, Stat_t);
534
535 if (!req->statdata) 662 if (!req->statdata)
536 croak ("out of memory during aio_req->statdata allocation"); 663 croak ("out of memory during aio_req->statdata allocation (sorry, i just leaked memory, too)");
537 664
538 if (SvPOK (fh_or_path)) 665 if (SvPOK (fh_or_path))
539 { 666 {
540 req->type = ix ? REQ_LSTAT : REQ_STAT; 667 req->type = ix;
541 req->data = newSVsv (fh_or_path); 668 req->data = newSVsv (fh_or_path);
542 req->dataptr = SvPV_nolen (req->data); 669 req->dataptr = SvPVbyte_nolen (req->data);
543 } 670 }
544 else 671 else
545 { 672 {
546 req->type = REQ_FSTAT; 673 req->type = REQ_FSTAT;
674 req->fh = newSVsv (fh_or_path);
547 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 675 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
548 } 676 }
549 677
550 req->callback = SvREFCNT_inc (callback);
551
552 send_req (req); 678 send_req (req);
553} 679}
554 680
555void 681void
556aio_unlink(pathname,callback) 682aio_unlink(pathname,callback=&PL_sv_undef)
557 SV * pathname 683 SV * pathname
558 SV * callback 684 SV * callback
559 PROTOTYPE: $$ 685 ALIAS:
686 aio_unlink = REQ_UNLINK
687 aio_rmdir = REQ_RMDIR
560 CODE: 688 CODE:
561{ 689{
562 aio_req req; 690 dREQ;
563 691
564 Newz (0, req, 1, aio_cb); 692 req->type = ix;
693 req->data = newSVsv (pathname);
694 req->dataptr = SvPVbyte_nolen (req->data);
565 695
566 if (!req) 696 send_req (req);
567 croak ("out of memory during aio_req allocation"); 697}
698
699void
700aio_symlink(oldpath,newpath,callback=&PL_sv_undef)
701 SV * oldpath
702 SV * newpath
703 SV * callback
704 CODE:
705{
706 dREQ;
568 707
569 req->type = REQ_UNLINK; 708 req->type = REQ_SYMLINK;
709 req->fh = newSVsv (oldpath);
710 req->data2ptr = SvPVbyte_nolen (req->fh);
570 req->data = newSVsv (pathname); 711 req->data = newSVsv (newpath);
571 req->dataptr = SvPV_nolen (req->data); 712 req->dataptr = SvPVbyte_nolen (req->data);
572 req->callback = SvREFCNT_inc (callback);
573 713
574 send_req (req); 714 send_req (req);
575} 715}
576 716
577void 717void
582 { 722 {
583 poll_wait (); 723 poll_wait ();
584 poll_cb (); 724 poll_cb ();
585 } 725 }
586 726
727void
728poll()
729 PROTOTYPE:
730 CODE:
731 if (nreqs)
732 {
733 poll_wait ();
734 poll_cb ();
735 }
736
587int 737int
588poll_fileno() 738poll_fileno()
589 PROTOTYPE: 739 PROTOTYPE:
590 CODE: 740 CODE:
591 RETVAL = respipe [0]; 741 RETVAL = respipe [0];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines