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.101 by root, Fri Jun 1 13:25:51 2007 UTC vs.
Revision 1.108 by root, Thu Oct 4 12:50:35 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 XPUSHs (fh);
471 } 533 }
472 break; 534 break;
473 535
474 case REQ_GROUP: 536 case REQ_GROUP:
582 req->flags |= FLAG_CANCELLED; 644 req->flags |= FLAG_CANCELLED;
583 645
584 req_cancel_subs (req); 646 req_cancel_subs (req);
585} 647}
586 648
587static void *aio_proc (void *arg); 649#ifdef USE_SOCKETS_AS_HANDLES
650# define TO_SOCKET(x) (win32_get_osfhandle (x))
651#else
652# define TO_SOCKET(x) (x)
653#endif
654
655static void
656create_respipe ()
657{
658 int old_readfd = respipe [0];
659
660 if (respipe [1] >= 0)
661 respipe_close (TO_SOCKET (respipe [1]));
662
663#ifdef _WIN32
664 if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe))
665#else
666 if (pipe (respipe))
667#endif
668 croak ("unable to initialize result pipe");
669
670 if (old_readfd >= 0)
671 {
672 if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0)
673 croak ("unable to initialize result pipe(2)");
674
675 respipe_close (respipe [0]);
676 respipe [0] = old_readfd;
677 }
678
679#ifdef _WIN32
680 int arg = 1;
681 if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg)
682 || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg))
683#else
684 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)
685 || fcntl (respipe [1], F_SETFL, O_NONBLOCK))
686#endif
687 croak ("unable to initialize result pipe(3)");
688
689 respipe_osf [0] = TO_SOCKET (respipe [0]);
690 respipe_osf [1] = TO_SOCKET (respipe [1]);
691}
692
693X_THREAD_PROC (aio_proc);
588 694
589static void start_thread (void) 695static void start_thread (void)
590{ 696{
591 worker *wrk = calloc (1, sizeof (worker)); 697 worker *wrk = calloc (1, sizeof (worker));
592 698
593 if (!wrk) 699 if (!wrk)
594 croak ("unable to allocate worker thread data"); 700 croak ("unable to allocate worker thread data");
595 701
596 LOCK (wrklock); 702 X_LOCK (wrklock);
597 703
598 if (thread_create (&wrk->tid, aio_proc, (void *)wrk)) 704 if (thread_create (&wrk->tid, aio_proc, (void *)wrk))
599 { 705 {
600 wrk->prev = &wrk_first; 706 wrk->prev = &wrk_first;
601 wrk->next = wrk_first.next; 707 wrk->next = wrk_first.next;
604 ++started; 710 ++started;
605 } 711 }
606 else 712 else
607 free (wrk); 713 free (wrk);
608 714
609 UNLOCK (wrklock); 715 X_UNLOCK (wrklock);
610} 716}
611 717
612static void maybe_start_thread () 718static void maybe_start_thread ()
613{ 719{
614 if (get_nthreads () >= wanted) 720 if (get_nthreads () >= wanted)
625{ 731{
626 block_sig (); 732 block_sig ();
627 733
628 ++nreqs; 734 ++nreqs;
629 735
630 LOCK (reqlock); 736 X_LOCK (reqlock);
631 ++nready; 737 ++nready;
632 reqq_push (&req_queue, req); 738 reqq_push (&req_queue, req);
633 COND_SIGNAL (reqwait); 739 X_COND_SIGNAL (reqwait);
634 UNLOCK (reqlock); 740 X_UNLOCK (reqlock);
635 741
636 unblock_sig (); 742 unblock_sig ();
637 743
638 maybe_start_thread (); 744 maybe_start_thread ();
639} 745}
645 Newz (0, req, 1, aio_cb); 751 Newz (0, req, 1, aio_cb);
646 752
647 req->type = REQ_QUIT; 753 req->type = REQ_QUIT;
648 req->pri = PRI_MAX + PRI_BIAS; 754 req->pri = PRI_MAX + PRI_BIAS;
649 755
650 LOCK (reqlock); 756 X_LOCK (reqlock);
651 reqq_push (&req_queue, req); 757 reqq_push (&req_queue, req);
652 COND_SIGNAL (reqwait); 758 X_COND_SIGNAL (reqwait);
653 UNLOCK (reqlock); 759 X_UNLOCK (reqlock);
654 760
655 LOCK (wrklock); 761 X_LOCK (wrklock);
656 --started; 762 --started;
657 UNLOCK (wrklock); 763 X_UNLOCK (wrklock);
658} 764}
659 765
660static void set_max_idle (int nthreads) 766static void set_max_idle (int nthreads)
661{ 767{
662 if (WORDACCESS_UNSAFE) LOCK (reqlock); 768 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
663 max_idle = nthreads <= 0 ? 1 : nthreads; 769 max_idle = nthreads <= 0 ? 1 : nthreads;
664 if (WORDACCESS_UNSAFE) UNLOCK (reqlock); 770 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
665} 771}
666 772
667static void min_parallel (int nthreads) 773static void min_parallel (int nthreads)
668{ 774{
669 if (wanted < nthreads) 775 if (wanted < nthreads)
684 fd_set rfd; 790 fd_set rfd;
685 791
686 while (nreqs) 792 while (nreqs)
687 { 793 {
688 int size; 794 int size;
689 if (WORDACCESS_UNSAFE) LOCK (reslock); 795 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
690 size = res_queue.size; 796 size = res_queue.size;
691 if (WORDACCESS_UNSAFE) UNLOCK (reslock); 797 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
692 798
693 if (size) 799 if (size)
694 return; 800 return;
695 801
696 maybe_start_thread (); 802 maybe_start_thread ();
697 803
698 FD_ZERO(&rfd); 804 FD_ZERO (&rfd);
699 FD_SET(respipe [0], &rfd); 805 FD_SET (respipe [0], &rfd);
700 806
701 select (respipe [0] + 1, &rfd, 0, 0, 0); 807 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
702 } 808 }
703} 809}
704 810
705static int poll_cb () 811static int poll_cb ()
706{ 812{
720 { 826 {
721 for (;;) 827 for (;;)
722 { 828 {
723 maybe_start_thread (); 829 maybe_start_thread ();
724 830
725 LOCK (reslock); 831 X_LOCK (reslock);
726 req = reqq_shift (&res_queue); 832 req = reqq_shift (&res_queue);
727 833
728 if (req) 834 if (req)
729 { 835 {
730 --npending; 836 --npending;
731 837
732 if (!res_queue.size) 838 if (!res_queue.size)
733 { 839 {
734 /* read any signals sent by the worker threads */ 840 /* read any signals sent by the worker threads */
735 char buf [4]; 841 char buf [4];
736 while (read (respipe [0], buf, 4) == 4) 842 while (respipe_read (respipe [0], buf, 4) == 4)
737 ; 843 ;
738 } 844 }
739 } 845 }
740 846
741 UNLOCK (reslock); 847 X_UNLOCK (reslock);
742 848
743 if (!req) 849 if (!req)
744 break; 850 break;
745 851
746 --nreqs; 852 --nreqs;
786 892
787 unblock_sig (); 893 unblock_sig ();
788 return count; 894 return count;
789} 895}
790 896
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/*****************************************************************************/ 897/*****************************************************************************/
804/* work around various missing functions */ 898/* work around various missing functions */
805 899
806#if !HAVE_PREADWRITE 900#if !HAVE_PREADWRITE
807# define pread aio_pread 901# define pread aio_pread
810/* 904/*
811 * make our pread/pwrite safe against themselves, but not against 905 * make our pread/pwrite safe against themselves, but not against
812 * normal read/write by using a mutex. slows down execution a lot, 906 * normal read/write by using a mutex. slows down execution a lot,
813 * but that's your problem, not mine. 907 * but that's your problem, not mine.
814 */ 908 */
815static mutex_t preadwritelock = MUTEX_INIT; 909static mutex_t preadwritelock = X_MUTEX_INIT;
816 910
817static ssize_t pread (int fd, void *buf, size_t count, off_t offset) 911static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
818{ 912{
819 ssize_t res; 913 ssize_t res;
820 off_t ooffset; 914 off_t ooffset;
821 915
822 LOCK (preadwritelock); 916 X_LOCK (preadwritelock);
823 ooffset = lseek (fd, 0, SEEK_CUR); 917 ooffset = lseek (fd, 0, SEEK_CUR);
824 lseek (fd, offset, SEEK_SET); 918 lseek (fd, offset, SEEK_SET);
825 res = read (fd, buf, count); 919 res = read (fd, buf, count);
826 lseek (fd, ooffset, SEEK_SET); 920 lseek (fd, ooffset, SEEK_SET);
827 UNLOCK (preadwritelock); 921 X_UNLOCK (preadwritelock);
828 922
829 return res; 923 return res;
830} 924}
831 925
832static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) 926static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset)
833{ 927{
834 ssize_t res; 928 ssize_t res;
835 off_t ooffset; 929 off_t ooffset;
836 930
837 LOCK (preadwritelock); 931 X_LOCK (preadwritelock);
838 ooffset = lseek (fd, 0, SEEK_CUR); 932 ooffset = lseek (fd, 0, SEEK_CUR);
839 lseek (fd, offset, SEEK_SET); 933 lseek (fd, offset, SEEK_SET);
840 res = write (fd, buf, count); 934 res = write (fd, buf, count);
841 lseek (fd, offset, SEEK_SET); 935 lseek (fd, offset, SEEK_SET);
842 UNLOCK (preadwritelock); 936 X_UNLOCK (preadwritelock);
843 937
844 return res; 938 return res;
845} 939}
846#endif 940#endif
847 941
880#if !HAVE_READAHEAD 974#if !HAVE_READAHEAD
881# define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) 975# define readahead(fd,offset,count) aio_readahead (fd, offset, count, self)
882 976
883static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) 977static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self)
884{ 978{
979 size_t todo = count;
885 dBUF; 980 dBUF;
886 981
887 while (count > 0) 982 while (todo > 0)
888 { 983 {
889 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; 984 size_t len = todo < AIO_BUFSIZE ? todo : AIO_BUFSIZE;
890 985
891 pread (fd, aio_buf, len, offset); 986 pread (fd, aio_buf, len, offset);
892 offset += len; 987 offset += len;
893 count -= len; 988 todo -= len;
894 } 989 }
895 990
896 errno = 0; 991 errno = 0;
992 return count;
897} 993}
898 994
899#endif 995#endif
900 996
901#if !HAVE_READDIR_R 997#if !HAVE_READDIR_R
902# define readdir_r aio_readdir_r 998# define readdir_r aio_readdir_r
903 999
904static mutex_t readdirlock = MUTEX_INIT; 1000static mutex_t readdirlock = X_MUTEX_INIT;
905 1001
906static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) 1002static int readdir_r (DIR *dirp, X_DIRENT *ent, X_DIRENT **res)
907{ 1003{
908 struct dirent *e; 1004 X_DIRENT *e;
909 int errorno; 1005 int errorno;
910 1006
911 LOCK (readdirlock); 1007 X_LOCK (readdirlock);
912 1008
913 e = readdir (dirp); 1009 e = readdir (dirp);
914 errorno = errno; 1010 errorno = errno;
915 1011
916 if (e) 1012 if (e)
919 strcpy (ent->d_name, e->d_name); 1015 strcpy (ent->d_name, e->d_name);
920 } 1016 }
921 else 1017 else
922 *res = 0; 1018 *res = 0;
923 1019
924 UNLOCK (readdirlock); 1020 X_UNLOCK (readdirlock);
925 1021
926 errno = errorno; 1022 errno = errorno;
927 return e ? 0 : -1; 1023 return e ? 0 : -1;
928} 1024}
929#endif 1025#endif
1025static void scandir_ (aio_req req, worker *self) 1121static void scandir_ (aio_req req, worker *self)
1026{ 1122{
1027 DIR *dirp; 1123 DIR *dirp;
1028 union 1124 union
1029 { 1125 {
1030 struct dirent d; 1126 X_DIRENT d;
1031 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 1127 char b [offsetof (X_DIRENT, d_name) + NAME_MAX + 1];
1032 } *u; 1128 } *u;
1033 struct dirent *entp; 1129 X_DIRENT *entp;
1034 char *name, *names; 1130 char *name, *names;
1035 int memlen = 4096; 1131 int memlen = 4096;
1036 int memofs = 0; 1132 int memofs = 0;
1037 int res = 0; 1133 int res = 0;
1038 int errorno;
1039 1134
1040 LOCK (wrklock); 1135 X_LOCK (wrklock);
1041 self->dirp = dirp = opendir (req->ptr1); 1136 self->dirp = dirp = opendir (req->ptr1);
1042 self->dbuf = u = malloc (sizeof (*u)); 1137 self->dbuf = u = malloc (sizeof (*u));
1043 req->flags |= FLAG_PTR2_FREE; 1138 req->flags |= FLAG_PTR2_FREE;
1044 req->ptr2 = names = malloc (memlen); 1139 req->ptr2 = names = malloc (memlen);
1045 UNLOCK (wrklock); 1140 X_UNLOCK (wrklock);
1046 1141
1047 if (dirp && u && names) 1142 if (dirp && u && names)
1048 for (;;) 1143 for (;;)
1049 { 1144 {
1050 errno = 0; 1145 errno = 0;
1062 res++; 1157 res++;
1063 1158
1064 while (memofs + len > memlen) 1159 while (memofs + len > memlen)
1065 { 1160 {
1066 memlen *= 2; 1161 memlen *= 2;
1067 LOCK (wrklock); 1162 X_LOCK (wrklock);
1068 req->ptr2 = names = realloc (names, memlen); 1163 req->ptr2 = names = realloc (names, memlen);
1069 UNLOCK (wrklock); 1164 X_UNLOCK (wrklock);
1070 1165
1071 if (!names) 1166 if (!names)
1072 break; 1167 break;
1073 } 1168 }
1074 1169
1083 req->result = res; 1178 req->result = res;
1084} 1179}
1085 1180
1086/*****************************************************************************/ 1181/*****************************************************************************/
1087 1182
1088static void *aio_proc (void *thr_arg) 1183X_THREAD_PROC (aio_proc)
1089{ 1184{
1185 {//D
1090 aio_req req; 1186 aio_req req;
1091 struct timespec ts; 1187 struct timespec ts;
1092 worker *self = (worker *)thr_arg; 1188 worker *self = (worker *)thr_arg;
1093 1189
1094 /* try to distribute timeouts somewhat evenly */ 1190 /* try to distribute timeouts somewhat randomly */
1095 ts.tv_nsec = (((unsigned long)self + (unsigned long)ts.tv_sec) & 1023UL) 1191 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1096 * (1000000000UL / 1024UL);
1097 1192
1098 for (;;) 1193 for (;;)
1099 { 1194 {
1100 ts.tv_sec = time (0) + IDLE_TIMEOUT; 1195 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1101 1196
1102 LOCK (reqlock); 1197 X_LOCK (reqlock);
1103 1198
1104 for (;;) 1199 for (;;)
1105 { 1200 {
1106 self->req = req = reqq_shift (&req_queue); 1201 self->req = req = reqq_shift (&req_queue);
1107 1202
1108 if (req) 1203 if (req)
1109 break; 1204 break;
1110 1205
1111 ++idle; 1206 ++idle;
1112 1207
1113 if (COND_TIMEDWAIT (reqwait, reqlock, ts) 1208 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts)
1114 == ETIMEDOUT) 1209 == ETIMEDOUT)
1115 { 1210 {
1116 if (idle > max_idle) 1211 if (idle > max_idle)
1117 { 1212 {
1118 --idle; 1213 --idle;
1119 UNLOCK (reqlock); 1214 X_UNLOCK (reqlock);
1120 LOCK (wrklock); 1215 X_LOCK (wrklock);
1121 --started; 1216 --started;
1122 UNLOCK (wrklock); 1217 X_UNLOCK (wrklock);
1123 goto quit; 1218 goto quit;
1124 } 1219 }
1125 1220
1126 /* we are allowed to idle, so do so without any timeout */ 1221 /* we are allowed to idle, so do so without any timeout */
1127 COND_WAIT (reqwait, reqlock); 1222 X_COND_WAIT (reqwait, reqlock);
1128 ts.tv_sec = time (0) + IDLE_TIMEOUT; 1223 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1129 } 1224 }
1130 1225
1131 --idle; 1226 --idle;
1132 } 1227 }
1133 1228
1134 --nready; 1229 --nready;
1135 1230
1136 UNLOCK (reqlock); 1231 X_UNLOCK (reqlock);
1137 1232
1138 errno = 0; /* strictly unnecessary */ 1233 errno = 0; /* strictly unnecessary */
1139 1234
1140 if (!(req->flags & FLAG_CANCELLED)) 1235 if (!(req->flags & FLAG_CANCELLED))
1141 switch (req->type) 1236 switch (req->type)
1156 1251
1157 case REQ_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break; 1252 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; 1253 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; 1254 case REQ_CHMOD: req->result = chmod (req->ptr1, req->mode); break;
1160 case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break; 1255 case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break;
1256 case REQ_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break;
1257 case REQ_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break;
1161 1258
1162 case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break; 1259 case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break;
1163 case REQ_CLOSE: req->result = close (req->int1); break; 1260 case REQ_CLOSE: req->result = PerlIO_close ((PerlIO *)req->ptr1); break;
1164 case REQ_UNLINK: req->result = unlink (req->ptr1); break; 1261 case REQ_UNLINK: req->result = unlink (req->ptr1); break;
1165 case REQ_RMDIR: req->result = rmdir (req->ptr1); break; 1262 case REQ_RMDIR: req->result = rmdir (req->ptr1); break;
1166 case REQ_MKDIR: req->result = mkdir (req->ptr1, req->mode); break; 1263 case REQ_MKDIR: req->result = mkdir (req->ptr1, req->mode); break;
1167 case REQ_RENAME: req->result = rename (req->ptr2, req->ptr1); break; 1264 case REQ_RENAME: req->result = rename (req->ptr2, req->ptr1); break;
1168 case REQ_LINK: req->result = link (req->ptr2, req->ptr1); break; 1265 case REQ_LINK: req->result = link (req->ptr2, req->ptr1); break;
1173 case REQ_FDATASYNC: req->result = fdatasync (req->int1); break; 1270 case REQ_FDATASYNC: req->result = fdatasync (req->int1); break;
1174 case REQ_FSYNC: req->result = fsync (req->int1); break; 1271 case REQ_FSYNC: req->result = fsync (req->int1); break;
1175 case REQ_READDIR: scandir_ (req, self); break; 1272 case REQ_READDIR: scandir_ (req, self); break;
1176 1273
1177 case REQ_BUSY: 1274 case REQ_BUSY:
1275#ifdef _WIN32
1276 Sleep (req->nv1 * 1000.);
1277#else
1178 { 1278 {
1179 struct timeval tv; 1279 struct timeval tv;
1180 1280
1181 tv.tv_sec = req->nv1; 1281 tv.tv_sec = req->nv1;
1182 tv.tv_usec = (req->nv1 - tv.tv_usec) * 1000000.; 1282 tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.;
1183 1283
1184 req->result = select (0, 0, 0, 0, &tv); 1284 req->result = select (0, 0, 0, 0, &tv);
1185 } 1285 }
1286#endif
1287 break;
1186 1288
1187 case REQ_UTIME: 1289 case REQ_UTIME:
1188 case REQ_FUTIME: 1290 case REQ_FUTIME:
1189 { 1291 {
1190 struct timeval tv[2]; 1292 struct timeval tv[2];
1220 break; 1322 break;
1221 } 1323 }
1222 1324
1223 req->errorno = errno; 1325 req->errorno = errno;
1224 1326
1225 LOCK (reslock); 1327 X_LOCK (reslock);
1226 1328
1227 ++npending; 1329 ++npending;
1228 1330
1229 if (!reqq_push (&res_queue, req)) 1331 if (!reqq_push (&res_queue, req))
1230 { 1332 {
1231 /* write a dummy byte to the pipe so fh becomes ready */ 1333 /* write a dummy byte to the pipe so fh becomes ready */
1232 write (respipe [1], &respipe, 1); 1334 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
1233 1335
1234 /* optionally signal the main thread asynchronously */ 1336 /* optionally signal the main thread asynchronously */
1235 if (main_sig) 1337 if (main_sig)
1236 pthread_kill (main_tid, main_sig); 1338 pthread_kill (main_tid, main_sig);
1237 } 1339 }
1238 1340
1239 self->req = 0; 1341 self->req = 0;
1240 worker_clear (self); 1342 worker_clear (self);
1241 1343
1242 UNLOCK (reslock); 1344 X_UNLOCK (reslock);
1243 } 1345 }
1244 1346
1245quit: 1347quit:
1246 LOCK (wrklock); 1348 X_LOCK (wrklock);
1247 worker_free (self); 1349 worker_free (self);
1248 UNLOCK (wrklock); 1350 X_UNLOCK (wrklock);
1249 1351
1250 return 0; 1352 return 0;
1353 }//D
1251} 1354}
1252 1355
1253/*****************************************************************************/ 1356/*****************************************************************************/
1254 1357
1255static void atfork_prepare (void) 1358static void atfork_prepare (void)
1256{ 1359{
1257 LOCK (wrklock); 1360 X_LOCK (wrklock);
1258 LOCK (reqlock); 1361 X_LOCK (reqlock);
1259 LOCK (reslock); 1362 X_LOCK (reslock);
1260#if !HAVE_PREADWRITE 1363#if !HAVE_PREADWRITE
1261 LOCK (preadwritelock); 1364 X_LOCK (preadwritelock);
1262#endif 1365#endif
1263#if !HAVE_READDIR_R 1366#if !HAVE_READDIR_R
1264 LOCK (readdirlock); 1367 X_LOCK (readdirlock);
1265#endif 1368#endif
1266} 1369}
1267 1370
1268static void atfork_parent (void) 1371static void atfork_parent (void)
1269{ 1372{
1270#if !HAVE_READDIR_R 1373#if !HAVE_READDIR_R
1271 UNLOCK (readdirlock); 1374 X_UNLOCK (readdirlock);
1272#endif 1375#endif
1273#if !HAVE_PREADWRITE 1376#if !HAVE_PREADWRITE
1274 UNLOCK (preadwritelock); 1377 X_UNLOCK (preadwritelock);
1275#endif 1378#endif
1276 UNLOCK (reslock); 1379 X_UNLOCK (reslock);
1277 UNLOCK (reqlock); 1380 X_UNLOCK (reqlock);
1278 UNLOCK (wrklock); 1381 X_UNLOCK (wrklock);
1279} 1382}
1280 1383
1281static void atfork_child (void) 1384static void atfork_child (void)
1282{ 1385{
1283 aio_req prv; 1386 aio_req prv;
1303 idle = 0; 1406 idle = 0;
1304 nreqs = 0; 1407 nreqs = 0;
1305 nready = 0; 1408 nready = 0;
1306 npending = 0; 1409 npending = 0;
1307 1410
1308 close (respipe [0]);
1309 close (respipe [1]);
1310 create_pipe (); 1411 create_respipe ();
1311 1412
1312 atfork_parent (); 1413 atfork_parent ();
1313} 1414}
1314 1415
1315#define dREQ \ 1416#define dREQ \
1337 1438
1338PROTOTYPES: ENABLE 1439PROTOTYPES: ENABLE
1339 1440
1340BOOT: 1441BOOT:
1341{ 1442{
1342 HV *stash = gv_stashpv ("IO::AIO", 1); 1443 stash = gv_stashpv ("IO::AIO", 1);
1343 1444
1344 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); 1445 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
1345 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); 1446 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
1346 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); 1447 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
1347 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); 1448 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT));
1348 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); 1449 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC));
1450#ifdef _WIN32
1451 X_MUTEX_CHECK (wrklock);
1452 X_MUTEX_CHECK (reslock);
1453 X_MUTEX_CHECK (reqlock);
1454 X_MUTEX_CHECK (reqwait);
1455 X_MUTEX_CHECK (preadwritelock);
1456 X_MUTEX_CHECK (readdirlock);
1457
1458 X_COND_CHECK (reqwait);
1459#else
1349 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); 1460 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO));
1350 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); 1461 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO));
1462#endif
1351 1463
1352 create_pipe (); 1464 create_respipe ();
1465
1353 ATFORK (atfork_prepare, atfork_parent, atfork_child); 1466 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child);
1354} 1467}
1355 1468
1356void 1469void
1357max_poll_reqs (int nreqs) 1470max_poll_reqs (int nreqs)
1358 PROTOTYPE: $ 1471 PROTOTYPE: $
1403 1516
1404 REQ_SEND; 1517 REQ_SEND;
1405} 1518}
1406 1519
1407void 1520void
1408aio_close (SV *fh, SV *callback=&PL_sv_undef) 1521aio_fsync (SV *fh, SV *callback=&PL_sv_undef)
1409 PROTOTYPE: $;$ 1522 PROTOTYPE: $;$
1410 ALIAS: 1523 ALIAS:
1411 aio_close = REQ_CLOSE
1412 aio_fsync = REQ_FSYNC 1524 aio_fsync = REQ_FSYNC
1413 aio_fdatasync = REQ_FDATASYNC 1525 aio_fdatasync = REQ_FDATASYNC
1414 PPCODE: 1526 PPCODE:
1415{ 1527{
1416 dREQ; 1528 dREQ;
1421 1533
1422 REQ_SEND (req); 1534 REQ_SEND (req);
1423} 1535}
1424 1536
1425void 1537void
1538aio_close (SV *fh, SV *callback=&PL_sv_undef)
1539 PROTOTYPE: $;$
1540 PPCODE:
1541{
1542 PerlIO *io = IoIFP (sv_2io (fh));
1543 int fd = PerlIO_fileno (io);
1544
1545 if (fd < 0)
1546 croak ("aio_close called with fd-less filehandle");
1547
1548 PerlIO_binmode (aTHX_ io, 0, 0, 0);
1549
1550 {
1551 dREQ;
1552
1553 req->type = REQ_CLOSE;
1554 req->sv1 = newSVsv (fh);
1555 req->ptr1 = (void *)io;
1556
1557 REQ_SEND (req);
1558 }
1559}
1560
1561void
1426aio_read (SV *fh, SV *offset, UV length, SV8 *data, UV dataoffset, SV *callback=&PL_sv_undef) 1562aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef)
1427 ALIAS: 1563 ALIAS:
1428 aio_read = REQ_READ 1564 aio_read = REQ_READ
1429 aio_write = REQ_WRITE 1565 aio_write = REQ_WRITE
1430 PROTOTYPE: $$$$$;$ 1566 PROTOTYPE: $$$$$;$
1431 PPCODE: 1567 PPCODE:
1432{ 1568{
1433 STRLEN svlen; 1569 STRLEN svlen;
1434 char *svptr = SvPVbyte (data, svlen); 1570 char *svptr = SvPVbyte (data, svlen);
1571 UV len = SvUV (length);
1435 1572
1436 SvUPGRADE (data, SVt_PV); 1573 SvUPGRADE (data, SVt_PV);
1437 SvPOK_on (data); 1574 SvPOK_on (data);
1438 1575
1439 if (dataoffset < 0) 1576 if (dataoffset < 0)
1440 dataoffset += svlen; 1577 dataoffset += svlen;
1441 1578
1442 if (dataoffset < 0 || dataoffset > svlen) 1579 if (dataoffset < 0 || dataoffset > svlen)
1443 croak ("data offset outside of string"); 1580 croak ("dataoffset outside of data scalar");
1444 1581
1445 if (ix == REQ_WRITE) 1582 if (ix == REQ_WRITE)
1446 { 1583 {
1447 /* write: check length and adjust. */ 1584 /* write: check length and adjust. */
1448 if (length < 0 || length + dataoffset > svlen) 1585 if (!SvOK (length) || len + dataoffset > svlen)
1449 length = svlen - dataoffset; 1586 len = svlen - dataoffset;
1450 } 1587 }
1451 else 1588 else
1452 { 1589 {
1453 /* read: grow scalar as necessary */ 1590 /* read: grow scalar as necessary */
1454 svptr = SvGROW (data, length + dataoffset + 1); 1591 svptr = SvGROW (data, len + dataoffset + 1);
1455 } 1592 }
1456 1593
1457 if (length < 0) 1594 if (len < 0)
1458 croak ("length must not be negative"); 1595 croak ("length must not be negative");
1459 1596
1460 { 1597 {
1461 dREQ; 1598 dREQ;
1462 1599
1463 req->type = ix; 1600 req->type = ix;
1464 req->sv1 = newSVsv (fh); 1601 req->sv1 = newSVsv (fh);
1465 req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) 1602 req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
1466 : IoOFP (sv_2io (fh))); 1603 : IoOFP (sv_2io (fh)));
1467 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; 1604 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
1468 req->size = length; 1605 req->size = len;
1469 req->sv2 = SvREFCNT_inc (data); 1606 req->sv2 = SvREFCNT_inc (data);
1470 req->ptr1 = (char *)svptr + dataoffset; 1607 req->ptr1 = (char *)svptr + dataoffset;
1471 req->stroffset = dataoffset; 1608 req->stroffset = dataoffset;
1472 1609
1473 if (!SvREADONLY (data)) 1610 if (!SvREADONLY (data))
1583 req->ptr1 = SvPVbyte_nolen (req->sv1); 1720 req->ptr1 = SvPVbyte_nolen (req->sv1);
1584 } 1721 }
1585 else 1722 else
1586 { 1723 {
1587 req->type = REQ_FUTIME; 1724 req->type = REQ_FUTIME;
1725 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1726 }
1727
1728 REQ_SEND;
1729}
1730
1731void
1732aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef)
1733 PPCODE:
1734{
1735 dREQ;
1736
1737 req->sv1 = newSVsv (fh_or_path);
1738 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
1739
1740 if (SvPOK (fh_or_path))
1741 {
1742 req->type = REQ_TRUNCATE;
1743 req->ptr1 = SvPVbyte_nolen (req->sv1);
1744 }
1745 else
1746 {
1747 req->type = REQ_FTRUNCATE;
1588 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 1748 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1589 } 1749 }
1590 1750
1591 REQ_SEND; 1751 REQ_SEND;
1592} 1752}
1808 CODE: 1968 CODE:
1809{ 1969{
1810 if (block_sig_level) 1970 if (block_sig_level)
1811 croak ("cannot call IO::AIO::setsig from within aio_block/callback"); 1971 croak ("cannot call IO::AIO::setsig from within aio_block/callback");
1812 1972
1813 LOCK (reslock); 1973 X_LOCK (reslock);
1814 main_tid = pthread_self (); 1974 main_tid = pthread_self ();
1815 main_sig = signum; 1975 main_sig = signum;
1816 UNLOCK (reslock); 1976 X_UNLOCK (reslock);
1817 1977
1818 if (main_sig && npending) 1978 if (main_sig && npending)
1819 pthread_kill (main_tid, main_sig); 1979 pthread_kill (main_tid, main_sig);
1820} 1980}
1821 1981
1865 2025
1866int 2026int
1867nthreads() 2027nthreads()
1868 PROTOTYPE: 2028 PROTOTYPE:
1869 CODE: 2029 CODE:
1870 if (WORDACCESS_UNSAFE) LOCK (wrklock); 2030 if (WORDACCESS_UNSAFE) X_LOCK (wrklock);
1871 RETVAL = started; 2031 RETVAL = started;
1872 if (WORDACCESS_UNSAFE) UNLOCK (wrklock); 2032 if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock);
1873 OUTPUT: 2033 OUTPUT:
1874 RETVAL 2034 RETVAL
1875 2035
1876PROTOTYPES: DISABLE 2036PROTOTYPES: DISABLE
1877 2037

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines