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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines