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.102 by root, Sun Jun 3 09:44:17 2007 UTC vs.
Revision 1.111 by root, Sun Mar 30 06:31:49 2008 UTC

4 4
5#include "EXTERN.h" 5#include "EXTERN.h"
6#include "perl.h" 6#include "perl.h"
7#include "XSUB.h" 7#include "XSUB.h"
8 8
9#include "autoconf/config.h"
10
11#include <stddef.h> 9#include <stddef.h>
12#include <stdlib.h> 10#include <stdlib.h>
13#include <errno.h> 11#include <errno.h>
14#include <sys/time.h>
15#include <sys/select.h>
16#include <sys/types.h> 12#include <sys/types.h>
17#include <sys/stat.h> 13#include <sys/stat.h>
18#include <limits.h> 14#include <limits.h>
19#include <unistd.h>
20#include <fcntl.h> 15#include <fcntl.h>
21#include <signal.h>
22#include <sched.h> 16#include <sched.h>
17
18#ifdef _WIN32
19
20# define SIGIO 0
21 typedef Direntry_t X_DIRENT;
22#undef malloc
23#undef free
24
25// perl overrides all those nice win32 functions
26# undef open
27# undef read
28# undef write
29# undef send
30# undef recv
31# undef stat
32# undef fstat
33# define lstat stat
34# undef truncate
35# undef ftruncate
36# undef open
37# undef close
38# undef unlink
39# undef rmdir
40# undef rename
41# undef lseek
42
43# define chown(a,b,c) (errno = ENOSYS, -1)
44# define fchown(a,b,c) (errno = ENOSYS, -1)
45# define fchmod(a,b) (errno = ENOSYS, -1)
46# define symlink(a,b) (errno = ENOSYS, -1)
47# define readlink(a,b,c) (errno = ENOSYS, -1)
48# define mknod(a,b,c) (errno = ENOSYS, -1)
49# define truncate(a,b) (errno = ENOSYS, -1)
50# define ftruncate(fd,o) chsize ((fd), (o))
51# define fsync(fd) _commit (fd)
52# define opendir(fd) (errno = ENOSYS, 0)
53# define readdir(fd) (errno = ENOSYS, -1)
54# define closedir(fd) (errno = ENOSYS, -1)
55# define mkdir(a,b) mkdir (a)
56
57#else
58
59# include "autoconf/config.h"
60# include <sys/time.h>
61# include <sys/select.h>
62# include <unistd.h>
23#include <utime.h> 63# include <utime.h>
64# include <signal.h>
65 typedef struct dirent X_DIRENT;
66
67#endif
24 68
25#if HAVE_SENDFILE 69#if HAVE_SENDFILE
26# if __linux 70# if __linux
27# include <sys/sendfile.h> 71# include <sys/sendfile.h>
28# elif __freebsd 72# elif __freebsd
53# define SvVAL64 SvIV 97# define SvVAL64 SvIV
54#else 98#else
55# define SvVAL64 SvNV 99# define SvVAL64 SvNV
56#endif 100#endif
57 101
102static HV *stash;
103
58#define dBUF \ 104#define dBUF \
59 char *aio_buf; \ 105 char *aio_buf; \
60 LOCK (wrklock); \ 106 X_LOCK (wrklock); \
61 self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ 107 self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \
62 UNLOCK (wrklock); \ 108 X_UNLOCK (wrklock); \
63 if (!aio_buf) \ 109 if (!aio_buf) \
64 return -1; 110 return -1;
65 111
66typedef SV SV8; /* byte-sv, used for argument-checking */ 112typedef SV SV8; /* byte-sv, used for argument-checking */
67 113
69 REQ_QUIT, 115 REQ_QUIT,
70 REQ_OPEN, REQ_CLOSE, 116 REQ_OPEN, REQ_CLOSE,
71 REQ_READ, REQ_WRITE, 117 REQ_READ, REQ_WRITE,
72 REQ_READAHEAD, REQ_SENDFILE, 118 REQ_READAHEAD, REQ_SENDFILE,
73 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 119 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
120 REQ_TRUNCATE, REQ_FTRUNCATE,
74 REQ_UTIME, REQ_FUTIME, 121 REQ_UTIME, REQ_FUTIME,
75 REQ_CHMOD, REQ_FCHMOD, 122 REQ_CHMOD, REQ_FCHMOD,
76 REQ_CHOWN, REQ_FCHOWN, 123 REQ_CHOWN, REQ_FCHOWN,
77 REQ_FSYNC, REQ_FDATASYNC, 124 REQ_SYNC, REQ_FSYNC, REQ_FDATASYNC,
78 REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME, 125 REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME,
79 REQ_MKNOD, REQ_READDIR, 126 REQ_MKNOD, REQ_READDIR,
80 REQ_LINK, REQ_SYMLINK, REQ_READLINK, 127 REQ_LINK, REQ_SYMLINK, REQ_READLINK,
81 REQ_GROUP, REQ_NOP, 128 REQ_GROUP, REQ_NOP,
82 REQ_BUSY, 129 REQ_BUSY,
142 189
143static thread_t main_tid; 190static thread_t main_tid;
144static int main_sig; 191static int main_sig;
145static int block_sig_level; 192static int block_sig_level;
146 193
147void block_sig () 194void block_sig (void)
148{ 195{
149 sigset_t ss; 196 sigset_t ss;
150 197
151 if (block_sig_level++) 198 if (block_sig_level++)
152 return; 199 return;
157 sigemptyset (&ss); 204 sigemptyset (&ss);
158 sigaddset (&ss, main_sig); 205 sigaddset (&ss, main_sig);
159 pthread_sigmask (SIG_BLOCK, &ss, 0); 206 pthread_sigmask (SIG_BLOCK, &ss, 0);
160} 207}
161 208
162void unblock_sig () 209void unblock_sig (void)
163{ 210{
164 sigset_t ss; 211 sigset_t ss;
165 212
166 if (--block_sig_level) 213 if (--block_sig_level)
167 return; 214 return;
177static int next_pri = DEFAULT_PRI + PRI_BIAS; 224static int next_pri = DEFAULT_PRI + PRI_BIAS;
178 225
179static unsigned int started, idle, wanted; 226static unsigned int started, idle, wanted;
180 227
181/* worker threads management */ 228/* worker threads management */
182static mutex_t wrklock = MUTEX_INIT; 229static mutex_t wrklock = X_MUTEX_INIT;
183 230
184typedef struct worker { 231typedef struct worker {
185 /* locked by wrklock */ 232 /* locked by wrklock */
186 struct worker *prev, *next; 233 struct worker *prev, *next;
187 234
219} 266}
220 267
221static volatile unsigned int nreqs, nready, npending; 268static volatile unsigned int nreqs, nready, npending;
222static volatile unsigned int max_idle = 4; 269static volatile unsigned int max_idle = 4;
223static volatile unsigned int max_outstanding = 0xffffffff; 270static volatile unsigned int max_outstanding = 0xffffffff;
224static int respipe [2]; 271static int respipe_osf [2], respipe [2] = { -1, -1 };
225 272
226static mutex_t reslock = MUTEX_INIT; 273static mutex_t reslock = X_MUTEX_INIT;
227static mutex_t reqlock = MUTEX_INIT; 274static mutex_t reqlock = X_MUTEX_INIT;
228static cond_t reqwait = COND_INIT; 275static cond_t reqwait = X_COND_INIT;
229 276
230#if WORDACCESS_UNSAFE 277#if WORDACCESS_UNSAFE
231 278
232static unsigned int get_nready () 279static unsigned int get_nready (void)
233{ 280{
234 unsigned int retval; 281 unsigned int retval;
235 282
236 LOCK (reqlock); 283 X_LOCK (reqlock);
237 retval = nready; 284 retval = nready;
238 UNLOCK (reqlock); 285 X_UNLOCK (reqlock);
239 286
240 return retval; 287 return retval;
241} 288}
242 289
243static unsigned int get_npending () 290static unsigned int get_npending (void)
244{ 291{
245 unsigned int retval; 292 unsigned int retval;
246 293
247 LOCK (reslock); 294 X_LOCK (reslock);
248 retval = npending; 295 retval = npending;
249 UNLOCK (reslock); 296 X_UNLOCK (reslock);
250 297
251 return retval; 298 return retval;
252} 299}
253 300
254static unsigned int get_nthreads () 301static unsigned int get_nthreads (void)
255{ 302{
256 unsigned int retval; 303 unsigned int retval;
257 304
258 LOCK (wrklock); 305 X_LOCK (wrklock);
259 retval = started; 306 retval = started;
260 UNLOCK (wrklock); 307 X_UNLOCK (wrklock);
261 308
262 return retval; 309 return retval;
263} 310}
264 311
265#else 312#else
322 } 369 }
323 370
324 abort (); 371 abort ();
325} 372}
326 373
327static int poll_cb (); 374static int poll_cb (void);
328static int req_invoke (aio_req req); 375static int req_invoke (aio_req req);
329static void req_destroy (aio_req req); 376static void req_destroy (aio_req req);
330static void req_cancel (aio_req req); 377static void req_cancel (aio_req req);
331 378
332/* must be called at most once */ 379/* must be called at most once */
456 break; 503 break;
457 504
458 case REQ_OPEN: 505 case REQ_OPEN:
459 { 506 {
460 /* convert fd to fh */ 507 /* convert fd to fh */
461 SV *fh; 508 SV *fh = &PL_sv_undef;
462 509
463 PUSHs (sv_2mortal (newSViv (req->result))); 510 if (req->result >= 0)
464 PUTBACK; 511 {
465 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 512 GV *gv = (GV *)sv_newmortal ();
466 SPAGAIN; 513 int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR);
514 char sym [64];
515 int symlen;
516
517 symlen = snprintf (sym, sizeof (sym), "fd#%d", req->result);
518 gv_init (gv, stash, sym, symlen, 0);
467 519
468 fh = POPs; 520 symlen = snprintf (
469 PUSHMARK (SP); 521 sym,
522 sizeof (sym),
523 "%s&=%d",
524 flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<",
525 req->result
526 );
527
528 if (do_open (gv, sym, symlen, 0, 0, 0, 0))
529 fh = (SV *)gv;
530 }
531
470 XPUSHs (fh); 532 PUSHs (fh);
471 } 533 }
472 break; 534 break;
473 535
474 case REQ_GROUP: 536 case REQ_GROUP:
475 req->int1 = 2; /* mark group as finished */ 537 req->int1 = 2; /* mark group as finished */
519 } 581 }
520 582
521 errno = req->errorno; 583 errno = req->errorno;
522 584
523 PUTBACK; 585 PUTBACK;
524 call_sv (req->callback, G_VOID | G_EVAL); 586 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD);
525 SPAGAIN; 587 SPAGAIN;
526 588
527 FREETMPS; 589 FREETMPS;
528 LEAVE; 590 LEAVE;
591
592 PUTBACK;
529 } 593 }
530 594
531 if (req->grp) 595 if (req->grp)
532 { 596 {
533 aio_req grp = req->grp; 597 aio_req grp = req->grp;
582 req->flags |= FLAG_CANCELLED; 646 req->flags |= FLAG_CANCELLED;
583 647
584 req_cancel_subs (req); 648 req_cancel_subs (req);
585} 649}
586 650
587static void *aio_proc (void *arg); 651#ifdef USE_SOCKETS_AS_HANDLES
652# define TO_SOCKET(x) (win32_get_osfhandle (x))
653#else
654# define TO_SOCKET(x) (x)
655#endif
656
657static void
658create_respipe (void)
659{
660 int old_readfd = respipe [0];
661
662 if (respipe [1] >= 0)
663 respipe_close (TO_SOCKET (respipe [1]));
664
665#ifdef _WIN32
666 if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe))
667#else
668 if (pipe (respipe))
669#endif
670 croak ("unable to initialize result pipe");
671
672 if (old_readfd >= 0)
673 {
674 if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0)
675 croak ("unable to initialize result pipe(2)");
676
677 respipe_close (respipe [0]);
678 respipe [0] = old_readfd;
679 }
680
681#ifdef _WIN32
682 int arg = 1;
683 if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg)
684 || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg))
685#else
686 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)
687 || fcntl (respipe [1], F_SETFL, O_NONBLOCK))
688#endif
689 croak ("unable to initialize result pipe(3)");
690
691 respipe_osf [0] = TO_SOCKET (respipe [0]);
692 respipe_osf [1] = TO_SOCKET (respipe [1]);
693}
694
695X_THREAD_PROC (aio_proc);
588 696
589static void start_thread (void) 697static void start_thread (void)
590{ 698{
591 worker *wrk = calloc (1, sizeof (worker)); 699 worker *wrk = calloc (1, sizeof (worker));
592 700
593 if (!wrk) 701 if (!wrk)
594 croak ("unable to allocate worker thread data"); 702 croak ("unable to allocate worker thread data");
595 703
596 LOCK (wrklock); 704 X_LOCK (wrklock);
597 705
598 if (thread_create (&wrk->tid, aio_proc, (void *)wrk)) 706 if (thread_create (&wrk->tid, aio_proc, (void *)wrk))
599 { 707 {
600 wrk->prev = &wrk_first; 708 wrk->prev = &wrk_first;
601 wrk->next = wrk_first.next; 709 wrk->next = wrk_first.next;
604 ++started; 712 ++started;
605 } 713 }
606 else 714 else
607 free (wrk); 715 free (wrk);
608 716
609 UNLOCK (wrklock); 717 X_UNLOCK (wrklock);
610} 718}
611 719
612static void maybe_start_thread () 720static void maybe_start_thread (void)
613{ 721{
614 if (get_nthreads () >= wanted) 722 if (get_nthreads () >= wanted)
615 return; 723 return;
616 724
617 /* todo: maybe use idle here, but might be less exact */ 725 /* todo: maybe use idle here, but might be less exact */
625{ 733{
626 block_sig (); 734 block_sig ();
627 735
628 ++nreqs; 736 ++nreqs;
629 737
630 LOCK (reqlock); 738 X_LOCK (reqlock);
631 ++nready; 739 ++nready;
632 reqq_push (&req_queue, req); 740 reqq_push (&req_queue, req);
633 COND_SIGNAL (reqwait); 741 X_COND_SIGNAL (reqwait);
634 UNLOCK (reqlock); 742 X_UNLOCK (reqlock);
635 743
636 unblock_sig (); 744 unblock_sig ();
637 745
638 maybe_start_thread (); 746 maybe_start_thread ();
639} 747}
645 Newz (0, req, 1, aio_cb); 753 Newz (0, req, 1, aio_cb);
646 754
647 req->type = REQ_QUIT; 755 req->type = REQ_QUIT;
648 req->pri = PRI_MAX + PRI_BIAS; 756 req->pri = PRI_MAX + PRI_BIAS;
649 757
650 LOCK (reqlock); 758 X_LOCK (reqlock);
651 reqq_push (&req_queue, req); 759 reqq_push (&req_queue, req);
652 COND_SIGNAL (reqwait); 760 X_COND_SIGNAL (reqwait);
653 UNLOCK (reqlock); 761 X_UNLOCK (reqlock);
654 762
655 LOCK (wrklock); 763 X_LOCK (wrklock);
656 --started; 764 --started;
657 UNLOCK (wrklock); 765 X_UNLOCK (wrklock);
658} 766}
659 767
660static void set_max_idle (int nthreads) 768static void set_max_idle (int nthreads)
661{ 769{
662 if (WORDACCESS_UNSAFE) LOCK (reqlock); 770 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
663 max_idle = nthreads <= 0 ? 1 : nthreads; 771 max_idle = nthreads <= 0 ? 1 : nthreads;
664 if (WORDACCESS_UNSAFE) UNLOCK (reqlock); 772 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
665} 773}
666 774
667static void min_parallel (int nthreads) 775static void min_parallel (int nthreads)
668{ 776{
669 if (wanted < nthreads) 777 if (wanted < nthreads)
677 785
678 while (started > wanted) 786 while (started > wanted)
679 end_thread (); 787 end_thread ();
680} 788}
681 789
682static void poll_wait () 790static void poll_wait (void)
683{ 791{
684 fd_set rfd; 792 fd_set rfd;
685 793
686 while (nreqs) 794 while (nreqs)
687 { 795 {
688 int size; 796 int size;
689 if (WORDACCESS_UNSAFE) LOCK (reslock); 797 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
690 size = res_queue.size; 798 size = res_queue.size;
691 if (WORDACCESS_UNSAFE) UNLOCK (reslock); 799 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
692 800
693 if (size) 801 if (size)
694 return; 802 return;
695 803
696 maybe_start_thread (); 804 maybe_start_thread ();
697 805
698 FD_ZERO(&rfd); 806 FD_ZERO (&rfd);
699 FD_SET(respipe [0], &rfd); 807 FD_SET (respipe [0], &rfd);
700 808
701 select (respipe [0] + 1, &rfd, 0, 0, 0); 809 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
702 } 810 }
703} 811}
704 812
705static int poll_cb () 813static int poll_cb (void)
706{ 814{
707 dSP; 815 dSP;
708 int count = 0; 816 int count = 0;
709 int maxreqs = max_poll_reqs; 817 int maxreqs = max_poll_reqs;
710 int do_croak = 0; 818 int do_croak = 0;
720 { 828 {
721 for (;;) 829 for (;;)
722 { 830 {
723 maybe_start_thread (); 831 maybe_start_thread ();
724 832
725 LOCK (reslock); 833 X_LOCK (reslock);
726 req = reqq_shift (&res_queue); 834 req = reqq_shift (&res_queue);
727 835
728 if (req) 836 if (req)
729 { 837 {
730 --npending; 838 --npending;
731 839
732 if (!res_queue.size) 840 if (!res_queue.size)
733 { 841 {
734 /* read any signals sent by the worker threads */ 842 /* read any signals sent by the worker threads */
735 char buf [4]; 843 char buf [4];
736 while (read (respipe [0], buf, 4) == 4) 844 while (respipe_read (respipe [0], buf, 4) == 4)
737 ; 845 ;
738 } 846 }
739 } 847 }
740 848
741 UNLOCK (reslock); 849 X_UNLOCK (reslock);
742 850
743 if (!req) 851 if (!req)
744 break; 852 break;
745 853
746 --nreqs; 854 --nreqs;
786 894
787 unblock_sig (); 895 unblock_sig ();
788 return count; 896 return count;
789} 897}
790 898
791static void create_pipe ()
792{
793 if (pipe (respipe))
794 croak ("unable to initialize result pipe");
795
796 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
797 croak ("cannot set result pipe to nonblocking mode");
798
799 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
800 croak ("cannot set result pipe to nonblocking mode");
801}
802
803/*****************************************************************************/ 899/*****************************************************************************/
804/* work around various missing functions */ 900/* work around various missing functions */
805 901
806#if !HAVE_PREADWRITE 902#if !HAVE_PREADWRITE
807# define pread aio_pread 903# define pread aio_pread
810/* 906/*
811 * make our pread/pwrite safe against themselves, but not against 907 * make our pread/pwrite safe against themselves, but not against
812 * normal read/write by using a mutex. slows down execution a lot, 908 * normal read/write by using a mutex. slows down execution a lot,
813 * but that's your problem, not mine. 909 * but that's your problem, not mine.
814 */ 910 */
815static mutex_t preadwritelock = MUTEX_INIT; 911static mutex_t preadwritelock = X_MUTEX_INIT;
816 912
817static ssize_t pread (int fd, void *buf, size_t count, off_t offset) 913static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
818{ 914{
819 ssize_t res; 915 ssize_t res;
820 off_t ooffset; 916 off_t ooffset;
821 917
822 LOCK (preadwritelock); 918 X_LOCK (preadwritelock);
823 ooffset = lseek (fd, 0, SEEK_CUR); 919 ooffset = lseek (fd, 0, SEEK_CUR);
824 lseek (fd, offset, SEEK_SET); 920 lseek (fd, offset, SEEK_SET);
825 res = read (fd, buf, count); 921 res = read (fd, buf, count);
826 lseek (fd, ooffset, SEEK_SET); 922 lseek (fd, ooffset, SEEK_SET);
827 UNLOCK (preadwritelock); 923 X_UNLOCK (preadwritelock);
828 924
829 return res; 925 return res;
830} 926}
831 927
832static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) 928static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset)
833{ 929{
834 ssize_t res; 930 ssize_t res;
835 off_t ooffset; 931 off_t ooffset;
836 932
837 LOCK (preadwritelock); 933 X_LOCK (preadwritelock);
838 ooffset = lseek (fd, 0, SEEK_CUR); 934 ooffset = lseek (fd, 0, SEEK_CUR);
839 lseek (fd, offset, SEEK_SET); 935 lseek (fd, offset, SEEK_SET);
840 res = write (fd, buf, count); 936 res = write (fd, buf, count);
841 lseek (fd, offset, SEEK_SET); 937 lseek (fd, offset, SEEK_SET);
842 UNLOCK (preadwritelock); 938 X_UNLOCK (preadwritelock);
843 939
844 return res; 940 return res;
845} 941}
846#endif 942#endif
847 943
880#if !HAVE_READAHEAD 976#if !HAVE_READAHEAD
881# define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) 977# define readahead(fd,offset,count) aio_readahead (fd, offset, count, self)
882 978
883static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) 979static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self)
884{ 980{
981 size_t todo = count;
885 dBUF; 982 dBUF;
886 983
887 while (count > 0) 984 while (todo > 0)
888 { 985 {
889 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; 986 size_t len = todo < AIO_BUFSIZE ? todo : AIO_BUFSIZE;
890 987
891 pread (fd, aio_buf, len, offset); 988 pread (fd, aio_buf, len, offset);
892 offset += len; 989 offset += len;
893 count -= len; 990 todo -= len;
894 } 991 }
895 992
896 errno = 0; 993 errno = 0;
994 return count;
897} 995}
898 996
899#endif 997#endif
900 998
901#if !HAVE_READDIR_R 999#if !HAVE_READDIR_R
902# define readdir_r aio_readdir_r 1000# define readdir_r aio_readdir_r
903 1001
904static mutex_t readdirlock = MUTEX_INIT; 1002static mutex_t readdirlock = X_MUTEX_INIT;
905 1003
906static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) 1004static int readdir_r (DIR *dirp, X_DIRENT *ent, X_DIRENT **res)
907{ 1005{
908 struct dirent *e; 1006 X_DIRENT *e;
909 int errorno; 1007 int errorno;
910 1008
911 LOCK (readdirlock); 1009 X_LOCK (readdirlock);
912 1010
913 e = readdir (dirp); 1011 e = readdir (dirp);
914 errorno = errno; 1012 errorno = errno;
915 1013
916 if (e) 1014 if (e)
919 strcpy (ent->d_name, e->d_name); 1017 strcpy (ent->d_name, e->d_name);
920 } 1018 }
921 else 1019 else
922 *res = 0; 1020 *res = 0;
923 1021
924 UNLOCK (readdirlock); 1022 X_UNLOCK (readdirlock);
925 1023
926 errno = errorno; 1024 errno = errorno;
927 return e ? 0 : -1; 1025 return e ? 0 : -1;
928} 1026}
929#endif 1027#endif
1025static void scandir_ (aio_req req, worker *self) 1123static void scandir_ (aio_req req, worker *self)
1026{ 1124{
1027 DIR *dirp; 1125 DIR *dirp;
1028 union 1126 union
1029 { 1127 {
1030 struct dirent d; 1128 X_DIRENT d;
1031 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 1129 char b [offsetof (X_DIRENT, d_name) + NAME_MAX + 1];
1032 } *u; 1130 } *u;
1033 struct dirent *entp; 1131 X_DIRENT *entp;
1034 char *name, *names; 1132 char *name, *names;
1035 int memlen = 4096; 1133 int memlen = 4096;
1036 int memofs = 0; 1134 int memofs = 0;
1037 int res = 0; 1135 int res = 0;
1038 int errorno;
1039 1136
1040 LOCK (wrklock); 1137 X_LOCK (wrklock);
1041 self->dirp = dirp = opendir (req->ptr1); 1138 self->dirp = dirp = opendir (req->ptr1);
1042 self->dbuf = u = malloc (sizeof (*u)); 1139 self->dbuf = u = malloc (sizeof (*u));
1043 req->flags |= FLAG_PTR2_FREE; 1140 req->flags |= FLAG_PTR2_FREE;
1044 req->ptr2 = names = malloc (memlen); 1141 req->ptr2 = names = malloc (memlen);
1045 UNLOCK (wrklock); 1142 X_UNLOCK (wrklock);
1046 1143
1047 if (dirp && u && names) 1144 if (dirp && u && names)
1048 for (;;) 1145 for (;;)
1049 { 1146 {
1050 errno = 0; 1147 errno = 0;
1062 res++; 1159 res++;
1063 1160
1064 while (memofs + len > memlen) 1161 while (memofs + len > memlen)
1065 { 1162 {
1066 memlen *= 2; 1163 memlen *= 2;
1067 LOCK (wrklock); 1164 X_LOCK (wrklock);
1068 req->ptr2 = names = realloc (names, memlen); 1165 req->ptr2 = names = realloc (names, memlen);
1069 UNLOCK (wrklock); 1166 X_UNLOCK (wrklock);
1070 1167
1071 if (!names) 1168 if (!names)
1072 break; 1169 break;
1073 } 1170 }
1074 1171
1083 req->result = res; 1180 req->result = res;
1084} 1181}
1085 1182
1086/*****************************************************************************/ 1183/*****************************************************************************/
1087 1184
1088static void *aio_proc (void *thr_arg) 1185X_THREAD_PROC (aio_proc)
1089{ 1186{
1187 {//D
1090 aio_req req; 1188 aio_req req;
1091 struct timespec ts; 1189 struct timespec ts;
1092 worker *self = (worker *)thr_arg; 1190 worker *self = (worker *)thr_arg;
1093 1191
1094 /* try to distribute timeouts somewhat evenly */ 1192 /* try to distribute timeouts somewhat randomly */
1095 ts.tv_nsec = (((unsigned long)self + (unsigned long)ts.tv_sec) & 1023UL) 1193 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1096 * (1000000000UL / 1024UL);
1097 1194
1098 for (;;) 1195 for (;;)
1099 { 1196 {
1100 ts.tv_sec = time (0) + IDLE_TIMEOUT; 1197 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1101 1198
1102 LOCK (reqlock); 1199 X_LOCK (reqlock);
1103 1200
1104 for (;;) 1201 for (;;)
1105 { 1202 {
1106 self->req = req = reqq_shift (&req_queue); 1203 self->req = req = reqq_shift (&req_queue);
1107 1204
1108 if (req) 1205 if (req)
1109 break; 1206 break;
1110 1207
1111 ++idle; 1208 ++idle;
1112 1209
1113 if (COND_TIMEDWAIT (reqwait, reqlock, ts) 1210 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts)
1114 == ETIMEDOUT) 1211 == ETIMEDOUT)
1115 { 1212 {
1116 if (idle > max_idle) 1213 if (idle > max_idle)
1117 { 1214 {
1118 --idle; 1215 --idle;
1119 UNLOCK (reqlock); 1216 X_UNLOCK (reqlock);
1120 LOCK (wrklock); 1217 X_LOCK (wrklock);
1121 --started; 1218 --started;
1122 UNLOCK (wrklock); 1219 X_UNLOCK (wrklock);
1123 goto quit; 1220 goto quit;
1124 } 1221 }
1125 1222
1126 /* we are allowed to idle, so do so without any timeout */ 1223 /* we are allowed to idle, so do so without any timeout */
1127 COND_WAIT (reqwait, reqlock); 1224 X_COND_WAIT (reqwait, reqlock);
1128 ts.tv_sec = time (0) + IDLE_TIMEOUT; 1225 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1129 } 1226 }
1130 1227
1131 --idle; 1228 --idle;
1132 } 1229 }
1133 1230
1134 --nready; 1231 --nready;
1135 1232
1136 UNLOCK (reqlock); 1233 X_UNLOCK (reqlock);
1137 1234
1138 errno = 0; /* strictly unnecessary */ 1235 errno = 0; /* strictly unnecessary */
1139 1236
1140 if (!(req->flags & FLAG_CANCELLED)) 1237 if (!(req->flags & FLAG_CANCELLED))
1141 switch (req->type) 1238 switch (req->type)
1156 1253
1157 case REQ_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break; 1254 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; 1255 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; 1256 case REQ_CHMOD: req->result = chmod (req->ptr1, req->mode); break;
1160 case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break; 1257 case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break;
1258 case REQ_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break;
1259 case REQ_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break;
1161 1260
1162 case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break; 1261 case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break;
1163 case REQ_CLOSE: req->result = close (req->int1); break; 1262 case REQ_CLOSE: req->result = close (req->int1); break;
1164 case REQ_UNLINK: req->result = unlink (req->ptr1); break; 1263 case REQ_UNLINK: req->result = unlink (req->ptr1); break;
1165 case REQ_RMDIR: req->result = rmdir (req->ptr1); break; 1264 case REQ_RMDIR: req->result = rmdir (req->ptr1); break;
1168 case REQ_LINK: req->result = link (req->ptr2, req->ptr1); break; 1267 case REQ_LINK: req->result = link (req->ptr2, req->ptr1); break;
1169 case REQ_SYMLINK: req->result = symlink (req->ptr2, req->ptr1); break; 1268 case REQ_SYMLINK: req->result = symlink (req->ptr2, req->ptr1); break;
1170 case REQ_MKNOD: req->result = mknod (req->ptr2, req->mode, (dev_t)req->offs); break; 1269 case REQ_MKNOD: req->result = mknod (req->ptr2, req->mode, (dev_t)req->offs); break;
1171 case REQ_READLINK: req->result = readlink (req->ptr2, req->ptr1, NAME_MAX); break; 1270 case REQ_READLINK: req->result = readlink (req->ptr2, req->ptr1, NAME_MAX); break;
1172 1271
1272 case REQ_SYNC: req->result = 0; sync (); break;
1273 case REQ_FSYNC: req->result = fsync (req->int1); break;
1173 case REQ_FDATASYNC: req->result = fdatasync (req->int1); break; 1274 case REQ_FDATASYNC: req->result = fdatasync (req->int1); break;
1174 case REQ_FSYNC: req->result = fsync (req->int1); break; 1275
1175 case REQ_READDIR: scandir_ (req, self); break; 1276 case REQ_READDIR: scandir_ (req, self); break;
1176 1277
1177 case REQ_BUSY: 1278 case REQ_BUSY:
1279#ifdef _WIN32
1280 Sleep (req->nv1 * 1000.);
1281#else
1178 { 1282 {
1179 struct timeval tv; 1283 struct timeval tv;
1180 1284
1181 tv.tv_sec = req->nv1; 1285 tv.tv_sec = req->nv1;
1182 tv.tv_usec = (req->nv1 - tv.tv_usec) * 1000000.; 1286 tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.;
1183 1287
1184 req->result = select (0, 0, 0, 0, &tv); 1288 req->result = select (0, 0, 0, 0, &tv);
1185 } 1289 }
1290#endif
1291 break;
1186 1292
1187 case REQ_UTIME: 1293 case REQ_UTIME:
1188 case REQ_FUTIME: 1294 case REQ_FUTIME:
1189 { 1295 {
1190 struct timeval tv[2]; 1296 struct timeval tv[2];
1220 break; 1326 break;
1221 } 1327 }
1222 1328
1223 req->errorno = errno; 1329 req->errorno = errno;
1224 1330
1225 LOCK (reslock); 1331 X_LOCK (reslock);
1226 1332
1227 ++npending; 1333 ++npending;
1228 1334
1229 if (!reqq_push (&res_queue, req)) 1335 if (!reqq_push (&res_queue, req))
1230 { 1336 {
1231 /* write a dummy byte to the pipe so fh becomes ready */ 1337 /* write a dummy byte to the pipe so fh becomes ready */
1232 write (respipe [1], &respipe, 1); 1338 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
1233 1339
1234 /* optionally signal the main thread asynchronously */ 1340 /* optionally signal the main thread asynchronously */
1235 if (main_sig) 1341 if (main_sig)
1236 pthread_kill (main_tid, main_sig); 1342 pthread_kill (main_tid, main_sig);
1237 } 1343 }
1238 1344
1239 self->req = 0; 1345 self->req = 0;
1240 worker_clear (self); 1346 worker_clear (self);
1241 1347
1242 UNLOCK (reslock); 1348 X_UNLOCK (reslock);
1243 } 1349 }
1244 1350
1245quit: 1351quit:
1246 LOCK (wrklock); 1352 X_LOCK (wrklock);
1247 worker_free (self); 1353 worker_free (self);
1248 UNLOCK (wrklock); 1354 X_UNLOCK (wrklock);
1249 1355
1250 return 0; 1356 return 0;
1357 }//D
1251} 1358}
1252 1359
1253/*****************************************************************************/ 1360/*****************************************************************************/
1254 1361
1255static void atfork_prepare (void) 1362static void atfork_prepare (void)
1256{ 1363{
1257 LOCK (wrklock); 1364 X_LOCK (wrklock);
1258 LOCK (reqlock); 1365 X_LOCK (reqlock);
1259 LOCK (reslock); 1366 X_LOCK (reslock);
1260#if !HAVE_PREADWRITE 1367#if !HAVE_PREADWRITE
1261 LOCK (preadwritelock); 1368 X_LOCK (preadwritelock);
1262#endif 1369#endif
1263#if !HAVE_READDIR_R 1370#if !HAVE_READDIR_R
1264 LOCK (readdirlock); 1371 X_LOCK (readdirlock);
1265#endif 1372#endif
1266} 1373}
1267 1374
1268static void atfork_parent (void) 1375static void atfork_parent (void)
1269{ 1376{
1270#if !HAVE_READDIR_R 1377#if !HAVE_READDIR_R
1271 UNLOCK (readdirlock); 1378 X_UNLOCK (readdirlock);
1272#endif 1379#endif
1273#if !HAVE_PREADWRITE 1380#if !HAVE_PREADWRITE
1274 UNLOCK (preadwritelock); 1381 X_UNLOCK (preadwritelock);
1275#endif 1382#endif
1276 UNLOCK (reslock); 1383 X_UNLOCK (reslock);
1277 UNLOCK (reqlock); 1384 X_UNLOCK (reqlock);
1278 UNLOCK (wrklock); 1385 X_UNLOCK (wrklock);
1279} 1386}
1280 1387
1281static void atfork_child (void) 1388static void atfork_child (void)
1282{ 1389{
1283 aio_req prv; 1390 aio_req prv;
1303 idle = 0; 1410 idle = 0;
1304 nreqs = 0; 1411 nreqs = 0;
1305 nready = 0; 1412 nready = 0;
1306 npending = 0; 1413 npending = 0;
1307 1414
1308 close (respipe [0]);
1309 close (respipe [1]);
1310 create_pipe (); 1415 create_respipe ();
1311 1416
1312 atfork_parent (); 1417 atfork_parent ();
1313} 1418}
1314 1419
1315#define dREQ \ 1420#define dREQ \
1337 1442
1338PROTOTYPES: ENABLE 1443PROTOTYPES: ENABLE
1339 1444
1340BOOT: 1445BOOT:
1341{ 1446{
1342 HV *stash = gv_stashpv ("IO::AIO", 1); 1447 stash = gv_stashpv ("IO::AIO", 1);
1343 1448
1344 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); 1449 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
1345 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); 1450 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
1346 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); 1451 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
1347 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); 1452 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT));
1348 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); 1453 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC));
1454#ifdef _WIN32
1455 X_MUTEX_CHECK (wrklock);
1456 X_MUTEX_CHECK (reslock);
1457 X_MUTEX_CHECK (reqlock);
1458 X_MUTEX_CHECK (reqwait);
1459 X_MUTEX_CHECK (preadwritelock);
1460 X_MUTEX_CHECK (readdirlock);
1461
1462 X_COND_CHECK (reqwait);
1463#else
1349 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); 1464 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO));
1350 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); 1465 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO));
1466#endif
1351 1467
1352 create_pipe (); 1468 create_respipe ();
1469
1353 ATFORK (atfork_prepare, atfork_parent, atfork_child); 1470 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child);
1354} 1471}
1355 1472
1356void 1473void
1357max_poll_reqs (int nreqs) 1474max_poll_reqs (int nreqs)
1358 PROTOTYPE: $ 1475 PROTOTYPE: $
1403 1520
1404 REQ_SEND; 1521 REQ_SEND;
1405} 1522}
1406 1523
1407void 1524void
1408aio_close (SV *fh, SV *callback=&PL_sv_undef) 1525aio_fsync (SV *fh, SV *callback=&PL_sv_undef)
1409 PROTOTYPE: $;$ 1526 PROTOTYPE: $;$
1410 ALIAS: 1527 ALIAS:
1411 aio_close = REQ_CLOSE
1412 aio_fsync = REQ_FSYNC 1528 aio_fsync = REQ_FSYNC
1413 aio_fdatasync = REQ_FDATASYNC 1529 aio_fdatasync = REQ_FDATASYNC
1414 PPCODE: 1530 PPCODE:
1415{ 1531{
1416 dREQ; 1532 dREQ;
1417 1533
1418 req->type = ix; 1534 req->type = ix;
1419 req->sv1 = newSVsv (fh); 1535 req->sv1 = newSVsv (fh);
1420 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); 1536 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
1537
1538 REQ_SEND (req);
1539}
1540
1541int
1542_dup (int fd)
1543 PROTOTYPE: $
1544 CODE:
1545 RETVAL = dup (fd);
1546 OUTPUT:
1547 RETVAL
1548
1549void
1550_aio_close (int fd, SV *callback=&PL_sv_undef)
1551 PROTOTYPE: $;$
1552 PPCODE:
1553{
1554 dREQ;
1555
1556 req->type = REQ_CLOSE;
1557 req->int1 = fd;
1421 1558
1422 REQ_SEND (req); 1559 REQ_SEND (req);
1423} 1560}
1424 1561
1425void 1562void
1591 1728
1592 REQ_SEND; 1729 REQ_SEND;
1593} 1730}
1594 1731
1595void 1732void
1733aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef)
1734 PPCODE:
1735{
1736 dREQ;
1737
1738 req->sv1 = newSVsv (fh_or_path);
1739 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
1740
1741 if (SvPOK (fh_or_path))
1742 {
1743 req->type = REQ_TRUNCATE;
1744 req->ptr1 = SvPVbyte_nolen (req->sv1);
1745 }
1746 else
1747 {
1748 req->type = REQ_FTRUNCATE;
1749 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1750 }
1751
1752 REQ_SEND;
1753}
1754
1755void
1596aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef) 1756aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef)
1597 PPCODE: 1757 PPCODE:
1598{ 1758{
1599 dREQ; 1759 dREQ;
1600 1760
1729 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 1889 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1730} 1890}
1731 1891
1732void 1892void
1733aio_nop (SV *callback=&PL_sv_undef) 1893aio_nop (SV *callback=&PL_sv_undef)
1894 ALIAS:
1895 aio_nop = REQ_NOP
1896 aio_sync = REQ_SYNC
1734 PPCODE: 1897 PPCODE:
1735{ 1898{
1736 dREQ; 1899 dREQ;
1737 1900
1738 req->type = REQ_NOP; 1901 req->type = ix;
1739 1902
1740 REQ_SEND; 1903 REQ_SEND;
1741} 1904}
1742 1905
1743int 1906int
1809 CODE: 1972 CODE:
1810{ 1973{
1811 if (block_sig_level) 1974 if (block_sig_level)
1812 croak ("cannot call IO::AIO::setsig from within aio_block/callback"); 1975 croak ("cannot call IO::AIO::setsig from within aio_block/callback");
1813 1976
1814 LOCK (reslock); 1977 X_LOCK (reslock);
1815 main_tid = pthread_self (); 1978 main_tid = pthread_self ();
1816 main_sig = signum; 1979 main_sig = signum;
1817 UNLOCK (reslock); 1980 X_UNLOCK (reslock);
1818 1981
1819 if (main_sig && npending) 1982 if (main_sig && npending)
1820 pthread_kill (main_tid, main_sig); 1983 pthread_kill (main_tid, main_sig);
1821} 1984}
1822 1985
1829 1992
1830 block_sig (); 1993 block_sig ();
1831 PUSHMARK (SP); 1994 PUSHMARK (SP);
1832 PUTBACK; 1995 PUTBACK;
1833 count = call_sv (cb, GIMME_V | G_NOARGS | G_EVAL); 1996 count = call_sv (cb, GIMME_V | G_NOARGS | G_EVAL);
1834 SPAGAIN;
1835 unblock_sig (); 1997 unblock_sig ();
1836 1998
1837 if (SvTRUE (ERRSV)) 1999 if (SvTRUE (ERRSV))
1838 croak (0); 2000 croak (0);
1839 2001
1866 2028
1867int 2029int
1868nthreads() 2030nthreads()
1869 PROTOTYPE: 2031 PROTOTYPE:
1870 CODE: 2032 CODE:
1871 if (WORDACCESS_UNSAFE) LOCK (wrklock); 2033 if (WORDACCESS_UNSAFE) X_LOCK (wrklock);
1872 RETVAL = started; 2034 RETVAL = started;
1873 if (WORDACCESS_UNSAFE) UNLOCK (wrklock); 2035 if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock);
1874 OUTPUT: 2036 OUTPUT:
1875 RETVAL 2037 RETVAL
1876 2038
1877PROTOTYPES: DISABLE 2039PROTOTYPES: DISABLE
1878 2040

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines