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.19 by root, Sun Jul 31 19:04:45 2005 UTC vs.
Revision 1.32 by root, Mon Aug 22 23:20:37 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines