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.7 by root, Mon Jul 11 01:49:14 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#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, REQ_SYMLINK,
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
156 if (SvOK (req->callback))
157 {
153 PUTBACK; 158 PUTBACK;
154 call_sv (req->callback, G_VOID | G_EVAL); 159 call_sv (req->callback, G_VOID | G_EVAL);
155 SPAGAIN; 160 SPAGAIN;
161 }
162
163 LEAVE;
156 164
157 if (req->callback) 165 if (req->callback)
158 SvREFCNT_dec (req->callback); 166 SvREFCNT_dec (req->callback);
159 167
160 errno = errorno; 168 errno = errorno;
161 count++; 169 count++;
162 } 170 }
163 171
172 prv = req;
173 req = req->next;
164 Safefree (req); 174 Safefree (prv);
175
176 /* TODO: croak on errors? */
165 } 177 }
166 178
167 return count; 179 return count;
168} 180}
169 181
224 req->type = REQ_QUIT; 236 req->type = REQ_QUIT;
225 237
226 send_req (req); 238 send_req (req);
227} 239}
228 240
229static void
230read_write (int dowrite, int fd, off_t offset, size_t length,
231 SV *data, STRLEN dataoffset, SV *callback)
232{
233 aio_req req;
234 STRLEN svlen;
235 char *svptr = SvPV (data, svlen);
236 241
237 SvUPGRADE (data, SVt_PV); 242static void min_parallel (int nthreads)
238 SvPOK_on (data); 243{
244 while (nthreads > started)
245 start_thread ();
246}
239 247
240 if (dataoffset < 0) 248static void max_parallel (int nthreads)
241 dataoffset += svlen; 249{
250 int cur = started;
251 while (cur > nthreads)
252 {
253 end_thread ();
254 cur--;
255 }
242 256
243 if (dataoffset < 0 || dataoffset > svlen) 257 while (started > nthreads)
244 croak ("data offset outside of string");
245
246 if (dowrite)
247 { 258 {
248 /* write: check length and adjust. */ 259 poll_wait ();
249 if (length < 0 || length + dataoffset > svlen) 260 poll_cb ();
250 length = svlen - dataoffset;
251 } 261 }
252 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)
253 { 374 {
254 /* read: grow scalar as necessary */ 375 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
255 svptr = SvGROW (data, length + dataoffset); 376
377 pread (fd, readahead_buf, len, offset);
378 offset += len;
379 count -= len;
256 } 380 }
257 381
258 if (length < 0) 382 errno = 0;
259 croak ("length must not be negative");
260
261 Newz (0, req, 1, aio_cb);
262
263 if (!req)
264 croak ("out of memory during aio_req allocation");
265
266 req->type = dowrite ? REQ_WRITE : REQ_READ;
267 req->fd = fd;
268 req->offset = offset;
269 req->length = length;
270 req->data = SvREFCNT_inc (data);
271 req->dataptr = (char *)svptr + dataoffset;
272 req->callback = SvREFCNT_inc (callback);
273
274 send_req (req);
275} 383}
384#endif
385
386/*****************************************************************************/
276 387
277static void * 388static void *
278aio_proc (void *thr_arg) 389aio_proc (void *thr_arg)
279{ 390{
280 aio_req req; 391 aio_req req;
306 417
307 type = req->type; 418 type = req->type;
308 419
309 switch (type) 420 switch (type)
310 { 421 {
311 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;
312 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;
313#if SYS_readahead 424
314 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;
315#else
316 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break;
317#endif
318 426
319 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 427 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
320 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 428 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
321 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 429 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
322 430
323 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;
324 case REQ_CLOSE: req->result = close (req->fd); break; 432 case REQ_CLOSE: req->result = close (req->fd); break;
325 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;
326 436
437 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
327 case REQ_FSYNC: req->result = fsync (req->fd); break; 438 case REQ_FSYNC: req->result = fsync (req->fd); break;
328 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
329 439
330 case REQ_QUIT: 440 case REQ_QUIT:
331 break; 441 break;
332 442
333 default: 443 default:
359 while (type != REQ_QUIT); 469 while (type != REQ_QUIT);
360 470
361 return 0; 471 return 0;
362} 472}
363 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
364MODULE = IO::AIO PACKAGE = IO::AIO 486MODULE = IO::AIO PACKAGE = IO::AIO
487
488PROTOTYPES: ENABLE
365 489
366BOOT: 490BOOT:
367{ 491{
368 if (pipe (respipe)) 492 if (pipe (respipe))
369 croak ("unable to initialize result pipe"); 493 croak ("unable to initialize result pipe");
371 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) 495 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
372 croak ("cannot set result pipe to nonblocking mode"); 496 croak ("cannot set result pipe to nonblocking mode");
373 497
374 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) 498 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
375 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);
376} 502}
377 503
378void 504void
379min_parallel(nthreads) 505min_parallel(nthreads)
380 int nthreads 506 int nthreads
381 PROTOTYPE: $ 507 PROTOTYPE: $
382 CODE:
383 while (nthreads > started)
384 start_thread ();
385 508
386void 509void
387max_parallel(nthreads) 510max_parallel(nthreads)
388 int nthreads 511 int nthreads
389 PROTOTYPE: $ 512 PROTOTYPE: $
390 CODE:
391{
392 int cur = started;
393 while (cur > nthreads)
394 {
395 end_thread ();
396 cur--;
397 }
398
399 while (started > nthreads)
400 {
401 poll_wait ();
402 poll_cb ();
403 }
404}
405 513
406int 514int
407max_outstanding(nreqs) 515max_outstanding(nreqs)
408 int nreqs 516 int nreqs
409 PROTOTYPE: $ 517 PROTOTYPE: $
410 CODE: 518 CODE:
411 RETVAL = max_outstanding; 519 RETVAL = max_outstanding;
412 max_outstanding = nreqs; 520 max_outstanding = nreqs;
413 521
414void 522void
415aio_open(pathname,flags,mode,callback) 523aio_open(pathname,flags,mode,callback=&PL_sv_undef)
416 SV * pathname 524 SV * pathname
417 int flags 525 int flags
418 int mode 526 int mode
419 SV * callback 527 SV * callback
420 PROTOTYPE: $$$$ 528 PROTOTYPE: $$$;$
421 CODE: 529 CODE:
422{ 530{
423 aio_req req; 531 dREQ;
424
425 Newz (0, req, 1, aio_cb);
426
427 if (!req)
428 croak ("out of memory during aio_req allocation");
429 532
430 req->type = REQ_OPEN; 533 req->type = REQ_OPEN;
431 req->data = newSVsv (pathname); 534 req->data = newSVsv (pathname);
432 req->dataptr = SvPV_nolen (req->data); 535 req->dataptr = SvPVbyte_nolen (req->data);
433 req->fd = flags; 536 req->fd = flags;
434 req->mode = mode; 537 req->mode = mode;
435 req->callback = SvREFCNT_inc (callback);
436 538
437 send_req (req); 539 send_req (req);
438} 540}
439 541
440void 542void
441aio_close(fh,callback) 543aio_close(fh,callback=&PL_sv_undef)
442 InputStream fh 544 SV * fh
443 SV * callback 545 SV * callback
444 PROTOTYPE: $$ 546 PROTOTYPE: $;$
445 ALIAS: 547 ALIAS:
446 aio_close = REQ_CLOSE 548 aio_close = REQ_CLOSE
447 aio_fsync = REQ_FSYNC 549 aio_fsync = REQ_FSYNC
448 aio_fdatasync = REQ_FDATASYNC 550 aio_fdatasync = REQ_FDATASYNC
449 CODE: 551 CODE:
450{ 552{
553 dREQ;
554
555 req->type = ix;
556 req->fh = newSVsv (fh);
557 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
558
559 send_req (req);
560}
561
562void
563aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
564 SV * fh
565 UV offset
566 IV length
567 SV * data
568 IV dataoffset
569 SV * callback
570 ALIAS:
571 aio_read = REQ_READ
572 aio_write = REQ_WRITE
573 PROTOTYPE: $$$$$;$
574 CODE:
575{
451 aio_req req; 576 aio_req req;
577 STRLEN svlen;
578 char *svptr = SvPVbyte (data, svlen);
452 579
453 Newz (0, req, 1, aio_cb); 580 SvUPGRADE (data, SVt_PV);
581 SvPOK_on (data);
454 582
455 if (!req) 583 if (dataoffset < 0)
456 croak ("out of memory during aio_req allocation"); 584 dataoffset += svlen;
457 585
458 req->type = ix; 586 if (dataoffset < 0 || dataoffset > svlen)
459 req->fd = PerlIO_fileno (fh); 587 croak ("data offset outside of string");
460 req->callback = SvREFCNT_inc (callback);
461 588
462 send_req (req); 589 if (ix == REQ_WRITE)
463} 590 {
464 591 /* write: check length and adjust. */
465void 592 if (length < 0 || length + dataoffset > svlen)
466aio_read(fh,offset,length,data,dataoffset,callback) 593 length = svlen - dataoffset;
467 InputStream fh 594 }
468 UV offset 595 else
469 IV length 596 {
470 SV * data 597 /* read: grow scalar as necessary */
471 IV dataoffset 598 svptr = SvGROW (data, length + dataoffset);
472 SV * callback 599 }
473 PROTOTYPE: $$$$$$
474 CODE:
475 read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
476
477void
478aio_write(fh,offset,length,data,dataoffset,callback)
479 OutputStream fh
480 UV offset
481 IV length
482 SV * data
483 IV dataoffset
484 SV * callback
485 PROTOTYPE: $$$$$$
486 CODE:
487 read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
488
489void
490aio_readahead(fh,offset,length,callback)
491 InputStream fh
492 UV offset
493 IV length
494 SV * callback
495 PROTOTYPE: $$$$
496 CODE:
497{
498 aio_req req;
499 600
500 if (length < 0) 601 if (length < 0)
501 croak ("length must not be negative"); 602 croak ("length must not be negative");
502 603
503 Newz (0, req, 1, aio_cb); 604 {
605 dREQ;
504 606
505 if (!req) 607 req->type = ix;
506 croak ("out of memory during aio_req allocation"); 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}
620
621void
622aio_readahead(fh,offset,length,callback=&PL_sv_undef)
623 SV * fh
624 UV offset
625 IV length
626 SV * callback
627 PROTOTYPE: $$$;$
628 CODE:
629{
630 dREQ;
507 631
508 req->type = REQ_READAHEAD; 632 req->type = REQ_READAHEAD;
633 req->fh = newSVsv (fh);
509 req->fd = PerlIO_fileno (fh); 634 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
510 req->offset = offset; 635 req->offset = offset;
511 req->length = length; 636 req->length = length;
512 req->callback = SvREFCNT_inc (callback);
513 637
514 send_req (req); 638 send_req (req);
515} 639}
516 640
517void 641void
518aio_stat(fh_or_path,callback) 642aio_stat(fh_or_path,callback=&PL_sv_undef)
519 SV * fh_or_path 643 SV * fh_or_path
520 SV * callback 644 SV * callback
521 PROTOTYPE: $$
522 ALIAS: 645 ALIAS:
646 aio_stat = REQ_STAT
523 aio_lstat = 1 647 aio_lstat = REQ_LSTAT
524 CODE: 648 CODE:
525{ 649{
526 aio_req req; 650 dREQ;
527
528 Newz (0, req, 1, aio_cb);
529
530 if (!req)
531 croak ("out of memory during aio_req allocation");
532 651
533 New (0, req->statdata, 1, Stat_t); 652 New (0, req->statdata, 1, Stat_t);
534
535 if (!req->statdata) 653 if (!req->statdata)
536 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)");
537 655
538 if (SvPOK (fh_or_path)) 656 if (SvPOK (fh_or_path))
539 { 657 {
540 req->type = ix ? REQ_LSTAT : REQ_STAT; 658 req->type = ix;
541 req->data = newSVsv (fh_or_path); 659 req->data = newSVsv (fh_or_path);
542 req->dataptr = SvPV_nolen (req->data); 660 req->dataptr = SvPVbyte_nolen (req->data);
543 } 661 }
544 else 662 else
545 { 663 {
546 req->type = REQ_FSTAT; 664 req->type = REQ_FSTAT;
665 req->fh = newSVsv (fh_or_path);
547 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 666 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
548 } 667 }
549 668
550 req->callback = SvREFCNT_inc (callback);
551
552 send_req (req); 669 send_req (req);
553} 670}
554 671
555void 672void
556aio_unlink(pathname,callback) 673aio_unlink(pathname,callback=&PL_sv_undef)
557 SV * pathname 674 SV * pathname
558 SV * callback 675 SV * callback
559 PROTOTYPE: $$ 676 ALIAS:
677 aio_unlink = REQ_UNLINK
678 aio_rmdir = REQ_RMDIR
560 CODE: 679 CODE:
561{ 680{
562 aio_req req; 681 dREQ;
563 682
564 Newz (0, req, 1, aio_cb); 683 req->type = ix;
684 req->data = newSVsv (pathname);
685 req->dataptr = SvPVbyte_nolen (req->data);
565 686
566 if (!req) 687 send_req (req);
567 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;
568 698
569 req->type = REQ_UNLINK; 699 req->type = REQ_SYMLINK;
700 req->fh = newSVsv (oldpath);
701 req->data2ptr = SvPVbyte_nolen (req->fh);
570 req->data = newSVsv (pathname); 702 req->data = newSVsv (newpath);
571 req->dataptr = SvPV_nolen (req->data); 703 req->dataptr = SvPVbyte_nolen (req->data);
572 req->callback = SvREFCNT_inc (callback);
573 704
574 send_req (req); 705 send_req (req);
575} 706}
576 707
577void 708void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines