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.109 by root, Sat Oct 6 14:05:19 2007 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_FSYNC, REQ_FDATASYNC,
78 REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME, 125 REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME,
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 ()
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 ()
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 ()
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
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 ()
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 ()
613{ 721{
614 if (get_nthreads () >= wanted) 722 if (get_nthreads () >= wanted)
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)
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 ()
706{ 814{
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;
1173 case REQ_FDATASYNC: req->result = fdatasync (req->int1); break; 1272 case REQ_FDATASYNC: req->result = fdatasync (req->int1); break;
1174 case REQ_FSYNC: req->result = fsync (req->int1); break; 1273 case REQ_FSYNC: req->result = fsync (req->int1); break;
1175 case REQ_READDIR: scandir_ (req, self); break; 1274 case REQ_READDIR: scandir_ (req, self); break;
1176 1275
1177 case REQ_BUSY: 1276 case REQ_BUSY:
1277#ifdef _WIN32
1278 Sleep (req->nv1 * 1000.);
1279#else
1178 { 1280 {
1179 struct timeval tv; 1281 struct timeval tv;
1180 1282
1181 tv.tv_sec = req->nv1; 1283 tv.tv_sec = req->nv1;
1182 tv.tv_usec = (req->nv1 - tv.tv_usec) * 1000000.; 1284 tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.;
1183 1285
1184 req->result = select (0, 0, 0, 0, &tv); 1286 req->result = select (0, 0, 0, 0, &tv);
1185 } 1287 }
1288#endif
1289 break;
1186 1290
1187 case REQ_UTIME: 1291 case REQ_UTIME:
1188 case REQ_FUTIME: 1292 case REQ_FUTIME:
1189 { 1293 {
1190 struct timeval tv[2]; 1294 struct timeval tv[2];
1220 break; 1324 break;
1221 } 1325 }
1222 1326
1223 req->errorno = errno; 1327 req->errorno = errno;
1224 1328
1225 LOCK (reslock); 1329 X_LOCK (reslock);
1226 1330
1227 ++npending; 1331 ++npending;
1228 1332
1229 if (!reqq_push (&res_queue, req)) 1333 if (!reqq_push (&res_queue, req))
1230 { 1334 {
1231 /* write a dummy byte to the pipe so fh becomes ready */ 1335 /* write a dummy byte to the pipe so fh becomes ready */
1232 write (respipe [1], &respipe, 1); 1336 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
1233 1337
1234 /* optionally signal the main thread asynchronously */ 1338 /* optionally signal the main thread asynchronously */
1235 if (main_sig) 1339 if (main_sig)
1236 pthread_kill (main_tid, main_sig); 1340 pthread_kill (main_tid, main_sig);
1237 } 1341 }
1238 1342
1239 self->req = 0; 1343 self->req = 0;
1240 worker_clear (self); 1344 worker_clear (self);
1241 1345
1242 UNLOCK (reslock); 1346 X_UNLOCK (reslock);
1243 } 1347 }
1244 1348
1245quit: 1349quit:
1246 LOCK (wrklock); 1350 X_LOCK (wrklock);
1247 worker_free (self); 1351 worker_free (self);
1248 UNLOCK (wrklock); 1352 X_UNLOCK (wrklock);
1249 1353
1250 return 0; 1354 return 0;
1355 }//D
1251} 1356}
1252 1357
1253/*****************************************************************************/ 1358/*****************************************************************************/
1254 1359
1255static void atfork_prepare (void) 1360static void atfork_prepare (void)
1256{ 1361{
1257 LOCK (wrklock); 1362 X_LOCK (wrklock);
1258 LOCK (reqlock); 1363 X_LOCK (reqlock);
1259 LOCK (reslock); 1364 X_LOCK (reslock);
1260#if !HAVE_PREADWRITE 1365#if !HAVE_PREADWRITE
1261 LOCK (preadwritelock); 1366 X_LOCK (preadwritelock);
1262#endif 1367#endif
1263#if !HAVE_READDIR_R 1368#if !HAVE_READDIR_R
1264 LOCK (readdirlock); 1369 X_LOCK (readdirlock);
1265#endif 1370#endif
1266} 1371}
1267 1372
1268static void atfork_parent (void) 1373static void atfork_parent (void)
1269{ 1374{
1270#if !HAVE_READDIR_R 1375#if !HAVE_READDIR_R
1271 UNLOCK (readdirlock); 1376 X_UNLOCK (readdirlock);
1272#endif 1377#endif
1273#if !HAVE_PREADWRITE 1378#if !HAVE_PREADWRITE
1274 UNLOCK (preadwritelock); 1379 X_UNLOCK (preadwritelock);
1275#endif 1380#endif
1276 UNLOCK (reslock); 1381 X_UNLOCK (reslock);
1277 UNLOCK (reqlock); 1382 X_UNLOCK (reqlock);
1278 UNLOCK (wrklock); 1383 X_UNLOCK (wrklock);
1279} 1384}
1280 1385
1281static void atfork_child (void) 1386static void atfork_child (void)
1282{ 1387{
1283 aio_req prv; 1388 aio_req prv;
1303 idle = 0; 1408 idle = 0;
1304 nreqs = 0; 1409 nreqs = 0;
1305 nready = 0; 1410 nready = 0;
1306 npending = 0; 1411 npending = 0;
1307 1412
1308 close (respipe [0]);
1309 close (respipe [1]);
1310 create_pipe (); 1413 create_respipe ();
1311 1414
1312 atfork_parent (); 1415 atfork_parent ();
1313} 1416}
1314 1417
1315#define dREQ \ 1418#define dREQ \
1337 1440
1338PROTOTYPES: ENABLE 1441PROTOTYPES: ENABLE
1339 1442
1340BOOT: 1443BOOT:
1341{ 1444{
1342 HV *stash = gv_stashpv ("IO::AIO", 1); 1445 stash = gv_stashpv ("IO::AIO", 1);
1343 1446
1344 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); 1447 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
1345 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); 1448 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
1346 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); 1449 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
1347 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); 1450 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT));
1348 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); 1451 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC));
1452#ifdef _WIN32
1453 X_MUTEX_CHECK (wrklock);
1454 X_MUTEX_CHECK (reslock);
1455 X_MUTEX_CHECK (reqlock);
1456 X_MUTEX_CHECK (reqwait);
1457 X_MUTEX_CHECK (preadwritelock);
1458 X_MUTEX_CHECK (readdirlock);
1459
1460 X_COND_CHECK (reqwait);
1461#else
1349 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); 1462 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO));
1350 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); 1463 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO));
1464#endif
1351 1465
1352 create_pipe (); 1466 create_respipe ();
1467
1353 ATFORK (atfork_prepare, atfork_parent, atfork_child); 1468 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child);
1354} 1469}
1355 1470
1356void 1471void
1357max_poll_reqs (int nreqs) 1472max_poll_reqs (int nreqs)
1358 PROTOTYPE: $ 1473 PROTOTYPE: $
1403 1518
1404 REQ_SEND; 1519 REQ_SEND;
1405} 1520}
1406 1521
1407void 1522void
1408aio_close (SV *fh, SV *callback=&PL_sv_undef) 1523aio_fsync (SV *fh, SV *callback=&PL_sv_undef)
1409 PROTOTYPE: $;$ 1524 PROTOTYPE: $;$
1410 ALIAS: 1525 ALIAS:
1411 aio_close = REQ_CLOSE
1412 aio_fsync = REQ_FSYNC 1526 aio_fsync = REQ_FSYNC
1413 aio_fdatasync = REQ_FDATASYNC 1527 aio_fdatasync = REQ_FDATASYNC
1414 PPCODE: 1528 PPCODE:
1415{ 1529{
1416 dREQ; 1530 dREQ;
1417 1531
1418 req->type = ix; 1532 req->type = ix;
1419 req->sv1 = newSVsv (fh); 1533 req->sv1 = newSVsv (fh);
1420 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); 1534 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
1535
1536 REQ_SEND (req);
1537}
1538
1539int
1540_dup (int fd)
1541 PROTOTYPE: $
1542 CODE:
1543 RETVAL = dup (fd);
1544 OUTPUT:
1545 RETVAL
1546
1547void
1548_aio_close (int fd, SV *callback=&PL_sv_undef)
1549 PROTOTYPE: $;$
1550 PPCODE:
1551{
1552 dREQ;
1553
1554 req->type = REQ_CLOSE;
1555 req->int1 = fd;
1421 1556
1422 REQ_SEND (req); 1557 REQ_SEND (req);
1423} 1558}
1424 1559
1425void 1560void
1584 req->ptr1 = SvPVbyte_nolen (req->sv1); 1719 req->ptr1 = SvPVbyte_nolen (req->sv1);
1585 } 1720 }
1586 else 1721 else
1587 { 1722 {
1588 req->type = REQ_FUTIME; 1723 req->type = REQ_FUTIME;
1724 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1725 }
1726
1727 REQ_SEND;
1728}
1729
1730void
1731aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef)
1732 PPCODE:
1733{
1734 dREQ;
1735
1736 req->sv1 = newSVsv (fh_or_path);
1737 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
1738
1739 if (SvPOK (fh_or_path))
1740 {
1741 req->type = REQ_TRUNCATE;
1742 req->ptr1 = SvPVbyte_nolen (req->sv1);
1743 }
1744 else
1745 {
1746 req->type = REQ_FTRUNCATE;
1589 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 1747 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1590 } 1748 }
1591 1749
1592 REQ_SEND; 1750 REQ_SEND;
1593} 1751}
1809 CODE: 1967 CODE:
1810{ 1968{
1811 if (block_sig_level) 1969 if (block_sig_level)
1812 croak ("cannot call IO::AIO::setsig from within aio_block/callback"); 1970 croak ("cannot call IO::AIO::setsig from within aio_block/callback");
1813 1971
1814 LOCK (reslock); 1972 X_LOCK (reslock);
1815 main_tid = pthread_self (); 1973 main_tid = pthread_self ();
1816 main_sig = signum; 1974 main_sig = signum;
1817 UNLOCK (reslock); 1975 X_UNLOCK (reslock);
1818 1976
1819 if (main_sig && npending) 1977 if (main_sig && npending)
1820 pthread_kill (main_tid, main_sig); 1978 pthread_kill (main_tid, main_sig);
1821} 1979}
1822 1980
1866 2024
1867int 2025int
1868nthreads() 2026nthreads()
1869 PROTOTYPE: 2027 PROTOTYPE:
1870 CODE: 2028 CODE:
1871 if (WORDACCESS_UNSAFE) LOCK (wrklock); 2029 if (WORDACCESS_UNSAFE) X_LOCK (wrklock);
1872 RETVAL = started; 2030 RETVAL = started;
1873 if (WORDACCESS_UNSAFE) UNLOCK (wrklock); 2031 if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock);
1874 OUTPUT: 2032 OUTPUT:
1875 RETVAL 2033 RETVAL
1876 2034
1877PROTOTYPES: DISABLE 2035PROTOTYPES: DISABLE
1878 2036

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines