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.105 by root, Sun Jul 8 11:12:15 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
55# define SvVAL64 SvNV 99# define SvVAL64 SvNV
56#endif 100#endif
57 101
58#define dBUF \ 102#define dBUF \
59 char *aio_buf; \ 103 char *aio_buf; \
60 LOCK (wrklock); \ 104 X_LOCK (wrklock); \
61 self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ 105 self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \
62 UNLOCK (wrklock); \ 106 X_UNLOCK (wrklock); \
63 if (!aio_buf) \ 107 if (!aio_buf) \
64 return -1; 108 return -1;
65 109
66typedef SV SV8; /* byte-sv, used for argument-checking */ 110typedef SV SV8; /* byte-sv, used for argument-checking */
67 111
69 REQ_QUIT, 113 REQ_QUIT,
70 REQ_OPEN, REQ_CLOSE, 114 REQ_OPEN, REQ_CLOSE,
71 REQ_READ, REQ_WRITE, 115 REQ_READ, REQ_WRITE,
72 REQ_READAHEAD, REQ_SENDFILE, 116 REQ_READAHEAD, REQ_SENDFILE,
73 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 117 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
118 REQ_TRUNCATE, REQ_FTRUNCATE,
74 REQ_UTIME, REQ_FUTIME, 119 REQ_UTIME, REQ_FUTIME,
75 REQ_CHMOD, REQ_FCHMOD, 120 REQ_CHMOD, REQ_FCHMOD,
76 REQ_CHOWN, REQ_FCHOWN, 121 REQ_CHOWN, REQ_FCHOWN,
77 REQ_FSYNC, REQ_FDATASYNC, 122 REQ_FSYNC, REQ_FDATASYNC,
78 REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME, 123 REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME,
177static int next_pri = DEFAULT_PRI + PRI_BIAS; 222static int next_pri = DEFAULT_PRI + PRI_BIAS;
178 223
179static unsigned int started, idle, wanted; 224static unsigned int started, idle, wanted;
180 225
181/* worker threads management */ 226/* worker threads management */
182static mutex_t wrklock = MUTEX_INIT; 227static mutex_t wrklock = X_MUTEX_INIT;
183 228
184typedef struct worker { 229typedef struct worker {
185 /* locked by wrklock */ 230 /* locked by wrklock */
186 struct worker *prev, *next; 231 struct worker *prev, *next;
187 232
219} 264}
220 265
221static volatile unsigned int nreqs, nready, npending; 266static volatile unsigned int nreqs, nready, npending;
222static volatile unsigned int max_idle = 4; 267static volatile unsigned int max_idle = 4;
223static volatile unsigned int max_outstanding = 0xffffffff; 268static volatile unsigned int max_outstanding = 0xffffffff;
224static int respipe [2]; 269static int respipe [2], respipe_osf [2];
225 270
226static mutex_t reslock = MUTEX_INIT; 271static mutex_t reslock = X_MUTEX_INIT;
227static mutex_t reqlock = MUTEX_INIT; 272static mutex_t reqlock = X_MUTEX_INIT;
228static cond_t reqwait = COND_INIT; 273static cond_t reqwait = X_COND_INIT;
229 274
230#if WORDACCESS_UNSAFE 275#if WORDACCESS_UNSAFE
231 276
232static unsigned int get_nready () 277static unsigned int get_nready ()
233{ 278{
234 unsigned int retval; 279 unsigned int retval;
235 280
236 LOCK (reqlock); 281 X_LOCK (reqlock);
237 retval = nready; 282 retval = nready;
238 UNLOCK (reqlock); 283 X_UNLOCK (reqlock);
239 284
240 return retval; 285 return retval;
241} 286}
242 287
243static unsigned int get_npending () 288static unsigned int get_npending ()
244{ 289{
245 unsigned int retval; 290 unsigned int retval;
246 291
247 LOCK (reslock); 292 X_LOCK (reslock);
248 retval = npending; 293 retval = npending;
249 UNLOCK (reslock); 294 X_UNLOCK (reslock);
250 295
251 return retval; 296 return retval;
252} 297}
253 298
254static unsigned int get_nthreads () 299static unsigned int get_nthreads ()
255{ 300{
256 unsigned int retval; 301 unsigned int retval;
257 302
258 LOCK (wrklock); 303 X_LOCK (wrklock);
259 retval = started; 304 retval = started;
260 UNLOCK (wrklock); 305 X_UNLOCK (wrklock);
261 306
262 return retval; 307 return retval;
263} 308}
264 309
265#else 310#else
582 req->flags |= FLAG_CANCELLED; 627 req->flags |= FLAG_CANCELLED;
583 628
584 req_cancel_subs (req); 629 req_cancel_subs (req);
585} 630}
586 631
587static void *aio_proc (void *arg); 632#ifdef USE_SOCKETS_AS_HANDLES
633# define TO_SOCKET(x) (win32_get_osfhandle (x))
634#else
635# define TO_SOCKET(x) (x)
636#endif
637
638static void
639create_pipe (int fd[2])
640{
641#ifdef _WIN32
642 int arg = 1;
643 if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, fd)
644 || ioctlsocket (TO_SOCKET (fd [0]), FIONBIO, &arg)
645 || ioctlsocket (TO_SOCKET (fd [1]), FIONBIO, &arg))
646#else
647 if (pipe (fd)
648 || fcntl (fd [0], F_SETFL, O_NONBLOCK)
649 || fcntl (fd [1], F_SETFL, O_NONBLOCK))
650#endif
651 croak ("unable to initialize result pipe");
652
653 respipe_osf [0] = TO_SOCKET (respipe [0]);
654 respipe_osf [1] = TO_SOCKET (respipe [1]);
655}
656
657X_THREAD_PROC (aio_proc);
588 658
589static void start_thread (void) 659static void start_thread (void)
590{ 660{
591 worker *wrk = calloc (1, sizeof (worker)); 661 worker *wrk = calloc (1, sizeof (worker));
592 662
593 if (!wrk) 663 if (!wrk)
594 croak ("unable to allocate worker thread data"); 664 croak ("unable to allocate worker thread data");
595 665
596 LOCK (wrklock); 666 X_LOCK (wrklock);
597 667
598 if (thread_create (&wrk->tid, aio_proc, (void *)wrk)) 668 if (thread_create (&wrk->tid, aio_proc, (void *)wrk))
599 { 669 {
600 wrk->prev = &wrk_first; 670 wrk->prev = &wrk_first;
601 wrk->next = wrk_first.next; 671 wrk->next = wrk_first.next;
604 ++started; 674 ++started;
605 } 675 }
606 else 676 else
607 free (wrk); 677 free (wrk);
608 678
609 UNLOCK (wrklock); 679 X_UNLOCK (wrklock);
610} 680}
611 681
612static void maybe_start_thread () 682static void maybe_start_thread ()
613{ 683{
614 if (get_nthreads () >= wanted) 684 if (get_nthreads () >= wanted)
625{ 695{
626 block_sig (); 696 block_sig ();
627 697
628 ++nreqs; 698 ++nreqs;
629 699
630 LOCK (reqlock); 700 X_LOCK (reqlock);
631 ++nready; 701 ++nready;
632 reqq_push (&req_queue, req); 702 reqq_push (&req_queue, req);
633 COND_SIGNAL (reqwait); 703 X_COND_SIGNAL (reqwait);
634 UNLOCK (reqlock); 704 X_UNLOCK (reqlock);
635 705
636 unblock_sig (); 706 unblock_sig ();
637 707
638 maybe_start_thread (); 708 maybe_start_thread ();
639} 709}
645 Newz (0, req, 1, aio_cb); 715 Newz (0, req, 1, aio_cb);
646 716
647 req->type = REQ_QUIT; 717 req->type = REQ_QUIT;
648 req->pri = PRI_MAX + PRI_BIAS; 718 req->pri = PRI_MAX + PRI_BIAS;
649 719
650 LOCK (reqlock); 720 X_LOCK (reqlock);
651 reqq_push (&req_queue, req); 721 reqq_push (&req_queue, req);
652 COND_SIGNAL (reqwait); 722 X_COND_SIGNAL (reqwait);
653 UNLOCK (reqlock); 723 X_UNLOCK (reqlock);
654 724
655 LOCK (wrklock); 725 X_LOCK (wrklock);
656 --started; 726 --started;
657 UNLOCK (wrklock); 727 X_UNLOCK (wrklock);
658} 728}
659 729
660static void set_max_idle (int nthreads) 730static void set_max_idle (int nthreads)
661{ 731{
662 if (WORDACCESS_UNSAFE) LOCK (reqlock); 732 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
663 max_idle = nthreads <= 0 ? 1 : nthreads; 733 max_idle = nthreads <= 0 ? 1 : nthreads;
664 if (WORDACCESS_UNSAFE) UNLOCK (reqlock); 734 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
665} 735}
666 736
667static void min_parallel (int nthreads) 737static void min_parallel (int nthreads)
668{ 738{
669 if (wanted < nthreads) 739 if (wanted < nthreads)
684 fd_set rfd; 754 fd_set rfd;
685 755
686 while (nreqs) 756 while (nreqs)
687 { 757 {
688 int size; 758 int size;
689 if (WORDACCESS_UNSAFE) LOCK (reslock); 759 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
690 size = res_queue.size; 760 size = res_queue.size;
691 if (WORDACCESS_UNSAFE) UNLOCK (reslock); 761 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
692 762
693 if (size) 763 if (size)
694 return; 764 return;
695 765
696 maybe_start_thread (); 766 maybe_start_thread ();
697 767
698 FD_ZERO(&rfd); 768 FD_ZERO (&rfd);
699 FD_SET(respipe [0], &rfd); 769 FD_SET (respipe [0], &rfd);
700 770
701 select (respipe [0] + 1, &rfd, 0, 0, 0); 771 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
702 } 772 }
703} 773}
704 774
705static int poll_cb () 775static int poll_cb ()
706{ 776{
720 { 790 {
721 for (;;) 791 for (;;)
722 { 792 {
723 maybe_start_thread (); 793 maybe_start_thread ();
724 794
725 LOCK (reslock); 795 X_LOCK (reslock);
726 req = reqq_shift (&res_queue); 796 req = reqq_shift (&res_queue);
727 797
728 if (req) 798 if (req)
729 { 799 {
730 --npending; 800 --npending;
731 801
732 if (!res_queue.size) 802 if (!res_queue.size)
733 { 803 {
734 /* read any signals sent by the worker threads */ 804 /* read any signals sent by the worker threads */
735 char buf [4]; 805 char buf [4];
736 while (read (respipe [0], buf, 4) == 4) 806 while (respipe_read (respipe [0], buf, 4) == 4)
737 ; 807 ;
738 } 808 }
739 } 809 }
740 810
741 UNLOCK (reslock); 811 X_UNLOCK (reslock);
742 812
743 if (!req) 813 if (!req)
744 break; 814 break;
745 815
746 --nreqs; 816 --nreqs;
786 856
787 unblock_sig (); 857 unblock_sig ();
788 return count; 858 return count;
789} 859}
790 860
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/*****************************************************************************/ 861/*****************************************************************************/
804/* work around various missing functions */ 862/* work around various missing functions */
805 863
806#if !HAVE_PREADWRITE 864#if !HAVE_PREADWRITE
807# define pread aio_pread 865# define pread aio_pread
810/* 868/*
811 * make our pread/pwrite safe against themselves, but not against 869 * make our pread/pwrite safe against themselves, but not against
812 * normal read/write by using a mutex. slows down execution a lot, 870 * normal read/write by using a mutex. slows down execution a lot,
813 * but that's your problem, not mine. 871 * but that's your problem, not mine.
814 */ 872 */
815static mutex_t preadwritelock = MUTEX_INIT; 873static mutex_t preadwritelock = X_MUTEX_INIT;
816 874
817static ssize_t pread (int fd, void *buf, size_t count, off_t offset) 875static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
818{ 876{
819 ssize_t res; 877 ssize_t res;
820 off_t ooffset; 878 off_t ooffset;
821 879
822 LOCK (preadwritelock); 880 X_LOCK (preadwritelock);
823 ooffset = lseek (fd, 0, SEEK_CUR); 881 ooffset = lseek (fd, 0, SEEK_CUR);
824 lseek (fd, offset, SEEK_SET); 882 lseek (fd, offset, SEEK_SET);
825 res = read (fd, buf, count); 883 res = read (fd, buf, count);
826 lseek (fd, ooffset, SEEK_SET); 884 lseek (fd, ooffset, SEEK_SET);
827 UNLOCK (preadwritelock); 885 X_UNLOCK (preadwritelock);
828 886
829 return res; 887 return res;
830} 888}
831 889
832static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) 890static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset)
833{ 891{
834 ssize_t res; 892 ssize_t res;
835 off_t ooffset; 893 off_t ooffset;
836 894
837 LOCK (preadwritelock); 895 X_LOCK (preadwritelock);
838 ooffset = lseek (fd, 0, SEEK_CUR); 896 ooffset = lseek (fd, 0, SEEK_CUR);
839 lseek (fd, offset, SEEK_SET); 897 lseek (fd, offset, SEEK_SET);
840 res = write (fd, buf, count); 898 res = write (fd, buf, count);
841 lseek (fd, offset, SEEK_SET); 899 lseek (fd, offset, SEEK_SET);
842 UNLOCK (preadwritelock); 900 X_UNLOCK (preadwritelock);
843 901
844 return res; 902 return res;
845} 903}
846#endif 904#endif
847 905
880#if !HAVE_READAHEAD 938#if !HAVE_READAHEAD
881# define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) 939# define readahead(fd,offset,count) aio_readahead (fd, offset, count, self)
882 940
883static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) 941static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self)
884{ 942{
943 size_t todo = count;
885 dBUF; 944 dBUF;
886 945
887 while (count > 0) 946 while (todo > 0)
888 { 947 {
889 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; 948 size_t len = todo < AIO_BUFSIZE ? todo : AIO_BUFSIZE;
890 949
891 pread (fd, aio_buf, len, offset); 950 pread (fd, aio_buf, len, offset);
892 offset += len; 951 offset += len;
893 count -= len; 952 todo -= len;
894 } 953 }
895 954
896 errno = 0; 955 errno = 0;
956 return count;
897} 957}
898 958
899#endif 959#endif
900 960
901#if !HAVE_READDIR_R 961#if !HAVE_READDIR_R
902# define readdir_r aio_readdir_r 962# define readdir_r aio_readdir_r
903 963
904static mutex_t readdirlock = MUTEX_INIT; 964static mutex_t readdirlock = X_MUTEX_INIT;
905 965
906static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) 966static int readdir_r (DIR *dirp, X_DIRENT *ent, X_DIRENT **res)
907{ 967{
908 struct dirent *e; 968 X_DIRENT *e;
909 int errorno; 969 int errorno;
910 970
911 LOCK (readdirlock); 971 X_LOCK (readdirlock);
912 972
913 e = readdir (dirp); 973 e = readdir (dirp);
914 errorno = errno; 974 errorno = errno;
915 975
916 if (e) 976 if (e)
919 strcpy (ent->d_name, e->d_name); 979 strcpy (ent->d_name, e->d_name);
920 } 980 }
921 else 981 else
922 *res = 0; 982 *res = 0;
923 983
924 UNLOCK (readdirlock); 984 X_UNLOCK (readdirlock);
925 985
926 errno = errorno; 986 errno = errorno;
927 return e ? 0 : -1; 987 return e ? 0 : -1;
928} 988}
929#endif 989#endif
1025static void scandir_ (aio_req req, worker *self) 1085static void scandir_ (aio_req req, worker *self)
1026{ 1086{
1027 DIR *dirp; 1087 DIR *dirp;
1028 union 1088 union
1029 { 1089 {
1030 struct dirent d; 1090 X_DIRENT d;
1031 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 1091 char b [offsetof (X_DIRENT, d_name) + NAME_MAX + 1];
1032 } *u; 1092 } *u;
1033 struct dirent *entp; 1093 X_DIRENT *entp;
1034 char *name, *names; 1094 char *name, *names;
1035 int memlen = 4096; 1095 int memlen = 4096;
1036 int memofs = 0; 1096 int memofs = 0;
1037 int res = 0; 1097 int res = 0;
1038 int errorno;
1039 1098
1040 LOCK (wrklock); 1099 X_LOCK (wrklock);
1041 self->dirp = dirp = opendir (req->ptr1); 1100 self->dirp = dirp = opendir (req->ptr1);
1042 self->dbuf = u = malloc (sizeof (*u)); 1101 self->dbuf = u = malloc (sizeof (*u));
1043 req->flags |= FLAG_PTR2_FREE; 1102 req->flags |= FLAG_PTR2_FREE;
1044 req->ptr2 = names = malloc (memlen); 1103 req->ptr2 = names = malloc (memlen);
1045 UNLOCK (wrklock); 1104 X_UNLOCK (wrklock);
1046 1105
1047 if (dirp && u && names) 1106 if (dirp && u && names)
1048 for (;;) 1107 for (;;)
1049 { 1108 {
1050 errno = 0; 1109 errno = 0;
1062 res++; 1121 res++;
1063 1122
1064 while (memofs + len > memlen) 1123 while (memofs + len > memlen)
1065 { 1124 {
1066 memlen *= 2; 1125 memlen *= 2;
1067 LOCK (wrklock); 1126 X_LOCK (wrklock);
1068 req->ptr2 = names = realloc (names, memlen); 1127 req->ptr2 = names = realloc (names, memlen);
1069 UNLOCK (wrklock); 1128 X_UNLOCK (wrklock);
1070 1129
1071 if (!names) 1130 if (!names)
1072 break; 1131 break;
1073 } 1132 }
1074 1133
1083 req->result = res; 1142 req->result = res;
1084} 1143}
1085 1144
1086/*****************************************************************************/ 1145/*****************************************************************************/
1087 1146
1088static void *aio_proc (void *thr_arg) 1147X_THREAD_PROC (aio_proc)
1089{ 1148{
1149 {//D
1090 aio_req req; 1150 aio_req req;
1091 struct timespec ts; 1151 struct timespec ts;
1092 worker *self = (worker *)thr_arg; 1152 worker *self = (worker *)thr_arg;
1093 1153
1094 /* try to distribute timeouts somewhat evenly */ 1154 /* try to distribute timeouts somewhat randomly */
1095 ts.tv_nsec = (((unsigned long)self + (unsigned long)ts.tv_sec) & 1023UL) 1155 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1096 * (1000000000UL / 1024UL);
1097 1156
1098 for (;;) 1157 for (;;)
1099 { 1158 {
1100 ts.tv_sec = time (0) + IDLE_TIMEOUT; 1159 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1101 1160
1102 LOCK (reqlock); 1161 X_LOCK (reqlock);
1103 1162
1104 for (;;) 1163 for (;;)
1105 { 1164 {
1106 self->req = req = reqq_shift (&req_queue); 1165 self->req = req = reqq_shift (&req_queue);
1107 1166
1108 if (req) 1167 if (req)
1109 break; 1168 break;
1110 1169
1111 ++idle; 1170 ++idle;
1112 1171
1113 if (COND_TIMEDWAIT (reqwait, reqlock, ts) 1172 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts)
1114 == ETIMEDOUT) 1173 == ETIMEDOUT)
1115 { 1174 {
1116 if (idle > max_idle) 1175 if (idle > max_idle)
1117 { 1176 {
1118 --idle; 1177 --idle;
1119 UNLOCK (reqlock); 1178 X_UNLOCK (reqlock);
1120 LOCK (wrklock); 1179 X_LOCK (wrklock);
1121 --started; 1180 --started;
1122 UNLOCK (wrklock); 1181 X_UNLOCK (wrklock);
1123 goto quit; 1182 goto quit;
1124 } 1183 }
1125 1184
1126 /* we are allowed to idle, so do so without any timeout */ 1185 /* we are allowed to idle, so do so without any timeout */
1127 COND_WAIT (reqwait, reqlock); 1186 X_COND_WAIT (reqwait, reqlock);
1128 ts.tv_sec = time (0) + IDLE_TIMEOUT; 1187 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1129 } 1188 }
1130 1189
1131 --idle; 1190 --idle;
1132 } 1191 }
1133 1192
1134 --nready; 1193 --nready;
1135 1194
1136 UNLOCK (reqlock); 1195 X_UNLOCK (reqlock);
1137 1196
1138 errno = 0; /* strictly unnecessary */ 1197 errno = 0; /* strictly unnecessary */
1139 1198
1140 if (!(req->flags & FLAG_CANCELLED)) 1199 if (!(req->flags & FLAG_CANCELLED))
1141 switch (req->type) 1200 switch (req->type)
1156 1215
1157 case REQ_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break; 1216 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; 1217 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; 1218 case REQ_CHMOD: req->result = chmod (req->ptr1, req->mode); break;
1160 case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break; 1219 case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break;
1220 case REQ_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break;
1221 case REQ_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break;
1161 1222
1162 case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break; 1223 case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break;
1163 case REQ_CLOSE: req->result = close (req->int1); break; 1224 case REQ_CLOSE: req->result = close (req->int1); break;
1164 case REQ_UNLINK: req->result = unlink (req->ptr1); break; 1225 case REQ_UNLINK: req->result = unlink (req->ptr1); break;
1165 case REQ_RMDIR: req->result = rmdir (req->ptr1); break; 1226 case REQ_RMDIR: req->result = rmdir (req->ptr1); break;
1173 case REQ_FDATASYNC: req->result = fdatasync (req->int1); break; 1234 case REQ_FDATASYNC: req->result = fdatasync (req->int1); break;
1174 case REQ_FSYNC: req->result = fsync (req->int1); break; 1235 case REQ_FSYNC: req->result = fsync (req->int1); break;
1175 case REQ_READDIR: scandir_ (req, self); break; 1236 case REQ_READDIR: scandir_ (req, self); break;
1176 1237
1177 case REQ_BUSY: 1238 case REQ_BUSY:
1239#ifdef _WIN32
1240 Sleep (req->nv1 * 1000.);
1241#else
1178 { 1242 {
1179 struct timeval tv; 1243 struct timeval tv;
1180 1244
1181 tv.tv_sec = req->nv1; 1245 tv.tv_sec = req->nv1;
1182 tv.tv_usec = (req->nv1 - tv.tv_usec) * 1000000.; 1246 tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.;
1183 1247
1184 req->result = select (0, 0, 0, 0, &tv); 1248 req->result = select (0, 0, 0, 0, &tv);
1185 } 1249 }
1250#endif
1251 break;
1186 1252
1187 case REQ_UTIME: 1253 case REQ_UTIME:
1188 case REQ_FUTIME: 1254 case REQ_FUTIME:
1189 { 1255 {
1190 struct timeval tv[2]; 1256 struct timeval tv[2];
1220 break; 1286 break;
1221 } 1287 }
1222 1288
1223 req->errorno = errno; 1289 req->errorno = errno;
1224 1290
1225 LOCK (reslock); 1291 X_LOCK (reslock);
1226 1292
1227 ++npending; 1293 ++npending;
1228 1294
1229 if (!reqq_push (&res_queue, req)) 1295 if (!reqq_push (&res_queue, req))
1230 { 1296 {
1231 /* write a dummy byte to the pipe so fh becomes ready */ 1297 /* write a dummy byte to the pipe so fh becomes ready */
1232 write (respipe [1], &respipe, 1); 1298 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
1233 1299
1234 /* optionally signal the main thread asynchronously */ 1300 /* optionally signal the main thread asynchronously */
1235 if (main_sig) 1301 if (main_sig)
1236 pthread_kill (main_tid, main_sig); 1302 pthread_kill (main_tid, main_sig);
1237 } 1303 }
1238 1304
1239 self->req = 0; 1305 self->req = 0;
1240 worker_clear (self); 1306 worker_clear (self);
1241 1307
1242 UNLOCK (reslock); 1308 X_UNLOCK (reslock);
1243 } 1309 }
1244 1310
1245quit: 1311quit:
1246 LOCK (wrklock); 1312 X_LOCK (wrklock);
1247 worker_free (self); 1313 worker_free (self);
1248 UNLOCK (wrklock); 1314 X_UNLOCK (wrklock);
1249 1315
1250 return 0; 1316 return 0;
1317 }//D
1251} 1318}
1252 1319
1253/*****************************************************************************/ 1320/*****************************************************************************/
1254 1321
1255static void atfork_prepare (void) 1322static void atfork_prepare (void)
1256{ 1323{
1257 LOCK (wrklock); 1324 X_LOCK (wrklock);
1258 LOCK (reqlock); 1325 X_LOCK (reqlock);
1259 LOCK (reslock); 1326 X_LOCK (reslock);
1260#if !HAVE_PREADWRITE 1327#if !HAVE_PREADWRITE
1261 LOCK (preadwritelock); 1328 X_LOCK (preadwritelock);
1262#endif 1329#endif
1263#if !HAVE_READDIR_R 1330#if !HAVE_READDIR_R
1264 LOCK (readdirlock); 1331 X_LOCK (readdirlock);
1265#endif 1332#endif
1266} 1333}
1267 1334
1268static void atfork_parent (void) 1335static void atfork_parent (void)
1269{ 1336{
1270#if !HAVE_READDIR_R 1337#if !HAVE_READDIR_R
1271 UNLOCK (readdirlock); 1338 X_UNLOCK (readdirlock);
1272#endif 1339#endif
1273#if !HAVE_PREADWRITE 1340#if !HAVE_PREADWRITE
1274 UNLOCK (preadwritelock); 1341 X_UNLOCK (preadwritelock);
1275#endif 1342#endif
1276 UNLOCK (reslock); 1343 X_UNLOCK (reslock);
1277 UNLOCK (reqlock); 1344 X_UNLOCK (reqlock);
1278 UNLOCK (wrklock); 1345 X_UNLOCK (wrklock);
1279} 1346}
1280 1347
1281static void atfork_child (void) 1348static void atfork_child (void)
1282{ 1349{
1283 aio_req prv; 1350 aio_req prv;
1303 idle = 0; 1370 idle = 0;
1304 nreqs = 0; 1371 nreqs = 0;
1305 nready = 0; 1372 nready = 0;
1306 npending = 0; 1373 npending = 0;
1307 1374
1308 close (respipe [0]); 1375 respipe_close (respipe [0]);
1309 close (respipe [1]); 1376 respipe_close (respipe [1]);
1377
1310 create_pipe (); 1378 create_pipe (respipe);
1311 1379
1312 atfork_parent (); 1380 atfork_parent ();
1313} 1381}
1314 1382
1315#define dREQ \ 1383#define dREQ \
1344 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); 1412 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
1345 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); 1413 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
1346 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); 1414 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
1347 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); 1415 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT));
1348 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); 1416 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC));
1417#ifdef _WIN32
1418 X_MUTEX_CHECK (wrklock);
1419 X_MUTEX_CHECK (reslock);
1420 X_MUTEX_CHECK (reqlock);
1421 X_MUTEX_CHECK (reqwait);
1422 X_MUTEX_CHECK (preadwritelock);
1423 X_MUTEX_CHECK (readdirlock);
1424
1425 X_COND_CHECK (reqwait);
1426#else
1349 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); 1427 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO));
1350 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); 1428 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO));
1429#endif
1351 1430
1352 create_pipe (); 1431 create_pipe (respipe);
1432
1353 ATFORK (atfork_prepare, atfork_parent, atfork_child); 1433 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child);
1354} 1434}
1355 1435
1356void 1436void
1357max_poll_reqs (int nreqs) 1437max_poll_reqs (int nreqs)
1358 PROTOTYPE: $ 1438 PROTOTYPE: $
1584 req->ptr1 = SvPVbyte_nolen (req->sv1); 1664 req->ptr1 = SvPVbyte_nolen (req->sv1);
1585 } 1665 }
1586 else 1666 else
1587 { 1667 {
1588 req->type = REQ_FUTIME; 1668 req->type = REQ_FUTIME;
1669 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1670 }
1671
1672 REQ_SEND;
1673}
1674
1675void
1676aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef)
1677 PPCODE:
1678{
1679 dREQ;
1680
1681 req->sv1 = newSVsv (fh_or_path);
1682 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
1683
1684 if (SvPOK (fh_or_path))
1685 {
1686 req->type = REQ_TRUNCATE;
1687 req->ptr1 = SvPVbyte_nolen (req->sv1);
1688 }
1689 else
1690 {
1691 req->type = REQ_FTRUNCATE;
1589 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 1692 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1590 } 1693 }
1591 1694
1592 REQ_SEND; 1695 REQ_SEND;
1593} 1696}
1809 CODE: 1912 CODE:
1810{ 1913{
1811 if (block_sig_level) 1914 if (block_sig_level)
1812 croak ("cannot call IO::AIO::setsig from within aio_block/callback"); 1915 croak ("cannot call IO::AIO::setsig from within aio_block/callback");
1813 1916
1814 LOCK (reslock); 1917 X_LOCK (reslock);
1815 main_tid = pthread_self (); 1918 main_tid = pthread_self ();
1816 main_sig = signum; 1919 main_sig = signum;
1817 UNLOCK (reslock); 1920 X_UNLOCK (reslock);
1818 1921
1819 if (main_sig && npending) 1922 if (main_sig && npending)
1820 pthread_kill (main_tid, main_sig); 1923 pthread_kill (main_tid, main_sig);
1821} 1924}
1822 1925
1866 1969
1867int 1970int
1868nthreads() 1971nthreads()
1869 PROTOTYPE: 1972 PROTOTYPE:
1870 CODE: 1973 CODE:
1871 if (WORDACCESS_UNSAFE) LOCK (wrklock); 1974 if (WORDACCESS_UNSAFE) X_LOCK (wrklock);
1872 RETVAL = started; 1975 RETVAL = started;
1873 if (WORDACCESS_UNSAFE) UNLOCK (wrklock); 1976 if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock);
1874 OUTPUT: 1977 OUTPUT:
1875 RETVAL 1978 RETVAL
1876 1979
1877PROTOTYPES: DISABLE 1980PROTOTYPES: DISABLE
1878 1981

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines