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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines