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.35 by root, Tue Aug 23 01:16:50 2005 UTC vs.
Revision 1.43 by root, Sat Oct 21 23:06:04 2006 UTC

7 7
8#include "autoconf/config.h" 8#include "autoconf/config.h"
9 9
10#include <pthread.h> 10#include <pthread.h>
11 11
12#include <stddef.h>
13#include <errno.h>
12#include <sys/types.h> 14#include <sys/types.h>
13#include <sys/stat.h> 15#include <sys/stat.h>
14 16#include <limits.h>
15#include <unistd.h> 17#include <unistd.h>
16#include <fcntl.h> 18#include <fcntl.h>
17#include <signal.h> 19#include <signal.h>
18#include <sched.h> 20#include <sched.h>
19 21
30# else 32# else
31# error sendfile support requested but not available 33# error sendfile support requested but not available
32# endif 34# endif
33#endif 35#endif
34 36
37/* used for struct dirent, AIX doesn't provide it */
38#ifndef NAME_MAX
39# define NAME_MAX 4096
40#endif
41
35#if __ia64 42#if __ia64
36# define STACKSIZE 65536 43# define STACKSIZE 65536
37#else 44#else
38# define STACKSIZE 4096 45# define STACKSIZE 8192
39#endif 46#endif
40 47
41enum { 48enum {
42 REQ_QUIT, 49 REQ_QUIT,
43 REQ_OPEN, REQ_CLOSE, 50 REQ_OPEN, REQ_CLOSE,
44 REQ_READ, REQ_WRITE, REQ_READAHEAD, 51 REQ_READ, REQ_WRITE, REQ_READAHEAD,
45 REQ_SENDFILE, 52 REQ_SENDFILE,
46 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 53 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
47 REQ_FSYNC, REQ_FDATASYNC, 54 REQ_FSYNC, REQ_FDATASYNC,
48 REQ_UNLINK, REQ_RMDIR, 55 REQ_UNLINK, REQ_RMDIR, REQ_RENAME,
49 REQ_SYMLINK, 56 REQ_READDIR,
57 REQ_LINK, REQ_SYMLINK,
50}; 58};
51 59
60#define AIO_CB_KLASS "IO::AIO::CB"
61
52typedef struct aio_cb { 62typedef struct aio_cb
63{
64 struct aio_cb *grp_prev, *grp_next;
65 struct aio_grp *grp;
66
53 struct aio_cb *volatile next; 67 struct aio_cb *volatile next;
54 68
55 int type; 69 SV *self; /* the perl counterpart of this request, if any */
56 70
57 int fd, fd2; 71 SV *data, *callback;
72 SV *fh, *fh2;
73 void *dataptr, *data2ptr;
74 Stat_t *statdata;
58 off_t offset; 75 off_t offset;
59 size_t length; 76 size_t length;
60 ssize_t result; 77 ssize_t result;
78
79 int type;
80 int fd, fd2;
81 int errorno;
82 STRLEN dataoffset;
61 mode_t mode; /* open */ 83 mode_t mode; /* open */
62 int errorno; 84 unsigned char cancelled;
63 SV *data, *callback;
64 SV *fh, *fh2;
65 void *dataptr, *data2ptr;
66 STRLEN dataoffset;
67
68 Stat_t *statdata;
69} aio_cb; 85} aio_cb;
70 86
71typedef aio_cb *aio_req; 87typedef aio_cb *aio_req;
88typedef aio_cb *aio_req_ornot;
72 89
73static int started, wanted; 90static int started, wanted;
74static volatile int nreqs; 91static volatile int nreqs;
75static int max_outstanding = 1<<30; 92static int max_outstanding = 1<<30;
76static int respipe [2]; 93static int respipe [2];
80static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 97static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
81 98
82static volatile aio_req reqs, reqe; /* queue start, queue end */ 99static volatile aio_req reqs, reqe; /* queue start, queue end */
83static volatile aio_req ress, rese; /* queue start, queue end */ 100static volatile aio_req ress, rese; /* queue start, queue end */
84 101
102typedef struct aio_grp
103{
104 struct aio_cb *first, *last;
105 SV *callback;
106 int busycount;
107} aio_grp;
108
109static void aio_grp_begin (aio_grp *grp)
110{
111 ++grp->busycount;
112}
113
114static void aio_grp_end (aio_grp *grp)
115{
116 --grp->busycount;
117
118 if (grp->busycount)
119 return;
120
121 SvREFCNT_dec (grp->callback);
122 grp->callback = 0;
123}
124
125static aio_grp *aio_grp_new ()
126{
127 aio_grp *grp;
128
129 Newz (0, grp, 1, aio_grp);
130 aio_grp_begin (grp);
131
132 return grp;
133}
134
135/* must be called at most once */
136static SV *req_sv (aio_req req)
137{
138 req->self = (SV *)newHV ();
139 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
140
141 return sv_bless (newRV_noinc (req->self), gv_stashpv (AIO_CB_KLASS, 1));
142}
143
144static aio_req SvAIO_REQ (SV *sv)
145{
146 if (!sv_derived_from (sv, AIO_CB_KLASS) || !SvROK (sv))
147 croak ("object of class " AIO_CB_KLASS " expected");
148
149 MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
150
151 return mg ? (aio_req)mg->mg_ptr : 0;
152}
153
85static void free_req (aio_req req) 154static void req_free (aio_req req)
86{ 155{
156 if (req->self)
157 {
158 sv_unmagic (req->self, PERL_MAGIC_ext);
159 SvREFCNT_dec (req->self);
160 }
161
87 if (req->data) 162 if (req->data)
88 SvREFCNT_dec (req->data); 163 SvREFCNT_dec (req->data);
89 164
90 if (req->fh) 165 if (req->fh)
91 SvREFCNT_dec (req->fh); 166 SvREFCNT_dec (req->fh);
96 if (req->statdata) 171 if (req->statdata)
97 Safefree (req->statdata); 172 Safefree (req->statdata);
98 173
99 if (req->callback) 174 if (req->callback)
100 SvREFCNT_dec (req->callback); 175 SvREFCNT_dec (req->callback);
176
177 if (req->type == REQ_READDIR && req->result >= 0)
178 free (req->data2ptr);
101 179
102 Safefree (req); 180 Safefree (req);
103} 181}
104 182
105static void 183static void
170 PL_statcache = *(req->statdata); 248 PL_statcache = *(req->statdata);
171 } 249 }
172 250
173 ENTER; 251 ENTER;
174 PUSHMARK (SP); 252 PUSHMARK (SP);
175 XPUSHs (sv_2mortal (newSViv (req->result)));
176 253
177 if (req->type == REQ_OPEN) 254 if (req->type == REQ_READDIR)
178 { 255 {
179 /* convert fd to fh */ 256 SV *rv = &PL_sv_undef;
180 SV *fh;
181 257
182 PUTBACK; 258 if (req->result >= 0)
183 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 259 {
184 SPAGAIN; 260 char *buf = req->data2ptr;
261 AV *av = newAV ();
185 262
186 fh = SvREFCNT_inc (POPs); 263 while (req->result)
264 {
265 SV *sv = newSVpv (buf, 0);
187 266
267 av_push (av, sv);
268 buf += SvCUR (sv) + 1;
269 req->result--;
270 }
271
272 rv = sv_2mortal (newRV_noinc ((SV *)av));
273 }
274
188 PUSHMARK (SP); 275 XPUSHs (rv);
189 XPUSHs (sv_2mortal (fh));
190 } 276 }
277 else
278 {
279 XPUSHs (sv_2mortal (newSViv (req->result)));
191 280
281 if (req->type == REQ_OPEN)
282 {
283 /* convert fd to fh */
284 SV *fh;
285
286 PUTBACK;
287 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
288 SPAGAIN;
289
290 fh = SvREFCNT_inc (POPs);
291
292 PUSHMARK (SP);
293 XPUSHs (sv_2mortal (fh));
294 }
295 }
296
192 if (SvOK (req->callback)) 297 if (SvOK (req->callback) && !req->cancelled)
193 { 298 {
194 PUTBACK; 299 PUTBACK;
195 call_sv (req->callback, G_VOID | G_EVAL); 300 call_sv (req->callback, G_VOID | G_EVAL);
196 SPAGAIN; 301 SPAGAIN;
197 302
198 if (SvTRUE (ERRSV)) 303 if (SvTRUE (ERRSV))
199 { 304 {
200 free_req (req); 305 req_free (req);
201 croak (0); 306 croak (0);
202 } 307 }
203 } 308 }
204 309
205 LEAVE; 310 LEAVE;
206 311
207 errno = errorno; 312 errno = errorno;
208 count++; 313 count++;
209 } 314 }
210 315
211 free_req (req); 316 req_free (req);
212 } 317 }
213 318
214 return count; 319 return count;
215} 320}
216 321
235 340
236 sigprocmask (SIG_SETMASK, &oldsigset, 0); 341 sigprocmask (SIG_SETMASK, &oldsigset, 0);
237} 342}
238 343
239static void 344static void
240send_req (aio_req req) 345req_send (aio_req req)
241{ 346{
242 while (started < wanted && nreqs >= started) 347 while (started < wanted && nreqs >= started)
243 start_thread (); 348 start_thread ();
244 349
245 nreqs++; 350 nreqs++;
276{ 381{
277 aio_req req; 382 aio_req req;
278 Newz (0, req, 1, aio_cb); 383 Newz (0, req, 1, aio_cb);
279 req->type = REQ_QUIT; 384 req->type = REQ_QUIT;
280 385
281 send_req (req); 386 req_send (req);
282} 387}
283 388
284static void min_parallel (int nthreads) 389static void min_parallel (int nthreads)
285{ 390{
286 if (wanted < nthreads) 391 if (wanted < nthreads)
316 croak ("cannot set result pipe to nonblocking mode"); 421 croak ("cannot set result pipe to nonblocking mode");
317 422
318 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) 423 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
319 croak ("cannot set result pipe to nonblocking mode"); 424 croak ("cannot set result pipe to nonblocking mode");
320} 425}
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{
336 aio_req prv;
337
338 started = 0;
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/* currently noops */
366#define LOCK_FD(fd) do { } while (0)
367#define UNLOCK_FD(fd) do { } while (0)
368 426
369/*****************************************************************************/ 427/*****************************************************************************/
370/* work around various missing functions */ 428/* work around various missing functions */
371 429
372#if !HAVE_PREADWRITE 430#if !HAVE_PREADWRITE
376/* 434/*
377 * make our pread/pwrite safe against themselves, but not against 435 * make our pread/pwrite safe against themselves, but not against
378 * normal read/write by using a mutex. slows down execution a lot, 436 * normal read/write by using a mutex. slows down execution a lot,
379 * but that's your problem, not mine. 437 * but that's your problem, not mine.
380 */ 438 */
381static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; 439static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER;
382 440
383static ssize_t 441static ssize_t
384pread (int fd, void *buf, size_t count, off_t offset) 442pread (int fd, void *buf, size_t count, off_t offset)
385{ 443{
386 ssize_t res; 444 ssize_t res;
387 off_t ooffset; 445 off_t ooffset;
388 446
389 LOCK_FD (fd); 447 pthread_mutex_lock (&preadwritelock);
390 pthread_mutex_lock (&iolock); /* replace by LOCK_FD and private buffer */
391 ooffset = lseek (fd, 0, SEEK_CUR); 448 ooffset = lseek (fd, 0, SEEK_CUR);
392 lseek (fd, offset, SEEK_SET); 449 lseek (fd, offset, SEEK_SET);
393 res = read (fd, buf, count); 450 res = read (fd, buf, count);
394 lseek (fd, ooffset, SEEK_SET); 451 lseek (fd, ooffset, SEEK_SET);
395 pthread_mutex_unlock (&iolock); 452 pthread_mutex_unlock (&preadwritelock);
396 UNLOCK_FD (d);
397 453
398 return res; 454 return res;
399} 455}
400 456
401static ssize_t 457static ssize_t
402pwrite (int fd, void *buf, size_t count, off_t offset) 458pwrite (int fd, void *buf, size_t count, off_t offset)
403{ 459{
404 ssize_t res; 460 ssize_t res;
405 off_t ooffset; 461 off_t ooffset;
406 462
407 LOCK_FD (fd); 463 pthread_mutex_lock (&preadwritelock);
408 pthread_mutex_lock (&iolock); /* replace by LOCK_FD and private buffer */
409 ooffset = lseek (fd, 0, SEEK_CUR); 464 ooffset = lseek (fd, 0, SEEK_CUR);
410 lseek (fd, offset, SEEK_SET); 465 lseek (fd, offset, SEEK_SET);
411 res = write (fd, buf, count); 466 res = write (fd, buf, count);
412 lseek (fd, offset, SEEK_SET); 467 lseek (fd, offset, SEEK_SET);
413 pthread_mutex_unlock (&iolock); 468 pthread_mutex_unlock (&preadwritelock);
414 UNLOCK_FD (d);
415 469
416 return res; 470 return res;
417} 471}
418#endif 472#endif
419 473
422#endif 476#endif
423 477
424#if !HAVE_READAHEAD 478#if !HAVE_READAHEAD
425# define readahead aio_readahead 479# define readahead aio_readahead
426 480
427static char readahead_buf[4096];
428
429static ssize_t 481static ssize_t
430readahead (int fd, off_t offset, size_t count) 482readahead (int fd, off_t offset, size_t count)
431{ 483{
484 char readahead_buf[4096];
485
432 while (count > 0) 486 while (count > 0)
433 { 487 {
434 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 488 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
435 489
436 pread (fd, readahead_buf, len, offset); 490 pread (fd, readahead_buf, len, offset);
440 494
441 errno = 0; 495 errno = 0;
442} 496}
443#endif 497#endif
444 498
499#if !HAVE_READDIR_R
500# define readdir_r aio_readdir_r
501
502static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER;
503
504static int
505readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
506{
507 struct dirent *e;
508 int errorno;
509
510 pthread_mutex_lock (&readdirlock);
511
512 e = readdir (dirp);
513 errorno = errno;
514
515 if (e)
516 {
517 *res = ent;
518 strcpy (ent->d_name, e->d_name);
519 }
520 else
521 *res = 0;
522
523 pthread_mutex_unlock (&readdirlock);
524
525 errno = errorno;
526 return e ? 0 : -1;
527}
528#endif
529
445/* sendfile always needs emulation */ 530/* sendfile always needs emulation */
446static ssize_t 531static ssize_t
447sendfile_ (int ofd, int ifd, off_t offset, size_t count) 532sendfile_ (int ofd, int ifd, off_t offset, size_t count)
448{ 533{
449 ssize_t res; 534 ssize_t res;
450 535
451 if (!count) 536 if (!count)
452 return 0; 537 return 0;
453
454 LOCK_FD (ofd);
455 538
456#if HAVE_SENDFILE 539#if HAVE_SENDFILE
457# if __linux 540# if __linux
458 res = sendfile (ofd, ifd, &offset, count); 541 res = sendfile (ofd, ifd, &offset, count);
459 542
460# elif __freebsd 543# elif __freebsd
461 /* 544 /*
462 * Of course, the freebsd sendfile is a dire hack with no thoughts 545 * Of course, the freebsd sendfile is a dire hack with no thoughts
463 * wasted on making it similar to other I/O functions. 546 * wasted on making it similar to other I/O functions.
464 */ 547 */
465 { 548 {
466 off_t sbytes; 549 off_t sbytes;
467 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 550 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
468 551
469 if (res < 0 && sbytes) 552 if (res < 0 && sbytes)
470 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ 553 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */
471 res = sbytes; 554 res = sbytes;
472 } 555 }
473 556
474# elif __hpux 557# elif __hpux
475 res = sendfile (ofd, ifd, offset, count, 0, 0); 558 res = sendfile (ofd, ifd, offset, count, 0, 0);
476 559
477# elif __solaris 560# elif __solaris
478 { 561 {
479 struct sendfilevec vec; 562 struct sendfilevec vec;
480 size_t sbytes; 563 size_t sbytes;
481 564
482 vec.sfv_fd = ifd; 565 vec.sfv_fd = ifd;
483 vec.sfv_flag = 0; 566 vec.sfv_flag = 0;
484 vec.sfv_off = offset; 567 vec.sfv_off = offset;
485 vec.sfv_len = count; 568 vec.sfv_len = count;
486 569
487 res = sendfilev (ofd, &vec, 1, &sbytes); 570 res = sendfilev (ofd, &vec, 1, &sbytes);
488 571
489 if (res < 0 && sbytes) 572 if (res < 0 && sbytes)
490 res = sbytes; 573 res = sbytes;
491 } 574 }
492 575
493# else
494 res = -1;
495 errno = ENOSYS;
496# endif 576# endif
577#else
578 res = -1;
579 errno = ENOSYS;
497#endif 580#endif
498 581
499 if (res < 0 582 if (res < 0
500 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK 583 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
501#if __solaris 584#if __solaris
502 || errno == EAFNOSUPPORT || errno == EPROTOTYPE 585 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
503#endif 586#endif
504 ) 587 )
505 ) 588 )
506 { 589 {
507 /* emulate sendfile. this is a major pain in the ass */ 590 /* emulate sendfile. this is a major pain in the ass */
508 char *buf = malloc (4096); 591 char buf[4096];
509 res = 0; 592 res = 0;
510 593
511 for (;;) 594 while (count)
512 { 595 {
513 ssize_t cnt; 596 ssize_t cnt;
514 597
515 cnt = pread (ifd, buf, 4096, offset); 598 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset);
516 599
517 if (cnt <= 0) 600 if (cnt <= 0)
518 { 601 {
519 if (cnt && !res) res = -1; 602 if (cnt && !res) res = -1;
520 break; 603 break;
521 } 604 }
522 605
523 cnt = write (ofd, buf, cnt); 606 cnt = write (ofd, buf, cnt);
524 607
525 if (cnt <= 0) 608 if (cnt <= 0)
526 { 609 {
527 if (cnt && !res) res = -1; 610 if (cnt && !res) res = -1;
528 break; 611 break;
529 } 612 }
530 613
531 offset += cnt; 614 offset += cnt;
532 res += cnt; 615 res += cnt;
616 count -= cnt;
533 } 617 }
534
535 {
536 int errorno = errno;
537 free (buf);
538 errno = errorno;
539 }
540 } 618 }
541 619
542 UNLOCK_FD (ofd);
543
544 return res; 620 return res;
621}
622
623/* read a full directory */
624static int
625scandir_ (const char *path, void **namesp)
626{
627 DIR *dirp = opendir (path);
628 union
629 {
630 struct dirent d;
631 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
632 } u;
633 struct dirent *entp;
634 char *name, *names;
635 int memlen = 4096;
636 int memofs = 0;
637 int res = 0;
638 int errorno;
639
640 if (!dirp)
641 return -1;
642
643 names = malloc (memlen);
644
645 for (;;)
646 {
647 errno = 0, readdir_r (dirp, &u.d, &entp);
648
649 if (!entp)
650 break;
651
652 name = entp->d_name;
653
654 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
655 {
656 int len = strlen (name) + 1;
657
658 res++;
659
660 while (memofs + len > memlen)
661 {
662 memlen *= 2;
663 names = realloc (names, memlen);
664 if (!names)
665 break;
666 }
667
668 memcpy (names + memofs, name, len);
669 memofs += len;
670 }
671 }
672
673 errorno = errno;
674 closedir (dirp);
675
676 if (errorno)
677 {
678 free (names);
679 errno = errorno;
680 res = -1;
681 }
682
683 *namesp = (void *)names;
684 return res;
545} 685}
546 686
547/*****************************************************************************/ 687/*****************************************************************************/
548 688
549static void * 689static void *
574 714
575 pthread_mutex_unlock (&reqlock); 715 pthread_mutex_unlock (&reqlock);
576 716
577 errno = 0; /* strictly unnecessary */ 717 errno = 0; /* strictly unnecessary */
578 718
579 type = req->type; 719 if (!req->cancelled)
580
581 switch (type) 720 switch (req->type)
582 { 721 {
583 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 722 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
584 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 723 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
585 724
586 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 725 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
587 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; 726 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break;
588 727
589 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 728 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
590 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 729 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
591 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 730 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
592 731
593 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; 732 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
594 case REQ_CLOSE: req->result = close (req->fd); break; 733 case REQ_CLOSE: req->result = close (req->fd); break;
595 case REQ_UNLINK: req->result = unlink (req->dataptr); break; 734 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
596 case REQ_RMDIR: req->result = rmdir (req->dataptr); break; 735 case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
736 case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break;
737 case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break;
597 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; 738 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
598 739
599 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 740 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
600 case REQ_FSYNC: req->result = fsync (req->fd); break; 741 case REQ_FSYNC: req->result = fsync (req->fd); break;
742 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break;
601 743
602 case REQ_QUIT: 744 case REQ_QUIT:
603 break; 745 break;
604 746
605 default: 747 default:
606 req->result = ENOSYS; 748 req->result = ENOSYS;
607 break; 749 break;
608 } 750 }
609 751
610 req->errorno = errno; 752 req->errorno = errno;
611 753
612 pthread_mutex_lock (&reslock); 754 pthread_mutex_lock (&reslock);
613 755
631 while (type != REQ_QUIT); 773 while (type != REQ_QUIT);
632 774
633 return 0; 775 return 0;
634} 776}
635 777
778/*****************************************************************************/
779
780static void atfork_prepare (void)
781{
782 pthread_mutex_lock (&reqlock);
783 pthread_mutex_lock (&reslock);
784#if !HAVE_PREADWRITE
785 pthread_mutex_lock (&preadwritelock);
786#endif
787#if !HAVE_READDIR_R
788 pthread_mutex_lock (&readdirlock);
789#endif
790}
791
792static void atfork_parent (void)
793{
794#if !HAVE_READDIR_R
795 pthread_mutex_unlock (&readdirlock);
796#endif
797#if !HAVE_PREADWRITE
798 pthread_mutex_unlock (&preadwritelock);
799#endif
800 pthread_mutex_unlock (&reslock);
801 pthread_mutex_unlock (&reqlock);
802}
803
804static void atfork_child (void)
805{
806 aio_req prv;
807
808 started = 0;
809
810 while (reqs)
811 {
812 prv = reqs;
813 reqs = prv->next;
814 req_free (prv);
815 }
816
817 reqs = reqe = 0;
818
819 while (ress)
820 {
821 prv = ress;
822 ress = prv->next;
823 req_free (prv);
824 }
825
826 ress = rese = 0;
827
828 close (respipe [0]);
829 close (respipe [1]);
830 create_pipe ();
831
832 atfork_parent ();
833}
834
636#define dREQ \ 835#define dREQ \
637 aio_req req; \ 836 aio_req req; \
638 \ 837 \
639 if (SvOK (callback) && !SvROK (callback)) \ 838 if (SvOK (callback) && !SvROK (callback)) \
640 croak ("clalback must be undef or of reference type"); \ 839 croak ("callback must be undef or of reference type"); \
641 \ 840 \
642 Newz (0, req, 1, aio_cb); \ 841 Newz (0, req, 1, aio_cb); \
643 if (!req) \ 842 if (!req) \
644 croak ("out of memory during aio_req allocation"); \ 843 croak ("out of memory during aio_req allocation"); \
645 \ 844 \
646 req->callback = newSVsv (callback); 845 req->callback = newSVsv (callback)
846
847#define REQ_SEND \
848 req_send (req); \
849 \
850 if (GIMME_V != G_VOID) \
851 XPUSHs (req_sv (req));
647 852
648MODULE = IO::AIO PACKAGE = IO::AIO 853MODULE = IO::AIO PACKAGE = IO::AIO
649 854
650PROTOTYPES: ENABLE 855PROTOTYPES: ENABLE
651 856
652BOOT: 857BOOT:
653{ 858{
859 HV *stash = gv_stashpv ("IO::AIO", 1);
860 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
861 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
862 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
863
654 create_pipe (); 864 create_pipe ();
655 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 865 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
656} 866}
657 867
658void 868void
659min_parallel(nthreads) 869min_parallel (nthreads)
660 int nthreads 870 int nthreads
661 PROTOTYPE: $ 871 PROTOTYPE: $
662 872
663void 873void
664max_parallel(nthreads) 874max_parallel (nthreads)
665 int nthreads 875 int nthreads
666 PROTOTYPE: $ 876 PROTOTYPE: $
667 877
668int 878int
669max_outstanding(nreqs) 879max_outstanding (nreqs)
670 int nreqs 880 int nreqs
671 PROTOTYPE: $ 881 PROTOTYPE: $
672 CODE: 882 CODE:
673 RETVAL = max_outstanding; 883 RETVAL = max_outstanding;
674 max_outstanding = nreqs; 884 max_outstanding = nreqs;
675 885
676void 886void
677aio_open(pathname,flags,mode,callback=&PL_sv_undef) 887aio_open (pathname,flags,mode,callback=&PL_sv_undef)
678 SV * pathname 888 SV * pathname
679 int flags 889 int flags
680 int mode 890 int mode
681 SV * callback 891 SV * callback
682 PROTOTYPE: $$$;$ 892 PROTOTYPE: $$$;$
683 CODE: 893 PPCODE:
684{ 894{
685 dREQ; 895 dREQ;
686 896
687 req->type = REQ_OPEN; 897 req->type = REQ_OPEN;
688 req->data = newSVsv (pathname); 898 req->data = newSVsv (pathname);
689 req->dataptr = SvPVbyte_nolen (req->data); 899 req->dataptr = SvPVbyte_nolen (req->data);
690 req->fd = flags; 900 req->fd = flags;
691 req->mode = mode; 901 req->mode = mode;
692 902
693 send_req (req); 903 REQ_SEND;
694} 904}
695 905
696void 906void
697aio_close(fh,callback=&PL_sv_undef) 907aio_close (fh,callback=&PL_sv_undef)
698 SV * fh 908 SV * fh
699 SV * callback 909 SV * callback
700 PROTOTYPE: $;$ 910 PROTOTYPE: $;$
701 ALIAS: 911 ALIAS:
702 aio_close = REQ_CLOSE 912 aio_close = REQ_CLOSE
703 aio_fsync = REQ_FSYNC 913 aio_fsync = REQ_FSYNC
704 aio_fdatasync = REQ_FDATASYNC 914 aio_fdatasync = REQ_FDATASYNC
705 CODE: 915 PPCODE:
706{ 916{
707 dREQ; 917 dREQ;
708 918
709 req->type = ix; 919 req->type = ix;
710 req->fh = newSVsv (fh); 920 req->fh = newSVsv (fh);
711 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 921 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
712 922
713 send_req (req); 923 REQ_SEND (req);
714} 924}
715 925
716void 926void
717aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 927aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
718 SV * fh 928 SV * fh
719 UV offset 929 UV offset
720 UV length 930 UV length
721 SV * data 931 SV * data
722 UV dataoffset 932 UV dataoffset
723 SV * callback 933 SV * callback
724 ALIAS: 934 ALIAS:
725 aio_read = REQ_READ 935 aio_read = REQ_READ
726 aio_write = REQ_WRITE 936 aio_write = REQ_WRITE
727 PROTOTYPE: $$$$$;$ 937 PROTOTYPE: $$$$$;$
728 CODE: 938 PPCODE:
729{ 939{
730 aio_req req; 940 aio_req req;
731 STRLEN svlen; 941 STRLEN svlen;
732 char *svptr = SvPVbyte (data, svlen); 942 char *svptr = SvPVbyte (data, svlen);
733 943
771 { 981 {
772 SvREADONLY_on (data); 982 SvREADONLY_on (data);
773 req->data2ptr = (void *)data; 983 req->data2ptr = (void *)data;
774 } 984 }
775 985
776 send_req (req); 986 REQ_SEND;
777 } 987 }
778} 988}
779 989
780void 990void
781aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) 991aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef)
782 SV * out_fh 992 SV * out_fh
783 SV * in_fh 993 SV * in_fh
784 UV in_offset 994 UV in_offset
785 UV length 995 UV length
786 SV * callback 996 SV * callback
787 PROTOTYPE: $$$$;$ 997 PROTOTYPE: $$$$;$
788 CODE: 998 PPCODE:
789{ 999{
790 dREQ; 1000 dREQ;
791 1001
792 req->type = REQ_SENDFILE; 1002 req->type = REQ_SENDFILE;
793 req->fh = newSVsv (out_fh); 1003 req->fh = newSVsv (out_fh);
795 req->fh2 = newSVsv (in_fh); 1005 req->fh2 = newSVsv (in_fh);
796 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); 1006 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
797 req->offset = in_offset; 1007 req->offset = in_offset;
798 req->length = length; 1008 req->length = length;
799 1009
800 send_req (req); 1010 REQ_SEND;
801} 1011}
802 1012
803void 1013void
804aio_readahead(fh,offset,length,callback=&PL_sv_undef) 1014aio_readahead (fh,offset,length,callback=&PL_sv_undef)
805 SV * fh 1015 SV * fh
806 UV offset 1016 UV offset
807 IV length 1017 IV length
808 SV * callback 1018 SV * callback
809 PROTOTYPE: $$$;$ 1019 PROTOTYPE: $$$;$
810 CODE: 1020 PPCODE:
811{ 1021{
812 dREQ; 1022 dREQ;
813 1023
814 req->type = REQ_READAHEAD; 1024 req->type = REQ_READAHEAD;
815 req->fh = newSVsv (fh); 1025 req->fh = newSVsv (fh);
816 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 1026 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
817 req->offset = offset; 1027 req->offset = offset;
818 req->length = length; 1028 req->length = length;
819 1029
820 send_req (req); 1030 REQ_SEND;
821} 1031}
822 1032
823void 1033void
824aio_stat(fh_or_path,callback=&PL_sv_undef) 1034aio_stat (fh_or_path,callback=&PL_sv_undef)
825 SV * fh_or_path 1035 SV * fh_or_path
826 SV * callback 1036 SV * callback
827 ALIAS: 1037 ALIAS:
828 aio_stat = REQ_STAT 1038 aio_stat = REQ_STAT
829 aio_lstat = REQ_LSTAT 1039 aio_lstat = REQ_LSTAT
830 CODE: 1040 PPCODE:
831{ 1041{
832 dREQ; 1042 dREQ;
833 1043
834 New (0, req->statdata, 1, Stat_t); 1044 New (0, req->statdata, 1, Stat_t);
835 if (!req->statdata) 1045 if (!req->statdata)
836 { 1046 {
837 free_req (req); 1047 req_free (req);
838 croak ("out of memory during aio_req->statdata allocation"); 1048 croak ("out of memory during aio_req->statdata allocation");
839 } 1049 }
840 1050
841 if (SvPOK (fh_or_path)) 1051 if (SvPOK (fh_or_path))
842 { 1052 {
849 req->type = REQ_FSTAT; 1059 req->type = REQ_FSTAT;
850 req->fh = newSVsv (fh_or_path); 1060 req->fh = newSVsv (fh_or_path);
851 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 1061 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
852 } 1062 }
853 1063
854 send_req (req); 1064 REQ_SEND;
855} 1065}
856 1066
857void 1067void
858aio_unlink(pathname,callback=&PL_sv_undef) 1068aio_unlink (pathname,callback=&PL_sv_undef)
859 SV * pathname 1069 SV * pathname
860 SV * callback 1070 SV * callback
861 ALIAS: 1071 ALIAS:
862 aio_unlink = REQ_UNLINK 1072 aio_unlink = REQ_UNLINK
863 aio_rmdir = REQ_RMDIR 1073 aio_rmdir = REQ_RMDIR
1074 aio_readdir = REQ_READDIR
864 CODE: 1075 PPCODE:
865{ 1076{
866 dREQ; 1077 dREQ;
867 1078
868 req->type = ix; 1079 req->type = ix;
869 req->data = newSVsv (pathname); 1080 req->data = newSVsv (pathname);
870 req->dataptr = SvPVbyte_nolen (req->data); 1081 req->dataptr = SvPVbyte_nolen (req->data);
871 1082
872 send_req (req); 1083 REQ_SEND;
873} 1084}
874 1085
875void 1086void
876aio_symlink(oldpath,newpath,callback=&PL_sv_undef) 1087aio_link (oldpath,newpath,callback=&PL_sv_undef)
877 SV * oldpath 1088 SV * oldpath
878 SV * newpath 1089 SV * newpath
879 SV * callback 1090 SV * callback
1091 ALIAS:
1092 aio_link = REQ_LINK
1093 aio_symlink = REQ_SYMLINK
1094 aio_rename = REQ_RENAME
880 CODE: 1095 PPCODE:
881{ 1096{
882 dREQ; 1097 dREQ;
883 1098
884 req->type = REQ_SYMLINK; 1099 req->type = ix;
885 req->fh = newSVsv (oldpath); 1100 req->fh = newSVsv (oldpath);
886 req->data2ptr = SvPVbyte_nolen (req->fh); 1101 req->data2ptr = SvPVbyte_nolen (req->fh);
887 req->data = newSVsv (newpath); 1102 req->data = newSVsv (newpath);
888 req->dataptr = SvPVbyte_nolen (req->data); 1103 req->dataptr = SvPVbyte_nolen (req->data);
889 1104
890 send_req (req); 1105 REQ_SEND;
891} 1106}
892 1107
1108#if 0
1109
1110# undocumented, because it does not cancel active requests
893void 1111void
1112cancel_most_requests ()
1113 PROTOTYPE:
1114 CODE:
1115{
1116 aio_req *req;
1117
1118 pthread_mutex_lock (&reqlock);
1119 for (req = reqs; req; req = req->next)
1120 req->flags |= 1;
1121 pthread_mutex_unlock (&reqlock);
1122
1123 pthread_mutex_lock (&reslock);
1124 for (req = ress; req; req = req->next)
1125 req->flags |= 1;
1126 pthread_mutex_unlock (&reslock);
1127}
1128
1129#endif
1130
1131void
894flush() 1132flush ()
895 PROTOTYPE: 1133 PROTOTYPE:
896 CODE: 1134 CODE:
897 while (nreqs) 1135 while (nreqs)
898 { 1136 {
899 poll_wait (); 1137 poll_wait ();
939 CODE: 1177 CODE:
940 RETVAL = nreqs; 1178 RETVAL = nreqs;
941 OUTPUT: 1179 OUTPUT:
942 RETVAL 1180 RETVAL
943 1181
1182MODULE = IO::AIO PACKAGE = IO::AIO::CB
1183
1184void
1185cancel (aio_req_ornot req)
1186 PROTOTYPE:
1187 CODE:
1188 req->cancelled = 1;
1189

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines