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.21 by root, Sun Aug 7 03:26:10 2005 UTC vs.
Revision 1.36 by root, Tue Aug 23 01:18:04 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines