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.17 by root, Sun Jul 31 18:45:48 2005 UTC vs.
Revision 1.33 by root, Mon Aug 22 23:21:57 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
5#include "autoconf/config.h" 8#include "autoconf/config.h"
9
10#include <pthread.h>
6 11
7#include <sys/types.h> 12#include <sys/types.h>
8#include <sys/stat.h> 13#include <sys/stat.h>
9 14
10#include <unistd.h> 15#include <unistd.h>
11#include <fcntl.h> 16#include <fcntl.h>
12#include <signal.h> 17#include <signal.h>
13#include <sched.h> 18#include <sched.h>
14 19
15#include <pthread.h> 20#if HAVE_SENDFILE
16 21# if __linux
17typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 22# include <sys/sendfile.h>
18typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 23# elif __freebsd
19typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 24# include <sys/socket.h>
25# include <sys/uio.h>
26# elif __hpux
27# include <sys/socket.h>
28# endif
29#endif
20 30
21#if __ia64 31#if __ia64
22# define STACKSIZE 65536 32# define STACKSIZE 65536
23#else 33#else
24# define STACKSIZE 4096 34# define STACKSIZE 4096
26 36
27enum { 37enum {
28 REQ_QUIT, 38 REQ_QUIT,
29 REQ_OPEN, REQ_CLOSE, 39 REQ_OPEN, REQ_CLOSE,
30 REQ_READ, REQ_WRITE, REQ_READAHEAD, 40 REQ_READ, REQ_WRITE, REQ_READAHEAD,
41 REQ_SENDFILE,
31 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, 42 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
32 REQ_FSYNC, REQ_FDATASYNC, 43 REQ_FSYNC, REQ_FDATASYNC,
44 REQ_UNLINK, REQ_RMDIR,
45 REQ_SYMLINK,
33}; 46};
34 47
35typedef struct aio_cb { 48typedef struct aio_cb {
36 struct aio_cb *volatile next; 49 struct aio_cb *volatile next;
37 50
38 int type; 51 int type;
39 52
40 int fd; 53 int fd, fd2;
41 off_t offset; 54 off_t offset;
42 size_t length; 55 size_t length;
43 ssize_t result; 56 ssize_t result;
44 mode_t mode; /* open */ 57 mode_t mode; /* open */
45 int errorno; 58 int errorno;
46 SV *data, *callback, *fh; 59 SV *data, *callback;
47 void *dataptr; 60 SV *fh, *fh2;
61 void *dataptr, *data2ptr;
48 STRLEN dataoffset; 62 STRLEN dataoffset;
49 63
50 Stat_t *statdata; 64 Stat_t *statdata;
51} aio_cb; 65} aio_cb;
52 66
53typedef aio_cb *aio_req; 67typedef aio_cb *aio_req;
54 68
55static int started; 69static int started, wanted;
56static volatile int nreqs; 70static volatile int nreqs;
57static int max_outstanding = 1<<30; 71static int max_outstanding = 1<<30;
58static int respipe [2]; 72static int respipe [2];
59 73
60static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 74static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER;
61static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 75static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
62static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 76static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
63 77
64static volatile aio_req reqs, reqe; /* queue start, queue end */ 78static volatile aio_req reqs, reqe; /* queue start, queue end */
65static volatile aio_req ress, rese; /* queue start, queue end */ 79static volatile aio_req ress, rese; /* queue start, queue end */
80
81static void free_req (aio_req req)
82{
83 if (req->data)
84 SvREFCNT_dec (req->data);
85
86 if (req->fh)
87 SvREFCNT_dec (req->fh);
88
89 if (req->fh2)
90 SvREFCNT_dec (req->fh2);
91
92 if (req->statdata)
93 Safefree (req->statdata);
94
95 if (req->callback)
96 SvREFCNT_dec (req->callback);
97
98 Safefree (req);
99}
66 100
67static void 101static void
68poll_wait () 102poll_wait ()
69{ 103{
70 if (nreqs && !ress) 104 if (nreqs && !ress)
80static int 114static int
81poll_cb () 115poll_cb ()
82{ 116{
83 dSP; 117 dSP;
84 int count = 0; 118 int count = 0;
119 int do_croak = 0;
85 aio_req req, prv; 120 aio_req req;
86 121
87 pthread_mutex_lock (&reslock); 122 for (;;)
88
89 {
90 /* read any signals sent by the worker threads */
91 char buf [32];
92 while (read (respipe [0], buf, 32) > 0)
93 ;
94 }
95
96 req = ress;
97 ress = rese = 0;
98
99 pthread_mutex_unlock (&reslock);
100
101 while (req)
102 { 123 {
124 pthread_mutex_lock (&reslock);
125 req = ress;
126
127 if (req)
128 {
129 ress = req->next;
130
131 if (!ress)
132 {
133 /* read any signals sent by the worker threads */
134 char buf [32];
135 while (read (respipe [0], buf, 32) == 32)
136 ;
137
138 rese = 0;
139 }
140 }
141
142 pthread_mutex_unlock (&reslock);
143
144 if (!req)
145 break;
146
103 nreqs--; 147 nreqs--;
104 148
105 if (req->type == REQ_QUIT) 149 if (req->type == REQ_QUIT)
106 started--; 150 started--;
107 else 151 else
108 { 152 {
109 int errorno = errno; 153 int errorno = errno;
110 errno = req->errorno; 154 errno = req->errorno;
111 155
112 if (req->type == REQ_READ) 156 if (req->type == REQ_READ)
113 SvCUR_set (req->data, req->dataoffset 157 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
114 + req->result > 0 ? req->result : 0);
115 158
159 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
160 SvREADONLY_off (req->data);
161
116 if (req->data) 162 if (req->statdata)
117 SvREFCNT_dec (req->data);
118
119 if (req->fh)
120 SvREFCNT_dec (req->fh);
121
122 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
123 { 163 {
124 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 164 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
125 PL_laststatval = req->result; 165 PL_laststatval = req->result;
126 PL_statcache = *(req->statdata); 166 PL_statcache = *(req->statdata);
127
128 Safefree (req->statdata);
129 } 167 }
130 168
131 ENTER; 169 ENTER;
132 PUSHMARK (SP); 170 PUSHMARK (SP);
133 XPUSHs (sv_2mortal (newSViv (req->result))); 171 XPUSHs (sv_2mortal (newSViv (req->result)));
150 if (SvOK (req->callback)) 188 if (SvOK (req->callback))
151 { 189 {
152 PUTBACK; 190 PUTBACK;
153 call_sv (req->callback, G_VOID | G_EVAL); 191 call_sv (req->callback, G_VOID | G_EVAL);
154 SPAGAIN; 192 SPAGAIN;
193
194 if (SvTRUE (ERRSV))
195 {
196 free_req (req);
197 croak (0);
198 }
155 } 199 }
156 200
157 LEAVE; 201 LEAVE;
158
159 if (req->callback)
160 SvREFCNT_dec (req->callback);
161 202
162 errno = errorno; 203 errno = errorno;
163 count++; 204 count++;
164 } 205 }
165 206
166 prv = req; 207 free_req (req);
167 req = req->next;
168 Safefree (prv);
169
170 /* TODO: croak on errors? */
171 } 208 }
172 209
173 return count; 210 return count;
174} 211}
175 212
196} 233}
197 234
198static void 235static void
199send_req (aio_req req) 236send_req (aio_req req)
200{ 237{
238 while (started < wanted && nreqs >= started)
239 start_thread ();
240
201 nreqs++; 241 nreqs++;
202 242
203 pthread_mutex_lock (&reqlock); 243 pthread_mutex_lock (&reqlock);
204 244
205 req->next = 0; 245 req->next = 0;
213 reqe = reqs = req; 253 reqe = reqs = req;
214 254
215 pthread_cond_signal (&reqwait); 255 pthread_cond_signal (&reqwait);
216 pthread_mutex_unlock (&reqlock); 256 pthread_mutex_unlock (&reqlock);
217 257
218 while (nreqs > max_outstanding) 258 if (nreqs > max_outstanding)
259 for (;;)
260 {
261 poll_cb ();
262
263 if (nreqs <= max_outstanding)
264 break;
265
266 poll_wait ();
267 }
268}
269
270static void
271end_thread (void)
272{
273 aio_req req;
274 Newz (0, req, 1, aio_cb);
275 req->type = REQ_QUIT;
276
277 send_req (req);
278}
279
280static void min_parallel (int nthreads)
281{
282 if (wanted < nthreads)
283 wanted = nthreads;
284}
285
286static void max_parallel (int nthreads)
287{
288 int cur = started;
289
290 if (wanted > nthreads)
291 wanted = nthreads;
292
293 while (cur > wanted)
294 {
295 end_thread ();
296 cur--;
297 }
298
299 while (started > wanted)
219 { 300 {
220 poll_wait (); 301 poll_wait ();
221 poll_cb (); 302 poll_cb ();
222 } 303 }
223} 304}
224 305
225static void 306static void create_pipe ()
226end_thread (void)
227{ 307{
308 if (pipe (respipe))
309 croak ("unable to initialize result pipe");
310
311 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
312 croak ("cannot set result pipe to nonblocking mode");
313
314 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
315 croak ("cannot set result pipe to nonblocking mode");
316}
317
318static void atfork_prepare (void)
319{
320 pthread_mutex_lock (&reqlock);
321 pthread_mutex_lock (&reslock);
322}
323
324static void atfork_parent (void)
325{
326 pthread_mutex_unlock (&reslock);
327 pthread_mutex_unlock (&reqlock);
328}
329
330static void atfork_child (void)
331{
228 aio_req req; 332 aio_req prv;
229 New (0, req, 1, aio_cb);
230 req->type = REQ_QUIT;
231 333
232 send_req (req); 334 started = 0;
233}
234 335
336 while (reqs)
337 {
338 prv = reqs;
339 reqs = prv->next;
340 free_req (prv);
341 }
342
343 reqs = reqe = 0;
344
345 while (ress)
346 {
347 prv = ress;
348 ress = prv->next;
349 free_req (prv);
350 }
351
352 ress = rese = 0;
353
354 close (respipe [0]);
355 close (respipe [1]);
356 create_pipe ();
357
358 atfork_parent ();
359}
360
361/*****************************************************************************/
235/* work around various missing functions */ 362/* work around various missing functions */
236 363
237#if !HAVE_PREADWRITE 364#if !HAVE_PREADWRITE
238# define pread aio_pread 365# define pread aio_pread
239# define pwrite aio_pwrite 366# define pwrite aio_pwrite
301 428
302 errno = 0; 429 errno = 0;
303} 430}
304#endif 431#endif
305 432
433/* sendfile always needs emulation */
434static ssize_t
435sendfile_ (int ofd, int ifd, off_t offset, size_t count)
436{
437 ssize_t res;
438
439 if (!count)
440 return 0;
441
442#if __linux
443 res = sendfile (ofd, ifd, &offset, count);
444
445#elif __freebsd
446 /*
447 * Of course, the freebsd sendfile is a dire hack with no thoughts
448 * wasted on making it similar to other i/o functions.
449 */
450 {
451 off_t sbytes;
452 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
453
454 if (!res && errno == EAGAIN)
455 /* maybe on others, too, as usual, the manpage leaves you guessing */
456 res = sbytes;
457 }
458
459#elif __hpux
460 res = sendfile (ofd, ifd, offset, count, 0, 0);
461
462#else
463 res = -1;
464 errno = ENOSYS;
465#endif
466
467 if (res < 0 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK))
468 {
469 /* emulate sendfile. this is a major pain in the ass */
470 char *buf = malloc (4096);
471 res = 0;
472
473 for (;;)
474 {
475 ssize_t cnt;
476
477 cnt = pread (ifd, buf, 4096, offset);
478
479 if (cnt <= 0)
480 {
481 if (cnt && !res) res = -1;
482 break;
483 }
484
485 cnt = write (ofd, buf, cnt);
486
487 if (cnt <= 0)
488 {
489 if (cnt && !res) res = -1;
490 break;
491 }
492
493 offset += cnt;
494 res += cnt;
495 }
496
497 {
498 int errorno = errno;
499 free (buf);
500 errno = errorno;
501 }
502 }
503
504 return res;
505}
506
507/*****************************************************************************/
508
306static void * 509static void *
307aio_proc (void *thr_arg) 510aio_proc (void *thr_arg)
308{ 511{
309 aio_req req; 512 aio_req req;
310 int type; 513 int type;
339 { 542 {
340 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 543 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
341 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 544 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
342 545
343 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 546 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
547 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break;
344 548
345 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 549 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
346 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 550 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
347 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 551 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
348 552
349 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; 553 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
350 case REQ_CLOSE: req->result = close (req->fd); break; 554 case REQ_CLOSE: req->result = close (req->fd); break;
351 case REQ_UNLINK: req->result = unlink (req->dataptr); break; 555 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
556 case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
557 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
352 558
353 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 559 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
354 case REQ_FSYNC: req->result = fsync (req->fd); break; 560 case REQ_FSYNC: req->result = fsync (req->fd); break;
355 561
356 case REQ_QUIT: 562 case REQ_QUIT:
385 while (type != REQ_QUIT); 591 while (type != REQ_QUIT);
386 592
387 return 0; 593 return 0;
388} 594}
389 595
596#define dREQ \
597 aio_req req; \
598 \
599 if (SvOK (callback) && !SvROK (callback)) \
600 croak ("clalback must be undef or of reference type"); \
601 \
602 Newz (0, req, 1, aio_cb); \
603 if (!req) \
604 croak ("out of memory during aio_req allocation"); \
605 \
606 req->callback = newSVsv (callback);
607
390MODULE = IO::AIO PACKAGE = IO::AIO 608MODULE = IO::AIO PACKAGE = IO::AIO
391 609
392PROTOTYPES: ENABLE 610PROTOTYPES: ENABLE
393 611
394BOOT: 612BOOT:
395{ 613{
396 if (pipe (respipe)) 614 create_pipe ();
397 croak ("unable to initialize result pipe"); 615 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
398
399 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
400 croak ("cannot set result pipe to nonblocking mode");
401
402 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
403 croak ("cannot set result pipe to nonblocking mode");
404} 616}
405 617
406void 618void
407min_parallel(nthreads) 619min_parallel(nthreads)
408 int nthreads 620 int nthreads
409 PROTOTYPE: $ 621 PROTOTYPE: $
410 CODE:
411 while (nthreads > started)
412 start_thread ();
413 622
414void 623void
415max_parallel(nthreads) 624max_parallel(nthreads)
416 int nthreads 625 int nthreads
417 PROTOTYPE: $ 626 PROTOTYPE: $
418 CODE:
419{
420 int cur = started;
421 while (cur > nthreads)
422 {
423 end_thread ();
424 cur--;
425 }
426
427 while (started > nthreads)
428 {
429 poll_wait ();
430 poll_cb ();
431 }
432}
433 627
434int 628int
435max_outstanding(nreqs) 629max_outstanding(nreqs)
436 int nreqs 630 int nreqs
437 PROTOTYPE: $ 631 PROTOTYPE: $
446 int mode 640 int mode
447 SV * callback 641 SV * callback
448 PROTOTYPE: $$$;$ 642 PROTOTYPE: $$$;$
449 CODE: 643 CODE:
450{ 644{
451 aio_req req; 645 dREQ;
452
453 Newz (0, req, 1, aio_cb);
454
455 if (!req)
456 croak ("out of memory during aio_req allocation");
457 646
458 req->type = REQ_OPEN; 647 req->type = REQ_OPEN;
459 req->data = newSVsv (pathname); 648 req->data = newSVsv (pathname);
460 req->dataptr = SvPV_nolen (req->data); 649 req->dataptr = SvPVbyte_nolen (req->data);
461 req->fd = flags; 650 req->fd = flags;
462 req->mode = mode; 651 req->mode = mode;
463 req->callback = SvREFCNT_inc (callback);
464 652
465 send_req (req); 653 send_req (req);
466} 654}
467 655
468void 656void
474 aio_close = REQ_CLOSE 662 aio_close = REQ_CLOSE
475 aio_fsync = REQ_FSYNC 663 aio_fsync = REQ_FSYNC
476 aio_fdatasync = REQ_FDATASYNC 664 aio_fdatasync = REQ_FDATASYNC
477 CODE: 665 CODE:
478{ 666{
479 aio_req req; 667 dREQ;
480
481 Newz (0, req, 1, aio_cb);
482
483 if (!req)
484 croak ("out of memory during aio_req allocation");
485 668
486 req->type = ix; 669 req->type = ix;
487 req->fh = newSVsv (fh); 670 req->fh = newSVsv (fh);
488 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 671 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
489 req->callback = SvREFCNT_inc (callback);
490 672
491 send_req (req); 673 send_req (req);
492} 674}
493 675
494void 676void
495aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 677aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
496 SV * fh 678 SV * fh
497 UV offset 679 UV offset
498 IV length 680 UV length
499 SV * data 681 SV * data
500 IV dataoffset 682 UV dataoffset
501 SV * callback 683 SV * callback
502 ALIAS: 684 ALIAS:
503 aio_read = REQ_READ 685 aio_read = REQ_READ
504 aio_write = REQ_WRITE 686 aio_write = REQ_WRITE
505 PROTOTYPE: $$$$$;$ 687 PROTOTYPE: $$$$$;$
506 CODE: 688 CODE:
507{ 689{
508 aio_req req; 690 aio_req req;
509 STRLEN svlen; 691 STRLEN svlen;
510 char *svptr = SvPV (data, svlen); 692 char *svptr = SvPVbyte (data, svlen);
511 693
512 SvUPGRADE (data, SVt_PV); 694 SvUPGRADE (data, SVt_PV);
513 SvPOK_on (data); 695 SvPOK_on (data);
514 696
515 if (dataoffset < 0) 697 if (dataoffset < 0)
531 } 713 }
532 714
533 if (length < 0) 715 if (length < 0)
534 croak ("length must not be negative"); 716 croak ("length must not be negative");
535 717
536 Newz (0, req, 1, aio_cb); 718 {
719 dREQ;
537 720
538 if (!req)
539 croak ("out of memory during aio_req allocation");
540
541 req->type = ix; 721 req->type = ix;
542 req->fh = newSVsv (fh); 722 req->fh = newSVsv (fh);
543 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) 723 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
544 : IoOFP (sv_2io (fh))); 724 : IoOFP (sv_2io (fh)));
545 req->offset = offset; 725 req->offset = offset;
726 req->length = length;
727 req->data = SvREFCNT_inc (data);
728 req->dataptr = (char *)svptr + dataoffset;
729
730 if (!SvREADONLY (data))
731 {
732 SvREADONLY_on (data);
733 req->data2ptr = (void *)data;
734 }
735
736 send_req (req);
737 }
738}
739
740void
741aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef)
742 SV * out_fh
743 SV * in_fh
744 UV in_offset
745 UV length
746 SV * callback
747 PROTOTYPE: $$$$;$
748 CODE:
749{
750 dREQ;
751
752 req->type = REQ_SENDFILE;
753 req->fh = newSVsv (out_fh);
754 req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh)));
755 req->fh2 = newSVsv (in_fh);
756 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
757 req->offset = in_offset;
546 req->length = length; 758 req->length = length;
547 req->data = SvREFCNT_inc (data);
548 req->dataptr = (char *)svptr + dataoffset;
549 req->callback = SvREFCNT_inc (callback);
550 759
551 send_req (req); 760 send_req (req);
552} 761}
553 762
554void 763void
558 IV length 767 IV length
559 SV * callback 768 SV * callback
560 PROTOTYPE: $$$;$ 769 PROTOTYPE: $$$;$
561 CODE: 770 CODE:
562{ 771{
563 aio_req req; 772 dREQ;
564
565 if (length < 0)
566 croak ("length must not be negative");
567
568 Newz (0, req, 1, aio_cb);
569
570 if (!req)
571 croak ("out of memory during aio_req allocation");
572 773
573 req->type = REQ_READAHEAD; 774 req->type = REQ_READAHEAD;
574 req->fh = newSVsv (fh); 775 req->fh = newSVsv (fh);
575 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 776 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
576 req->offset = offset; 777 req->offset = offset;
577 req->length = length; 778 req->length = length;
578 req->callback = SvREFCNT_inc (callback);
579 779
580 send_req (req); 780 send_req (req);
581} 781}
582 782
583void 783void
587 ALIAS: 787 ALIAS:
588 aio_stat = REQ_STAT 788 aio_stat = REQ_STAT
589 aio_lstat = REQ_LSTAT 789 aio_lstat = REQ_LSTAT
590 CODE: 790 CODE:
591{ 791{
592 aio_req req; 792 dREQ;
593
594 Newz (0, req, 1, aio_cb);
595
596 if (!req)
597 croak ("out of memory during aio_req allocation");
598 793
599 New (0, req->statdata, 1, Stat_t); 794 New (0, req->statdata, 1, Stat_t);
600
601 if (!req->statdata) 795 if (!req->statdata)
796 {
797 free_req (req);
602 croak ("out of memory during aio_req->statdata allocation"); 798 croak ("out of memory during aio_req->statdata allocation");
799 }
603 800
604 if (SvPOK (fh_or_path)) 801 if (SvPOK (fh_or_path))
605 { 802 {
606 req->type = ix; 803 req->type = ix;
607 req->data = newSVsv (fh_or_path); 804 req->data = newSVsv (fh_or_path);
608 req->dataptr = SvPV_nolen (req->data); 805 req->dataptr = SvPVbyte_nolen (req->data);
609 } 806 }
610 else 807 else
611 { 808 {
612 req->type = REQ_FSTAT; 809 req->type = REQ_FSTAT;
613 req->fh = newSVsv (fh_or_path); 810 req->fh = newSVsv (fh_or_path);
614 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 811 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
615 } 812 }
616 813
617 req->callback = SvREFCNT_inc (callback);
618
619 send_req (req); 814 send_req (req);
620} 815}
621 816
622void 817void
623aio_unlink(pathname,callback=&PL_sv_undef) 818aio_unlink(pathname,callback=&PL_sv_undef)
624 SV * pathname 819 SV * pathname
625 SV * callback 820 SV * callback
821 ALIAS:
822 aio_unlink = REQ_UNLINK
823 aio_rmdir = REQ_RMDIR
626 CODE: 824 CODE:
627{ 825{
628 aio_req req; 826 dREQ;
629 827
630 Newz (0, req, 1, aio_cb); 828 req->type = ix;
829 req->data = newSVsv (pathname);
830 req->dataptr = SvPVbyte_nolen (req->data);
631 831
632 if (!req) 832 send_req (req);
633 croak ("out of memory during aio_req allocation"); 833}
834
835void
836aio_symlink(oldpath,newpath,callback=&PL_sv_undef)
837 SV * oldpath
838 SV * newpath
839 SV * callback
840 CODE:
841{
842 dREQ;
634 843
635 req->type = REQ_UNLINK; 844 req->type = REQ_SYMLINK;
845 req->fh = newSVsv (oldpath);
846 req->data2ptr = SvPVbyte_nolen (req->fh);
636 req->data = newSVsv (pathname); 847 req->data = newSVsv (newpath);
637 req->dataptr = SvPV_nolen (req->data); 848 req->dataptr = SvPVbyte_nolen (req->data);
638 req->callback = SvREFCNT_inc (callback);
639 849
640 send_req (req); 850 send_req (req);
641} 851}
642 852
643void 853void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines