… | |
… | |
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 | |
66 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
110 | typedef 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, |
… | |
… | |
177 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
222 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
178 | |
223 | |
179 | static unsigned int started, idle, wanted; |
224 | static unsigned int started, idle, wanted; |
180 | |
225 | |
181 | /* worker threads management */ |
226 | /* worker threads management */ |
182 | static mutex_t wrklock = MUTEX_INIT; |
227 | static mutex_t wrklock = X_MUTEX_INIT; |
183 | |
228 | |
184 | typedef struct worker { |
229 | typedef 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 | |
221 | static volatile unsigned int nreqs, nready, npending; |
266 | static volatile unsigned int nreqs, nready, npending; |
222 | static volatile unsigned int max_idle = 4; |
267 | static volatile unsigned int max_idle = 4; |
223 | static volatile unsigned int max_outstanding = 0xffffffff; |
268 | static volatile unsigned int max_outstanding = 0xffffffff; |
224 | static int respipe [2]; |
269 | static int respipe_osf [2], respipe [2] = { -1, -1 }; |
225 | |
270 | |
226 | static mutex_t reslock = MUTEX_INIT; |
271 | static mutex_t reslock = X_MUTEX_INIT; |
227 | static mutex_t reqlock = MUTEX_INIT; |
272 | static mutex_t reqlock = X_MUTEX_INIT; |
228 | static cond_t reqwait = COND_INIT; |
273 | static cond_t reqwait = X_COND_INIT; |
229 | |
274 | |
230 | #if WORDACCESS_UNSAFE |
275 | #if WORDACCESS_UNSAFE |
231 | |
276 | |
232 | static unsigned int get_nready () |
277 | static 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 | |
243 | static unsigned int get_npending () |
288 | static 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 | |
254 | static unsigned int get_nthreads () |
299 | static 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 | |
587 | static 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 | |
|
|
638 | static void |
|
|
639 | create_respipe () |
|
|
640 | { |
|
|
641 | int old_readfd = respipe [0]; |
|
|
642 | |
|
|
643 | if (respipe [1] >= 0) |
|
|
644 | respipe_close (TO_SOCKET (respipe [1])); |
|
|
645 | |
|
|
646 | #ifdef _WIN32 |
|
|
647 | if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe)) |
|
|
648 | #else |
|
|
649 | if (pipe (respipe)) |
|
|
650 | #endif |
|
|
651 | croak ("unable to initialize result pipe"); |
|
|
652 | |
|
|
653 | if (old_readfd >= 0) |
|
|
654 | { |
|
|
655 | if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0) |
|
|
656 | croak ("unable to initialize result pipe(2)"); |
|
|
657 | |
|
|
658 | respipe_close (respipe [0]); |
|
|
659 | respipe [0] = old_readfd; |
|
|
660 | } |
|
|
661 | |
|
|
662 | #ifdef _WIN32 |
|
|
663 | int arg = 1; |
|
|
664 | if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg) |
|
|
665 | || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg)) |
|
|
666 | #else |
|
|
667 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK) |
|
|
668 | || fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
669 | #endif |
|
|
670 | croak ("unable to initialize result pipe(3)"); |
|
|
671 | |
|
|
672 | respipe_osf [0] = TO_SOCKET (respipe [0]); |
|
|
673 | respipe_osf [1] = TO_SOCKET (respipe [1]); |
|
|
674 | } |
|
|
675 | |
|
|
676 | X_THREAD_PROC (aio_proc); |
588 | |
677 | |
589 | static void start_thread (void) |
678 | static void start_thread (void) |
590 | { |
679 | { |
591 | worker *wrk = calloc (1, sizeof (worker)); |
680 | worker *wrk = calloc (1, sizeof (worker)); |
592 | |
681 | |
593 | if (!wrk) |
682 | if (!wrk) |
594 | croak ("unable to allocate worker thread data"); |
683 | croak ("unable to allocate worker thread data"); |
595 | |
684 | |
596 | LOCK (wrklock); |
685 | X_LOCK (wrklock); |
597 | |
686 | |
598 | if (thread_create (&wrk->tid, aio_proc, (void *)wrk)) |
687 | if (thread_create (&wrk->tid, aio_proc, (void *)wrk)) |
599 | { |
688 | { |
600 | wrk->prev = &wrk_first; |
689 | wrk->prev = &wrk_first; |
601 | wrk->next = wrk_first.next; |
690 | wrk->next = wrk_first.next; |
… | |
… | |
604 | ++started; |
693 | ++started; |
605 | } |
694 | } |
606 | else |
695 | else |
607 | free (wrk); |
696 | free (wrk); |
608 | |
697 | |
609 | UNLOCK (wrklock); |
698 | X_UNLOCK (wrklock); |
610 | } |
699 | } |
611 | |
700 | |
612 | static void maybe_start_thread () |
701 | static void maybe_start_thread () |
613 | { |
702 | { |
614 | if (get_nthreads () >= wanted) |
703 | if (get_nthreads () >= wanted) |
… | |
… | |
625 | { |
714 | { |
626 | block_sig (); |
715 | block_sig (); |
627 | |
716 | |
628 | ++nreqs; |
717 | ++nreqs; |
629 | |
718 | |
630 | LOCK (reqlock); |
719 | X_LOCK (reqlock); |
631 | ++nready; |
720 | ++nready; |
632 | reqq_push (&req_queue, req); |
721 | reqq_push (&req_queue, req); |
633 | COND_SIGNAL (reqwait); |
722 | X_COND_SIGNAL (reqwait); |
634 | UNLOCK (reqlock); |
723 | X_UNLOCK (reqlock); |
635 | |
724 | |
636 | unblock_sig (); |
725 | unblock_sig (); |
637 | |
726 | |
638 | maybe_start_thread (); |
727 | maybe_start_thread (); |
639 | } |
728 | } |
… | |
… | |
645 | Newz (0, req, 1, aio_cb); |
734 | Newz (0, req, 1, aio_cb); |
646 | |
735 | |
647 | req->type = REQ_QUIT; |
736 | req->type = REQ_QUIT; |
648 | req->pri = PRI_MAX + PRI_BIAS; |
737 | req->pri = PRI_MAX + PRI_BIAS; |
649 | |
738 | |
650 | LOCK (reqlock); |
739 | X_LOCK (reqlock); |
651 | reqq_push (&req_queue, req); |
740 | reqq_push (&req_queue, req); |
652 | COND_SIGNAL (reqwait); |
741 | X_COND_SIGNAL (reqwait); |
653 | UNLOCK (reqlock); |
742 | X_UNLOCK (reqlock); |
654 | |
743 | |
655 | LOCK (wrklock); |
744 | X_LOCK (wrklock); |
656 | --started; |
745 | --started; |
657 | UNLOCK (wrklock); |
746 | X_UNLOCK (wrklock); |
658 | } |
747 | } |
659 | |
748 | |
660 | static void set_max_idle (int nthreads) |
749 | static void set_max_idle (int nthreads) |
661 | { |
750 | { |
662 | if (WORDACCESS_UNSAFE) LOCK (reqlock); |
751 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
663 | max_idle = nthreads <= 0 ? 1 : nthreads; |
752 | max_idle = nthreads <= 0 ? 1 : nthreads; |
664 | if (WORDACCESS_UNSAFE) UNLOCK (reqlock); |
753 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
665 | } |
754 | } |
666 | |
755 | |
667 | static void min_parallel (int nthreads) |
756 | static void min_parallel (int nthreads) |
668 | { |
757 | { |
669 | if (wanted < nthreads) |
758 | if (wanted < nthreads) |
… | |
… | |
684 | fd_set rfd; |
773 | fd_set rfd; |
685 | |
774 | |
686 | while (nreqs) |
775 | while (nreqs) |
687 | { |
776 | { |
688 | int size; |
777 | int size; |
689 | if (WORDACCESS_UNSAFE) LOCK (reslock); |
778 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
690 | size = res_queue.size; |
779 | size = res_queue.size; |
691 | if (WORDACCESS_UNSAFE) UNLOCK (reslock); |
780 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
692 | |
781 | |
693 | if (size) |
782 | if (size) |
694 | return; |
783 | return; |
695 | |
784 | |
696 | maybe_start_thread (); |
785 | maybe_start_thread (); |
697 | |
786 | |
698 | FD_ZERO(&rfd); |
787 | FD_ZERO (&rfd); |
699 | FD_SET(respipe [0], &rfd); |
788 | FD_SET (respipe [0], &rfd); |
700 | |
789 | |
701 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
790 | PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); |
702 | } |
791 | } |
703 | } |
792 | } |
704 | |
793 | |
705 | static int poll_cb () |
794 | static int poll_cb () |
706 | { |
795 | { |
… | |
… | |
720 | { |
809 | { |
721 | for (;;) |
810 | for (;;) |
722 | { |
811 | { |
723 | maybe_start_thread (); |
812 | maybe_start_thread (); |
724 | |
813 | |
725 | LOCK (reslock); |
814 | X_LOCK (reslock); |
726 | req = reqq_shift (&res_queue); |
815 | req = reqq_shift (&res_queue); |
727 | |
816 | |
728 | if (req) |
817 | if (req) |
729 | { |
818 | { |
730 | --npending; |
819 | --npending; |
731 | |
820 | |
732 | if (!res_queue.size) |
821 | if (!res_queue.size) |
733 | { |
822 | { |
734 | /* read any signals sent by the worker threads */ |
823 | /* read any signals sent by the worker threads */ |
735 | char buf [4]; |
824 | char buf [4]; |
736 | while (read (respipe [0], buf, 4) == 4) |
825 | while (respipe_read (respipe [0], buf, 4) == 4) |
737 | ; |
826 | ; |
738 | } |
827 | } |
739 | } |
828 | } |
740 | |
829 | |
741 | UNLOCK (reslock); |
830 | X_UNLOCK (reslock); |
742 | |
831 | |
743 | if (!req) |
832 | if (!req) |
744 | break; |
833 | break; |
745 | |
834 | |
746 | --nreqs; |
835 | --nreqs; |
… | |
… | |
786 | |
875 | |
787 | unblock_sig (); |
876 | unblock_sig (); |
788 | return count; |
877 | return count; |
789 | } |
878 | } |
790 | |
879 | |
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 | /*****************************************************************************/ |
880 | /*****************************************************************************/ |
804 | /* work around various missing functions */ |
881 | /* work around various missing functions */ |
805 | |
882 | |
806 | #if !HAVE_PREADWRITE |
883 | #if !HAVE_PREADWRITE |
807 | # define pread aio_pread |
884 | # define pread aio_pread |
… | |
… | |
810 | /* |
887 | /* |
811 | * make our pread/pwrite safe against themselves, but not against |
888 | * make our pread/pwrite safe against themselves, but not against |
812 | * normal read/write by using a mutex. slows down execution a lot, |
889 | * normal read/write by using a mutex. slows down execution a lot, |
813 | * but that's your problem, not mine. |
890 | * but that's your problem, not mine. |
814 | */ |
891 | */ |
815 | static mutex_t preadwritelock = MUTEX_INIT; |
892 | static mutex_t preadwritelock = X_MUTEX_INIT; |
816 | |
893 | |
817 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
894 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
818 | { |
895 | { |
819 | ssize_t res; |
896 | ssize_t res; |
820 | off_t ooffset; |
897 | off_t ooffset; |
821 | |
898 | |
822 | LOCK (preadwritelock); |
899 | X_LOCK (preadwritelock); |
823 | ooffset = lseek (fd, 0, SEEK_CUR); |
900 | ooffset = lseek (fd, 0, SEEK_CUR); |
824 | lseek (fd, offset, SEEK_SET); |
901 | lseek (fd, offset, SEEK_SET); |
825 | res = read (fd, buf, count); |
902 | res = read (fd, buf, count); |
826 | lseek (fd, ooffset, SEEK_SET); |
903 | lseek (fd, ooffset, SEEK_SET); |
827 | UNLOCK (preadwritelock); |
904 | X_UNLOCK (preadwritelock); |
828 | |
905 | |
829 | return res; |
906 | return res; |
830 | } |
907 | } |
831 | |
908 | |
832 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
909 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
833 | { |
910 | { |
834 | ssize_t res; |
911 | ssize_t res; |
835 | off_t ooffset; |
912 | off_t ooffset; |
836 | |
913 | |
837 | LOCK (preadwritelock); |
914 | X_LOCK (preadwritelock); |
838 | ooffset = lseek (fd, 0, SEEK_CUR); |
915 | ooffset = lseek (fd, 0, SEEK_CUR); |
839 | lseek (fd, offset, SEEK_SET); |
916 | lseek (fd, offset, SEEK_SET); |
840 | res = write (fd, buf, count); |
917 | res = write (fd, buf, count); |
841 | lseek (fd, offset, SEEK_SET); |
918 | lseek (fd, offset, SEEK_SET); |
842 | UNLOCK (preadwritelock); |
919 | X_UNLOCK (preadwritelock); |
843 | |
920 | |
844 | return res; |
921 | return res; |
845 | } |
922 | } |
846 | #endif |
923 | #endif |
847 | |
924 | |
… | |
… | |
880 | #if !HAVE_READAHEAD |
957 | #if !HAVE_READAHEAD |
881 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
958 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
882 | |
959 | |
883 | static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) |
960 | static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) |
884 | { |
961 | { |
|
|
962 | size_t todo = count; |
885 | dBUF; |
963 | dBUF; |
886 | |
964 | |
887 | while (count > 0) |
965 | while (todo > 0) |
888 | { |
966 | { |
889 | size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; |
967 | size_t len = todo < AIO_BUFSIZE ? todo : AIO_BUFSIZE; |
890 | |
968 | |
891 | pread (fd, aio_buf, len, offset); |
969 | pread (fd, aio_buf, len, offset); |
892 | offset += len; |
970 | offset += len; |
893 | count -= len; |
971 | todo -= len; |
894 | } |
972 | } |
895 | |
973 | |
896 | errno = 0; |
974 | errno = 0; |
|
|
975 | return count; |
897 | } |
976 | } |
898 | |
977 | |
899 | #endif |
978 | #endif |
900 | |
979 | |
901 | #if !HAVE_READDIR_R |
980 | #if !HAVE_READDIR_R |
902 | # define readdir_r aio_readdir_r |
981 | # define readdir_r aio_readdir_r |
903 | |
982 | |
904 | static mutex_t readdirlock = MUTEX_INIT; |
983 | static mutex_t readdirlock = X_MUTEX_INIT; |
905 | |
984 | |
906 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
985 | static int readdir_r (DIR *dirp, X_DIRENT *ent, X_DIRENT **res) |
907 | { |
986 | { |
908 | struct dirent *e; |
987 | X_DIRENT *e; |
909 | int errorno; |
988 | int errorno; |
910 | |
989 | |
911 | LOCK (readdirlock); |
990 | X_LOCK (readdirlock); |
912 | |
991 | |
913 | e = readdir (dirp); |
992 | e = readdir (dirp); |
914 | errorno = errno; |
993 | errorno = errno; |
915 | |
994 | |
916 | if (e) |
995 | if (e) |
… | |
… | |
919 | strcpy (ent->d_name, e->d_name); |
998 | strcpy (ent->d_name, e->d_name); |
920 | } |
999 | } |
921 | else |
1000 | else |
922 | *res = 0; |
1001 | *res = 0; |
923 | |
1002 | |
924 | UNLOCK (readdirlock); |
1003 | X_UNLOCK (readdirlock); |
925 | |
1004 | |
926 | errno = errorno; |
1005 | errno = errorno; |
927 | return e ? 0 : -1; |
1006 | return e ? 0 : -1; |
928 | } |
1007 | } |
929 | #endif |
1008 | #endif |
… | |
… | |
1025 | static void scandir_ (aio_req req, worker *self) |
1104 | static void scandir_ (aio_req req, worker *self) |
1026 | { |
1105 | { |
1027 | DIR *dirp; |
1106 | DIR *dirp; |
1028 | union |
1107 | union |
1029 | { |
1108 | { |
1030 | struct dirent d; |
1109 | X_DIRENT d; |
1031 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
1110 | char b [offsetof (X_DIRENT, d_name) + NAME_MAX + 1]; |
1032 | } *u; |
1111 | } *u; |
1033 | struct dirent *entp; |
1112 | X_DIRENT *entp; |
1034 | char *name, *names; |
1113 | char *name, *names; |
1035 | int memlen = 4096; |
1114 | int memlen = 4096; |
1036 | int memofs = 0; |
1115 | int memofs = 0; |
1037 | int res = 0; |
1116 | int res = 0; |
1038 | int errorno; |
|
|
1039 | |
1117 | |
1040 | LOCK (wrklock); |
1118 | X_LOCK (wrklock); |
1041 | self->dirp = dirp = opendir (req->ptr1); |
1119 | self->dirp = dirp = opendir (req->ptr1); |
1042 | self->dbuf = u = malloc (sizeof (*u)); |
1120 | self->dbuf = u = malloc (sizeof (*u)); |
1043 | req->flags |= FLAG_PTR2_FREE; |
1121 | req->flags |= FLAG_PTR2_FREE; |
1044 | req->ptr2 = names = malloc (memlen); |
1122 | req->ptr2 = names = malloc (memlen); |
1045 | UNLOCK (wrklock); |
1123 | X_UNLOCK (wrklock); |
1046 | |
1124 | |
1047 | if (dirp && u && names) |
1125 | if (dirp && u && names) |
1048 | for (;;) |
1126 | for (;;) |
1049 | { |
1127 | { |
1050 | errno = 0; |
1128 | errno = 0; |
… | |
… | |
1062 | res++; |
1140 | res++; |
1063 | |
1141 | |
1064 | while (memofs + len > memlen) |
1142 | while (memofs + len > memlen) |
1065 | { |
1143 | { |
1066 | memlen *= 2; |
1144 | memlen *= 2; |
1067 | LOCK (wrklock); |
1145 | X_LOCK (wrklock); |
1068 | req->ptr2 = names = realloc (names, memlen); |
1146 | req->ptr2 = names = realloc (names, memlen); |
1069 | UNLOCK (wrklock); |
1147 | X_UNLOCK (wrklock); |
1070 | |
1148 | |
1071 | if (!names) |
1149 | if (!names) |
1072 | break; |
1150 | break; |
1073 | } |
1151 | } |
1074 | |
1152 | |
… | |
… | |
1083 | req->result = res; |
1161 | req->result = res; |
1084 | } |
1162 | } |
1085 | |
1163 | |
1086 | /*****************************************************************************/ |
1164 | /*****************************************************************************/ |
1087 | |
1165 | |
1088 | static void *aio_proc (void *thr_arg) |
1166 | X_THREAD_PROC (aio_proc) |
1089 | { |
1167 | { |
|
|
1168 | {//D |
1090 | aio_req req; |
1169 | aio_req req; |
1091 | struct timespec ts; |
1170 | struct timespec ts; |
1092 | worker *self = (worker *)thr_arg; |
1171 | worker *self = (worker *)thr_arg; |
1093 | |
1172 | |
1094 | /* try to distribute timeouts somewhat evenly */ |
1173 | /* try to distribute timeouts somewhat randomly */ |
1095 | ts.tv_nsec = (((unsigned long)self + (unsigned long)ts.tv_sec) & 1023UL) |
1174 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
1096 | * (1000000000UL / 1024UL); |
|
|
1097 | |
1175 | |
1098 | for (;;) |
1176 | for (;;) |
1099 | { |
1177 | { |
1100 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1178 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1101 | |
1179 | |
1102 | LOCK (reqlock); |
1180 | X_LOCK (reqlock); |
1103 | |
1181 | |
1104 | for (;;) |
1182 | for (;;) |
1105 | { |
1183 | { |
1106 | self->req = req = reqq_shift (&req_queue); |
1184 | self->req = req = reqq_shift (&req_queue); |
1107 | |
1185 | |
1108 | if (req) |
1186 | if (req) |
1109 | break; |
1187 | break; |
1110 | |
1188 | |
1111 | ++idle; |
1189 | ++idle; |
1112 | |
1190 | |
1113 | if (COND_TIMEDWAIT (reqwait, reqlock, ts) |
1191 | if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) |
1114 | == ETIMEDOUT) |
1192 | == ETIMEDOUT) |
1115 | { |
1193 | { |
1116 | if (idle > max_idle) |
1194 | if (idle > max_idle) |
1117 | { |
1195 | { |
1118 | --idle; |
1196 | --idle; |
1119 | UNLOCK (reqlock); |
1197 | X_UNLOCK (reqlock); |
1120 | LOCK (wrklock); |
1198 | X_LOCK (wrklock); |
1121 | --started; |
1199 | --started; |
1122 | UNLOCK (wrklock); |
1200 | X_UNLOCK (wrklock); |
1123 | goto quit; |
1201 | goto quit; |
1124 | } |
1202 | } |
1125 | |
1203 | |
1126 | /* we are allowed to idle, so do so without any timeout */ |
1204 | /* we are allowed to idle, so do so without any timeout */ |
1127 | COND_WAIT (reqwait, reqlock); |
1205 | X_COND_WAIT (reqwait, reqlock); |
1128 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1206 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1129 | } |
1207 | } |
1130 | |
1208 | |
1131 | --idle; |
1209 | --idle; |
1132 | } |
1210 | } |
1133 | |
1211 | |
1134 | --nready; |
1212 | --nready; |
1135 | |
1213 | |
1136 | UNLOCK (reqlock); |
1214 | X_UNLOCK (reqlock); |
1137 | |
1215 | |
1138 | errno = 0; /* strictly unnecessary */ |
1216 | errno = 0; /* strictly unnecessary */ |
1139 | |
1217 | |
1140 | if (!(req->flags & FLAG_CANCELLED)) |
1218 | if (!(req->flags & FLAG_CANCELLED)) |
1141 | switch (req->type) |
1219 | switch (req->type) |
… | |
… | |
1156 | |
1234 | |
1157 | case REQ_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break; |
1235 | 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; |
1236 | 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; |
1237 | case REQ_CHMOD: req->result = chmod (req->ptr1, req->mode); break; |
1160 | case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break; |
1238 | case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break; |
|
|
1239 | case REQ_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break; |
|
|
1240 | case REQ_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; |
1161 | |
1241 | |
1162 | case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break; |
1242 | case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break; |
1163 | case REQ_CLOSE: req->result = close (req->int1); break; |
1243 | case REQ_CLOSE: req->result = close (req->int1); break; |
1164 | case REQ_UNLINK: req->result = unlink (req->ptr1); break; |
1244 | case REQ_UNLINK: req->result = unlink (req->ptr1); break; |
1165 | case REQ_RMDIR: req->result = rmdir (req->ptr1); break; |
1245 | case REQ_RMDIR: req->result = rmdir (req->ptr1); break; |
… | |
… | |
1173 | case REQ_FDATASYNC: req->result = fdatasync (req->int1); break; |
1253 | case REQ_FDATASYNC: req->result = fdatasync (req->int1); break; |
1174 | case REQ_FSYNC: req->result = fsync (req->int1); break; |
1254 | case REQ_FSYNC: req->result = fsync (req->int1); break; |
1175 | case REQ_READDIR: scandir_ (req, self); break; |
1255 | case REQ_READDIR: scandir_ (req, self); break; |
1176 | |
1256 | |
1177 | case REQ_BUSY: |
1257 | case REQ_BUSY: |
|
|
1258 | #ifdef _WIN32 |
|
|
1259 | Sleep (req->nv1 * 1000.); |
|
|
1260 | #else |
1178 | { |
1261 | { |
1179 | struct timeval tv; |
1262 | struct timeval tv; |
1180 | |
1263 | |
1181 | tv.tv_sec = req->nv1; |
1264 | tv.tv_sec = req->nv1; |
1182 | tv.tv_usec = (req->nv1 - tv.tv_usec) * 1000000.; |
1265 | tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.; |
1183 | |
1266 | |
1184 | req->result = select (0, 0, 0, 0, &tv); |
1267 | req->result = select (0, 0, 0, 0, &tv); |
1185 | } |
1268 | } |
|
|
1269 | #endif |
|
|
1270 | break; |
1186 | |
1271 | |
1187 | case REQ_UTIME: |
1272 | case REQ_UTIME: |
1188 | case REQ_FUTIME: |
1273 | case REQ_FUTIME: |
1189 | { |
1274 | { |
1190 | struct timeval tv[2]; |
1275 | struct timeval tv[2]; |
… | |
… | |
1220 | break; |
1305 | break; |
1221 | } |
1306 | } |
1222 | |
1307 | |
1223 | req->errorno = errno; |
1308 | req->errorno = errno; |
1224 | |
1309 | |
1225 | LOCK (reslock); |
1310 | X_LOCK (reslock); |
1226 | |
1311 | |
1227 | ++npending; |
1312 | ++npending; |
1228 | |
1313 | |
1229 | if (!reqq_push (&res_queue, req)) |
1314 | if (!reqq_push (&res_queue, req)) |
1230 | { |
1315 | { |
1231 | /* write a dummy byte to the pipe so fh becomes ready */ |
1316 | /* write a dummy byte to the pipe so fh becomes ready */ |
1232 | write (respipe [1], &respipe, 1); |
1317 | respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1); |
1233 | |
1318 | |
1234 | /* optionally signal the main thread asynchronously */ |
1319 | /* optionally signal the main thread asynchronously */ |
1235 | if (main_sig) |
1320 | if (main_sig) |
1236 | pthread_kill (main_tid, main_sig); |
1321 | pthread_kill (main_tid, main_sig); |
1237 | } |
1322 | } |
1238 | |
1323 | |
1239 | self->req = 0; |
1324 | self->req = 0; |
1240 | worker_clear (self); |
1325 | worker_clear (self); |
1241 | |
1326 | |
1242 | UNLOCK (reslock); |
1327 | X_UNLOCK (reslock); |
1243 | } |
1328 | } |
1244 | |
1329 | |
1245 | quit: |
1330 | quit: |
1246 | LOCK (wrklock); |
1331 | X_LOCK (wrklock); |
1247 | worker_free (self); |
1332 | worker_free (self); |
1248 | UNLOCK (wrklock); |
1333 | X_UNLOCK (wrklock); |
1249 | |
1334 | |
1250 | return 0; |
1335 | return 0; |
|
|
1336 | }//D |
1251 | } |
1337 | } |
1252 | |
1338 | |
1253 | /*****************************************************************************/ |
1339 | /*****************************************************************************/ |
1254 | |
1340 | |
1255 | static void atfork_prepare (void) |
1341 | static void atfork_prepare (void) |
1256 | { |
1342 | { |
1257 | LOCK (wrklock); |
1343 | X_LOCK (wrklock); |
1258 | LOCK (reqlock); |
1344 | X_LOCK (reqlock); |
1259 | LOCK (reslock); |
1345 | X_LOCK (reslock); |
1260 | #if !HAVE_PREADWRITE |
1346 | #if !HAVE_PREADWRITE |
1261 | LOCK (preadwritelock); |
1347 | X_LOCK (preadwritelock); |
1262 | #endif |
1348 | #endif |
1263 | #if !HAVE_READDIR_R |
1349 | #if !HAVE_READDIR_R |
1264 | LOCK (readdirlock); |
1350 | X_LOCK (readdirlock); |
1265 | #endif |
1351 | #endif |
1266 | } |
1352 | } |
1267 | |
1353 | |
1268 | static void atfork_parent (void) |
1354 | static void atfork_parent (void) |
1269 | { |
1355 | { |
1270 | #if !HAVE_READDIR_R |
1356 | #if !HAVE_READDIR_R |
1271 | UNLOCK (readdirlock); |
1357 | X_UNLOCK (readdirlock); |
1272 | #endif |
1358 | #endif |
1273 | #if !HAVE_PREADWRITE |
1359 | #if !HAVE_PREADWRITE |
1274 | UNLOCK (preadwritelock); |
1360 | X_UNLOCK (preadwritelock); |
1275 | #endif |
1361 | #endif |
1276 | UNLOCK (reslock); |
1362 | X_UNLOCK (reslock); |
1277 | UNLOCK (reqlock); |
1363 | X_UNLOCK (reqlock); |
1278 | UNLOCK (wrklock); |
1364 | X_UNLOCK (wrklock); |
1279 | } |
1365 | } |
1280 | |
1366 | |
1281 | static void atfork_child (void) |
1367 | static void atfork_child (void) |
1282 | { |
1368 | { |
1283 | aio_req prv; |
1369 | aio_req prv; |
… | |
… | |
1303 | idle = 0; |
1389 | idle = 0; |
1304 | nreqs = 0; |
1390 | nreqs = 0; |
1305 | nready = 0; |
1391 | nready = 0; |
1306 | npending = 0; |
1392 | npending = 0; |
1307 | |
1393 | |
1308 | close (respipe [0]); |
|
|
1309 | close (respipe [1]); |
|
|
1310 | create_pipe (); |
1394 | create_respipe (); |
1311 | |
1395 | |
1312 | atfork_parent (); |
1396 | atfork_parent (); |
1313 | } |
1397 | } |
1314 | |
1398 | |
1315 | #define dREQ \ |
1399 | #define dREQ \ |
… | |
… | |
1344 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
1428 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
1345 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
1429 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
1346 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
1430 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
1347 | newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); |
1431 | newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); |
1348 | newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); |
1432 | newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); |
|
|
1433 | #ifdef _WIN32 |
|
|
1434 | X_MUTEX_CHECK (wrklock); |
|
|
1435 | X_MUTEX_CHECK (reslock); |
|
|
1436 | X_MUTEX_CHECK (reqlock); |
|
|
1437 | X_MUTEX_CHECK (reqwait); |
|
|
1438 | X_MUTEX_CHECK (preadwritelock); |
|
|
1439 | X_MUTEX_CHECK (readdirlock); |
|
|
1440 | |
|
|
1441 | X_COND_CHECK (reqwait); |
|
|
1442 | #else |
1349 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
1443 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
1350 | newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); |
1444 | newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); |
|
|
1445 | #endif |
1351 | |
1446 | |
1352 | create_pipe (); |
1447 | create_respipe (); |
|
|
1448 | |
1353 | ATFORK (atfork_prepare, atfork_parent, atfork_child); |
1449 | X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child); |
1354 | } |
1450 | } |
1355 | |
1451 | |
1356 | void |
1452 | void |
1357 | max_poll_reqs (int nreqs) |
1453 | max_poll_reqs (int nreqs) |
1358 | PROTOTYPE: $ |
1454 | PROTOTYPE: $ |
… | |
… | |
1584 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1680 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1585 | } |
1681 | } |
1586 | else |
1682 | else |
1587 | { |
1683 | { |
1588 | req->type = REQ_FUTIME; |
1684 | req->type = REQ_FUTIME; |
|
|
1685 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
1686 | } |
|
|
1687 | |
|
|
1688 | REQ_SEND; |
|
|
1689 | } |
|
|
1690 | |
|
|
1691 | void |
|
|
1692 | aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef) |
|
|
1693 | PPCODE: |
|
|
1694 | { |
|
|
1695 | dREQ; |
|
|
1696 | |
|
|
1697 | req->sv1 = newSVsv (fh_or_path); |
|
|
1698 | req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; |
|
|
1699 | |
|
|
1700 | if (SvPOK (fh_or_path)) |
|
|
1701 | { |
|
|
1702 | req->type = REQ_TRUNCATE; |
|
|
1703 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1704 | } |
|
|
1705 | else |
|
|
1706 | { |
|
|
1707 | req->type = REQ_FTRUNCATE; |
1589 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1708 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1590 | } |
1709 | } |
1591 | |
1710 | |
1592 | REQ_SEND; |
1711 | REQ_SEND; |
1593 | } |
1712 | } |
… | |
… | |
1809 | CODE: |
1928 | CODE: |
1810 | { |
1929 | { |
1811 | if (block_sig_level) |
1930 | if (block_sig_level) |
1812 | croak ("cannot call IO::AIO::setsig from within aio_block/callback"); |
1931 | croak ("cannot call IO::AIO::setsig from within aio_block/callback"); |
1813 | |
1932 | |
1814 | LOCK (reslock); |
1933 | X_LOCK (reslock); |
1815 | main_tid = pthread_self (); |
1934 | main_tid = pthread_self (); |
1816 | main_sig = signum; |
1935 | main_sig = signum; |
1817 | UNLOCK (reslock); |
1936 | X_UNLOCK (reslock); |
1818 | |
1937 | |
1819 | if (main_sig && npending) |
1938 | if (main_sig && npending) |
1820 | pthread_kill (main_tid, main_sig); |
1939 | pthread_kill (main_tid, main_sig); |
1821 | } |
1940 | } |
1822 | |
1941 | |
… | |
… | |
1866 | |
1985 | |
1867 | int |
1986 | int |
1868 | nthreads() |
1987 | nthreads() |
1869 | PROTOTYPE: |
1988 | PROTOTYPE: |
1870 | CODE: |
1989 | CODE: |
1871 | if (WORDACCESS_UNSAFE) LOCK (wrklock); |
1990 | if (WORDACCESS_UNSAFE) X_LOCK (wrklock); |
1872 | RETVAL = started; |
1991 | RETVAL = started; |
1873 | if (WORDACCESS_UNSAFE) UNLOCK (wrklock); |
1992 | if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock); |
1874 | OUTPUT: |
1993 | OUTPUT: |
1875 | RETVAL |
1994 | RETVAL |
1876 | |
1995 | |
1877 | PROTOTYPES: DISABLE |
1996 | PROTOTYPES: DISABLE |
1878 | |
1997 | |