… | |
… | |
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 stat |
|
|
30 | # undef fstat |
|
|
31 | # define lstat stat |
|
|
32 | # undef truncate |
|
|
33 | # undef ftruncate |
|
|
34 | # undef open |
|
|
35 | # undef close |
|
|
36 | # undef unlink |
|
|
37 | # undef rmdir |
|
|
38 | # undef rename |
|
|
39 | # undef lseek |
|
|
40 | |
|
|
41 | # define chown(a,b,c) (errno = ENOSYS, -1) |
|
|
42 | # define fchown(a,b,c) (errno = ENOSYS, -1) |
|
|
43 | # define fchmod(a,b) (errno = ENOSYS, -1) |
|
|
44 | # define symlink(a,b) (errno = ENOSYS, -1) |
|
|
45 | # define readlink(a,b,c) (errno = ENOSYS, -1) |
|
|
46 | # define mknod(a,b,c) (errno = ENOSYS, -1) |
|
|
47 | # define truncate(a,b) (errno = ENOSYS, -1) |
|
|
48 | # define ftruncate(fd,o) chsize ((fd), (o)) |
|
|
49 | # define fsync(fd) _commit (fd) |
|
|
50 | # define opendir(fd) (errno = ENOSYS, 0) |
|
|
51 | # define readdir(fd) (errno = ENOSYS, -1) |
|
|
52 | # define closedir(fd) (errno = ENOSYS, -1) |
|
|
53 | # define mkdir(a,b) mkdir (a) |
|
|
54 | |
|
|
55 | #else |
|
|
56 | |
|
|
57 | # include "autoconf/config.h" |
|
|
58 | # include <sys/time.h> |
|
|
59 | # include <sys/select.h> |
|
|
60 | # include <unistd.h> |
23 | #include <utime.h> |
61 | # include <utime.h> |
|
|
62 | # include <signal.h> |
|
|
63 | typedef struct dirent X_DIRENT; |
|
|
64 | |
|
|
65 | #endif |
24 | |
66 | |
25 | #if HAVE_SENDFILE |
67 | #if HAVE_SENDFILE |
26 | # if __linux |
68 | # if __linux |
27 | # include <sys/sendfile.h> |
69 | # include <sys/sendfile.h> |
28 | # elif __freebsd |
70 | # elif __freebsd |
… | |
… | |
55 | # define SvVAL64 SvNV |
97 | # define SvVAL64 SvNV |
56 | #endif |
98 | #endif |
57 | |
99 | |
58 | #define dBUF \ |
100 | #define dBUF \ |
59 | char *aio_buf; \ |
101 | char *aio_buf; \ |
60 | LOCK (wrklock); \ |
102 | X_LOCK (wrklock); \ |
61 | self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ |
103 | self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ |
62 | UNLOCK (wrklock); \ |
104 | X_UNLOCK (wrklock); \ |
63 | if (!aio_buf) \ |
105 | if (!aio_buf) \ |
64 | return -1; |
106 | return -1; |
65 | |
107 | |
66 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
108 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
67 | |
109 | |
… | |
… | |
69 | REQ_QUIT, |
111 | REQ_QUIT, |
70 | REQ_OPEN, REQ_CLOSE, |
112 | REQ_OPEN, REQ_CLOSE, |
71 | REQ_READ, REQ_WRITE, |
113 | REQ_READ, REQ_WRITE, |
72 | REQ_READAHEAD, REQ_SENDFILE, |
114 | REQ_READAHEAD, REQ_SENDFILE, |
73 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
115 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
|
|
116 | REQ_TRUNCATE, REQ_FTRUNCATE, |
74 | REQ_UTIME, REQ_FUTIME, |
117 | REQ_UTIME, REQ_FUTIME, |
75 | REQ_CHMOD, REQ_FCHMOD, |
118 | REQ_CHMOD, REQ_FCHMOD, |
76 | REQ_CHOWN, REQ_FCHOWN, |
119 | REQ_CHOWN, REQ_FCHOWN, |
77 | REQ_FSYNC, REQ_FDATASYNC, |
120 | REQ_FSYNC, REQ_FDATASYNC, |
78 | REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME, |
121 | REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME, |
… | |
… | |
177 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
220 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
178 | |
221 | |
179 | static unsigned int started, idle, wanted; |
222 | static unsigned int started, idle, wanted; |
180 | |
223 | |
181 | /* worker threads management */ |
224 | /* worker threads management */ |
182 | static mutex_t wrklock = MUTEX_INIT; |
225 | static mutex_t wrklock = X_MUTEX_INIT; |
183 | |
226 | |
184 | typedef struct worker { |
227 | typedef struct worker { |
185 | /* locked by wrklock */ |
228 | /* locked by wrklock */ |
186 | struct worker *prev, *next; |
229 | struct worker *prev, *next; |
187 | |
230 | |
… | |
… | |
221 | static volatile unsigned int nreqs, nready, npending; |
264 | static volatile unsigned int nreqs, nready, npending; |
222 | static volatile unsigned int max_idle = 4; |
265 | static volatile unsigned int max_idle = 4; |
223 | static volatile unsigned int max_outstanding = 0xffffffff; |
266 | static volatile unsigned int max_outstanding = 0xffffffff; |
224 | static int respipe [2]; |
267 | static int respipe [2]; |
225 | |
268 | |
226 | static mutex_t reslock = MUTEX_INIT; |
269 | static mutex_t reslock = X_MUTEX_INIT; |
227 | static mutex_t reqlock = MUTEX_INIT; |
270 | static mutex_t reqlock = X_MUTEX_INIT; |
228 | static cond_t reqwait = COND_INIT; |
271 | static cond_t reqwait = X_COND_INIT; |
229 | |
272 | |
230 | #if WORDACCESS_UNSAFE |
273 | #if WORDACCESS_UNSAFE |
231 | |
274 | |
232 | static unsigned int get_nready () |
275 | static unsigned int get_nready () |
233 | { |
276 | { |
234 | unsigned int retval; |
277 | unsigned int retval; |
235 | |
278 | |
236 | LOCK (reqlock); |
279 | X_LOCK (reqlock); |
237 | retval = nready; |
280 | retval = nready; |
238 | UNLOCK (reqlock); |
281 | X_UNLOCK (reqlock); |
239 | |
282 | |
240 | return retval; |
283 | return retval; |
241 | } |
284 | } |
242 | |
285 | |
243 | static unsigned int get_npending () |
286 | static unsigned int get_npending () |
244 | { |
287 | { |
245 | unsigned int retval; |
288 | unsigned int retval; |
246 | |
289 | |
247 | LOCK (reslock); |
290 | X_LOCK (reslock); |
248 | retval = npending; |
291 | retval = npending; |
249 | UNLOCK (reslock); |
292 | X_UNLOCK (reslock); |
250 | |
293 | |
251 | return retval; |
294 | return retval; |
252 | } |
295 | } |
253 | |
296 | |
254 | static unsigned int get_nthreads () |
297 | static unsigned int get_nthreads () |
255 | { |
298 | { |
256 | unsigned int retval; |
299 | unsigned int retval; |
257 | |
300 | |
258 | LOCK (wrklock); |
301 | X_LOCK (wrklock); |
259 | retval = started; |
302 | retval = started; |
260 | UNLOCK (wrklock); |
303 | X_UNLOCK (wrklock); |
261 | |
304 | |
262 | return retval; |
305 | return retval; |
263 | } |
306 | } |
264 | |
307 | |
265 | #else |
308 | #else |
… | |
… | |
582 | req->flags |= FLAG_CANCELLED; |
625 | req->flags |= FLAG_CANCELLED; |
583 | |
626 | |
584 | req_cancel_subs (req); |
627 | req_cancel_subs (req); |
585 | } |
628 | } |
586 | |
629 | |
587 | static void *aio_proc (void *arg); |
630 | X_THREAD_PROC (aio_proc); |
588 | |
631 | |
589 | static void start_thread (void) |
632 | static void start_thread (void) |
590 | { |
633 | { |
591 | worker *wrk = calloc (1, sizeof (worker)); |
634 | worker *wrk = calloc (1, sizeof (worker)); |
592 | |
635 | |
593 | if (!wrk) |
636 | if (!wrk) |
594 | croak ("unable to allocate worker thread data"); |
637 | croak ("unable to allocate worker thread data"); |
595 | |
638 | |
596 | LOCK (wrklock); |
639 | X_LOCK (wrklock); |
597 | |
640 | |
598 | if (thread_create (&wrk->tid, aio_proc, (void *)wrk)) |
641 | if (thread_create (&wrk->tid, aio_proc, (void *)wrk)) |
599 | { |
642 | { |
600 | wrk->prev = &wrk_first; |
643 | wrk->prev = &wrk_first; |
601 | wrk->next = wrk_first.next; |
644 | wrk->next = wrk_first.next; |
… | |
… | |
604 | ++started; |
647 | ++started; |
605 | } |
648 | } |
606 | else |
649 | else |
607 | free (wrk); |
650 | free (wrk); |
608 | |
651 | |
609 | UNLOCK (wrklock); |
652 | X_UNLOCK (wrklock); |
610 | } |
653 | } |
611 | |
654 | |
612 | static void maybe_start_thread () |
655 | static void maybe_start_thread () |
613 | { |
656 | { |
614 | if (get_nthreads () >= wanted) |
657 | if (get_nthreads () >= wanted) |
… | |
… | |
625 | { |
668 | { |
626 | block_sig (); |
669 | block_sig (); |
627 | |
670 | |
628 | ++nreqs; |
671 | ++nreqs; |
629 | |
672 | |
630 | LOCK (reqlock); |
673 | X_LOCK (reqlock); |
631 | ++nready; |
674 | ++nready; |
632 | reqq_push (&req_queue, req); |
675 | reqq_push (&req_queue, req); |
633 | COND_SIGNAL (reqwait); |
676 | X_COND_SIGNAL (reqwait); |
634 | UNLOCK (reqlock); |
677 | X_UNLOCK (reqlock); |
635 | |
678 | |
636 | unblock_sig (); |
679 | unblock_sig (); |
637 | |
680 | |
638 | maybe_start_thread (); |
681 | maybe_start_thread (); |
639 | } |
682 | } |
… | |
… | |
645 | Newz (0, req, 1, aio_cb); |
688 | Newz (0, req, 1, aio_cb); |
646 | |
689 | |
647 | req->type = REQ_QUIT; |
690 | req->type = REQ_QUIT; |
648 | req->pri = PRI_MAX + PRI_BIAS; |
691 | req->pri = PRI_MAX + PRI_BIAS; |
649 | |
692 | |
650 | LOCK (reqlock); |
693 | X_LOCK (reqlock); |
651 | reqq_push (&req_queue, req); |
694 | reqq_push (&req_queue, req); |
652 | COND_SIGNAL (reqwait); |
695 | X_COND_SIGNAL (reqwait); |
653 | UNLOCK (reqlock); |
696 | X_UNLOCK (reqlock); |
654 | |
697 | |
655 | LOCK (wrklock); |
698 | X_LOCK (wrklock); |
656 | --started; |
699 | --started; |
657 | UNLOCK (wrklock); |
700 | X_UNLOCK (wrklock); |
658 | } |
701 | } |
659 | |
702 | |
660 | static void set_max_idle (int nthreads) |
703 | static void set_max_idle (int nthreads) |
661 | { |
704 | { |
662 | if (WORDACCESS_UNSAFE) LOCK (reqlock); |
705 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
663 | max_idle = nthreads <= 0 ? 1 : nthreads; |
706 | max_idle = nthreads <= 0 ? 1 : nthreads; |
664 | if (WORDACCESS_UNSAFE) UNLOCK (reqlock); |
707 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
665 | } |
708 | } |
666 | |
709 | |
667 | static void min_parallel (int nthreads) |
710 | static void min_parallel (int nthreads) |
668 | { |
711 | { |
669 | if (wanted < nthreads) |
712 | if (wanted < nthreads) |
… | |
… | |
684 | fd_set rfd; |
727 | fd_set rfd; |
685 | |
728 | |
686 | while (nreqs) |
729 | while (nreqs) |
687 | { |
730 | { |
688 | int size; |
731 | int size; |
689 | if (WORDACCESS_UNSAFE) LOCK (reslock); |
732 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
690 | size = res_queue.size; |
733 | size = res_queue.size; |
691 | if (WORDACCESS_UNSAFE) UNLOCK (reslock); |
734 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
692 | |
735 | |
693 | if (size) |
736 | if (size) |
694 | return; |
737 | return; |
695 | |
738 | |
696 | maybe_start_thread (); |
739 | maybe_start_thread (); |
… | |
… | |
720 | { |
763 | { |
721 | for (;;) |
764 | for (;;) |
722 | { |
765 | { |
723 | maybe_start_thread (); |
766 | maybe_start_thread (); |
724 | |
767 | |
725 | LOCK (reslock); |
768 | X_LOCK (reslock); |
726 | req = reqq_shift (&res_queue); |
769 | req = reqq_shift (&res_queue); |
727 | |
770 | |
728 | if (req) |
771 | if (req) |
729 | { |
772 | { |
730 | --npending; |
773 | --npending; |
… | |
… | |
736 | while (read (respipe [0], buf, 4) == 4) |
779 | while (read (respipe [0], buf, 4) == 4) |
737 | ; |
780 | ; |
738 | } |
781 | } |
739 | } |
782 | } |
740 | |
783 | |
741 | UNLOCK (reslock); |
784 | X_UNLOCK (reslock); |
742 | |
785 | |
743 | if (!req) |
786 | if (!req) |
744 | break; |
787 | break; |
745 | |
788 | |
746 | --nreqs; |
789 | --nreqs; |
… | |
… | |
786 | |
829 | |
787 | unblock_sig (); |
830 | unblock_sig (); |
788 | return count; |
831 | return count; |
789 | } |
832 | } |
790 | |
833 | |
791 | static 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 | /*****************************************************************************/ |
834 | /*****************************************************************************/ |
804 | /* work around various missing functions */ |
835 | /* work around various missing functions */ |
805 | |
836 | |
806 | #if !HAVE_PREADWRITE |
837 | #if !HAVE_PREADWRITE |
807 | # define pread aio_pread |
838 | # define pread aio_pread |
… | |
… | |
810 | /* |
841 | /* |
811 | * make our pread/pwrite safe against themselves, but not against |
842 | * make our pread/pwrite safe against themselves, but not against |
812 | * normal read/write by using a mutex. slows down execution a lot, |
843 | * normal read/write by using a mutex. slows down execution a lot, |
813 | * but that's your problem, not mine. |
844 | * but that's your problem, not mine. |
814 | */ |
845 | */ |
815 | static mutex_t preadwritelock = MUTEX_INIT; |
846 | static mutex_t preadwritelock = X_MUTEX_INIT; |
816 | |
847 | |
817 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
848 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
818 | { |
849 | { |
819 | ssize_t res; |
850 | ssize_t res; |
820 | off_t ooffset; |
851 | off_t ooffset; |
821 | |
852 | |
822 | LOCK (preadwritelock); |
853 | X_LOCK (preadwritelock); |
823 | ooffset = lseek (fd, 0, SEEK_CUR); |
854 | ooffset = lseek (fd, 0, SEEK_CUR); |
824 | lseek (fd, offset, SEEK_SET); |
855 | lseek (fd, offset, SEEK_SET); |
825 | res = read (fd, buf, count); |
856 | res = read (fd, buf, count); |
826 | lseek (fd, ooffset, SEEK_SET); |
857 | lseek (fd, ooffset, SEEK_SET); |
827 | UNLOCK (preadwritelock); |
858 | X_UNLOCK (preadwritelock); |
828 | |
859 | |
829 | return res; |
860 | return res; |
830 | } |
861 | } |
831 | |
862 | |
832 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
863 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
833 | { |
864 | { |
834 | ssize_t res; |
865 | ssize_t res; |
835 | off_t ooffset; |
866 | off_t ooffset; |
836 | |
867 | |
837 | LOCK (preadwritelock); |
868 | X_LOCK (preadwritelock); |
838 | ooffset = lseek (fd, 0, SEEK_CUR); |
869 | ooffset = lseek (fd, 0, SEEK_CUR); |
839 | lseek (fd, offset, SEEK_SET); |
870 | lseek (fd, offset, SEEK_SET); |
840 | res = write (fd, buf, count); |
871 | res = write (fd, buf, count); |
841 | lseek (fd, offset, SEEK_SET); |
872 | lseek (fd, offset, SEEK_SET); |
842 | UNLOCK (preadwritelock); |
873 | X_UNLOCK (preadwritelock); |
843 | |
874 | |
844 | return res; |
875 | return res; |
845 | } |
876 | } |
846 | #endif |
877 | #endif |
847 | |
878 | |
… | |
… | |
880 | #if !HAVE_READAHEAD |
911 | #if !HAVE_READAHEAD |
881 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
912 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
882 | |
913 | |
883 | static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) |
914 | static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) |
884 | { |
915 | { |
|
|
916 | size_t todo = count; |
885 | dBUF; |
917 | dBUF; |
886 | |
918 | |
887 | while (count > 0) |
919 | while (todo > 0) |
888 | { |
920 | { |
889 | size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; |
921 | size_t len = todo < AIO_BUFSIZE ? todo : AIO_BUFSIZE; |
890 | |
922 | |
891 | pread (fd, aio_buf, len, offset); |
923 | pread (fd, aio_buf, len, offset); |
892 | offset += len; |
924 | offset += len; |
893 | count -= len; |
925 | todo -= len; |
894 | } |
926 | } |
895 | |
927 | |
896 | errno = 0; |
928 | errno = 0; |
|
|
929 | return count; |
897 | } |
930 | } |
898 | |
931 | |
899 | #endif |
932 | #endif |
900 | |
933 | |
901 | #if !HAVE_READDIR_R |
934 | #if !HAVE_READDIR_R |
902 | # define readdir_r aio_readdir_r |
935 | # define readdir_r aio_readdir_r |
903 | |
936 | |
904 | static mutex_t readdirlock = MUTEX_INIT; |
937 | static mutex_t readdirlock = X_MUTEX_INIT; |
905 | |
938 | |
906 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
939 | static int readdir_r (DIR *dirp, X_DIRENT *ent, X_DIRENT **res) |
907 | { |
940 | { |
908 | struct dirent *e; |
941 | X_DIRENT *e; |
909 | int errorno; |
942 | int errorno; |
910 | |
943 | |
911 | LOCK (readdirlock); |
944 | X_LOCK (readdirlock); |
912 | |
945 | |
913 | e = readdir (dirp); |
946 | e = readdir (dirp); |
914 | errorno = errno; |
947 | errorno = errno; |
915 | |
948 | |
916 | if (e) |
949 | if (e) |
… | |
… | |
919 | strcpy (ent->d_name, e->d_name); |
952 | strcpy (ent->d_name, e->d_name); |
920 | } |
953 | } |
921 | else |
954 | else |
922 | *res = 0; |
955 | *res = 0; |
923 | |
956 | |
924 | UNLOCK (readdirlock); |
957 | X_UNLOCK (readdirlock); |
925 | |
958 | |
926 | errno = errorno; |
959 | errno = errorno; |
927 | return e ? 0 : -1; |
960 | return e ? 0 : -1; |
928 | } |
961 | } |
929 | #endif |
962 | #endif |
… | |
… | |
1025 | static void scandir_ (aio_req req, worker *self) |
1058 | static void scandir_ (aio_req req, worker *self) |
1026 | { |
1059 | { |
1027 | DIR *dirp; |
1060 | DIR *dirp; |
1028 | union |
1061 | union |
1029 | { |
1062 | { |
1030 | struct dirent d; |
1063 | X_DIRENT d; |
1031 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
1064 | char b [offsetof (X_DIRENT, d_name) + NAME_MAX + 1]; |
1032 | } *u; |
1065 | } *u; |
1033 | struct dirent *entp; |
1066 | X_DIRENT *entp; |
1034 | char *name, *names; |
1067 | char *name, *names; |
1035 | int memlen = 4096; |
1068 | int memlen = 4096; |
1036 | int memofs = 0; |
1069 | int memofs = 0; |
1037 | int res = 0; |
1070 | int res = 0; |
1038 | int errorno; |
|
|
1039 | |
1071 | |
1040 | LOCK (wrklock); |
1072 | X_LOCK (wrklock); |
1041 | self->dirp = dirp = opendir (req->ptr1); |
1073 | self->dirp = dirp = opendir (req->ptr1); |
1042 | self->dbuf = u = malloc (sizeof (*u)); |
1074 | self->dbuf = u = malloc (sizeof (*u)); |
1043 | req->flags |= FLAG_PTR2_FREE; |
1075 | req->flags |= FLAG_PTR2_FREE; |
1044 | req->ptr2 = names = malloc (memlen); |
1076 | req->ptr2 = names = malloc (memlen); |
1045 | UNLOCK (wrklock); |
1077 | X_UNLOCK (wrklock); |
1046 | |
1078 | |
1047 | if (dirp && u && names) |
1079 | if (dirp && u && names) |
1048 | for (;;) |
1080 | for (;;) |
1049 | { |
1081 | { |
1050 | errno = 0; |
1082 | errno = 0; |
… | |
… | |
1062 | res++; |
1094 | res++; |
1063 | |
1095 | |
1064 | while (memofs + len > memlen) |
1096 | while (memofs + len > memlen) |
1065 | { |
1097 | { |
1066 | memlen *= 2; |
1098 | memlen *= 2; |
1067 | LOCK (wrklock); |
1099 | X_LOCK (wrklock); |
1068 | req->ptr2 = names = realloc (names, memlen); |
1100 | req->ptr2 = names = realloc (names, memlen); |
1069 | UNLOCK (wrklock); |
1101 | X_UNLOCK (wrklock); |
1070 | |
1102 | |
1071 | if (!names) |
1103 | if (!names) |
1072 | break; |
1104 | break; |
1073 | } |
1105 | } |
1074 | |
1106 | |
… | |
… | |
1083 | req->result = res; |
1115 | req->result = res; |
1084 | } |
1116 | } |
1085 | |
1117 | |
1086 | /*****************************************************************************/ |
1118 | /*****************************************************************************/ |
1087 | |
1119 | |
1088 | static void *aio_proc (void *thr_arg) |
1120 | X_THREAD_PROC (aio_proc) |
1089 | { |
1121 | { |
|
|
1122 | {//D |
1090 | aio_req req; |
1123 | aio_req req; |
1091 | struct timespec ts; |
1124 | struct timespec ts; |
1092 | worker *self = (worker *)thr_arg; |
1125 | worker *self = (worker *)thr_arg; |
1093 | |
1126 | |
1094 | /* try to distribute timeouts somewhat evenly */ |
1127 | /* try to distribute timeouts somewhat randomly */ |
1095 | ts.tv_nsec = (((unsigned long)self + (unsigned long)ts.tv_sec) & 1023UL) |
1128 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
1096 | * (1000000000UL / 1024UL); |
|
|
1097 | |
1129 | |
1098 | for (;;) |
1130 | for (;;) |
1099 | { |
1131 | { |
1100 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1132 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1101 | |
1133 | |
1102 | LOCK (reqlock); |
1134 | X_LOCK (reqlock); |
1103 | |
1135 | |
1104 | for (;;) |
1136 | for (;;) |
1105 | { |
1137 | { |
1106 | self->req = req = reqq_shift (&req_queue); |
1138 | self->req = req = reqq_shift (&req_queue); |
1107 | |
1139 | |
1108 | if (req) |
1140 | if (req) |
1109 | break; |
1141 | break; |
1110 | |
1142 | |
1111 | ++idle; |
1143 | ++idle; |
1112 | |
1144 | |
1113 | if (COND_TIMEDWAIT (reqwait, reqlock, ts) |
1145 | if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) |
1114 | == ETIMEDOUT) |
1146 | == ETIMEDOUT) |
1115 | { |
1147 | { |
1116 | if (idle > max_idle) |
1148 | if (idle > max_idle) |
1117 | { |
1149 | { |
1118 | --idle; |
1150 | --idle; |
1119 | UNLOCK (reqlock); |
1151 | X_UNLOCK (reqlock); |
1120 | LOCK (wrklock); |
1152 | X_LOCK (wrklock); |
1121 | --started; |
1153 | --started; |
1122 | UNLOCK (wrklock); |
1154 | X_UNLOCK (wrklock); |
1123 | goto quit; |
1155 | goto quit; |
1124 | } |
1156 | } |
1125 | |
1157 | |
1126 | /* we are allowed to idle, so do so without any timeout */ |
1158 | /* we are allowed to idle, so do so without any timeout */ |
1127 | COND_WAIT (reqwait, reqlock); |
1159 | X_COND_WAIT (reqwait, reqlock); |
1128 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1160 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1129 | } |
1161 | } |
1130 | |
1162 | |
1131 | --idle; |
1163 | --idle; |
1132 | } |
1164 | } |
1133 | |
1165 | |
1134 | --nready; |
1166 | --nready; |
1135 | |
1167 | |
1136 | UNLOCK (reqlock); |
1168 | X_UNLOCK (reqlock); |
1137 | |
1169 | |
1138 | errno = 0; /* strictly unnecessary */ |
1170 | errno = 0; /* strictly unnecessary */ |
1139 | |
1171 | |
1140 | if (!(req->flags & FLAG_CANCELLED)) |
1172 | if (!(req->flags & FLAG_CANCELLED)) |
1141 | switch (req->type) |
1173 | switch (req->type) |
… | |
… | |
1156 | |
1188 | |
1157 | case REQ_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break; |
1189 | 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; |
1190 | 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; |
1191 | case REQ_CHMOD: req->result = chmod (req->ptr1, req->mode); break; |
1160 | case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break; |
1192 | case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break; |
|
|
1193 | case REQ_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break; |
|
|
1194 | case REQ_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; |
1161 | |
1195 | |
1162 | case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break; |
1196 | case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break; |
1163 | case REQ_CLOSE: req->result = close (req->int1); break; |
1197 | case REQ_CLOSE: req->result = close (req->int1); break; |
1164 | case REQ_UNLINK: req->result = unlink (req->ptr1); break; |
1198 | case REQ_UNLINK: req->result = unlink (req->ptr1); break; |
1165 | case REQ_RMDIR: req->result = rmdir (req->ptr1); break; |
1199 | case REQ_RMDIR: req->result = rmdir (req->ptr1); break; |
… | |
… | |
1173 | case REQ_FDATASYNC: req->result = fdatasync (req->int1); break; |
1207 | case REQ_FDATASYNC: req->result = fdatasync (req->int1); break; |
1174 | case REQ_FSYNC: req->result = fsync (req->int1); break; |
1208 | case REQ_FSYNC: req->result = fsync (req->int1); break; |
1175 | case REQ_READDIR: scandir_ (req, self); break; |
1209 | case REQ_READDIR: scandir_ (req, self); break; |
1176 | |
1210 | |
1177 | case REQ_BUSY: |
1211 | case REQ_BUSY: |
|
|
1212 | #ifdef _WIN32 |
|
|
1213 | Sleep (req->nv1 * 1000.); |
|
|
1214 | #else |
1178 | { |
1215 | { |
1179 | struct timeval tv; |
1216 | struct timeval tv; |
1180 | |
1217 | |
1181 | tv.tv_sec = req->nv1; |
1218 | tv.tv_sec = req->nv1; |
1182 | tv.tv_usec = (req->nv1 - tv.tv_usec) * 1000000.; |
1219 | tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.; |
1183 | |
1220 | |
1184 | req->result = select (0, 0, 0, 0, &tv); |
1221 | req->result = select (0, 0, 0, 0, &tv); |
1185 | } |
1222 | } |
|
|
1223 | #endif |
|
|
1224 | break; |
1186 | |
1225 | |
1187 | case REQ_UTIME: |
1226 | case REQ_UTIME: |
1188 | case REQ_FUTIME: |
1227 | case REQ_FUTIME: |
1189 | { |
1228 | { |
1190 | struct timeval tv[2]; |
1229 | struct timeval tv[2]; |
… | |
… | |
1220 | break; |
1259 | break; |
1221 | } |
1260 | } |
1222 | |
1261 | |
1223 | req->errorno = errno; |
1262 | req->errorno = errno; |
1224 | |
1263 | |
1225 | LOCK (reslock); |
1264 | X_LOCK (reslock); |
1226 | |
1265 | |
1227 | ++npending; |
1266 | ++npending; |
1228 | |
1267 | |
1229 | if (!reqq_push (&res_queue, req)) |
1268 | if (!reqq_push (&res_queue, req)) |
1230 | { |
1269 | { |
… | |
… | |
1237 | } |
1276 | } |
1238 | |
1277 | |
1239 | self->req = 0; |
1278 | self->req = 0; |
1240 | worker_clear (self); |
1279 | worker_clear (self); |
1241 | |
1280 | |
1242 | UNLOCK (reslock); |
1281 | X_UNLOCK (reslock); |
1243 | } |
1282 | } |
1244 | |
1283 | |
1245 | quit: |
1284 | quit: |
1246 | LOCK (wrklock); |
1285 | X_LOCK (wrklock); |
1247 | worker_free (self); |
1286 | worker_free (self); |
1248 | UNLOCK (wrklock); |
1287 | X_UNLOCK (wrklock); |
1249 | |
1288 | |
1250 | return 0; |
1289 | return 0; |
|
|
1290 | }//D |
1251 | } |
1291 | } |
1252 | |
1292 | |
1253 | /*****************************************************************************/ |
1293 | /*****************************************************************************/ |
1254 | |
1294 | |
1255 | static void atfork_prepare (void) |
1295 | static void atfork_prepare (void) |
1256 | { |
1296 | { |
1257 | LOCK (wrklock); |
1297 | X_LOCK (wrklock); |
1258 | LOCK (reqlock); |
1298 | X_LOCK (reqlock); |
1259 | LOCK (reslock); |
1299 | X_LOCK (reslock); |
1260 | #if !HAVE_PREADWRITE |
1300 | #if !HAVE_PREADWRITE |
1261 | LOCK (preadwritelock); |
1301 | X_LOCK (preadwritelock); |
1262 | #endif |
1302 | #endif |
1263 | #if !HAVE_READDIR_R |
1303 | #if !HAVE_READDIR_R |
1264 | LOCK (readdirlock); |
1304 | X_LOCK (readdirlock); |
1265 | #endif |
1305 | #endif |
1266 | } |
1306 | } |
1267 | |
1307 | |
1268 | static void atfork_parent (void) |
1308 | static void atfork_parent (void) |
1269 | { |
1309 | { |
1270 | #if !HAVE_READDIR_R |
1310 | #if !HAVE_READDIR_R |
1271 | UNLOCK (readdirlock); |
1311 | X_UNLOCK (readdirlock); |
1272 | #endif |
1312 | #endif |
1273 | #if !HAVE_PREADWRITE |
1313 | #if !HAVE_PREADWRITE |
1274 | UNLOCK (preadwritelock); |
1314 | X_UNLOCK (preadwritelock); |
1275 | #endif |
1315 | #endif |
1276 | UNLOCK (reslock); |
1316 | X_UNLOCK (reslock); |
1277 | UNLOCK (reqlock); |
1317 | X_UNLOCK (reqlock); |
1278 | UNLOCK (wrklock); |
1318 | X_UNLOCK (wrklock); |
1279 | } |
1319 | } |
1280 | |
1320 | |
1281 | static void atfork_child (void) |
1321 | static void atfork_child (void) |
1282 | { |
1322 | { |
1283 | aio_req prv; |
1323 | aio_req prv; |
… | |
… | |
1305 | nready = 0; |
1345 | nready = 0; |
1306 | npending = 0; |
1346 | npending = 0; |
1307 | |
1347 | |
1308 | close (respipe [0]); |
1348 | close (respipe [0]); |
1309 | close (respipe [1]); |
1349 | close (respipe [1]); |
1310 | create_pipe (); |
1350 | |
|
|
1351 | if (!create_pipe (respipe)) |
|
|
1352 | croak ("cannot set result pipe to nonblocking mode"); |
1311 | |
1353 | |
1312 | atfork_parent (); |
1354 | atfork_parent (); |
1313 | } |
1355 | } |
1314 | |
1356 | |
1315 | #define dREQ \ |
1357 | #define dREQ \ |
… | |
… | |
1344 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
1386 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
1345 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
1387 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
1346 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
1388 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
1347 | newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); |
1389 | newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); |
1348 | newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); |
1390 | newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); |
|
|
1391 | #ifdef _WIN32 |
|
|
1392 | X_MUTEX_CHECK (wrklock); |
|
|
1393 | X_MUTEX_CHECK (reslock); |
|
|
1394 | X_MUTEX_CHECK (reqlock); |
|
|
1395 | X_MUTEX_CHECK (reqwait); |
|
|
1396 | X_MUTEX_CHECK (preadwritelock); |
|
|
1397 | X_MUTEX_CHECK (readdirlock); |
|
|
1398 | #else |
1349 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
1399 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
1350 | newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); |
1400 | newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); |
|
|
1401 | #endif |
1351 | |
1402 | |
1352 | create_pipe (); |
1403 | if (!create_pipe (respipe)) |
|
|
1404 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
1405 | |
1353 | ATFORK (atfork_prepare, atfork_parent, atfork_child); |
1406 | X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child); |
1354 | } |
1407 | } |
1355 | |
1408 | |
1356 | void |
1409 | void |
1357 | max_poll_reqs (int nreqs) |
1410 | max_poll_reqs (int nreqs) |
1358 | PROTOTYPE: $ |
1411 | PROTOTYPE: $ |
… | |
… | |
1584 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1637 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1585 | } |
1638 | } |
1586 | else |
1639 | else |
1587 | { |
1640 | { |
1588 | req->type = REQ_FUTIME; |
1641 | req->type = REQ_FUTIME; |
|
|
1642 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
1643 | } |
|
|
1644 | |
|
|
1645 | REQ_SEND; |
|
|
1646 | } |
|
|
1647 | |
|
|
1648 | void |
|
|
1649 | aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef) |
|
|
1650 | PPCODE: |
|
|
1651 | { |
|
|
1652 | dREQ; |
|
|
1653 | |
|
|
1654 | req->sv1 = newSVsv (fh_or_path); |
|
|
1655 | req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; |
|
|
1656 | |
|
|
1657 | if (SvPOK (fh_or_path)) |
|
|
1658 | { |
|
|
1659 | req->type = REQ_TRUNCATE; |
|
|
1660 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1661 | } |
|
|
1662 | else |
|
|
1663 | { |
|
|
1664 | req->type = REQ_FTRUNCATE; |
1589 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1665 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1590 | } |
1666 | } |
1591 | |
1667 | |
1592 | REQ_SEND; |
1668 | REQ_SEND; |
1593 | } |
1669 | } |
… | |
… | |
1809 | CODE: |
1885 | CODE: |
1810 | { |
1886 | { |
1811 | if (block_sig_level) |
1887 | if (block_sig_level) |
1812 | croak ("cannot call IO::AIO::setsig from within aio_block/callback"); |
1888 | croak ("cannot call IO::AIO::setsig from within aio_block/callback"); |
1813 | |
1889 | |
1814 | LOCK (reslock); |
1890 | X_LOCK (reslock); |
1815 | main_tid = pthread_self (); |
1891 | main_tid = pthread_self (); |
1816 | main_sig = signum; |
1892 | main_sig = signum; |
1817 | UNLOCK (reslock); |
1893 | X_UNLOCK (reslock); |
1818 | |
1894 | |
1819 | if (main_sig && npending) |
1895 | if (main_sig && npending) |
1820 | pthread_kill (main_tid, main_sig); |
1896 | pthread_kill (main_tid, main_sig); |
1821 | } |
1897 | } |
1822 | |
1898 | |
… | |
… | |
1866 | |
1942 | |
1867 | int |
1943 | int |
1868 | nthreads() |
1944 | nthreads() |
1869 | PROTOTYPE: |
1945 | PROTOTYPE: |
1870 | CODE: |
1946 | CODE: |
1871 | if (WORDACCESS_UNSAFE) LOCK (wrklock); |
1947 | if (WORDACCESS_UNSAFE) X_LOCK (wrklock); |
1872 | RETVAL = started; |
1948 | RETVAL = started; |
1873 | if (WORDACCESS_UNSAFE) UNLOCK (wrklock); |
1949 | if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock); |
1874 | OUTPUT: |
1950 | OUTPUT: |
1875 | RETVAL |
1951 | RETVAL |
1876 | |
1952 | |
1877 | PROTOTYPES: DISABLE |
1953 | PROTOTYPES: DISABLE |
1878 | |
1954 | |