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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines