… | |
… | |
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 |
… | |
… | |
46 | #endif |
90 | #endif |
47 | |
91 | |
48 | /* buffer size for various temporary buffers */ |
92 | /* buffer size for various temporary buffers */ |
49 | #define AIO_BUFSIZE 65536 |
93 | #define AIO_BUFSIZE 65536 |
50 | |
94 | |
|
|
95 | /* use NV for 32 bit perls as it allows larger offsets */ |
|
|
96 | #if IVSIZE >= 8 |
|
|
97 | # define SvVAL64 SvIV |
|
|
98 | #else |
|
|
99 | # define SvVAL64 SvNV |
|
|
100 | #endif |
|
|
101 | |
51 | #define dBUF \ |
102 | #define dBUF \ |
52 | char *aio_buf; \ |
103 | char *aio_buf; \ |
53 | LOCK (wrklock); \ |
104 | X_LOCK (wrklock); \ |
54 | self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ |
105 | self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ |
55 | UNLOCK (wrklock); \ |
106 | X_UNLOCK (wrklock); \ |
56 | if (!aio_buf) \ |
107 | if (!aio_buf) \ |
57 | return -1; |
108 | return -1; |
58 | |
109 | |
59 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
110 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
60 | |
111 | |
61 | enum { |
112 | enum { |
62 | REQ_QUIT, |
113 | REQ_QUIT, |
63 | REQ_OPEN, REQ_CLOSE, |
114 | REQ_OPEN, REQ_CLOSE, |
64 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
115 | REQ_READ, REQ_WRITE, |
65 | REQ_SENDFILE, |
116 | REQ_READAHEAD, REQ_SENDFILE, |
66 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
117 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
67 | REQ_UTIME, REQ_FUTIME, /* must be consecutive */ |
118 | REQ_TRUNCATE, REQ_FTRUNCATE, |
68 | REQ_CHMOD, REQ_FCHMOD, /* must be consecutive */ |
119 | REQ_UTIME, REQ_FUTIME, |
69 | REQ_CHOWN, REQ_FCHOWN, /* must be consecutive */ |
120 | REQ_CHMOD, REQ_FCHMOD, |
|
|
121 | REQ_CHOWN, REQ_FCHOWN, |
70 | REQ_FSYNC, REQ_FDATASYNC, |
122 | REQ_FSYNC, REQ_FDATASYNC, |
71 | REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME, |
123 | REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME, |
72 | REQ_MKNOD, REQ_READDIR, |
124 | REQ_MKNOD, REQ_READDIR, |
73 | REQ_LINK, REQ_SYMLINK, REQ_READLINK, |
125 | REQ_LINK, REQ_SYMLINK, REQ_READLINK, |
74 | REQ_GROUP, REQ_NOP, |
126 | REQ_GROUP, REQ_NOP, |
… | |
… | |
170 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
222 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
171 | |
223 | |
172 | static unsigned int started, idle, wanted; |
224 | static unsigned int started, idle, wanted; |
173 | |
225 | |
174 | /* worker threads management */ |
226 | /* worker threads management */ |
175 | static mutex_t wrklock = MUTEX_INIT; |
227 | static mutex_t wrklock = X_MUTEX_INIT; |
176 | |
228 | |
177 | typedef struct worker { |
229 | typedef struct worker { |
178 | /* locked by wrklock */ |
230 | /* locked by wrklock */ |
179 | struct worker *prev, *next; |
231 | struct worker *prev, *next; |
180 | |
232 | |
… | |
… | |
212 | } |
264 | } |
213 | |
265 | |
214 | static volatile unsigned int nreqs, nready, npending; |
266 | static volatile unsigned int nreqs, nready, npending; |
215 | static volatile unsigned int max_idle = 4; |
267 | static volatile unsigned int max_idle = 4; |
216 | static volatile unsigned int max_outstanding = 0xffffffff; |
268 | static volatile unsigned int max_outstanding = 0xffffffff; |
217 | static int respipe [2]; |
269 | static int respipe_osf [2], respipe [2] = { -1, -1 }; |
218 | |
270 | |
219 | static mutex_t reslock = MUTEX_INIT; |
271 | static mutex_t reslock = X_MUTEX_INIT; |
220 | static mutex_t reqlock = MUTEX_INIT; |
272 | static mutex_t reqlock = X_MUTEX_INIT; |
221 | static cond_t reqwait = COND_INIT; |
273 | static cond_t reqwait = X_COND_INIT; |
222 | |
274 | |
223 | #if WORDACCESS_UNSAFE |
275 | #if WORDACCESS_UNSAFE |
224 | |
276 | |
225 | static unsigned int get_nready () |
277 | static unsigned int get_nready () |
226 | { |
278 | { |
227 | unsigned int retval; |
279 | unsigned int retval; |
228 | |
280 | |
229 | LOCK (reqlock); |
281 | X_LOCK (reqlock); |
230 | retval = nready; |
282 | retval = nready; |
231 | UNLOCK (reqlock); |
283 | X_UNLOCK (reqlock); |
232 | |
284 | |
233 | return retval; |
285 | return retval; |
234 | } |
286 | } |
235 | |
287 | |
236 | static unsigned int get_npending () |
288 | static unsigned int get_npending () |
237 | { |
289 | { |
238 | unsigned int retval; |
290 | unsigned int retval; |
239 | |
291 | |
240 | LOCK (reslock); |
292 | X_LOCK (reslock); |
241 | retval = npending; |
293 | retval = npending; |
242 | UNLOCK (reslock); |
294 | X_UNLOCK (reslock); |
243 | |
295 | |
244 | return retval; |
296 | return retval; |
245 | } |
297 | } |
246 | |
298 | |
247 | static unsigned int get_nthreads () |
299 | static unsigned int get_nthreads () |
248 | { |
300 | { |
249 | unsigned int retval; |
301 | unsigned int retval; |
250 | |
302 | |
251 | LOCK (wrklock); |
303 | X_LOCK (wrklock); |
252 | retval = started; |
304 | retval = started; |
253 | UNLOCK (wrklock); |
305 | X_UNLOCK (wrklock); |
254 | |
306 | |
255 | return retval; |
307 | return retval; |
256 | } |
308 | } |
257 | |
309 | |
258 | #else |
310 | #else |
… | |
… | |
317 | abort (); |
369 | abort (); |
318 | } |
370 | } |
319 | |
371 | |
320 | static int poll_cb (); |
372 | static int poll_cb (); |
321 | static int req_invoke (aio_req req); |
373 | static int req_invoke (aio_req req); |
322 | static void req_free (aio_req req); |
374 | static void req_destroy (aio_req req); |
323 | static void req_cancel (aio_req req); |
375 | static void req_cancel (aio_req req); |
324 | |
376 | |
325 | /* must be called at most once */ |
377 | /* must be called at most once */ |
326 | static SV *req_sv (aio_req req, const char *klass) |
378 | static SV *req_sv (aio_req req, const char *klass) |
327 | { |
379 | { |
… | |
… | |
393 | { |
445 | { |
394 | block_sig (); |
446 | block_sig (); |
395 | |
447 | |
396 | if (!req_invoke (grp)) |
448 | if (!req_invoke (grp)) |
397 | { |
449 | { |
398 | req_free (grp); |
450 | req_destroy (grp); |
399 | unblock_sig (); |
451 | unblock_sig (); |
400 | croak (0); |
452 | croak (0); |
401 | } |
453 | } |
402 | |
454 | |
403 | req_free (grp); |
455 | req_destroy (grp); |
404 | unblock_sig (); |
456 | unblock_sig (); |
405 | } |
457 | } |
406 | } |
458 | } |
407 | |
459 | |
408 | static int req_invoke (aio_req req) |
460 | static int req_invoke (aio_req req) |
… | |
… | |
536 | } |
588 | } |
537 | |
589 | |
538 | return !SvTRUE (ERRSV); |
590 | return !SvTRUE (ERRSV); |
539 | } |
591 | } |
540 | |
592 | |
541 | static void req_free (aio_req req) |
593 | static void req_destroy (aio_req req) |
542 | { |
594 | { |
543 | if (req->self) |
595 | if (req->self) |
544 | { |
596 | { |
545 | sv_unmagic (req->self, PERL_MAGIC_ext); |
597 | sv_unmagic (req->self, PERL_MAGIC_ext); |
546 | SvREFCNT_dec (req->self); |
598 | SvREFCNT_dec (req->self); |
… | |
… | |
575 | req->flags |= FLAG_CANCELLED; |
627 | req->flags |= FLAG_CANCELLED; |
576 | |
628 | |
577 | req_cancel_subs (req); |
629 | req_cancel_subs (req); |
578 | } |
630 | } |
579 | |
631 | |
580 | 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); |
581 | |
677 | |
582 | static void start_thread (void) |
678 | static void start_thread (void) |
583 | { |
679 | { |
584 | worker *wrk = calloc (1, sizeof (worker)); |
680 | worker *wrk = calloc (1, sizeof (worker)); |
585 | |
681 | |
586 | if (!wrk) |
682 | if (!wrk) |
587 | croak ("unable to allocate worker thread data"); |
683 | croak ("unable to allocate worker thread data"); |
588 | |
684 | |
589 | LOCK (wrklock); |
685 | X_LOCK (wrklock); |
590 | |
686 | |
591 | if (thread_create (&wrk->tid, aio_proc, (void *)wrk)) |
687 | if (thread_create (&wrk->tid, aio_proc, (void *)wrk)) |
592 | { |
688 | { |
593 | wrk->prev = &wrk_first; |
689 | wrk->prev = &wrk_first; |
594 | wrk->next = wrk_first.next; |
690 | wrk->next = wrk_first.next; |
… | |
… | |
597 | ++started; |
693 | ++started; |
598 | } |
694 | } |
599 | else |
695 | else |
600 | free (wrk); |
696 | free (wrk); |
601 | |
697 | |
602 | UNLOCK (wrklock); |
698 | X_UNLOCK (wrklock); |
603 | } |
699 | } |
604 | |
700 | |
605 | static void maybe_start_thread () |
701 | static void maybe_start_thread () |
606 | { |
702 | { |
607 | if (get_nthreads () >= wanted) |
703 | if (get_nthreads () >= wanted) |
… | |
… | |
618 | { |
714 | { |
619 | block_sig (); |
715 | block_sig (); |
620 | |
716 | |
621 | ++nreqs; |
717 | ++nreqs; |
622 | |
718 | |
623 | LOCK (reqlock); |
719 | X_LOCK (reqlock); |
624 | ++nready; |
720 | ++nready; |
625 | reqq_push (&req_queue, req); |
721 | reqq_push (&req_queue, req); |
626 | COND_SIGNAL (reqwait); |
722 | X_COND_SIGNAL (reqwait); |
627 | UNLOCK (reqlock); |
723 | X_UNLOCK (reqlock); |
628 | |
724 | |
629 | unblock_sig (); |
725 | unblock_sig (); |
630 | |
726 | |
631 | maybe_start_thread (); |
727 | maybe_start_thread (); |
632 | } |
728 | } |
… | |
… | |
638 | Newz (0, req, 1, aio_cb); |
734 | Newz (0, req, 1, aio_cb); |
639 | |
735 | |
640 | req->type = REQ_QUIT; |
736 | req->type = REQ_QUIT; |
641 | req->pri = PRI_MAX + PRI_BIAS; |
737 | req->pri = PRI_MAX + PRI_BIAS; |
642 | |
738 | |
643 | LOCK (reqlock); |
739 | X_LOCK (reqlock); |
644 | reqq_push (&req_queue, req); |
740 | reqq_push (&req_queue, req); |
645 | COND_SIGNAL (reqwait); |
741 | X_COND_SIGNAL (reqwait); |
646 | UNLOCK (reqlock); |
742 | X_UNLOCK (reqlock); |
647 | |
743 | |
648 | LOCK (wrklock); |
744 | X_LOCK (wrklock); |
649 | --started; |
745 | --started; |
650 | UNLOCK (wrklock); |
746 | X_UNLOCK (wrklock); |
651 | } |
747 | } |
652 | |
748 | |
653 | static void set_max_idle (int nthreads) |
749 | static void set_max_idle (int nthreads) |
654 | { |
750 | { |
655 | if (WORDACCESS_UNSAFE) LOCK (reqlock); |
751 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
656 | max_idle = nthreads <= 0 ? 1 : nthreads; |
752 | max_idle = nthreads <= 0 ? 1 : nthreads; |
657 | if (WORDACCESS_UNSAFE) UNLOCK (reqlock); |
753 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
658 | } |
754 | } |
659 | |
755 | |
660 | static void min_parallel (int nthreads) |
756 | static void min_parallel (int nthreads) |
661 | { |
757 | { |
662 | if (wanted < nthreads) |
758 | if (wanted < nthreads) |
… | |
… | |
677 | fd_set rfd; |
773 | fd_set rfd; |
678 | |
774 | |
679 | while (nreqs) |
775 | while (nreqs) |
680 | { |
776 | { |
681 | int size; |
777 | int size; |
682 | if (WORDACCESS_UNSAFE) LOCK (reslock); |
778 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
683 | size = res_queue.size; |
779 | size = res_queue.size; |
684 | if (WORDACCESS_UNSAFE) UNLOCK (reslock); |
780 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
685 | |
781 | |
686 | if (size) |
782 | if (size) |
687 | return; |
783 | return; |
688 | |
784 | |
689 | maybe_start_thread (); |
785 | maybe_start_thread (); |
690 | |
786 | |
691 | FD_ZERO(&rfd); |
787 | FD_ZERO (&rfd); |
692 | FD_SET(respipe [0], &rfd); |
788 | FD_SET (respipe [0], &rfd); |
693 | |
789 | |
694 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
790 | PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); |
695 | } |
791 | } |
696 | } |
792 | } |
697 | |
793 | |
698 | static int poll_cb () |
794 | static int poll_cb () |
699 | { |
795 | { |
… | |
… | |
713 | { |
809 | { |
714 | for (;;) |
810 | for (;;) |
715 | { |
811 | { |
716 | maybe_start_thread (); |
812 | maybe_start_thread (); |
717 | |
813 | |
718 | LOCK (reslock); |
814 | X_LOCK (reslock); |
719 | req = reqq_shift (&res_queue); |
815 | req = reqq_shift (&res_queue); |
720 | |
816 | |
721 | if (req) |
817 | if (req) |
722 | { |
818 | { |
723 | --npending; |
819 | --npending; |
724 | |
820 | |
725 | if (!res_queue.size) |
821 | if (!res_queue.size) |
726 | { |
822 | { |
727 | /* read any signals sent by the worker threads */ |
823 | /* read any signals sent by the worker threads */ |
728 | char buf [4]; |
824 | char buf [4]; |
729 | while (read (respipe [0], buf, 4) == 4) |
825 | while (respipe_read (respipe [0], buf, 4) == 4) |
730 | ; |
826 | ; |
731 | } |
827 | } |
732 | } |
828 | } |
733 | |
829 | |
734 | UNLOCK (reslock); |
830 | X_UNLOCK (reslock); |
735 | |
831 | |
736 | if (!req) |
832 | if (!req) |
737 | break; |
833 | break; |
738 | |
834 | |
739 | --nreqs; |
835 | --nreqs; |
… | |
… | |
745 | } |
841 | } |
746 | else |
842 | else |
747 | { |
843 | { |
748 | if (!req_invoke (req)) |
844 | if (!req_invoke (req)) |
749 | { |
845 | { |
750 | req_free (req); |
846 | req_destroy (req); |
751 | unblock_sig (); |
847 | unblock_sig (); |
752 | croak (0); |
848 | croak (0); |
753 | } |
849 | } |
754 | |
850 | |
755 | count++; |
851 | count++; |
756 | } |
852 | } |
757 | |
853 | |
758 | req_free (req); |
854 | req_destroy (req); |
759 | |
855 | |
760 | if (maxreqs && !--maxreqs) |
856 | if (maxreqs && !--maxreqs) |
761 | break; |
857 | break; |
762 | |
858 | |
763 | if (max_poll_time) |
859 | if (max_poll_time) |
… | |
… | |
777 | ++maxreqs; |
873 | ++maxreqs; |
778 | } |
874 | } |
779 | |
875 | |
780 | unblock_sig (); |
876 | unblock_sig (); |
781 | return count; |
877 | return count; |
782 | } |
|
|
783 | |
|
|
784 | static void create_pipe () |
|
|
785 | { |
|
|
786 | if (pipe (respipe)) |
|
|
787 | croak ("unable to initialize result pipe"); |
|
|
788 | |
|
|
789 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
|
|
790 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
791 | |
|
|
792 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
793 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
794 | } |
878 | } |
795 | |
879 | |
796 | /*****************************************************************************/ |
880 | /*****************************************************************************/ |
797 | /* work around various missing functions */ |
881 | /* work around various missing functions */ |
798 | |
882 | |
… | |
… | |
803 | /* |
887 | /* |
804 | * make our pread/pwrite safe against themselves, but not against |
888 | * make our pread/pwrite safe against themselves, but not against |
805 | * 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, |
806 | * but that's your problem, not mine. |
890 | * but that's your problem, not mine. |
807 | */ |
891 | */ |
808 | static mutex_t preadwritelock = MUTEX_INIT; |
892 | static mutex_t preadwritelock = X_MUTEX_INIT; |
809 | |
893 | |
810 | 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) |
811 | { |
895 | { |
812 | ssize_t res; |
896 | ssize_t res; |
813 | off_t ooffset; |
897 | off_t ooffset; |
814 | |
898 | |
815 | LOCK (preadwritelock); |
899 | X_LOCK (preadwritelock); |
816 | ooffset = lseek (fd, 0, SEEK_CUR); |
900 | ooffset = lseek (fd, 0, SEEK_CUR); |
817 | lseek (fd, offset, SEEK_SET); |
901 | lseek (fd, offset, SEEK_SET); |
818 | res = read (fd, buf, count); |
902 | res = read (fd, buf, count); |
819 | lseek (fd, ooffset, SEEK_SET); |
903 | lseek (fd, ooffset, SEEK_SET); |
820 | UNLOCK (preadwritelock); |
904 | X_UNLOCK (preadwritelock); |
821 | |
905 | |
822 | return res; |
906 | return res; |
823 | } |
907 | } |
824 | |
908 | |
825 | 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) |
826 | { |
910 | { |
827 | ssize_t res; |
911 | ssize_t res; |
828 | off_t ooffset; |
912 | off_t ooffset; |
829 | |
913 | |
830 | LOCK (preadwritelock); |
914 | X_LOCK (preadwritelock); |
831 | ooffset = lseek (fd, 0, SEEK_CUR); |
915 | ooffset = lseek (fd, 0, SEEK_CUR); |
832 | lseek (fd, offset, SEEK_SET); |
916 | lseek (fd, offset, SEEK_SET); |
833 | res = write (fd, buf, count); |
917 | res = write (fd, buf, count); |
834 | lseek (fd, offset, SEEK_SET); |
918 | lseek (fd, offset, SEEK_SET); |
835 | UNLOCK (preadwritelock); |
919 | X_UNLOCK (preadwritelock); |
836 | |
920 | |
837 | return res; |
921 | return res; |
838 | } |
922 | } |
839 | #endif |
923 | #endif |
840 | |
924 | |
… | |
… | |
873 | #if !HAVE_READAHEAD |
957 | #if !HAVE_READAHEAD |
874 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
958 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
875 | |
959 | |
876 | 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) |
877 | { |
961 | { |
|
|
962 | size_t todo = count; |
878 | dBUF; |
963 | dBUF; |
879 | |
964 | |
880 | while (count > 0) |
965 | while (todo > 0) |
881 | { |
966 | { |
882 | size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; |
967 | size_t len = todo < AIO_BUFSIZE ? todo : AIO_BUFSIZE; |
883 | |
968 | |
884 | pread (fd, aio_buf, len, offset); |
969 | pread (fd, aio_buf, len, offset); |
885 | offset += len; |
970 | offset += len; |
886 | count -= len; |
971 | todo -= len; |
887 | } |
972 | } |
888 | |
973 | |
889 | errno = 0; |
974 | errno = 0; |
|
|
975 | return count; |
890 | } |
976 | } |
891 | |
977 | |
892 | #endif |
978 | #endif |
893 | |
979 | |
894 | #if !HAVE_READDIR_R |
980 | #if !HAVE_READDIR_R |
895 | # define readdir_r aio_readdir_r |
981 | # define readdir_r aio_readdir_r |
896 | |
982 | |
897 | static mutex_t readdirlock = MUTEX_INIT; |
983 | static mutex_t readdirlock = X_MUTEX_INIT; |
898 | |
984 | |
899 | 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) |
900 | { |
986 | { |
901 | struct dirent *e; |
987 | X_DIRENT *e; |
902 | int errorno; |
988 | int errorno; |
903 | |
989 | |
904 | LOCK (readdirlock); |
990 | X_LOCK (readdirlock); |
905 | |
991 | |
906 | e = readdir (dirp); |
992 | e = readdir (dirp); |
907 | errorno = errno; |
993 | errorno = errno; |
908 | |
994 | |
909 | if (e) |
995 | if (e) |
… | |
… | |
912 | strcpy (ent->d_name, e->d_name); |
998 | strcpy (ent->d_name, e->d_name); |
913 | } |
999 | } |
914 | else |
1000 | else |
915 | *res = 0; |
1001 | *res = 0; |
916 | |
1002 | |
917 | UNLOCK (readdirlock); |
1003 | X_UNLOCK (readdirlock); |
918 | |
1004 | |
919 | errno = errorno; |
1005 | errno = errorno; |
920 | return e ? 0 : -1; |
1006 | return e ? 0 : -1; |
921 | } |
1007 | } |
922 | #endif |
1008 | #endif |
… | |
… | |
1018 | static void scandir_ (aio_req req, worker *self) |
1104 | static void scandir_ (aio_req req, worker *self) |
1019 | { |
1105 | { |
1020 | DIR *dirp; |
1106 | DIR *dirp; |
1021 | union |
1107 | union |
1022 | { |
1108 | { |
1023 | struct dirent d; |
1109 | X_DIRENT d; |
1024 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
1110 | char b [offsetof (X_DIRENT, d_name) + NAME_MAX + 1]; |
1025 | } *u; |
1111 | } *u; |
1026 | struct dirent *entp; |
1112 | X_DIRENT *entp; |
1027 | char *name, *names; |
1113 | char *name, *names; |
1028 | int memlen = 4096; |
1114 | int memlen = 4096; |
1029 | int memofs = 0; |
1115 | int memofs = 0; |
1030 | int res = 0; |
1116 | int res = 0; |
1031 | int errorno; |
|
|
1032 | |
1117 | |
1033 | LOCK (wrklock); |
1118 | X_LOCK (wrklock); |
1034 | self->dirp = dirp = opendir (req->ptr1); |
1119 | self->dirp = dirp = opendir (req->ptr1); |
1035 | self->dbuf = u = malloc (sizeof (*u)); |
1120 | self->dbuf = u = malloc (sizeof (*u)); |
1036 | req->flags |= FLAG_PTR2_FREE; |
1121 | req->flags |= FLAG_PTR2_FREE; |
1037 | req->ptr2 = names = malloc (memlen); |
1122 | req->ptr2 = names = malloc (memlen); |
1038 | UNLOCK (wrklock); |
1123 | X_UNLOCK (wrklock); |
1039 | |
1124 | |
1040 | if (dirp && u && names) |
1125 | if (dirp && u && names) |
1041 | for (;;) |
1126 | for (;;) |
1042 | { |
1127 | { |
1043 | errno = 0; |
1128 | errno = 0; |
… | |
… | |
1055 | res++; |
1140 | res++; |
1056 | |
1141 | |
1057 | while (memofs + len > memlen) |
1142 | while (memofs + len > memlen) |
1058 | { |
1143 | { |
1059 | memlen *= 2; |
1144 | memlen *= 2; |
1060 | LOCK (wrklock); |
1145 | X_LOCK (wrklock); |
1061 | req->ptr2 = names = realloc (names, memlen); |
1146 | req->ptr2 = names = realloc (names, memlen); |
1062 | UNLOCK (wrklock); |
1147 | X_UNLOCK (wrklock); |
1063 | |
1148 | |
1064 | if (!names) |
1149 | if (!names) |
1065 | break; |
1150 | break; |
1066 | } |
1151 | } |
1067 | |
1152 | |
… | |
… | |
1076 | req->result = res; |
1161 | req->result = res; |
1077 | } |
1162 | } |
1078 | |
1163 | |
1079 | /*****************************************************************************/ |
1164 | /*****************************************************************************/ |
1080 | |
1165 | |
1081 | static void *aio_proc (void *thr_arg) |
1166 | X_THREAD_PROC (aio_proc) |
1082 | { |
1167 | { |
|
|
1168 | {//D |
1083 | aio_req req; |
1169 | aio_req req; |
1084 | struct timespec ts; |
1170 | struct timespec ts; |
1085 | worker *self = (worker *)thr_arg; |
1171 | worker *self = (worker *)thr_arg; |
1086 | |
1172 | |
1087 | /* try to distribute timeouts somewhat evenly */ |
1173 | /* try to distribute timeouts somewhat randomly */ |
1088 | ts.tv_nsec = (((unsigned long)self + (unsigned long)ts.tv_sec) & 1023UL) |
1174 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
1089 | * (1000000000UL / 1024UL); |
|
|
1090 | |
1175 | |
1091 | for (;;) |
1176 | for (;;) |
1092 | { |
1177 | { |
1093 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1178 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1094 | |
1179 | |
1095 | LOCK (reqlock); |
1180 | X_LOCK (reqlock); |
1096 | |
1181 | |
1097 | for (;;) |
1182 | for (;;) |
1098 | { |
1183 | { |
1099 | self->req = req = reqq_shift (&req_queue); |
1184 | self->req = req = reqq_shift (&req_queue); |
1100 | |
1185 | |
1101 | if (req) |
1186 | if (req) |
1102 | break; |
1187 | break; |
1103 | |
1188 | |
1104 | ++idle; |
1189 | ++idle; |
1105 | |
1190 | |
1106 | if (COND_TIMEDWAIT (reqwait, reqlock, ts) |
1191 | if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) |
1107 | == ETIMEDOUT) |
1192 | == ETIMEDOUT) |
1108 | { |
1193 | { |
1109 | if (idle > max_idle) |
1194 | if (idle > max_idle) |
1110 | { |
1195 | { |
1111 | --idle; |
1196 | --idle; |
1112 | UNLOCK (reqlock); |
1197 | X_UNLOCK (reqlock); |
1113 | LOCK (wrklock); |
1198 | X_LOCK (wrklock); |
1114 | --started; |
1199 | --started; |
1115 | UNLOCK (wrklock); |
1200 | X_UNLOCK (wrklock); |
1116 | goto quit; |
1201 | goto quit; |
1117 | } |
1202 | } |
1118 | |
1203 | |
1119 | /* we are allowed to idle, so do so without any timeout */ |
1204 | /* we are allowed to idle, so do so without any timeout */ |
1120 | COND_WAIT (reqwait, reqlock); |
1205 | X_COND_WAIT (reqwait, reqlock); |
1121 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1206 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1122 | } |
1207 | } |
1123 | |
1208 | |
1124 | --idle; |
1209 | --idle; |
1125 | } |
1210 | } |
1126 | |
1211 | |
1127 | --nready; |
1212 | --nready; |
1128 | |
1213 | |
1129 | UNLOCK (reqlock); |
1214 | X_UNLOCK (reqlock); |
1130 | |
1215 | |
1131 | errno = 0; /* strictly unnecessary */ |
1216 | errno = 0; /* strictly unnecessary */ |
1132 | |
1217 | |
1133 | if (!(req->flags & FLAG_CANCELLED)) |
1218 | if (!(req->flags & FLAG_CANCELLED)) |
1134 | switch (req->type) |
1219 | switch (req->type) |
1135 | { |
1220 | { |
1136 | case REQ_READ: req->result = pread (req->int1, req->ptr1, req->size, req->offs); break; |
1221 | case REQ_READ: req->result = req->offs >= 0 |
1137 | case REQ_WRITE: req->result = pwrite (req->int1, req->ptr1, req->size, req->offs); break; |
1222 | ? pread (req->int1, req->ptr1, req->size, req->offs) |
|
|
1223 | : read (req->int1, req->ptr1, req->size); break; |
|
|
1224 | case REQ_WRITE: req->result = req->offs >= 0 |
|
|
1225 | ? pwrite (req->int1, req->ptr1, req->size, req->offs) |
|
|
1226 | : write (req->int1, req->ptr1, req->size); break; |
1138 | |
1227 | |
1139 | case REQ_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
1228 | case REQ_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
1140 | case REQ_SENDFILE: req->result = sendfile_ (req->int1, req->int2, req->offs, req->size, self); break; |
1229 | case REQ_SENDFILE: req->result = sendfile_ (req->int1, req->int2, req->offs, req->size, self); break; |
1141 | |
1230 | |
1142 | case REQ_STAT: req->result = stat (req->ptr1, (Stat_t *)req->ptr2); break; |
1231 | case REQ_STAT: req->result = stat (req->ptr1, (Stat_t *)req->ptr2); break; |
… | |
… | |
1145 | |
1234 | |
1146 | 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; |
1147 | 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; |
1148 | case REQ_CHMOD: req->result = chmod (req->ptr1, req->mode); break; |
1237 | case REQ_CHMOD: req->result = chmod (req->ptr1, req->mode); break; |
1149 | 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; |
1150 | |
1241 | |
1151 | 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; |
1152 | case REQ_CLOSE: req->result = close (req->int1); break; |
1243 | case REQ_CLOSE: req->result = close (req->int1); break; |
1153 | case REQ_UNLINK: req->result = unlink (req->ptr1); break; |
1244 | case REQ_UNLINK: req->result = unlink (req->ptr1); break; |
1154 | case REQ_RMDIR: req->result = rmdir (req->ptr1); break; |
1245 | case REQ_RMDIR: req->result = rmdir (req->ptr1); break; |
… | |
… | |
1162 | case REQ_FDATASYNC: req->result = fdatasync (req->int1); break; |
1253 | case REQ_FDATASYNC: req->result = fdatasync (req->int1); break; |
1163 | case REQ_FSYNC: req->result = fsync (req->int1); break; |
1254 | case REQ_FSYNC: req->result = fsync (req->int1); break; |
1164 | case REQ_READDIR: scandir_ (req, self); break; |
1255 | case REQ_READDIR: scandir_ (req, self); break; |
1165 | |
1256 | |
1166 | case REQ_BUSY: |
1257 | case REQ_BUSY: |
|
|
1258 | #ifdef _WIN32 |
|
|
1259 | Sleep (req->nv1 * 1000.); |
|
|
1260 | #else |
1167 | { |
1261 | { |
1168 | struct timeval tv; |
1262 | struct timeval tv; |
1169 | |
1263 | |
1170 | tv.tv_sec = req->nv1; |
1264 | tv.tv_sec = req->nv1; |
1171 | tv.tv_usec = (req->nv1 - tv.tv_usec) * 1000000.; |
1265 | tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.; |
1172 | |
1266 | |
1173 | req->result = select (0, 0, 0, 0, &tv); |
1267 | req->result = select (0, 0, 0, 0, &tv); |
1174 | } |
1268 | } |
|
|
1269 | #endif |
|
|
1270 | break; |
1175 | |
1271 | |
1176 | case REQ_UTIME: |
1272 | case REQ_UTIME: |
1177 | case REQ_FUTIME: |
1273 | case REQ_FUTIME: |
1178 | { |
1274 | { |
1179 | struct timeval tv[2]; |
1275 | struct timeval tv[2]; |
… | |
… | |
1209 | break; |
1305 | break; |
1210 | } |
1306 | } |
1211 | |
1307 | |
1212 | req->errorno = errno; |
1308 | req->errorno = errno; |
1213 | |
1309 | |
1214 | LOCK (reslock); |
1310 | X_LOCK (reslock); |
1215 | |
1311 | |
1216 | ++npending; |
1312 | ++npending; |
1217 | |
1313 | |
1218 | if (!reqq_push (&res_queue, req)) |
1314 | if (!reqq_push (&res_queue, req)) |
1219 | { |
1315 | { |
1220 | /* write a dummy byte to the pipe so fh becomes ready */ |
1316 | /* write a dummy byte to the pipe so fh becomes ready */ |
1221 | write (respipe [1], &respipe, 1); |
1317 | respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1); |
1222 | |
1318 | |
1223 | /* optionally signal the main thread asynchronously */ |
1319 | /* optionally signal the main thread asynchronously */ |
1224 | if (main_sig) |
1320 | if (main_sig) |
1225 | pthread_kill (main_tid, main_sig); |
1321 | pthread_kill (main_tid, main_sig); |
1226 | } |
1322 | } |
1227 | |
1323 | |
1228 | self->req = 0; |
1324 | self->req = 0; |
1229 | worker_clear (self); |
1325 | worker_clear (self); |
1230 | |
1326 | |
1231 | UNLOCK (reslock); |
1327 | X_UNLOCK (reslock); |
1232 | } |
1328 | } |
1233 | |
1329 | |
1234 | quit: |
1330 | quit: |
1235 | LOCK (wrklock); |
1331 | X_LOCK (wrklock); |
1236 | worker_free (self); |
1332 | worker_free (self); |
1237 | UNLOCK (wrklock); |
1333 | X_UNLOCK (wrklock); |
1238 | |
1334 | |
1239 | return 0; |
1335 | return 0; |
|
|
1336 | }//D |
1240 | } |
1337 | } |
1241 | |
1338 | |
1242 | /*****************************************************************************/ |
1339 | /*****************************************************************************/ |
1243 | |
1340 | |
1244 | static void atfork_prepare (void) |
1341 | static void atfork_prepare (void) |
1245 | { |
1342 | { |
1246 | LOCK (wrklock); |
1343 | X_LOCK (wrklock); |
1247 | LOCK (reqlock); |
1344 | X_LOCK (reqlock); |
1248 | LOCK (reslock); |
1345 | X_LOCK (reslock); |
1249 | #if !HAVE_PREADWRITE |
1346 | #if !HAVE_PREADWRITE |
1250 | LOCK (preadwritelock); |
1347 | X_LOCK (preadwritelock); |
1251 | #endif |
1348 | #endif |
1252 | #if !HAVE_READDIR_R |
1349 | #if !HAVE_READDIR_R |
1253 | LOCK (readdirlock); |
1350 | X_LOCK (readdirlock); |
1254 | #endif |
1351 | #endif |
1255 | } |
1352 | } |
1256 | |
1353 | |
1257 | static void atfork_parent (void) |
1354 | static void atfork_parent (void) |
1258 | { |
1355 | { |
1259 | #if !HAVE_READDIR_R |
1356 | #if !HAVE_READDIR_R |
1260 | UNLOCK (readdirlock); |
1357 | X_UNLOCK (readdirlock); |
1261 | #endif |
1358 | #endif |
1262 | #if !HAVE_PREADWRITE |
1359 | #if !HAVE_PREADWRITE |
1263 | UNLOCK (preadwritelock); |
1360 | X_UNLOCK (preadwritelock); |
1264 | #endif |
1361 | #endif |
1265 | UNLOCK (reslock); |
1362 | X_UNLOCK (reslock); |
1266 | UNLOCK (reqlock); |
1363 | X_UNLOCK (reqlock); |
1267 | UNLOCK (wrklock); |
1364 | X_UNLOCK (wrklock); |
1268 | } |
1365 | } |
1269 | |
1366 | |
1270 | static void atfork_child (void) |
1367 | static void atfork_child (void) |
1271 | { |
1368 | { |
1272 | aio_req prv; |
1369 | aio_req prv; |
1273 | |
1370 | |
1274 | while (prv = reqq_shift (&req_queue)) |
1371 | while (prv = reqq_shift (&req_queue)) |
1275 | req_free (prv); |
1372 | req_destroy (prv); |
1276 | |
1373 | |
1277 | while (prv = reqq_shift (&res_queue)) |
1374 | while (prv = reqq_shift (&res_queue)) |
1278 | req_free (prv); |
1375 | req_destroy (prv); |
1279 | |
1376 | |
1280 | while (wrk_first.next != &wrk_first) |
1377 | while (wrk_first.next != &wrk_first) |
1281 | { |
1378 | { |
1282 | worker *wrk = wrk_first.next; |
1379 | worker *wrk = wrk_first.next; |
1283 | |
1380 | |
1284 | if (wrk->req) |
1381 | if (wrk->req) |
1285 | req_free (wrk->req); |
1382 | req_destroy (wrk->req); |
1286 | |
1383 | |
1287 | worker_clear (wrk); |
1384 | worker_clear (wrk); |
1288 | worker_free (wrk); |
1385 | worker_free (wrk); |
1289 | } |
1386 | } |
1290 | |
1387 | |
… | |
… | |
1292 | idle = 0; |
1389 | idle = 0; |
1293 | nreqs = 0; |
1390 | nreqs = 0; |
1294 | nready = 0; |
1391 | nready = 0; |
1295 | npending = 0; |
1392 | npending = 0; |
1296 | |
1393 | |
1297 | close (respipe [0]); |
|
|
1298 | close (respipe [1]); |
|
|
1299 | create_pipe (); |
1394 | create_respipe (); |
1300 | |
1395 | |
1301 | atfork_parent (); |
1396 | atfork_parent (); |
1302 | } |
1397 | } |
1303 | |
1398 | |
1304 | #define dREQ \ |
1399 | #define dREQ \ |
… | |
… | |
1333 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
1428 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
1334 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
1429 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
1335 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
1430 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
1336 | newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); |
1431 | newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); |
1337 | 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 |
1338 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
1443 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
1339 | newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); |
1444 | newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); |
|
|
1445 | #endif |
1340 | |
1446 | |
1341 | create_pipe (); |
1447 | create_respipe (); |
|
|
1448 | |
1342 | ATFORK (atfork_prepare, atfork_parent, atfork_child); |
1449 | X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child); |
1343 | } |
1450 | } |
1344 | |
1451 | |
1345 | void |
1452 | void |
1346 | max_poll_reqs (int nreqs) |
1453 | max_poll_reqs (int nreqs) |
1347 | PROTOTYPE: $ |
1454 | PROTOTYPE: $ |
… | |
… | |
1410 | |
1517 | |
1411 | REQ_SEND (req); |
1518 | REQ_SEND (req); |
1412 | } |
1519 | } |
1413 | |
1520 | |
1414 | void |
1521 | void |
1415 | aio_read (SV *fh, UV offset, UV length, SV8 *data, UV dataoffset, SV *callback=&PL_sv_undef) |
1522 | aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef) |
1416 | ALIAS: |
1523 | ALIAS: |
1417 | aio_read = REQ_READ |
1524 | aio_read = REQ_READ |
1418 | aio_write = REQ_WRITE |
1525 | aio_write = REQ_WRITE |
1419 | PROTOTYPE: $$$$$;$ |
1526 | PROTOTYPE: $$$$$;$ |
1420 | PPCODE: |
1527 | PPCODE: |
1421 | { |
1528 | { |
1422 | STRLEN svlen; |
1529 | STRLEN svlen; |
1423 | char *svptr = SvPVbyte (data, svlen); |
1530 | char *svptr = SvPVbyte (data, svlen); |
|
|
1531 | UV len = SvUV (length); |
1424 | |
1532 | |
1425 | SvUPGRADE (data, SVt_PV); |
1533 | SvUPGRADE (data, SVt_PV); |
1426 | SvPOK_on (data); |
1534 | SvPOK_on (data); |
1427 | |
1535 | |
1428 | if (dataoffset < 0) |
1536 | if (dataoffset < 0) |
1429 | dataoffset += svlen; |
1537 | dataoffset += svlen; |
1430 | |
1538 | |
1431 | if (dataoffset < 0 || dataoffset > svlen) |
1539 | if (dataoffset < 0 || dataoffset > svlen) |
1432 | croak ("data offset outside of string"); |
1540 | croak ("dataoffset outside of data scalar"); |
1433 | |
1541 | |
1434 | if (ix == REQ_WRITE) |
1542 | if (ix == REQ_WRITE) |
1435 | { |
1543 | { |
1436 | /* write: check length and adjust. */ |
1544 | /* write: check length and adjust. */ |
1437 | if (length < 0 || length + dataoffset > svlen) |
1545 | if (!SvOK (length) || len + dataoffset > svlen) |
1438 | length = svlen - dataoffset; |
1546 | len = svlen - dataoffset; |
1439 | } |
1547 | } |
1440 | else |
1548 | else |
1441 | { |
1549 | { |
1442 | /* read: grow scalar as necessary */ |
1550 | /* read: grow scalar as necessary */ |
1443 | svptr = SvGROW (data, length + dataoffset + 1); |
1551 | svptr = SvGROW (data, len + dataoffset + 1); |
1444 | } |
1552 | } |
1445 | |
1553 | |
1446 | if (length < 0) |
1554 | if (len < 0) |
1447 | croak ("length must not be negative"); |
1555 | croak ("length must not be negative"); |
1448 | |
1556 | |
1449 | { |
1557 | { |
1450 | dREQ; |
1558 | dREQ; |
1451 | |
1559 | |
1452 | req->type = ix; |
1560 | req->type = ix; |
1453 | req->sv1 = newSVsv (fh); |
1561 | req->sv1 = newSVsv (fh); |
1454 | req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
1562 | req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
1455 | : IoOFP (sv_2io (fh))); |
1563 | : IoOFP (sv_2io (fh))); |
1456 | req->offs = offset; |
1564 | req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; |
1457 | req->size = length; |
1565 | req->size = len; |
1458 | req->sv2 = SvREFCNT_inc (data); |
1566 | req->sv2 = SvREFCNT_inc (data); |
1459 | req->ptr1 = (char *)svptr + dataoffset; |
1567 | req->ptr1 = (char *)svptr + dataoffset; |
1460 | req->stroffset = dataoffset; |
1568 | req->stroffset = dataoffset; |
1461 | |
1569 | |
1462 | if (!SvREADONLY (data)) |
1570 | if (!SvREADONLY (data)) |
… | |
… | |
1488 | |
1596 | |
1489 | REQ_SEND; |
1597 | REQ_SEND; |
1490 | } |
1598 | } |
1491 | |
1599 | |
1492 | void |
1600 | void |
1493 | aio_sendfile (SV *out_fh, SV *in_fh, UV in_offset, UV length, SV *callback=&PL_sv_undef) |
1601 | aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef) |
1494 | PROTOTYPE: $$$$;$ |
1602 | PROTOTYPE: $$$$;$ |
1495 | PPCODE: |
1603 | PPCODE: |
1496 | { |
1604 | { |
1497 | dREQ; |
1605 | dREQ; |
1498 | |
1606 | |
1499 | req->type = REQ_SENDFILE; |
1607 | req->type = REQ_SENDFILE; |
1500 | req->sv1 = newSVsv (out_fh); |
1608 | req->sv1 = newSVsv (out_fh); |
1501 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
1609 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
1502 | req->sv2 = newSVsv (in_fh); |
1610 | req->sv2 = newSVsv (in_fh); |
1503 | req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
1611 | req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
1504 | req->offs = in_offset; |
1612 | req->offs = SvVAL64 (in_offset); |
1505 | req->size = length; |
1613 | req->size = length; |
1506 | |
1614 | |
1507 | REQ_SEND; |
1615 | REQ_SEND; |
1508 | } |
1616 | } |
1509 | |
1617 | |
1510 | void |
1618 | void |
1511 | aio_readahead (SV *fh, UV offset, IV length, SV *callback=&PL_sv_undef) |
1619 | aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef) |
1512 | PROTOTYPE: $$$;$ |
1620 | PROTOTYPE: $$$;$ |
1513 | PPCODE: |
1621 | PPCODE: |
1514 | { |
1622 | { |
1515 | dREQ; |
1623 | dREQ; |
1516 | |
1624 | |
1517 | req->type = REQ_READAHEAD; |
1625 | req->type = REQ_READAHEAD; |
1518 | req->sv1 = newSVsv (fh); |
1626 | req->sv1 = newSVsv (fh); |
1519 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
1627 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
1520 | req->offs = offset; |
1628 | req->offs = SvVAL64 (offset); |
1521 | req->size = length; |
1629 | req->size = length; |
1522 | |
1630 | |
1523 | REQ_SEND; |
1631 | REQ_SEND; |
1524 | } |
1632 | } |
1525 | |
1633 | |
… | |
… | |
1533 | dREQ; |
1641 | dREQ; |
1534 | |
1642 | |
1535 | req->ptr2 = malloc (sizeof (Stat_t)); |
1643 | req->ptr2 = malloc (sizeof (Stat_t)); |
1536 | if (!req->ptr2) |
1644 | if (!req->ptr2) |
1537 | { |
1645 | { |
1538 | req_free (req); |
1646 | req_destroy (req); |
1539 | croak ("out of memory during aio_stat statdata allocation"); |
1647 | croak ("out of memory during aio_stat statdata allocation"); |
1540 | } |
1648 | } |
1541 | |
1649 | |
1542 | req->flags |= FLAG_PTR2_FREE; |
1650 | req->flags |= FLAG_PTR2_FREE; |
1543 | req->sv1 = newSVsv (fh_or_path); |
1651 | req->sv1 = newSVsv (fh_or_path); |
… | |
… | |
1572 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1680 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1573 | } |
1681 | } |
1574 | else |
1682 | else |
1575 | { |
1683 | { |
1576 | 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; |
1577 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1708 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1578 | } |
1709 | } |
1579 | |
1710 | |
1580 | REQ_SEND; |
1711 | REQ_SEND; |
1581 | } |
1712 | } |
… | |
… | |
1797 | CODE: |
1928 | CODE: |
1798 | { |
1929 | { |
1799 | if (block_sig_level) |
1930 | if (block_sig_level) |
1800 | croak ("cannot call IO::AIO::setsig from within aio_block/callback"); |
1931 | croak ("cannot call IO::AIO::setsig from within aio_block/callback"); |
1801 | |
1932 | |
1802 | LOCK (reslock); |
1933 | X_LOCK (reslock); |
1803 | main_tid = pthread_self (); |
1934 | main_tid = pthread_self (); |
1804 | main_sig = signum; |
1935 | main_sig = signum; |
1805 | UNLOCK (reslock); |
1936 | X_UNLOCK (reslock); |
1806 | |
1937 | |
1807 | if (main_sig && npending) |
1938 | if (main_sig && npending) |
1808 | pthread_kill (main_tid, main_sig); |
1939 | pthread_kill (main_tid, main_sig); |
1809 | } |
1940 | } |
1810 | |
1941 | |
… | |
… | |
1854 | |
1985 | |
1855 | int |
1986 | int |
1856 | nthreads() |
1987 | nthreads() |
1857 | PROTOTYPE: |
1988 | PROTOTYPE: |
1858 | CODE: |
1989 | CODE: |
1859 | if (WORDACCESS_UNSAFE) LOCK (wrklock); |
1990 | if (WORDACCESS_UNSAFE) X_LOCK (wrklock); |
1860 | RETVAL = started; |
1991 | RETVAL = started; |
1861 | if (WORDACCESS_UNSAFE) UNLOCK (wrklock); |
1992 | if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock); |
1862 | OUTPUT: |
1993 | OUTPUT: |
1863 | RETVAL |
1994 | RETVAL |
1864 | |
1995 | |
1865 | PROTOTYPES: DISABLE |
1996 | PROTOTYPES: DISABLE |
1866 | |
1997 | |