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.93 by root, Wed Nov 8 02:01:02 2006 UTC vs.
Revision 1.101 by root, Fri Jun 1 13:25:51 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;
474 break; 490 break;
475 491
476 case REQ_READLINK: 492 case REQ_READLINK:
477 if (req->result > 0) 493 if (req->result > 0)
478 { 494 {
479 SvCUR_set (req->sv1, req->result); 495 SvCUR_set (req->sv2, req->result);
480 *SvEND (req->sv1) = 0; 496 *SvEND (req->sv2) = 0;
481 PUSHs (req->sv1); 497 PUSHs (req->sv2);
482 } 498 }
483 break; 499 break;
484 500
485 case REQ_STAT: 501 case REQ_STAT:
486 case REQ_LSTAT: 502 case REQ_LSTAT:
490 PL_statcache = *(Stat_t *)(req->ptr2); 506 PL_statcache = *(Stat_t *)(req->ptr2);
491 PUSHs (sv_2mortal (newSViv (req->result))); 507 PUSHs (sv_2mortal (newSViv (req->result)));
492 break; 508 break;
493 509
494 case REQ_READ: 510 case REQ_READ:
495 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));
496 *SvEND (req->sv1) = 0; 512 *SvEND (req->sv2) = 0;
497 PUSHs (sv_2mortal (newSViv (req->result))); 513 PUSHs (sv_2mortal (newSViv (req->result)));
498 break; 514 break;
499 515
500 default: 516 default:
501 PUSHs (sv_2mortal (newSViv (req->result))); 517 PUSHs (sv_2mortal (newSViv (req->result)));
524 grp->grp_first = req->grp_next; 540 grp->grp_first = req->grp_next;
525 541
526 aio_grp_dec (grp); 542 aio_grp_dec (grp);
527 } 543 }
528 544
529 if (SvTRUE (ERRSV)) 545 return !SvTRUE (ERRSV);
530 {
531 req_free (req);
532 croak (0);
533 }
534} 546}
535 547
536static void req_free (aio_req req) 548static void req_destroy (aio_req req)
537{ 549{
538 if (req->self) 550 if (req->self)
539 { 551 {
540 sv_unmagic (req->self, PERL_MAGIC_ext); 552 sv_unmagic (req->self, PERL_MAGIC_ext);
541 SvREFCNT_dec (req->self); 553 SvREFCNT_dec (req->self);
542 } 554 }
543 555
544 SvREFCNT_dec (req->fh);
545 SvREFCNT_dec (req->sv1); 556 SvREFCNT_dec (req->sv1);
546 SvREFCNT_dec (req->sv2); 557 SvREFCNT_dec (req->sv2);
547 SvREFCNT_dec (req->callback); 558 SvREFCNT_dec (req->callback);
548 559
549 if (req->flags & FLAG_PTR2_FREE) 560 if (req->flags & FLAG_PTR2_FREE)
571 req->flags |= FLAG_CANCELLED; 582 req->flags |= FLAG_CANCELLED;
572 583
573 req_cancel_subs (req); 584 req_cancel_subs (req);
574} 585}
575 586
576static void *aio_proc(void *arg); 587static void *aio_proc (void *arg);
577 588
578static void start_thread (void) 589static void start_thread (void)
579{ 590{
580 sigset_t fullsigset, oldsigset;
581 pthread_attr_t attr;
582
583 worker *wrk = calloc (1, sizeof (worker)); 591 worker *wrk = calloc (1, sizeof (worker));
584 592
585 if (!wrk) 593 if (!wrk)
586 croak ("unable to allocate worker thread data"); 594 croak ("unable to allocate worker thread data");
587 595
588 pthread_attr_init (&attr);
589 pthread_attr_setstacksize (&attr, STACKSIZE);
590 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
591#ifdef PTHREAD_SCOPE_PROCESS
592 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
593#endif
594
595 sigfillset (&fullsigset);
596
597 LOCK (wrklock); 596 LOCK (wrklock);
598 sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset);
599 597
600 if (pthread_create (&wrk->tid, &attr, aio_proc, (void *)wrk) == 0) 598 if (thread_create (&wrk->tid, aio_proc, (void *)wrk))
601 { 599 {
602 wrk->prev = &wrk_first; 600 wrk->prev = &wrk_first;
603 wrk->next = wrk_first.next; 601 wrk->next = wrk_first.next;
604 wrk_first.next->prev = wrk; 602 wrk_first.next->prev = wrk;
605 wrk_first.next = wrk; 603 wrk_first.next = wrk;
606 ++started; 604 ++started;
607 } 605 }
608 else 606 else
609 free (wrk); 607 free (wrk);
610 608
611 sigprocmask (SIG_SETMASK, &oldsigset, 0);
612 UNLOCK (wrklock); 609 UNLOCK (wrklock);
613} 610}
614 611
615static void maybe_start_thread () 612static void maybe_start_thread ()
616{ 613{
624 start_thread (); 621 start_thread ();
625} 622}
626 623
627static void req_send (aio_req req) 624static void req_send (aio_req req)
628{ 625{
626 block_sig ();
627
629 ++nreqs; 628 ++nreqs;
630 629
631 LOCK (reqlock); 630 LOCK (reqlock);
632 ++nready; 631 ++nready;
633 reqq_push (&req_queue, req); 632 reqq_push (&req_queue, req);
634 pthread_cond_signal (&reqwait); 633 COND_SIGNAL (reqwait);
635 UNLOCK (reqlock); 634 UNLOCK (reqlock);
635
636 unblock_sig ();
636 637
637 maybe_start_thread (); 638 maybe_start_thread ();
638} 639}
639 640
640static void end_thread (void) 641static void end_thread (void)
646 req->type = REQ_QUIT; 647 req->type = REQ_QUIT;
647 req->pri = PRI_MAX + PRI_BIAS; 648 req->pri = PRI_MAX + PRI_BIAS;
648 649
649 LOCK (reqlock); 650 LOCK (reqlock);
650 reqq_push (&req_queue, req); 651 reqq_push (&req_queue, req);
651 pthread_cond_signal (&reqwait); 652 COND_SIGNAL (reqwait);
652 UNLOCK (reqlock); 653 UNLOCK (reqlock);
653 654
654 LOCK (wrklock); 655 LOCK (wrklock);
655 --started; 656 --started;
656 UNLOCK (wrklock); 657 UNLOCK (wrklock);
711 aio_req req; 712 aio_req req;
712 713
713 if (max_poll_time) 714 if (max_poll_time)
714 gettimeofday (&tv_start, 0); 715 gettimeofday (&tv_start, 0);
715 716
717 block_sig ();
718
716 for (;;) 719 for (;;)
717 { 720 {
718 for (;;) 721 for (;;)
719 { 722 {
720 maybe_start_thread (); 723 maybe_start_thread ();
727 --npending; 730 --npending;
728 731
729 if (!res_queue.size) 732 if (!res_queue.size)
730 { 733 {
731 /* read any signals sent by the worker threads */ 734 /* read any signals sent by the worker threads */
732 char buf [32]; 735 char buf [4];
733 while (read (respipe [0], buf, 32) == 32) 736 while (read (respipe [0], buf, 4) == 4)
734 ; 737 ;
735 } 738 }
736 } 739 }
737 740
738 UNLOCK (reslock); 741 UNLOCK (reslock);
747 req->int1 = 1; /* mark request as delayed */ 750 req->int1 = 1; /* mark request as delayed */
748 continue; 751 continue;
749 } 752 }
750 else 753 else
751 { 754 {
752 req_invoke (req); 755 if (!req_invoke (req))
756 {
757 req_destroy (req);
758 unblock_sig ();
759 croak (0);
760 }
753 761
754 count++; 762 count++;
755 } 763 }
756 764
757 req_free (req); 765 req_destroy (req);
758 766
759 if (maxreqs && !--maxreqs) 767 if (maxreqs && !--maxreqs)
760 break; 768 break;
761 769
762 if (max_poll_time) 770 if (max_poll_time)
774 poll_wait (); 782 poll_wait ();
775 783
776 ++maxreqs; 784 ++maxreqs;
777 } 785 }
778 786
787 unblock_sig ();
779 return count; 788 return count;
780} 789}
781 790
782static void create_pipe () 791static void create_pipe ()
783{ 792{
801/* 810/*
802 * make our pread/pwrite safe against themselves, but not against 811 * make our pread/pwrite safe against themselves, but not against
803 * 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,
804 * but that's your problem, not mine. 813 * but that's your problem, not mine.
805 */ 814 */
806static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; 815static mutex_t preadwritelock = MUTEX_INIT;
807 816
808static 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)
809{ 818{
810 ssize_t res; 819 ssize_t res;
811 off_t ooffset; 820 off_t ooffset;
834 843
835 return res; 844 return res;
836} 845}
837#endif 846#endif
838 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
839#if !HAVE_FDATASYNC 876#if !HAVE_FDATASYNC
840# define fdatasync fsync 877# define fdatasync fsync
841#endif 878#endif
842 879
843#if !HAVE_READAHEAD 880#if !HAVE_READAHEAD
862#endif 899#endif
863 900
864#if !HAVE_READDIR_R 901#if !HAVE_READDIR_R
865# define readdir_r aio_readdir_r 902# define readdir_r aio_readdir_r
866 903
867static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; 904static mutex_t readdirlock = MUTEX_INIT;
868 905
869static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) 906static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
870{ 907{
871 struct dirent *e; 908 struct dirent *e;
872 int errorno; 909 int errorno;
1071 if (req) 1108 if (req)
1072 break; 1109 break;
1073 1110
1074 ++idle; 1111 ++idle;
1075 1112
1076 if (pthread_cond_timedwait (&reqwait, &reqlock, &ts) 1113 if (COND_TIMEDWAIT (reqwait, reqlock, ts)
1077 == ETIMEDOUT) 1114 == ETIMEDOUT)
1078 { 1115 {
1079 if (idle > max_idle) 1116 if (idle > max_idle)
1080 { 1117 {
1081 --idle; 1118 --idle;
1085 UNLOCK (wrklock); 1122 UNLOCK (wrklock);
1086 goto quit; 1123 goto quit;
1087 } 1124 }
1088 1125
1089 /* we are allowed to idle, so do so without any timeout */ 1126 /* we are allowed to idle, so do so without any timeout */
1090 pthread_cond_wait (&reqwait, &reqlock); 1127 COND_WAIT (reqwait, reqlock);
1091 ts.tv_sec = time (0) + IDLE_TIMEOUT; 1128 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1092 } 1129 }
1093 1130
1094 --idle; 1131 --idle;
1095 } 1132 }
1101 errno = 0; /* strictly unnecessary */ 1138 errno = 0; /* strictly unnecessary */
1102 1139
1103 if (!(req->flags & FLAG_CANCELLED)) 1140 if (!(req->flags & FLAG_CANCELLED))
1104 switch (req->type) 1141 switch (req->type)
1105 { 1142 {
1106 case REQ_READ: req->result = pread (req->int1, req->ptr1, req->size, req->offs); break; 1143 case REQ_READ: req->result = req->offs >= 0
1107 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;
1108 1149
1109 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;
1110 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;
1111 1152
1112 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;
1113 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;
1114 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;
1115 1161
1116 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;
1117 case REQ_CLOSE: req->result = close (req->int1); break; 1163 case REQ_CLOSE: req->result = close (req->int1); break;
1118 case REQ_UNLINK: req->result = unlink (req->ptr1); break; 1164 case REQ_UNLINK: req->result = unlink (req->ptr1); break;
1119 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;
1120 case REQ_RENAME: req->result = rename (req->ptr2, req->ptr1); break; 1167 case REQ_RENAME: req->result = rename (req->ptr2, req->ptr1); break;
1121 case REQ_LINK: req->result = link (req->ptr2, req->ptr1); break; 1168 case REQ_LINK: req->result = link (req->ptr2, req->ptr1); break;
1122 case REQ_SYMLINK: req->result = symlink (req->ptr2, req->ptr1); break; 1169 case REQ_SYMLINK: req->result = symlink (req->ptr2, req->ptr1); break;
1123 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;
1124 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;
1129 1176
1130 case REQ_BUSY: 1177 case REQ_BUSY:
1131 { 1178 {
1132 struct timeval tv; 1179 struct timeval tv;
1133 1180
1134 tv.tv_sec = req->int1; 1181 tv.tv_sec = req->nv1;
1135 tv.tv_usec = req->int2; 1182 tv.tv_usec = (req->nv1 - tv.tv_usec) * 1000000.;
1136 1183
1137 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);
1138 } 1209 }
1139 1210
1140 case REQ_GROUP: 1211 case REQ_GROUP:
1141 case REQ_NOP: 1212 case REQ_NOP:
1142 break; 1213 break;
1143 1214
1144 case REQ_QUIT: 1215 case REQ_QUIT:
1145 goto quit; 1216 goto quit;
1146 1217
1147 default: 1218 default:
1148 req->result = ENOSYS; 1219 req->result = -1;
1149 break; 1220 break;
1150 } 1221 }
1151 1222
1152 req->errorno = errno; 1223 req->errorno = errno;
1153 1224
1154 LOCK (reslock); 1225 LOCK (reslock);
1155 1226
1156 ++npending; 1227 ++npending;
1157 1228
1158 if (!reqq_push (&res_queue, req)) 1229 if (!reqq_push (&res_queue, req))
1230 {
1159 /* write a dummy byte to the pipe so fh becomes ready */ 1231 /* write a dummy byte to the pipe so fh becomes ready */
1160 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 }
1161 1238
1162 self->req = 0; 1239 self->req = 0;
1163 worker_clear (self); 1240 worker_clear (self);
1164 1241
1165 UNLOCK (reslock); 1242 UNLOCK (reslock);
1204static void atfork_child (void) 1281static void atfork_child (void)
1205{ 1282{
1206 aio_req prv; 1283 aio_req prv;
1207 1284
1208 while (prv = reqq_shift (&req_queue)) 1285 while (prv = reqq_shift (&req_queue))
1209 req_free (prv); 1286 req_destroy (prv);
1210 1287
1211 while (prv = reqq_shift (&res_queue)) 1288 while (prv = reqq_shift (&res_queue))
1212 req_free (prv); 1289 req_destroy (prv);
1213 1290
1214 while (wrk_first.next != &wrk_first) 1291 while (wrk_first.next != &wrk_first)
1215 { 1292 {
1216 worker *wrk = wrk_first.next; 1293 worker *wrk = wrk_first.next;
1217 1294
1218 if (wrk->req) 1295 if (wrk->req)
1219 req_free (wrk->req); 1296 req_destroy (wrk->req);
1220 1297
1221 worker_clear (wrk); 1298 worker_clear (wrk);
1222 worker_free (wrk); 1299 worker_free (wrk);
1223 } 1300 }
1224 1301
1268 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); 1345 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
1269 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); 1346 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
1270 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); 1347 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT));
1271 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); 1348 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC));
1272 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); 1349 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO));
1350 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO));
1273 1351
1274 create_pipe (); 1352 create_pipe ();
1275 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 1353 ATFORK (atfork_prepare, atfork_parent, atfork_child);
1276} 1354}
1277 1355
1278void 1356void
1279max_poll_reqs (int nreqs) 1357max_poll_reqs (int nreqs)
1280 PROTOTYPE: $ 1358 PROTOTYPE: $
1309 max_outstanding = maxreqs; 1387 max_outstanding = maxreqs;
1310 OUTPUT: 1388 OUTPUT:
1311 RETVAL 1389 RETVAL
1312 1390
1313void 1391void
1314aio_open (pathname,flags,mode,callback=&PL_sv_undef) 1392aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef)
1315 SV8 * pathname
1316 int flags
1317 int mode
1318 SV * callback
1319 PROTOTYPE: $$$;$ 1393 PROTOTYPE: $$$;$
1320 PPCODE: 1394 PPCODE:
1321{ 1395{
1322 dREQ; 1396 dREQ;
1323 1397
1329 1403
1330 REQ_SEND; 1404 REQ_SEND;
1331} 1405}
1332 1406
1333void 1407void
1334aio_close (fh,callback=&PL_sv_undef) 1408aio_close (SV *fh, SV *callback=&PL_sv_undef)
1335 SV * fh
1336 SV * callback
1337 PROTOTYPE: $;$ 1409 PROTOTYPE: $;$
1338 ALIAS: 1410 ALIAS:
1339 aio_close = REQ_CLOSE 1411 aio_close = REQ_CLOSE
1340 aio_fsync = REQ_FSYNC 1412 aio_fsync = REQ_FSYNC
1341 aio_fdatasync = REQ_FDATASYNC 1413 aio_fdatasync = REQ_FDATASYNC
1342 PPCODE: 1414 PPCODE:
1343{ 1415{
1344 dREQ; 1416 dREQ;
1345 1417
1346 req->type = ix; 1418 req->type = ix;
1347 req->fh = newSVsv (fh); 1419 req->sv1 = newSVsv (fh);
1348 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); 1420 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
1349 1421
1350 REQ_SEND (req); 1422 REQ_SEND (req);
1351} 1423}
1352 1424
1353void 1425void
1354aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 1426aio_read (SV *fh, SV *offset, UV length, SV8 *data, UV dataoffset, SV *callback=&PL_sv_undef)
1355 SV * fh
1356 UV offset
1357 UV length
1358 SV8 * data
1359 UV dataoffset
1360 SV * callback
1361 ALIAS: 1427 ALIAS:
1362 aio_read = REQ_READ 1428 aio_read = REQ_READ
1363 aio_write = REQ_WRITE 1429 aio_write = REQ_WRITE
1364 PROTOTYPE: $$$$$;$ 1430 PROTOTYPE: $$$$$;$
1365 PPCODE: 1431 PPCODE:
1383 length = svlen - dataoffset; 1449 length = svlen - dataoffset;
1384 } 1450 }
1385 else 1451 else
1386 { 1452 {
1387 /* read: grow scalar as necessary */ 1453 /* read: grow scalar as necessary */
1388 svptr = SvGROW (data, length + dataoffset); 1454 svptr = SvGROW (data, length + dataoffset + 1);
1389 } 1455 }
1390 1456
1391 if (length < 0) 1457 if (length < 0)
1392 croak ("length must not be negative"); 1458 croak ("length must not be negative");
1393 1459
1394 { 1460 {
1395 dREQ; 1461 dREQ;
1396 1462
1397 req->type = ix; 1463 req->type = ix;
1398 req->fh = newSVsv (fh); 1464 req->sv1 = newSVsv (fh);
1399 req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) 1465 req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
1400 : IoOFP (sv_2io (fh))); 1466 : IoOFP (sv_2io (fh)));
1401 req->offs = offset; 1467 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
1402 req->size = length; 1468 req->size = length;
1403 req->sv1 = SvREFCNT_inc (data); 1469 req->sv2 = SvREFCNT_inc (data);
1404 req->ptr1 = (char *)svptr + dataoffset; 1470 req->ptr1 = (char *)svptr + dataoffset;
1405 req->stroffset = dataoffset; 1471 req->stroffset = dataoffset;
1406 1472
1407 if (!SvREADONLY (data)) 1473 if (!SvREADONLY (data))
1408 { 1474 {
1409 SvREADONLY_on (data); 1475 SvREADONLY_on (data);
1410 req->flags |= FLAG_SV1_RO_OFF; 1476 req->flags |= FLAG_SV2_RO_OFF;
1411 } 1477 }
1412 1478
1413 REQ_SEND; 1479 REQ_SEND;
1414 } 1480 }
1415} 1481}
1416 1482
1417void 1483void
1418aio_readlink (path,callback=&PL_sv_undef) 1484aio_readlink (SV8 *path, SV *callback=&PL_sv_undef)
1419 SV8 * path
1420 SV * callback
1421 PROTOTYPE: $$;$ 1485 PROTOTYPE: $$;$
1422 PPCODE: 1486 PPCODE:
1423{ 1487{
1424 SV *data; 1488 SV *data;
1425 dREQ; 1489 dREQ;
1426 1490
1427 data = newSV (NAME_MAX); 1491 data = newSV (NAME_MAX);
1428 SvPOK_on (data); 1492 SvPOK_on (data);
1429 1493
1430 req->type = REQ_READLINK; 1494 req->type = REQ_READLINK;
1431 req->fh = newSVsv (path); 1495 req->sv1 = newSVsv (path);
1432 req->ptr2 = SvPVbyte_nolen (req->fh); 1496 req->ptr2 = SvPVbyte_nolen (req->sv1);
1433 req->sv1 = data; 1497 req->sv2 = data;
1434 req->ptr1 = SvPVbyte_nolen (data); 1498 req->ptr1 = SvPVbyte_nolen (data);
1435 1499
1436 REQ_SEND; 1500 REQ_SEND;
1437} 1501}
1438 1502
1439void 1503void
1440aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) 1504aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef)
1441 SV * out_fh
1442 SV * in_fh
1443 UV in_offset
1444 UV length
1445 SV * callback
1446 PROTOTYPE: $$$$;$ 1505 PROTOTYPE: $$$$;$
1447 PPCODE: 1506 PPCODE:
1448{ 1507{
1449 dREQ; 1508 dREQ;
1450 1509
1451 req->type = REQ_SENDFILE; 1510 req->type = REQ_SENDFILE;
1452 req->fh = newSVsv (out_fh); 1511 req->sv1 = newSVsv (out_fh);
1453 req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh))); 1512 req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh)));
1454 req->sv2 = newSVsv (in_fh); 1513 req->sv2 = newSVsv (in_fh);
1455 req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); 1514 req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
1456 req->offs = in_offset; 1515 req->offs = SvVAL64 (in_offset);
1457 req->size = length; 1516 req->size = length;
1458 1517
1459 REQ_SEND; 1518 REQ_SEND;
1460} 1519}
1461 1520
1462void 1521void
1463aio_readahead (fh,offset,length,callback=&PL_sv_undef) 1522aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef)
1464 SV * fh
1465 UV offset
1466 IV length
1467 SV * callback
1468 PROTOTYPE: $$$;$ 1523 PROTOTYPE: $$$;$
1469 PPCODE: 1524 PPCODE:
1470{ 1525{
1471 dREQ; 1526 dREQ;
1472 1527
1473 req->type = REQ_READAHEAD; 1528 req->type = REQ_READAHEAD;
1474 req->fh = newSVsv (fh); 1529 req->sv1 = newSVsv (fh);
1475 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); 1530 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
1476 req->offs = offset; 1531 req->offs = SvVAL64 (offset);
1477 req->size = length; 1532 req->size = length;
1478 1533
1479 REQ_SEND; 1534 REQ_SEND;
1480} 1535}
1481 1536
1482void 1537void
1483aio_stat (fh_or_path,callback=&PL_sv_undef) 1538aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef)
1484 SV8 * fh_or_path
1485 SV * callback
1486 ALIAS: 1539 ALIAS:
1487 aio_stat = REQ_STAT 1540 aio_stat = REQ_STAT
1488 aio_lstat = REQ_LSTAT 1541 aio_lstat = REQ_LSTAT
1489 PPCODE: 1542 PPCODE:
1490{ 1543{
1491 dREQ; 1544 dREQ;
1492 1545
1493 req->ptr2 = malloc (sizeof (Stat_t)); 1546 req->ptr2 = malloc (sizeof (Stat_t));
1494 if (!req->ptr2) 1547 if (!req->ptr2)
1495 { 1548 {
1496 req_free (req); 1549 req_destroy (req);
1497 croak ("out of memory during aio_stat statdata allocation"); 1550 croak ("out of memory during aio_stat statdata allocation");
1498 } 1551 }
1499 1552
1500 req->flags |= FLAG_PTR2_FREE; 1553 req->flags |= FLAG_PTR2_FREE;
1554 req->sv1 = newSVsv (fh_or_path);
1501 1555
1502 if (SvPOK (fh_or_path)) 1556 if (SvPOK (fh_or_path))
1503 { 1557 {
1504 req->type = ix; 1558 req->type = ix;
1505 req->sv1 = newSVsv (fh_or_path);
1506 req->ptr1 = SvPVbyte_nolen (req->sv1); 1559 req->ptr1 = SvPVbyte_nolen (req->sv1);
1507 } 1560 }
1508 else 1561 else
1509 { 1562 {
1510 req->type = REQ_FSTAT; 1563 req->type = REQ_FSTAT;
1511 req->fh = newSVsv (fh_or_path);
1512 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 1564 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1513 } 1565 }
1514 1566
1515 REQ_SEND; 1567 REQ_SEND;
1516} 1568}
1517 1569
1518void 1570void
1571aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef)
1572 PPCODE:
1573{
1574 dREQ;
1575
1576 req->nv1 = SvOK (atime) ? SvNV (atime) : -1.;
1577 req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.;
1578 req->sv1 = newSVsv (fh_or_path);
1579
1580 if (SvPOK (fh_or_path))
1581 {
1582 req->type = REQ_UTIME;
1583 req->ptr1 = SvPVbyte_nolen (req->sv1);
1584 }
1585 else
1586 {
1587 req->type = REQ_FUTIME;
1588 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1589 }
1590
1591 REQ_SEND;
1592}
1593
1594void
1595aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef)
1596 PPCODE:
1597{
1598 dREQ;
1599
1600 req->mode = mode;
1601 req->sv1 = newSVsv (fh_or_path);
1602
1603 if (SvPOK (fh_or_path))
1604 {
1605 req->type = REQ_CHMOD;
1606 req->ptr1 = SvPVbyte_nolen (req->sv1);
1607 }
1608 else
1609 {
1610 req->type = REQ_FCHMOD;
1611 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1612 }
1613
1614 REQ_SEND;
1615}
1616
1617void
1618aio_chown (SV8 *fh_or_path, SV *uid, SV *gid, SV *callback=&PL_sv_undef)
1619 PPCODE:
1620{
1621 dREQ;
1622
1623 req->int2 = SvOK (uid) ? SvIV (uid) : -1;
1624 req->int3 = SvOK (gid) ? SvIV (gid) : -1;
1625 req->sv1 = newSVsv (fh_or_path);
1626
1627 if (SvPOK (fh_or_path))
1628 {
1629 req->type = REQ_CHOWN;
1630 req->ptr1 = SvPVbyte_nolen (req->sv1);
1631 }
1632 else
1633 {
1634 req->type = REQ_FCHOWN;
1635 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1636 }
1637
1638 REQ_SEND;
1639}
1640
1641void
1519aio_unlink (pathname,callback=&PL_sv_undef) 1642aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef)
1520 SV8 * pathname
1521 SV * callback
1522 ALIAS: 1643 ALIAS:
1523 aio_unlink = REQ_UNLINK 1644 aio_unlink = REQ_UNLINK
1524 aio_rmdir = REQ_RMDIR 1645 aio_rmdir = REQ_RMDIR
1525 aio_readdir = REQ_READDIR 1646 aio_readdir = REQ_READDIR
1526 PPCODE: 1647 PPCODE:
1533 1654
1534 REQ_SEND; 1655 REQ_SEND;
1535} 1656}
1536 1657
1537void 1658void
1659aio_mkdir (SV8 *pathname, int mode, SV *callback=&PL_sv_undef)
1660 PPCODE:
1661{
1662 dREQ;
1663
1664 req->type = REQ_MKDIR;
1665 req->sv1 = newSVsv (pathname);
1666 req->ptr1 = SvPVbyte_nolen (req->sv1);
1667 req->mode = mode;
1668
1669 REQ_SEND;
1670}
1671
1672void
1538aio_link (oldpath,newpath,callback=&PL_sv_undef) 1673aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef)
1539 SV8 * oldpath
1540 SV8 * newpath
1541 SV * callback
1542 ALIAS: 1674 ALIAS:
1543 aio_link = REQ_LINK 1675 aio_link = REQ_LINK
1544 aio_symlink = REQ_SYMLINK 1676 aio_symlink = REQ_SYMLINK
1545 aio_rename = REQ_RENAME 1677 aio_rename = REQ_RENAME
1546 PPCODE: 1678 PPCODE:
1547{ 1679{
1548 dREQ; 1680 dREQ;
1549 1681
1550 req->type = ix; 1682 req->type = ix;
1551 req->fh = newSVsv (oldpath); 1683 req->sv2 = newSVsv (oldpath);
1552 req->ptr2 = SvPVbyte_nolen (req->fh); 1684 req->ptr2 = SvPVbyte_nolen (req->sv2);
1553 req->sv1 = newSVsv (newpath); 1685 req->sv1 = newSVsv (newpath);
1554 req->ptr1 = SvPVbyte_nolen (req->sv1); 1686 req->ptr1 = SvPVbyte_nolen (req->sv1);
1555 1687
1556 REQ_SEND; 1688 REQ_SEND;
1557} 1689}
1558 1690
1559void 1691void
1560aio_mknod (pathname,mode,dev,callback=&PL_sv_undef) 1692aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef)
1561 SV8 * pathname
1562 SV * callback
1563 UV mode
1564 UV dev
1565 PPCODE: 1693 PPCODE:
1566{ 1694{
1567 dREQ; 1695 dREQ;
1568 1696
1569 req->type = REQ_MKNOD; 1697 req->type = REQ_MKNOD;
1574 1702
1575 REQ_SEND; 1703 REQ_SEND;
1576} 1704}
1577 1705
1578void 1706void
1579aio_busy (delay,callback=&PL_sv_undef) 1707aio_busy (double delay, SV *callback=&PL_sv_undef)
1580 double delay
1581 SV * callback
1582 PPCODE: 1708 PPCODE:
1583{ 1709{
1584 dREQ; 1710 dREQ;
1585 1711
1586 req->type = REQ_BUSY; 1712 req->type = REQ_BUSY;
1587 req->int1 = delay < 0. ? 0 : delay; 1713 req->nv1 = delay < 0. ? 0. : delay;
1588 req->int2 = delay < 0. ? 0 : 1000. * (delay - req->int1);
1589 1714
1590 REQ_SEND; 1715 REQ_SEND;
1591} 1716}
1592 1717
1593void 1718void
1594aio_group (callback=&PL_sv_undef) 1719aio_group (SV *callback=&PL_sv_undef)
1595 SV * callback
1596 PROTOTYPE: ;$ 1720 PROTOTYPE: ;$
1597 PPCODE: 1721 PPCODE:
1598{ 1722{
1599 dREQ; 1723 dREQ;
1600 1724
1603 req_send (req); 1727 req_send (req);
1604 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 1728 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1605} 1729}
1606 1730
1607void 1731void
1608aio_nop (callback=&PL_sv_undef) 1732aio_nop (SV *callback=&PL_sv_undef)
1609 SV * callback
1610 PPCODE: 1733 PPCODE:
1611{ 1734{
1612 dREQ; 1735 dREQ;
1613 1736
1614 req->type = REQ_NOP; 1737 req->type = REQ_NOP;
1677poll_wait() 1800poll_wait()
1678 PROTOTYPE: 1801 PROTOTYPE:
1679 CODE: 1802 CODE:
1680 poll_wait (); 1803 poll_wait ();
1681 1804
1805void
1806setsig (int signum = SIGIO)
1807 PROTOTYPE: ;$
1808 CODE:
1809{
1810 if (block_sig_level)
1811 croak ("cannot call IO::AIO::setsig from within aio_block/callback");
1812
1813 LOCK (reslock);
1814 main_tid = pthread_self ();
1815 main_sig = signum;
1816 UNLOCK (reslock);
1817
1818 if (main_sig && npending)
1819 pthread_kill (main_tid, main_sig);
1820}
1821
1822void
1823aio_block (SV *cb)
1824 PROTOTYPE: &
1825 PPCODE:
1826{
1827 int count;
1828
1829 block_sig ();
1830 PUSHMARK (SP);
1831 PUTBACK;
1832 count = call_sv (cb, GIMME_V | G_NOARGS | G_EVAL);
1833 SPAGAIN;
1834 unblock_sig ();
1835
1836 if (SvTRUE (ERRSV))
1837 croak (0);
1838
1839 XSRETURN (count);
1840}
1841
1682int 1842int
1683nreqs() 1843nreqs()
1684 PROTOTYPE: 1844 PROTOTYPE:
1685 CODE: 1845 CODE:
1686 RETVAL = nreqs; 1846 RETVAL = nreqs;
1735 PPCODE: 1895 PPCODE:
1736{ 1896{
1737 int i; 1897 int i;
1738 aio_req req; 1898 aio_req req;
1739 1899
1900 if (main_sig && !block_sig_level)
1901 croak ("aio_group->add called outside aio_block/callback context while IO::AIO::setsig is in use");
1902
1740 if (grp->int1 == 2) 1903 if (grp->int1 == 2)
1741 croak ("cannot add requests to IO::AIO::GRP after the group finished"); 1904 croak ("cannot add requests to IO::AIO::GRP after the group finished");
1742 1905
1743 for (i = 1; i < items; ++i ) 1906 for (i = 1; i < items; ++i )
1744 { 1907 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines