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.91 by root, Wed Nov 8 01:57:43 2006 UTC vs.
Revision 1.102 by root, Sun Jun 3 09:44:17 2007 UTC

1/* solaris */ 1#include "xthread.h"
2#define _POSIX_PTHREAD_SEMANTICS 1
3
4#if __linux && !defined(_GNU_SOURCE)
5# define _GNU_SOURCE
6#endif
7
8/* just in case */
9#define _REENTRANT 1
10 2
11#include <errno.h> 3#include <errno.h>
12 4
13#include "EXTERN.h" 5#include "EXTERN.h"
14#include "perl.h" 6#include "perl.h"
15#include "XSUB.h" 7#include "XSUB.h"
16 8
17#include "autoconf/config.h" 9#include "autoconf/config.h"
18 10
19#include <pthread.h>
20
21#include <stddef.h> 11#include <stddef.h>
12#include <stdlib.h>
22#include <errno.h> 13#include <errno.h>
23#include <sys/time.h> 14#include <sys/time.h>
24#include <sys/select.h> 15#include <sys/select.h>
25#include <sys/types.h> 16#include <sys/types.h>
26#include <sys/stat.h> 17#include <sys/stat.h>
27#include <limits.h> 18#include <limits.h>
28#include <unistd.h> 19#include <unistd.h>
29#include <fcntl.h> 20#include <fcntl.h>
30#include <signal.h> 21#include <signal.h>
31#include <sched.h> 22#include <sched.h>
23#include <utime.h>
32 24
33#if HAVE_SENDFILE 25#if HAVE_SENDFILE
34# if __linux 26# if __linux
35# include <sys/sendfile.h> 27# include <sys/sendfile.h>
36# elif __freebsd 28# elif __freebsd
51/* used for struct dirent, AIX doesn't provide it */ 43/* used for struct dirent, AIX doesn't provide it */
52#ifndef NAME_MAX 44#ifndef NAME_MAX
53# define NAME_MAX 4096 45# define NAME_MAX 4096
54#endif 46#endif
55 47
56#ifndef PTHREAD_STACK_MIN
57/* care for broken platforms, e.g. windows */
58# define PTHREAD_STACK_MIN 16384
59#endif
60
61#if __ia64
62# define STACKSIZE 65536
63#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
64# define STACKSIZE PTHREAD_STACK_MIN
65#else
66# define STACKSIZE 16384
67#endif
68
69/* wether word reads are potentially non-atomic.
70 * this is conservatice, likely most arches this runs
71 * on have atomic word read/writes.
72 */
73#ifndef WORDACCESS_UNSAFE
74# if __i386 || __x86_64
75# define WORDACCESS_UNSAFE 0
76# else
77# define WORDACCESS_UNSAFE 1
78# endif
79#endif
80
81/* buffer size for various temporary buffers */ 48/* buffer size for various temporary buffers */
82#define AIO_BUFSIZE 65536 49#define AIO_BUFSIZE 65536
50
51/* use NV for 32 bit perls as it allows larger offsets */
52#if IVSIZE >= 8
53# define SvVAL64 SvIV
54#else
55# define SvVAL64 SvNV
56#endif
83 57
84#define dBUF \ 58#define dBUF \
85 char *aio_buf; \ 59 char *aio_buf; \
86 LOCK (wrklock); \ 60 LOCK (wrklock); \
87 self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ 61 self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \
92typedef SV SV8; /* byte-sv, used for argument-checking */ 66typedef SV SV8; /* byte-sv, used for argument-checking */
93 67
94enum { 68enum {
95 REQ_QUIT, 69 REQ_QUIT,
96 REQ_OPEN, REQ_CLOSE, 70 REQ_OPEN, REQ_CLOSE,
97 REQ_READ, REQ_WRITE, REQ_READAHEAD, 71 REQ_READ, REQ_WRITE,
98 REQ_SENDFILE, 72 REQ_READAHEAD, REQ_SENDFILE,
99 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 73 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
74 REQ_UTIME, REQ_FUTIME,
75 REQ_CHMOD, REQ_FCHMOD,
76 REQ_CHOWN, REQ_FCHOWN,
100 REQ_FSYNC, REQ_FDATASYNC, 77 REQ_FSYNC, REQ_FDATASYNC,
101 REQ_UNLINK, REQ_RMDIR, REQ_RENAME, 78 REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME,
102 REQ_MKNOD, REQ_READDIR, 79 REQ_MKNOD, REQ_READDIR,
103 REQ_LINK, REQ_SYMLINK, REQ_READLINK, 80 REQ_LINK, REQ_SYMLINK, REQ_READLINK,
104 REQ_GROUP, REQ_NOP, 81 REQ_GROUP, REQ_NOP,
105 REQ_BUSY, 82 REQ_BUSY,
106}; 83};
110 87
111typedef struct aio_cb 88typedef struct aio_cb
112{ 89{
113 struct aio_cb *volatile next; 90 struct aio_cb *volatile next;
114 91
115 SV *callback, *fh; 92 SV *callback;
116 SV *sv1, *sv2; 93 SV *sv1, *sv2;
117 void *ptr1, *ptr2; 94 void *ptr1, *ptr2;
118 off_t offs; 95 off_t offs;
119 size_t size; 96 size_t size;
120 ssize_t result; 97 ssize_t result;
98 double nv1, nv2;
121 99
122 STRLEN stroffset; 100 STRLEN stroffset;
123 int type; 101 int type;
124 int int1, int2; 102 int int1, int2, int3;
125 int errorno; 103 int errorno;
126 mode_t mode; /* open */ 104 mode_t mode; /* open */
127 105
128 unsigned char flags; 106 unsigned char flags;
129 unsigned char pri; 107 unsigned char pri;
132 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; 110 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first;
133} aio_cb; 111} aio_cb;
134 112
135enum { 113enum {
136 FLAG_CANCELLED = 0x01, /* request was cancelled */ 114 FLAG_CANCELLED = 0x01, /* request was cancelled */
137 FLAG_SV1_RO_OFF = 0x40, /* data was set readonly */ 115 FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */
138 FLAG_PTR2_FREE = 0x80, /* need to free(ptr2) */ 116 FLAG_PTR2_FREE = 0x80, /* need to free(ptr2) */
139}; 117};
140 118
141typedef aio_cb *aio_req; 119typedef aio_cb *aio_req;
142typedef aio_cb *aio_req_ornot; 120typedef aio_cb *aio_req_ornot;
160{ 138{
161 return (tv2->tv_sec - tv1->tv_sec ) * AIO_TICKS 139 return (tv2->tv_sec - tv1->tv_sec ) * AIO_TICKS
162 + ((tv2->tv_usec - tv1->tv_usec) >> 10); 140 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
163} 141}
164 142
143static thread_t main_tid;
144static int main_sig;
145static int block_sig_level;
146
147void block_sig ()
148{
149 sigset_t ss;
150
151 if (block_sig_level++)
152 return;
153
154 if (!main_sig)
155 return;
156
157 sigemptyset (&ss);
158 sigaddset (&ss, main_sig);
159 pthread_sigmask (SIG_BLOCK, &ss, 0);
160}
161
162void unblock_sig ()
163{
164 sigset_t ss;
165
166 if (--block_sig_level)
167 return;
168
169 if (!main_sig)
170 return;
171
172 sigemptyset (&ss);
173 sigaddset (&ss, main_sig);
174 pthread_sigmask (SIG_UNBLOCK, &ss, 0);
175}
176
165static int next_pri = DEFAULT_PRI + PRI_BIAS; 177static int next_pri = DEFAULT_PRI + PRI_BIAS;
166 178
167static unsigned int started, idle, wanted; 179static unsigned int started, idle, wanted;
168 180
169#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
170# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
171#else
172# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
173#endif
174
175#define LOCK(mutex) pthread_mutex_lock (&(mutex))
176#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
177
178/* worker threads management */ 181/* worker threads management */
179static pthread_mutex_t wrklock = AIO_MUTEX_INIT; 182static mutex_t wrklock = MUTEX_INIT;
180 183
181typedef struct worker { 184typedef struct worker {
182 /* locked by wrklock */ 185 /* locked by wrklock */
183 struct worker *prev, *next; 186 struct worker *prev, *next;
184 187
185 pthread_t tid; 188 thread_t tid;
186 189
187 /* locked by reslock, reqlock or wrklock */ 190 /* locked by reslock, reqlock or wrklock */
188 aio_req req; /* currently processed request */ 191 aio_req req; /* currently processed request */
189 void *dbuf; 192 void *dbuf;
190 DIR *dirp; 193 DIR *dirp;
218static volatile unsigned int nreqs, nready, npending; 221static volatile unsigned int nreqs, nready, npending;
219static volatile unsigned int max_idle = 4; 222static volatile unsigned int max_idle = 4;
220static volatile unsigned int max_outstanding = 0xffffffff; 223static volatile unsigned int max_outstanding = 0xffffffff;
221static int respipe [2]; 224static int respipe [2];
222 225
223static pthread_mutex_t reslock = AIO_MUTEX_INIT; 226static mutex_t reslock = MUTEX_INIT;
224static pthread_mutex_t reqlock = AIO_MUTEX_INIT; 227static mutex_t reqlock = MUTEX_INIT;
225static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 228static cond_t reqwait = COND_INIT;
226 229
227#if WORDACCESS_UNSAFE 230#if WORDACCESS_UNSAFE
228 231
229static unsigned int get_nready () 232static unsigned int get_nready ()
230{ 233{
320 323
321 abort (); 324 abort ();
322} 325}
323 326
324static int poll_cb (); 327static int poll_cb ();
325static void req_invoke (aio_req req); 328static int req_invoke (aio_req req);
326static void req_free (aio_req req); 329static void req_destroy (aio_req req);
327static void req_cancel (aio_req req); 330static void req_cancel (aio_req req);
328 331
329/* must be called at most once */ 332/* must be called at most once */
330static SV *req_sv (aio_req req, const char *klass) 333static SV *req_sv (aio_req req, const char *klass)
331{ 334{
350 return mg ? (aio_req)mg->mg_ptr : 0; 353 return mg ? (aio_req)mg->mg_ptr : 0;
351} 354}
352 355
353static void aio_grp_feed (aio_req grp) 356static void aio_grp_feed (aio_req grp)
354{ 357{
358 block_sig ();
359
355 while (grp->size < grp->int2 && !(grp->flags & FLAG_CANCELLED)) 360 while (grp->size < grp->int2 && !(grp->flags & FLAG_CANCELLED))
356 { 361 {
357 int old_len = grp->size; 362 int old_len = grp->size;
358 363
359 if (grp->sv2 && SvOK (grp->sv2)) 364 if (grp->sv2 && SvOK (grp->sv2))
377 SvREFCNT_dec (grp->sv2); 382 SvREFCNT_dec (grp->sv2);
378 grp->sv2 = 0; 383 grp->sv2 = 0;
379 break; 384 break;
380 } 385 }
381 } 386 }
387
388 unblock_sig ();
382} 389}
383 390
384static void aio_grp_dec (aio_req grp) 391static void aio_grp_dec (aio_req grp)
385{ 392{
386 --grp->size; 393 --grp->size;
389 aio_grp_feed (grp); 396 aio_grp_feed (grp);
390 397
391 /* finish, if done */ 398 /* finish, if done */
392 if (!grp->size && grp->int1) 399 if (!grp->size && grp->int1)
393 { 400 {
401 block_sig ();
402
394 req_invoke (grp); 403 if (!req_invoke (grp))
404 {
405 req_destroy (grp);
406 unblock_sig ();
407 croak (0);
408 }
409
395 req_free (grp); 410 req_destroy (grp);
411 unblock_sig ();
396 } 412 }
397} 413}
398 414
399static void req_invoke (aio_req req) 415static int req_invoke (aio_req req)
400{ 416{
401 dSP; 417 dSP;
402 418
403 if (req->flags & FLAG_SV1_RO_OFF) 419 if (req->flags & FLAG_SV2_RO_OFF)
404 SvREADONLY_off (req->sv1); 420 SvREADONLY_off (req->sv2);
405 421
406 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) 422 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback))
407 { 423 {
408 ENTER; 424 ENTER;
409 SAVETMPS; 425 SAVETMPS;
447 PUSHs (sv_2mortal (newSViv (req->result))); 463 PUSHs (sv_2mortal (newSViv (req->result)));
448 PUTBACK; 464 PUTBACK;
449 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 465 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
450 SPAGAIN; 466 SPAGAIN;
451 467
452 fh = SvREFCNT_inc (POPs); 468 fh = POPs;
453
454 PUSHMARK (SP); 469 PUSHMARK (SP);
455 XPUSHs (sv_2mortal (fh)); 470 XPUSHs (fh);
456 } 471 }
457 break; 472 break;
458 473
459 case REQ_GROUP: 474 case REQ_GROUP:
460 req->int1 = 2; /* mark group as finished */ 475 req->int1 = 2; /* mark group as finished */
475 break; 490 break;
476 491
477 case REQ_READLINK: 492 case REQ_READLINK:
478 if (req->result > 0) 493 if (req->result > 0)
479 { 494 {
480 SvCUR_set (req->sv1, req->result); 495 SvCUR_set (req->sv2, req->result);
481 *SvEND (req->sv1) = 0; 496 *SvEND (req->sv2) = 0;
482 PUSHs (req->sv1); 497 PUSHs (req->sv2);
483 } 498 }
484 break; 499 break;
485 500
486 case REQ_STAT: 501 case REQ_STAT:
487 case REQ_LSTAT: 502 case REQ_LSTAT:
491 PL_statcache = *(Stat_t *)(req->ptr2); 506 PL_statcache = *(Stat_t *)(req->ptr2);
492 PUSHs (sv_2mortal (newSViv (req->result))); 507 PUSHs (sv_2mortal (newSViv (req->result)));
493 break; 508 break;
494 509
495 case REQ_READ: 510 case REQ_READ:
496 SvCUR_set (req->sv1, req->stroffset + (req->result > 0 ? req->result : 0)); 511 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0));
497 *SvEND (req->sv1) = 0; 512 *SvEND (req->sv2) = 0;
498 PUSHs (sv_2mortal (newSViv (req->result))); 513 PUSHs (sv_2mortal (newSViv (req->result)));
499 break; 514 break;
500 515
501 default: 516 default:
502 PUSHs (sv_2mortal (newSViv (req->result))); 517 PUSHs (sv_2mortal (newSViv (req->result)));
525 grp->grp_first = req->grp_next; 540 grp->grp_first = req->grp_next;
526 541
527 aio_grp_dec (grp); 542 aio_grp_dec (grp);
528 } 543 }
529 544
530 if (SvTRUE (ERRSV)) 545 return !SvTRUE (ERRSV);
531 {
532 req_free (req);
533 croak (0);
534 }
535} 546}
536 547
537static void req_free (aio_req req) 548static void req_destroy (aio_req req)
538{ 549{
539 if (req->self) 550 if (req->self)
540 { 551 {
541 sv_unmagic (req->self, PERL_MAGIC_ext); 552 sv_unmagic (req->self, PERL_MAGIC_ext);
542 SvREFCNT_dec (req->self); 553 SvREFCNT_dec (req->self);
543 } 554 }
544 555
545 SvREFCNT_dec (req->fh);
546 SvREFCNT_dec (req->sv1); 556 SvREFCNT_dec (req->sv1);
547 SvREFCNT_dec (req->sv2); 557 SvREFCNT_dec (req->sv2);
548 SvREFCNT_dec (req->callback); 558 SvREFCNT_dec (req->callback);
549 559
550 if (req->flags & FLAG_PTR2_FREE) 560 if (req->flags & FLAG_PTR2_FREE)
572 req->flags |= FLAG_CANCELLED; 582 req->flags |= FLAG_CANCELLED;
573 583
574 req_cancel_subs (req); 584 req_cancel_subs (req);
575} 585}
576 586
577static void *aio_proc(void *arg); 587static void *aio_proc (void *arg);
578 588
579static void start_thread (void) 589static void start_thread (void)
580{ 590{
581 sigset_t fullsigset, oldsigset;
582 pthread_attr_t attr;
583
584 worker *wrk = calloc (1, sizeof (worker)); 591 worker *wrk = calloc (1, sizeof (worker));
585 592
586 if (!wrk) 593 if (!wrk)
587 croak ("unable to allocate worker thread data"); 594 croak ("unable to allocate worker thread data");
588 595
589 pthread_attr_init (&attr);
590 pthread_attr_setstacksize (&attr, STACKSIZE);
591 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
592#ifdef PTHREAD_SCOPE_PROCESS
593 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
594#endif
595
596 sigfillset (&fullsigset);
597
598 LOCK (wrklock); 596 LOCK (wrklock);
599 sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset);
600 597
601 if (pthread_create (&wrk->tid, &attr, aio_proc, (void *)wrk) == 0) 598 if (thread_create (&wrk->tid, aio_proc, (void *)wrk))
602 { 599 {
603 wrk->prev = &wrk_first; 600 wrk->prev = &wrk_first;
604 wrk->next = wrk_first.next; 601 wrk->next = wrk_first.next;
605 wrk_first.next->prev = wrk; 602 wrk_first.next->prev = wrk;
606 wrk_first.next = wrk; 603 wrk_first.next = wrk;
607 ++started; 604 ++started;
608 } 605 }
609 else 606 else
610 free (wrk); 607 free (wrk);
611 608
612 sigprocmask (SIG_SETMASK, &oldsigset, 0);
613 UNLOCK (wrklock); 609 UNLOCK (wrklock);
614} 610}
615 611
616static void maybe_start_thread () 612static void maybe_start_thread ()
617{ 613{
625 start_thread (); 621 start_thread ();
626} 622}
627 623
628static void req_send (aio_req req) 624static void req_send (aio_req req)
629{ 625{
626 block_sig ();
627
630 ++nreqs; 628 ++nreqs;
631 629
632 LOCK (reqlock); 630 LOCK (reqlock);
633 ++nready; 631 ++nready;
634 reqq_push (&req_queue, req); 632 reqq_push (&req_queue, req);
635 pthread_cond_signal (&reqwait); 633 COND_SIGNAL (reqwait);
636 UNLOCK (reqlock); 634 UNLOCK (reqlock);
635
636 unblock_sig ();
637 637
638 maybe_start_thread (); 638 maybe_start_thread ();
639} 639}
640 640
641static void end_thread (void) 641static void end_thread (void)
647 req->type = REQ_QUIT; 647 req->type = REQ_QUIT;
648 req->pri = PRI_MAX + PRI_BIAS; 648 req->pri = PRI_MAX + PRI_BIAS;
649 649
650 LOCK (reqlock); 650 LOCK (reqlock);
651 reqq_push (&req_queue, req); 651 reqq_push (&req_queue, req);
652 pthread_cond_signal (&reqwait); 652 COND_SIGNAL (reqwait);
653 UNLOCK (reqlock); 653 UNLOCK (reqlock);
654 654
655 LOCK (wrklock); 655 LOCK (wrklock);
656 --started; 656 --started;
657 UNLOCK (wrklock); 657 UNLOCK (wrklock);
712 aio_req req; 712 aio_req req;
713 713
714 if (max_poll_time) 714 if (max_poll_time)
715 gettimeofday (&tv_start, 0); 715 gettimeofday (&tv_start, 0);
716 716
717 block_sig ();
718
717 for (;;) 719 for (;;)
718 { 720 {
719 for (;;) 721 for (;;)
720 { 722 {
721 maybe_start_thread (); 723 maybe_start_thread ();
728 --npending; 730 --npending;
729 731
730 if (!res_queue.size) 732 if (!res_queue.size)
731 { 733 {
732 /* read any signals sent by the worker threads */ 734 /* read any signals sent by the worker threads */
733 char buf [32]; 735 char buf [4];
734 while (read (respipe [0], buf, 32) == 32) 736 while (read (respipe [0], buf, 4) == 4)
735 ; 737 ;
736 } 738 }
737 } 739 }
738 740
739 UNLOCK (reslock); 741 UNLOCK (reslock);
748 req->int1 = 1; /* mark request as delayed */ 750 req->int1 = 1; /* mark request as delayed */
749 continue; 751 continue;
750 } 752 }
751 else 753 else
752 { 754 {
753 req_invoke (req); 755 if (!req_invoke (req))
756 {
757 req_destroy (req);
758 unblock_sig ();
759 croak (0);
760 }
754 761
755 count++; 762 count++;
756 } 763 }
757 764
758 req_free (req); 765 req_destroy (req);
759 766
760 if (maxreqs && !--maxreqs) 767 if (maxreqs && !--maxreqs)
761 break; 768 break;
762 769
763 if (max_poll_time) 770 if (max_poll_time)
775 poll_wait (); 782 poll_wait ();
776 783
777 ++maxreqs; 784 ++maxreqs;
778 } 785 }
779 786
787 unblock_sig ();
780 return count; 788 return count;
781} 789}
782 790
783static void create_pipe () 791static void create_pipe ()
784{ 792{
802/* 810/*
803 * make our pread/pwrite safe against themselves, but not against 811 * make our pread/pwrite safe against themselves, but not against
804 * normal read/write by using a mutex. slows down execution a lot, 812 * normal read/write by using a mutex. slows down execution a lot,
805 * but that's your problem, not mine. 813 * but that's your problem, not mine.
806 */ 814 */
807static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; 815static mutex_t preadwritelock = MUTEX_INIT;
808 816
809static ssize_t pread (int fd, void *buf, size_t count, off_t offset) 817static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
810{ 818{
811 ssize_t res; 819 ssize_t res;
812 off_t ooffset; 820 off_t ooffset;
835 843
836 return res; 844 return res;
837} 845}
838#endif 846#endif
839 847
848#ifndef HAVE_FUTIMES
849
850# define utimes(path,times) aio_utimes (path, times)
851# define futimes(fd,times) aio_futimes (fd, times)
852
853int aio_utimes (const char *filename, const struct timeval times[2])
854{
855 if (times)
856 {
857 struct utimbuf buf;
858
859 buf.actime = times[0].tv_sec;
860 buf.modtime = times[1].tv_sec;
861
862 return utime (filename, &buf);
863 }
864 else
865 return utime (filename, 0);
866}
867
868int aio_futimes (int fd, const struct timeval tv[2])
869{
870 errno = ENOSYS;
871 return -1;
872}
873
874#endif
875
840#if !HAVE_FDATASYNC 876#if !HAVE_FDATASYNC
841# define fdatasync fsync 877# define fdatasync fsync
842#endif 878#endif
843 879
844#if !HAVE_READAHEAD 880#if !HAVE_READAHEAD
863#endif 899#endif
864 900
865#if !HAVE_READDIR_R 901#if !HAVE_READDIR_R
866# define readdir_r aio_readdir_r 902# define readdir_r aio_readdir_r
867 903
868static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; 904static mutex_t readdirlock = MUTEX_INIT;
869 905
870static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) 906static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
871{ 907{
872 struct dirent *e; 908 struct dirent *e;
873 int errorno; 909 int errorno;
1072 if (req) 1108 if (req)
1073 break; 1109 break;
1074 1110
1075 ++idle; 1111 ++idle;
1076 1112
1077 if (pthread_cond_timedwait (&reqwait, &reqlock, &ts) 1113 if (COND_TIMEDWAIT (reqwait, reqlock, ts)
1078 == ETIMEDOUT) 1114 == ETIMEDOUT)
1079 { 1115 {
1080 if (idle > max_idle) 1116 if (idle > max_idle)
1081 { 1117 {
1082 --idle; 1118 --idle;
1086 UNLOCK (wrklock); 1122 UNLOCK (wrklock);
1087 goto quit; 1123 goto quit;
1088 } 1124 }
1089 1125
1090 /* we are allowed to idle, so do so without any timeout */ 1126 /* we are allowed to idle, so do so without any timeout */
1091 pthread_cond_wait (&reqwait, &reqlock); 1127 COND_WAIT (reqwait, reqlock);
1092 ts.tv_sec = time (0) + IDLE_TIMEOUT; 1128 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1093 } 1129 }
1094 1130
1095 --idle; 1131 --idle;
1096 } 1132 }
1102 errno = 0; /* strictly unnecessary */ 1138 errno = 0; /* strictly unnecessary */
1103 1139
1104 if (!(req->flags & FLAG_CANCELLED)) 1140 if (!(req->flags & FLAG_CANCELLED))
1105 switch (req->type) 1141 switch (req->type)
1106 { 1142 {
1107 case REQ_READ: req->result = pread (req->int1, req->ptr1, req->size, req->offs); break; 1143 case REQ_READ: req->result = req->offs >= 0
1108 case REQ_WRITE: req->result = pwrite (req->int1, req->ptr1, req->size, req->offs); break; 1144 ? pread (req->int1, req->ptr1, req->size, req->offs)
1145 : read (req->int1, req->ptr1, req->size); break;
1146 case REQ_WRITE: req->result = req->offs >= 0
1147 ? pwrite (req->int1, req->ptr1, req->size, req->offs)
1148 : write (req->int1, req->ptr1, req->size); break;
1109 1149
1110 case REQ_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; 1150 case REQ_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break;
1111 case REQ_SENDFILE: req->result = sendfile_ (req->int1, req->int2, req->offs, req->size, self); break; 1151 case REQ_SENDFILE: req->result = sendfile_ (req->int1, req->int2, req->offs, req->size, self); break;
1112 1152
1113 case REQ_STAT: req->result = stat (req->ptr1, (Stat_t *)req->ptr2); break; 1153 case REQ_STAT: req->result = stat (req->ptr1, (Stat_t *)req->ptr2); break;
1114 case REQ_LSTAT: req->result = lstat (req->ptr1, (Stat_t *)req->ptr2); break; 1154 case REQ_LSTAT: req->result = lstat (req->ptr1, (Stat_t *)req->ptr2); break;
1115 case REQ_FSTAT: req->result = fstat (req->int1, (Stat_t *)req->ptr2); break; 1155 case REQ_FSTAT: req->result = fstat (req->int1, (Stat_t *)req->ptr2); break;
1156
1157 case REQ_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break;
1158 case REQ_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break;
1159 case REQ_CHMOD: req->result = chmod (req->ptr1, req->mode); break;
1160 case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break;
1116 1161
1117 case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break; 1162 case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break;
1118 case REQ_CLOSE: req->result = close (req->int1); break; 1163 case REQ_CLOSE: req->result = close (req->int1); break;
1119 case REQ_UNLINK: req->result = unlink (req->ptr1); break; 1164 case REQ_UNLINK: req->result = unlink (req->ptr1); break;
1120 case REQ_RMDIR: req->result = rmdir (req->ptr1); break; 1165 case REQ_RMDIR: req->result = rmdir (req->ptr1); break;
1166 case REQ_MKDIR: req->result = mkdir (req->ptr1, req->mode); break;
1121 case REQ_RENAME: req->result = rename (req->ptr2, req->ptr1); break; 1167 case REQ_RENAME: req->result = rename (req->ptr2, req->ptr1); break;
1122 case REQ_LINK: req->result = link (req->ptr2, req->ptr1); break; 1168 case REQ_LINK: req->result = link (req->ptr2, req->ptr1); break;
1123 case REQ_SYMLINK: req->result = symlink (req->ptr2, req->ptr1); break; 1169 case REQ_SYMLINK: req->result = symlink (req->ptr2, req->ptr1); break;
1124 case REQ_MKNOD: req->result = mknod (req->ptr2, req->mode, (dev_t)req->offs); break; 1170 case REQ_MKNOD: req->result = mknod (req->ptr2, req->mode, (dev_t)req->offs); break;
1125 case REQ_READLINK: req->result = readlink (req->ptr2, req->ptr1, NAME_MAX); break; 1171 case REQ_READLINK: req->result = readlink (req->ptr2, req->ptr1, NAME_MAX); break;
1130 1176
1131 case REQ_BUSY: 1177 case REQ_BUSY:
1132 { 1178 {
1133 struct timeval tv; 1179 struct timeval tv;
1134 1180
1135 tv.tv_sec = req->int1; 1181 tv.tv_sec = req->nv1;
1136 tv.tv_usec = req->int2; 1182 tv.tv_usec = (req->nv1 - tv.tv_usec) * 1000000.;
1137 1183
1138 req->result = select (0, 0, 0, 0, &tv); 1184 req->result = select (0, 0, 0, 0, &tv);
1185 }
1186
1187 case REQ_UTIME:
1188 case REQ_FUTIME:
1189 {
1190 struct timeval tv[2];
1191 struct timeval *times;
1192
1193 if (req->nv1 != -1. || req->nv2 != -1.)
1194 {
1195 tv[0].tv_sec = req->nv1;
1196 tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.;
1197 tv[1].tv_sec = req->nv2;
1198 tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.;
1199
1200 times = tv;
1201 }
1202 else
1203 times = 0;
1204
1205
1206 req->result = req->type == REQ_FUTIME
1207 ? futimes (req->int1, times)
1208 : utimes (req->ptr1, times);
1139 } 1209 }
1140 1210
1141 case REQ_GROUP: 1211 case REQ_GROUP:
1142 case REQ_NOP: 1212 case REQ_NOP:
1143 break; 1213 break;
1144 1214
1145 case REQ_QUIT: 1215 case REQ_QUIT:
1146 goto quit; 1216 goto quit;
1147 1217
1148 default: 1218 default:
1149 req->result = ENOSYS; 1219 req->result = -1;
1150 break; 1220 break;
1151 } 1221 }
1152 1222
1153 req->errorno = errno; 1223 req->errorno = errno;
1154 1224
1155 LOCK (reslock); 1225 LOCK (reslock);
1156 1226
1157 ++npending; 1227 ++npending;
1158 1228
1159 if (!reqq_push (&res_queue, req)) 1229 if (!reqq_push (&res_queue, req))
1230 {
1160 /* write a dummy byte to the pipe so fh becomes ready */ 1231 /* write a dummy byte to the pipe so fh becomes ready */
1161 write (respipe [1], &respipe, 1); 1232 write (respipe [1], &respipe, 1);
1233
1234 /* optionally signal the main thread asynchronously */
1235 if (main_sig)
1236 pthread_kill (main_tid, main_sig);
1237 }
1162 1238
1163 self->req = 0; 1239 self->req = 0;
1164 worker_clear (self); 1240 worker_clear (self);
1165 1241
1166 UNLOCK (reslock); 1242 UNLOCK (reslock);
1205static void atfork_child (void) 1281static void atfork_child (void)
1206{ 1282{
1207 aio_req prv; 1283 aio_req prv;
1208 1284
1209 while (prv = reqq_shift (&req_queue)) 1285 while (prv = reqq_shift (&req_queue))
1210 req_free (prv); 1286 req_destroy (prv);
1211 1287
1212 while (prv = reqq_shift (&res_queue)) 1288 while (prv = reqq_shift (&res_queue))
1213 req_free (prv); 1289 req_destroy (prv);
1214 1290
1215 while (wrk_first.next != &wrk_first) 1291 while (wrk_first.next != &wrk_first)
1216 { 1292 {
1217 worker *wrk = wrk_first.next; 1293 worker *wrk = wrk_first.next;
1218 1294
1219 if (wrk->req) 1295 if (wrk->req)
1220 req_free (wrk->req); 1296 req_destroy (wrk->req);
1221 1297
1222 worker_clear (wrk); 1298 worker_clear (wrk);
1223 worker_free (wrk); 1299 worker_free (wrk);
1224 } 1300 }
1225 1301
1269 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); 1345 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
1270 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); 1346 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
1271 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); 1347 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT));
1272 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); 1348 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC));
1273 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); 1349 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO));
1350 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO));
1274 1351
1275 create_pipe (); 1352 create_pipe ();
1276 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 1353 ATFORK (atfork_prepare, atfork_parent, atfork_child);
1277} 1354}
1278 1355
1279void 1356void
1280max_poll_reqs (int nreqs) 1357max_poll_reqs (int nreqs)
1281 PROTOTYPE: $ 1358 PROTOTYPE: $
1310 max_outstanding = maxreqs; 1387 max_outstanding = maxreqs;
1311 OUTPUT: 1388 OUTPUT:
1312 RETVAL 1389 RETVAL
1313 1390
1314void 1391void
1315aio_open (pathname,flags,mode,callback=&PL_sv_undef) 1392aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef)
1316 SV8 * pathname
1317 int flags
1318 int mode
1319 SV * callback
1320 PROTOTYPE: $$$;$ 1393 PROTOTYPE: $$$;$
1321 PPCODE: 1394 PPCODE:
1322{ 1395{
1323 dREQ; 1396 dREQ;
1324 1397
1330 1403
1331 REQ_SEND; 1404 REQ_SEND;
1332} 1405}
1333 1406
1334void 1407void
1335aio_close (fh,callback=&PL_sv_undef) 1408aio_close (SV *fh, SV *callback=&PL_sv_undef)
1336 SV * fh
1337 SV * callback
1338 PROTOTYPE: $;$ 1409 PROTOTYPE: $;$
1339 ALIAS: 1410 ALIAS:
1340 aio_close = REQ_CLOSE 1411 aio_close = REQ_CLOSE
1341 aio_fsync = REQ_FSYNC 1412 aio_fsync = REQ_FSYNC
1342 aio_fdatasync = REQ_FDATASYNC 1413 aio_fdatasync = REQ_FDATASYNC
1343 PPCODE: 1414 PPCODE:
1344{ 1415{
1345 dREQ; 1416 dREQ;
1346 1417
1347 req->type = ix; 1418 req->type = ix;
1348 req->fh = newSVsv (fh); 1419 req->sv1 = newSVsv (fh);
1349 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); 1420 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
1350 1421
1351 REQ_SEND (req); 1422 REQ_SEND (req);
1352} 1423}
1353 1424
1354void 1425void
1355aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 1426aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef)
1356 SV * fh
1357 UV offset
1358 UV length
1359 SV8 * data
1360 UV dataoffset
1361 SV * callback
1362 ALIAS: 1427 ALIAS:
1363 aio_read = REQ_READ 1428 aio_read = REQ_READ
1364 aio_write = REQ_WRITE 1429 aio_write = REQ_WRITE
1365 PROTOTYPE: $$$$$;$ 1430 PROTOTYPE: $$$$$;$
1366 PPCODE: 1431 PPCODE:
1367{ 1432{
1368 STRLEN svlen; 1433 STRLEN svlen;
1369 char *svptr = SvPVbyte (data, svlen); 1434 char *svptr = SvPVbyte (data, svlen);
1435 UV len = SvUV (length);
1370 1436
1371 SvUPGRADE (data, SVt_PV); 1437 SvUPGRADE (data, SVt_PV);
1372 SvPOK_on (data); 1438 SvPOK_on (data);
1373 1439
1374 if (dataoffset < 0) 1440 if (dataoffset < 0)
1375 dataoffset += svlen; 1441 dataoffset += svlen;
1376 1442
1377 if (dataoffset < 0 || dataoffset > svlen) 1443 if (dataoffset < 0 || dataoffset > svlen)
1378 croak ("data offset outside of string"); 1444 croak ("dataoffset outside of data scalar");
1379 1445
1380 if (ix == REQ_WRITE) 1446 if (ix == REQ_WRITE)
1381 { 1447 {
1382 /* write: check length and adjust. */ 1448 /* write: check length and adjust. */
1383 if (length < 0 || length + dataoffset > svlen) 1449 if (!SvOK (length) || len + dataoffset > svlen)
1384 length = svlen - dataoffset; 1450 len = svlen - dataoffset;
1385 } 1451 }
1386 else 1452 else
1387 { 1453 {
1388 /* read: grow scalar as necessary */ 1454 /* read: grow scalar as necessary */
1389 svptr = SvGROW (data, length + dataoffset); 1455 svptr = SvGROW (data, len + dataoffset + 1);
1390 } 1456 }
1391 1457
1392 if (length < 0) 1458 if (len < 0)
1393 croak ("length must not be negative"); 1459 croak ("length must not be negative");
1394 1460
1395 { 1461 {
1396 dREQ; 1462 dREQ;
1397 1463
1398 req->type = ix; 1464 req->type = ix;
1399 req->fh = newSVsv (fh); 1465 req->sv1 = newSVsv (fh);
1400 req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) 1466 req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
1401 : IoOFP (sv_2io (fh))); 1467 : IoOFP (sv_2io (fh)));
1402 req->offs = offset; 1468 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
1403 req->size = length; 1469 req->size = len;
1404 req->sv1 = SvREFCNT_inc (data); 1470 req->sv2 = SvREFCNT_inc (data);
1405 req->ptr1 = (char *)svptr + dataoffset; 1471 req->ptr1 = (char *)svptr + dataoffset;
1406 req->stroffset = dataoffset; 1472 req->stroffset = dataoffset;
1407 1473
1408 if (!SvREADONLY (data)) 1474 if (!SvREADONLY (data))
1409 { 1475 {
1410 SvREADONLY_on (data); 1476 SvREADONLY_on (data);
1411 req->flags |= FLAG_SV1_RO_OFF; 1477 req->flags |= FLAG_SV2_RO_OFF;
1412 } 1478 }
1413 1479
1414 REQ_SEND; 1480 REQ_SEND;
1415 } 1481 }
1416} 1482}
1417 1483
1418void 1484void
1419aio_readlink (path,callback=&PL_sv_undef) 1485aio_readlink (SV8 *path, SV *callback=&PL_sv_undef)
1420 SV8 * path
1421 SV * callback
1422 PROTOTYPE: $$;$ 1486 PROTOTYPE: $$;$
1423 PPCODE: 1487 PPCODE:
1424{ 1488{
1425 SV *data; 1489 SV *data;
1426 dREQ; 1490 dREQ;
1427 1491
1428 data = newSV (NAME_MAX); 1492 data = newSV (NAME_MAX);
1429 SvPOK_on (data); 1493 SvPOK_on (data);
1430 1494
1431 req->type = REQ_READLINK; 1495 req->type = REQ_READLINK;
1432 req->fh = newSVsv (path); 1496 req->sv1 = newSVsv (path);
1433 req->ptr2 = SvPVbyte_nolen (req->fh); 1497 req->ptr2 = SvPVbyte_nolen (req->sv1);
1434 req->sv1 = data; 1498 req->sv2 = data;
1435 req->ptr1 = SvPVbyte_nolen (data); 1499 req->ptr1 = SvPVbyte_nolen (data);
1436 1500
1437 REQ_SEND; 1501 REQ_SEND;
1438} 1502}
1439 1503
1440void 1504void
1441aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) 1505aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef)
1442 SV * out_fh
1443 SV * in_fh
1444 UV in_offset
1445 UV length
1446 SV * callback
1447 PROTOTYPE: $$$$;$ 1506 PROTOTYPE: $$$$;$
1448 PPCODE: 1507 PPCODE:
1449{ 1508{
1450 dREQ; 1509 dREQ;
1451 1510
1452 req->type = REQ_SENDFILE; 1511 req->type = REQ_SENDFILE;
1453 req->fh = newSVsv (out_fh); 1512 req->sv1 = newSVsv (out_fh);
1454 req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh))); 1513 req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh)));
1455 req->sv2 = newSVsv (in_fh); 1514 req->sv2 = newSVsv (in_fh);
1456 req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); 1515 req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
1457 req->offs = in_offset; 1516 req->offs = SvVAL64 (in_offset);
1458 req->size = length; 1517 req->size = length;
1459 1518
1460 REQ_SEND; 1519 REQ_SEND;
1461} 1520}
1462 1521
1463void 1522void
1464aio_readahead (fh,offset,length,callback=&PL_sv_undef) 1523aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef)
1465 SV * fh
1466 UV offset
1467 IV length
1468 SV * callback
1469 PROTOTYPE: $$$;$ 1524 PROTOTYPE: $$$;$
1470 PPCODE: 1525 PPCODE:
1471{ 1526{
1472 dREQ; 1527 dREQ;
1473 1528
1474 req->type = REQ_READAHEAD; 1529 req->type = REQ_READAHEAD;
1475 req->fh = newSVsv (fh); 1530 req->sv1 = newSVsv (fh);
1476 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); 1531 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
1477 req->offs = offset; 1532 req->offs = SvVAL64 (offset);
1478 req->size = length; 1533 req->size = length;
1479 1534
1480 REQ_SEND; 1535 REQ_SEND;
1481} 1536}
1482 1537
1483void 1538void
1484aio_stat (fh_or_path,callback=&PL_sv_undef) 1539aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef)
1485 SV8 * fh_or_path
1486 SV * callback
1487 ALIAS: 1540 ALIAS:
1488 aio_stat = REQ_STAT 1541 aio_stat = REQ_STAT
1489 aio_lstat = REQ_LSTAT 1542 aio_lstat = REQ_LSTAT
1490 PPCODE: 1543 PPCODE:
1491{ 1544{
1492 dREQ; 1545 dREQ;
1493 1546
1494 req->ptr2 = malloc (sizeof (Stat_t)); 1547 req->ptr2 = malloc (sizeof (Stat_t));
1495 if (!req->ptr2) 1548 if (!req->ptr2)
1496 { 1549 {
1497 req_free (req); 1550 req_destroy (req);
1498 croak ("out of memory during aio_stat statdata allocation"); 1551 croak ("out of memory during aio_stat statdata allocation");
1499 } 1552 }
1500 1553
1501 req->flags |= FLAG_PTR2_FREE; 1554 req->flags |= FLAG_PTR2_FREE;
1555 req->sv1 = newSVsv (fh_or_path);
1502 1556
1503 if (SvPOK (fh_or_path)) 1557 if (SvPOK (fh_or_path))
1504 { 1558 {
1505 req->type = ix; 1559 req->type = ix;
1506 req->sv1 = newSVsv (fh_or_path);
1507 req->ptr1 = SvPVbyte_nolen (req->sv1); 1560 req->ptr1 = SvPVbyte_nolen (req->sv1);
1508 } 1561 }
1509 else 1562 else
1510 { 1563 {
1511 req->type = REQ_FSTAT; 1564 req->type = REQ_FSTAT;
1512 req->fh = newSVsv (fh_or_path);
1513 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 1565 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1514 } 1566 }
1515 1567
1516 REQ_SEND; 1568 REQ_SEND;
1517} 1569}
1518 1570
1519void 1571void
1572aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef)
1573 PPCODE:
1574{
1575 dREQ;
1576
1577 req->nv1 = SvOK (atime) ? SvNV (atime) : -1.;
1578 req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.;
1579 req->sv1 = newSVsv (fh_or_path);
1580
1581 if (SvPOK (fh_or_path))
1582 {
1583 req->type = REQ_UTIME;
1584 req->ptr1 = SvPVbyte_nolen (req->sv1);
1585 }
1586 else
1587 {
1588 req->type = REQ_FUTIME;
1589 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1590 }
1591
1592 REQ_SEND;
1593}
1594
1595void
1596aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef)
1597 PPCODE:
1598{
1599 dREQ;
1600
1601 req->mode = mode;
1602 req->sv1 = newSVsv (fh_or_path);
1603
1604 if (SvPOK (fh_or_path))
1605 {
1606 req->type = REQ_CHMOD;
1607 req->ptr1 = SvPVbyte_nolen (req->sv1);
1608 }
1609 else
1610 {
1611 req->type = REQ_FCHMOD;
1612 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1613 }
1614
1615 REQ_SEND;
1616}
1617
1618void
1619aio_chown (SV8 *fh_or_path, SV *uid, SV *gid, SV *callback=&PL_sv_undef)
1620 PPCODE:
1621{
1622 dREQ;
1623
1624 req->int2 = SvOK (uid) ? SvIV (uid) : -1;
1625 req->int3 = SvOK (gid) ? SvIV (gid) : -1;
1626 req->sv1 = newSVsv (fh_or_path);
1627
1628 if (SvPOK (fh_or_path))
1629 {
1630 req->type = REQ_CHOWN;
1631 req->ptr1 = SvPVbyte_nolen (req->sv1);
1632 }
1633 else
1634 {
1635 req->type = REQ_FCHOWN;
1636 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1637 }
1638
1639 REQ_SEND;
1640}
1641
1642void
1520aio_unlink (pathname,callback=&PL_sv_undef) 1643aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef)
1521 SV8 * pathname
1522 SV * callback
1523 ALIAS: 1644 ALIAS:
1524 aio_unlink = REQ_UNLINK 1645 aio_unlink = REQ_UNLINK
1525 aio_rmdir = REQ_RMDIR 1646 aio_rmdir = REQ_RMDIR
1526 aio_readdir = REQ_READDIR 1647 aio_readdir = REQ_READDIR
1527 PPCODE: 1648 PPCODE:
1534 1655
1535 REQ_SEND; 1656 REQ_SEND;
1536} 1657}
1537 1658
1538void 1659void
1660aio_mkdir (SV8 *pathname, int mode, SV *callback=&PL_sv_undef)
1661 PPCODE:
1662{
1663 dREQ;
1664
1665 req->type = REQ_MKDIR;
1666 req->sv1 = newSVsv (pathname);
1667 req->ptr1 = SvPVbyte_nolen (req->sv1);
1668 req->mode = mode;
1669
1670 REQ_SEND;
1671}
1672
1673void
1539aio_link (oldpath,newpath,callback=&PL_sv_undef) 1674aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef)
1540 SV8 * oldpath
1541 SV8 * newpath
1542 SV * callback
1543 ALIAS: 1675 ALIAS:
1544 aio_link = REQ_LINK 1676 aio_link = REQ_LINK
1545 aio_symlink = REQ_SYMLINK 1677 aio_symlink = REQ_SYMLINK
1546 aio_rename = REQ_RENAME 1678 aio_rename = REQ_RENAME
1547 PPCODE: 1679 PPCODE:
1548{ 1680{
1549 dREQ; 1681 dREQ;
1550 1682
1551 req->type = ix; 1683 req->type = ix;
1552 req->fh = newSVsv (oldpath); 1684 req->sv2 = newSVsv (oldpath);
1553 req->ptr2 = SvPVbyte_nolen (req->fh); 1685 req->ptr2 = SvPVbyte_nolen (req->sv2);
1554 req->sv1 = newSVsv (newpath); 1686 req->sv1 = newSVsv (newpath);
1555 req->ptr1 = SvPVbyte_nolen (req->sv1); 1687 req->ptr1 = SvPVbyte_nolen (req->sv1);
1556 1688
1557 REQ_SEND; 1689 REQ_SEND;
1558} 1690}
1559 1691
1560void 1692void
1561aio_mknod (pathname,mode,dev,callback=&PL_sv_undef) 1693aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef)
1562 SV8 * pathname
1563 SV * callback
1564 UV mode
1565 UV dev
1566 PPCODE: 1694 PPCODE:
1567{ 1695{
1568 dREQ; 1696 dREQ;
1569 1697
1570 req->type = REQ_MKNOD; 1698 req->type = REQ_MKNOD;
1575 1703
1576 REQ_SEND; 1704 REQ_SEND;
1577} 1705}
1578 1706
1579void 1707void
1580aio_busy (delay,callback=&PL_sv_undef) 1708aio_busy (double delay, SV *callback=&PL_sv_undef)
1581 double delay
1582 SV * callback
1583 PPCODE: 1709 PPCODE:
1584{ 1710{
1585 dREQ; 1711 dREQ;
1586 1712
1587 req->type = REQ_BUSY; 1713 req->type = REQ_BUSY;
1588 req->int1 = delay < 0. ? 0 : delay; 1714 req->nv1 = delay < 0. ? 0. : delay;
1589 req->int2 = delay < 0. ? 0 : 1000. * (delay - req->int1);
1590 1715
1591 REQ_SEND; 1716 REQ_SEND;
1592} 1717}
1593 1718
1594void 1719void
1595aio_group (callback=&PL_sv_undef) 1720aio_group (SV *callback=&PL_sv_undef)
1596 SV * callback
1597 PROTOTYPE: ;$ 1721 PROTOTYPE: ;$
1598 PPCODE: 1722 PPCODE:
1599{ 1723{
1600 dREQ; 1724 dREQ;
1601 1725
1604 req_send (req); 1728 req_send (req);
1605 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 1729 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1606} 1730}
1607 1731
1608void 1732void
1609aio_nop (callback=&PL_sv_undef) 1733aio_nop (SV *callback=&PL_sv_undef)
1610 SV * callback
1611 PPCODE: 1734 PPCODE:
1612{ 1735{
1613 dREQ; 1736 dREQ;
1614 1737
1615 req->type = REQ_NOP; 1738 req->type = REQ_NOP;
1651 1774
1652int 1775int
1653poll() 1776poll()
1654 PROTOTYPE: 1777 PROTOTYPE:
1655 CODE: 1778 CODE:
1656 if (nreqs)
1657 {
1658 poll_wait (); 1779 poll_wait ();
1659 RETVAL = poll_cb (); 1780 RETVAL = poll_cb ();
1660 }
1661 else
1662 RETVAL = 0;
1663 OUTPUT: 1781 OUTPUT:
1664 RETVAL 1782 RETVAL
1665 1783
1666int 1784int
1667poll_fileno() 1785poll_fileno()
1681 1799
1682void 1800void
1683poll_wait() 1801poll_wait()
1684 PROTOTYPE: 1802 PROTOTYPE:
1685 CODE: 1803 CODE:
1686 if (nreqs)
1687 poll_wait (); 1804 poll_wait ();
1805
1806void
1807setsig (int signum = SIGIO)
1808 PROTOTYPE: ;$
1809 CODE:
1810{
1811 if (block_sig_level)
1812 croak ("cannot call IO::AIO::setsig from within aio_block/callback");
1813
1814 LOCK (reslock);
1815 main_tid = pthread_self ();
1816 main_sig = signum;
1817 UNLOCK (reslock);
1818
1819 if (main_sig && npending)
1820 pthread_kill (main_tid, main_sig);
1821}
1822
1823void
1824aio_block (SV *cb)
1825 PROTOTYPE: &
1826 PPCODE:
1827{
1828 int count;
1829
1830 block_sig ();
1831 PUSHMARK (SP);
1832 PUTBACK;
1833 count = call_sv (cb, GIMME_V | G_NOARGS | G_EVAL);
1834 SPAGAIN;
1835 unblock_sig ();
1836
1837 if (SvTRUE (ERRSV))
1838 croak (0);
1839
1840 XSRETURN (count);
1841}
1688 1842
1689int 1843int
1690nreqs() 1844nreqs()
1691 PROTOTYPE: 1845 PROTOTYPE:
1692 CODE: 1846 CODE:
1742 PPCODE: 1896 PPCODE:
1743{ 1897{
1744 int i; 1898 int i;
1745 aio_req req; 1899 aio_req req;
1746 1900
1901 if (main_sig && !block_sig_level)
1902 croak ("aio_group->add called outside aio_block/callback context while IO::AIO::setsig is in use");
1903
1747 if (grp->int1 == 2) 1904 if (grp->int1 == 2)
1748 croak ("cannot add requests to IO::AIO::GRP after the group finished"); 1905 croak ("cannot add requests to IO::AIO::GRP after the group finished");
1749 1906
1750 for (i = 1; i < items; ++i ) 1907 for (i = 1; i < items; ++i )
1751 { 1908 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines