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.9 by root, Tue Jul 12 11:02:54 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 17
12#include <pthread.h> 18#include <pthread.h>
13#include <sys/syscall.h>
14 19
15typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 20typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
16typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 21typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
17typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 22typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
18 23
24 29
25enum { 30enum {
26 REQ_QUIT, 31 REQ_QUIT,
27 REQ_OPEN, REQ_CLOSE, 32 REQ_OPEN, REQ_CLOSE,
28 REQ_READ, REQ_WRITE, REQ_READAHEAD, 33 REQ_READ, REQ_WRITE, REQ_READAHEAD,
29 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, 34 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
30 REQ_FSYNC, REQ_FDATASYNC, 35 REQ_FSYNC, REQ_FDATASYNC,
36 REQ_UNLINK, REQ_RMDIR,
37 REQ_SYMLINK,
31}; 38};
32 39
33typedef struct aio_cb { 40typedef struct aio_cb {
34 struct aio_cb *volatile next; 41 struct aio_cb *volatile next;
35 42
39 off_t offset; 46 off_t offset;
40 size_t length; 47 size_t length;
41 ssize_t result; 48 ssize_t result;
42 mode_t mode; /* open */ 49 mode_t mode; /* open */
43 int errorno; 50 int errorno;
44 SV *data, *callback; 51 SV *data, *callback, *fh;
45 void *dataptr; 52 void *dataptr, *data2ptr;
46 STRLEN dataoffset; 53 STRLEN dataoffset;
47 54
48 Stat_t *statdata; 55 Stat_t *statdata;
49} aio_cb; 56} aio_cb;
50 57
51typedef aio_cb *aio_req; 58typedef aio_cb *aio_req;
52 59
53static int started; 60static int started;
54static int nreqs; 61static volatile int nreqs;
55static int max_outstanding = 1<<30; 62static int max_outstanding = 1<<30;
56static int respipe [2]; 63static int respipe [2];
57 64
58static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 65static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER;
59static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 66static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
67static pthread_mutex_t frklock = PTHREAD_MUTEX_INITIALIZER;
60static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 68static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
61 69
62static volatile aio_req reqs, reqe; /* queue start, queue end */ 70static volatile aio_req reqs, reqe; /* queue start, queue end */
63static volatile aio_req ress, rese; /* queue start, queue end */ 71static volatile aio_req ress, rese; /* queue start, queue end */
64 72
65static void 73static void
66poll_wait () 74poll_wait ()
67{ 75{
68 if (!nreqs) 76 if (nreqs && !ress)
69 return; 77 {
70
71 fd_set rfd; 78 fd_set rfd;
72 FD_ZERO(&rfd); 79 FD_ZERO(&rfd);
73 FD_SET(respipe [0], &rfd); 80 FD_SET(respipe [0], &rfd);
74 81
75 select (respipe [0] + 1, &rfd, 0, 0, 0); 82 select (respipe [0] + 1, &rfd, 0, 0, 0);
83 }
76} 84}
77 85
78static int 86static int
79poll_cb () 87poll_cb ()
80{ 88{
81 dSP; 89 dSP;
82 int count = 0; 90 int count = 0;
91 int do_croak = 0;
83 aio_req req; 92 aio_req req, prv;
84 93
94 pthread_mutex_lock (&reslock);
95
85 { 96 {
86 /* read and signals sent by the worker threads */ 97 /* read any signals sent by the worker threads */
87 char buf [32]; 98 char buf [32];
88 while (read (respipe [0], buf, 32) > 0) 99 while (read (respipe [0], buf, 32) == 32)
89 ; 100 ;
90 } 101 }
91 102
92 for (;;) 103 req = ress;
104 ress = rese = 0;
105
106 pthread_mutex_unlock (&reslock);
107
108 while (req)
93 { 109 {
94 pthread_mutex_lock (&reslock);
95
96 req = ress;
97
98 if (ress)
99 {
100 ress = ress->next;
101 if (!ress) rese = 0;
102 }
103
104 pthread_mutex_unlock (&reslock);
105
106 if (!req)
107 break;
108
109 nreqs--; 110 nreqs--;
110 111
111 if (req->type == REQ_QUIT) 112 if (req->type == REQ_QUIT)
112 started--; 113 started--;
113 else 114 else
120 + req->result > 0 ? req->result : 0); 121 + req->result > 0 ? req->result : 0);
121 122
122 if (req->data) 123 if (req->data)
123 SvREFCNT_dec (req->data); 124 SvREFCNT_dec (req->data);
124 125
126 if (req->fh)
127 SvREFCNT_dec (req->fh);
128
125 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)
126 { 130 {
127 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 131 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
128 PL_laststatval = req->result; 132 PL_laststatval = req->result;
129 PL_statcache = *(req->statdata); 133 PL_statcache = *(req->statdata);
130 134
131 Safefree (req->statdata); 135 Safefree (req->statdata);
132 } 136 }
133 137
138 ENTER;
134 PUSHMARK (SP); 139 PUSHMARK (SP);
135 XPUSHs (sv_2mortal (newSViv (req->result))); 140 XPUSHs (sv_2mortal (newSViv (req->result)));
136 141
137 if (req->type == REQ_OPEN) 142 if (req->type == REQ_OPEN)
138 { 143 {
141 146
142 PUTBACK; 147 PUTBACK;
143 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 148 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
144 SPAGAIN; 149 SPAGAIN;
145 150
146 fh = POPs; 151 fh = SvREFCNT_inc (POPs);
147 152
148 PUSHMARK (SP); 153 PUSHMARK (SP);
149 XPUSHs (fh); 154 XPUSHs (sv_2mortal (fh));
150 } 155 }
151 156
152 if (SvOK (req->callback)) 157 if (SvOK (req->callback))
153 { 158 {
154 PUTBACK; 159 PUTBACK;
155 call_sv (req->callback, G_VOID | G_EVAL); 160 call_sv (req->callback, G_VOID | G_EVAL);
156 SPAGAIN; 161 SPAGAIN;
157 } 162 }
163
164 do_croak = SvTRUE (ERRSV);
165
166 LEAVE;
158 167
159 if (req->callback) 168 if (req->callback)
160 SvREFCNT_dec (req->callback); 169 SvREFCNT_dec (req->callback);
161 170
162 errno = errorno; 171 errno = errorno;
163 count++; 172 count++;
164 } 173 }
165 174
175 prv = req;
176 req = req->next;
166 Safefree (req); 177 Safefree (prv);
178
179 if (do_croak)
180 croak (0);
167 } 181 }
168 182
169 return count; 183 return count;
170} 184}
171 185
226 req->type = REQ_QUIT; 240 req->type = REQ_QUIT;
227 241
228 send_req (req); 242 send_req (req);
229} 243}
230 244
231static void
232read_write (int dowrite, int fd, off_t offset, size_t length,
233 SV *data, STRLEN dataoffset, SV *callback)
234{
235 aio_req req;
236 STRLEN svlen;
237 char *svptr = SvPV (data, svlen);
238 245
239 SvUPGRADE (data, SVt_PV); 246static void min_parallel (int nthreads)
240 SvPOK_on (data); 247{
248 while (nthreads > started)
249 start_thread ();
250}
241 251
242 if (dataoffset < 0) 252static void max_parallel (int nthreads)
243 dataoffset += svlen; 253{
254 int cur = started;
255 while (cur > nthreads)
256 {
257 end_thread ();
258 cur--;
259 }
244 260
245 if (dataoffset < 0 || dataoffset > svlen) 261 while (started > nthreads)
246 croak ("data offset outside of string");
247
248 if (dowrite)
249 { 262 {
250 /* write: check length and adjust. */ 263 poll_wait ();
251 if (length < 0 || length + dataoffset > svlen) 264 poll_cb ();
252 length = svlen - dataoffset;
253 } 265 }
254 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)
255 { 378 {
256 /* read: grow scalar as necessary */ 379 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
257 svptr = SvGROW (data, length + dataoffset); 380
381 pread (fd, readahead_buf, len, offset);
382 offset += len;
383 count -= len;
258 } 384 }
259 385
260 if (length < 0) 386 errno = 0;
261 croak ("length must not be negative");
262
263 Newz (0, req, 1, aio_cb);
264
265 if (!req)
266 croak ("out of memory during aio_req allocation");
267
268 req->type = dowrite ? REQ_WRITE : REQ_READ;
269 req->fd = fd;
270 req->offset = offset;
271 req->length = length;
272 req->data = SvREFCNT_inc (data);
273 req->dataptr = (char *)svptr + dataoffset;
274 req->callback = SvREFCNT_inc (callback);
275
276 send_req (req);
277} 387}
388#endif
389
390/*****************************************************************************/
278 391
279static void * 392static void *
280aio_proc (void *thr_arg) 393aio_proc (void *thr_arg)
281{ 394{
282 aio_req req; 395 aio_req req;
308 421
309 type = req->type; 422 type = req->type;
310 423
311 switch (type) 424 switch (type)
312 { 425 {
313 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;
314 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;
315#if SYS_readahead 428
316 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;
317#else
318 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break;
319#endif
320 430
321 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 431 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
322 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 432 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
323 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 433 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
324 434
325 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;
326 case REQ_CLOSE: req->result = close (req->fd); break; 436 case REQ_CLOSE: req->result = close (req->fd); break;
327 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;
328 440
441 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
329 case REQ_FSYNC: req->result = fsync (req->fd); break; 442 case REQ_FSYNC: req->result = fsync (req->fd); break;
330 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
331 443
332 case REQ_QUIT: 444 case REQ_QUIT:
333 break; 445 break;
334 446
335 default: 447 default:
361 while (type != REQ_QUIT); 473 while (type != REQ_QUIT);
362 474
363 return 0; 475 return 0;
364} 476}
365 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
366MODULE = IO::AIO PACKAGE = IO::AIO 490MODULE = IO::AIO PACKAGE = IO::AIO
367 491
368PROTOTYPES: ENABLE 492PROTOTYPES: ENABLE
369 493
370BOOT: 494BOOT:
375 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) 499 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
376 croak ("cannot set result pipe to nonblocking mode"); 500 croak ("cannot set result pipe to nonblocking mode");
377 501
378 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) 502 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
379 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);
380} 506}
381 507
382void 508void
383min_parallel(nthreads) 509min_parallel(nthreads)
384 int nthreads 510 int nthreads
385 PROTOTYPE: $ 511 PROTOTYPE: $
386 CODE:
387 while (nthreads > started)
388 start_thread ();
389 512
390void 513void
391max_parallel(nthreads) 514max_parallel(nthreads)
392 int nthreads 515 int nthreads
393 PROTOTYPE: $ 516 PROTOTYPE: $
394 CODE:
395{
396 int cur = started;
397 while (cur > nthreads)
398 {
399 end_thread ();
400 cur--;
401 }
402
403 while (started > nthreads)
404 {
405 poll_wait ();
406 poll_cb ();
407 }
408}
409 517
410int 518int
411max_outstanding(nreqs) 519max_outstanding(nreqs)
412 int nreqs 520 int nreqs
413 PROTOTYPE: $ 521 PROTOTYPE: $
422 int mode 530 int mode
423 SV * callback 531 SV * callback
424 PROTOTYPE: $$$;$ 532 PROTOTYPE: $$$;$
425 CODE: 533 CODE:
426{ 534{
427 aio_req req; 535 dREQ;
428
429 Newz (0, req, 1, aio_cb);
430
431 if (!req)
432 croak ("out of memory during aio_req allocation");
433 536
434 req->type = REQ_OPEN; 537 req->type = REQ_OPEN;
435 req->data = newSVsv (pathname); 538 req->data = newSVsv (pathname);
436 req->dataptr = SvPV_nolen (req->data); 539 req->dataptr = SvPVbyte_nolen (req->data);
437 req->fd = flags; 540 req->fd = flags;
438 req->mode = mode; 541 req->mode = mode;
439 req->callback = SvREFCNT_inc (callback);
440 542
441 send_req (req); 543 send_req (req);
442} 544}
443 545
444void 546void
445aio_close(fh,callback=&PL_sv_undef) 547aio_close(fh,callback=&PL_sv_undef)
446 InputStream fh 548 SV * fh
447 SV * callback 549 SV * callback
448 PROTOTYPE: $;$ 550 PROTOTYPE: $;$
449 ALIAS: 551 ALIAS:
450 aio_close = REQ_CLOSE 552 aio_close = REQ_CLOSE
451 aio_fsync = REQ_FSYNC 553 aio_fsync = REQ_FSYNC
452 aio_fdatasync = REQ_FDATASYNC 554 aio_fdatasync = REQ_FDATASYNC
453 CODE: 555 CODE:
454{ 556{
455 aio_req req; 557 dREQ;
456
457 Newz (0, req, 1, aio_cb);
458
459 if (!req)
460 croak ("out of memory during aio_req allocation");
461 558
462 req->type = ix; 559 req->type = ix;
560 req->fh = newSVsv (fh);
463 req->fd = PerlIO_fileno (fh); 561 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
464 req->callback = SvREFCNT_inc (callback);
465 562
466 send_req (req); 563 send_req (req);
467} 564}
468 565
469void 566void
470aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 567aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
471 InputStream fh 568 SV * fh
472 UV offset 569 UV offset
473 IV length 570 IV length
474 SV * data 571 SV * data
475 IV dataoffset 572 IV dataoffset
476 SV * callback 573 SV * callback
574 ALIAS:
575 aio_read = REQ_READ
576 aio_write = REQ_WRITE
477 PROTOTYPE: $$$$$;$ 577 PROTOTYPE: $$$$$;$
478 CODE: 578 CODE:
479 read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 579{
580 aio_req req;
581 STRLEN svlen;
582 char *svptr = SvPVbyte (data, svlen);
480 583
481void 584 SvUPGRADE (data, SVt_PV);
482aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 585 SvPOK_on (data);
483 OutputStream fh 586
484 UV offset 587 if (dataoffset < 0)
485 IV length 588 dataoffset += svlen;
486 SV * data 589
487 IV dataoffset 590 if (dataoffset < 0 || dataoffset > svlen)
488 SV * callback 591 croak ("data offset outside of string");
489 PROTOTYPE: $$$$$;$ 592
490 CODE: 593 if (ix == REQ_WRITE)
491 read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 594 {
595 /* write: check length and adjust. */
596 if (length < 0 || length + dataoffset > svlen)
597 length = svlen - dataoffset;
598 }
599 else
600 {
601 /* read: grow scalar as necessary */
602 svptr = SvGROW (data, length + dataoffset);
603 }
604
605 if (length < 0)
606 croak ("length must not be negative");
607
608 {
609 dREQ;
610
611 req->type = ix;
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}
492 624
493void 625void
494aio_readahead(fh,offset,length,callback=&PL_sv_undef) 626aio_readahead(fh,offset,length,callback=&PL_sv_undef)
495 InputStream fh 627 SV * fh
496 UV offset 628 UV offset
497 IV length 629 IV length
498 SV * callback 630 SV * callback
499 PROTOTYPE: $$$;$ 631 PROTOTYPE: $$$;$
500 CODE: 632 CODE:
501{ 633{
502 aio_req req; 634 dREQ;
503
504 if (length < 0)
505 croak ("length must not be negative");
506
507 Newz (0, req, 1, aio_cb);
508
509 if (!req)
510 croak ("out of memory during aio_req allocation");
511 635
512 req->type = REQ_READAHEAD; 636 req->type = REQ_READAHEAD;
637 req->fh = newSVsv (fh);
513 req->fd = PerlIO_fileno (fh); 638 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
514 req->offset = offset; 639 req->offset = offset;
515 req->length = length; 640 req->length = length;
516 req->callback = SvREFCNT_inc (callback);
517 641
518 send_req (req); 642 send_req (req);
519} 643}
520 644
521void 645void
525 ALIAS: 649 ALIAS:
526 aio_stat = REQ_STAT 650 aio_stat = REQ_STAT
527 aio_lstat = REQ_LSTAT 651 aio_lstat = REQ_LSTAT
528 CODE: 652 CODE:
529{ 653{
530 aio_req req; 654 dREQ;
531
532 Newz (0, req, 1, aio_cb);
533
534 if (!req)
535 croak ("out of memory during aio_req allocation");
536 655
537 New (0, req->statdata, 1, Stat_t); 656 New (0, req->statdata, 1, Stat_t);
538
539 if (!req->statdata) 657 if (!req->statdata)
540 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)");
541 659
542 if (SvPOK (fh_or_path)) 660 if (SvPOK (fh_or_path))
543 { 661 {
544 req->type = ix; 662 req->type = ix;
545 req->data = newSVsv (fh_or_path); 663 req->data = newSVsv (fh_or_path);
546 req->dataptr = SvPV_nolen (req->data); 664 req->dataptr = SvPVbyte_nolen (req->data);
547 } 665 }
548 else 666 else
549 { 667 {
550 req->type = REQ_FSTAT; 668 req->type = REQ_FSTAT;
669 req->fh = newSVsv (fh_or_path);
551 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 670 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
552 } 671 }
553
554 req->callback = SvREFCNT_inc (callback);
555 672
556 send_req (req); 673 send_req (req);
557} 674}
558 675
559void 676void
560aio_unlink(pathname,callback=&PL_sv_undef) 677aio_unlink(pathname,callback=&PL_sv_undef)
561 SV * pathname 678 SV * pathname
562 SV * callback 679 SV * callback
680 ALIAS:
681 aio_unlink = REQ_UNLINK
682 aio_rmdir = REQ_RMDIR
563 CODE: 683 CODE:
564{ 684{
565 aio_req req; 685 dREQ;
566 686
567 Newz (0, req, 1, aio_cb); 687 req->type = ix;
688 req->data = newSVsv (pathname);
689 req->dataptr = SvPVbyte_nolen (req->data);
568 690
569 if (!req) 691 send_req (req);
570 croak ("out of memory during aio_req allocation"); 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;
571 702
572 req->type = REQ_UNLINK; 703 req->type = REQ_SYMLINK;
704 req->fh = newSVsv (oldpath);
705 req->data2ptr = SvPVbyte_nolen (req->fh);
573 req->data = newSVsv (pathname); 706 req->data = newSVsv (newpath);
574 req->dataptr = SvPV_nolen (req->data); 707 req->dataptr = SvPVbyte_nolen (req->data);
575 req->callback = SvREFCNT_inc (callback);
576 708
577 send_req (req); 709 send_req (req);
578} 710}
579 711
580void 712void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines