… | |
… | |
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 [2], respipe_osf [2]; |
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_pipe (int fd[2]) |
|
|
640 | { |
|
|
641 | #ifdef _WIN32 |
|
|
642 | int arg = 1; |
|
|
643 | if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, fd) |
|
|
644 | || ioctlsocket (TO_SOCKET (fd [0]), FIONBIO, &arg) |
|
|
645 | || ioctlsocket (TO_SOCKET (fd [1]), FIONBIO, &arg)) |
|
|
646 | #else |
|
|
647 | if (pipe (fd) |
|
|
648 | || fcntl (fd [0], F_SETFL, O_NONBLOCK) |
|
|
649 | || fcntl (fd [1], F_SETFL, O_NONBLOCK)) |
|
|
650 | #endif |
|
|
651 | croak ("unable to initialize result pipe"); |
|
|
652 | |
|
|
653 | respipe_osf [0] = TO_SOCKET (respipe [0]); |
|
|
654 | respipe_osf [1] = TO_SOCKET (respipe [1]); |
|
|
655 | } |
|
|
656 | |
|
|
657 | X_THREAD_PROC (aio_proc); |
581 | |
658 | |
582 | static void start_thread (void) |
659 | static void start_thread (void) |
583 | { |
660 | { |
584 | worker *wrk = calloc (1, sizeof (worker)); |
661 | worker *wrk = calloc (1, sizeof (worker)); |
585 | |
662 | |
586 | if (!wrk) |
663 | if (!wrk) |
587 | croak ("unable to allocate worker thread data"); |
664 | croak ("unable to allocate worker thread data"); |
588 | |
665 | |
589 | LOCK (wrklock); |
666 | X_LOCK (wrklock); |
590 | |
667 | |
591 | if (thread_create (&wrk->tid, aio_proc, (void *)wrk)) |
668 | if (thread_create (&wrk->tid, aio_proc, (void *)wrk)) |
592 | { |
669 | { |
593 | wrk->prev = &wrk_first; |
670 | wrk->prev = &wrk_first; |
594 | wrk->next = wrk_first.next; |
671 | wrk->next = wrk_first.next; |
… | |
… | |
597 | ++started; |
674 | ++started; |
598 | } |
675 | } |
599 | else |
676 | else |
600 | free (wrk); |
677 | free (wrk); |
601 | |
678 | |
602 | UNLOCK (wrklock); |
679 | X_UNLOCK (wrklock); |
603 | } |
680 | } |
604 | |
681 | |
605 | static void maybe_start_thread () |
682 | static void maybe_start_thread () |
606 | { |
683 | { |
607 | if (get_nthreads () >= wanted) |
684 | if (get_nthreads () >= wanted) |
… | |
… | |
618 | { |
695 | { |
619 | block_sig (); |
696 | block_sig (); |
620 | |
697 | |
621 | ++nreqs; |
698 | ++nreqs; |
622 | |
699 | |
623 | LOCK (reqlock); |
700 | X_LOCK (reqlock); |
624 | ++nready; |
701 | ++nready; |
625 | reqq_push (&req_queue, req); |
702 | reqq_push (&req_queue, req); |
626 | COND_SIGNAL (reqwait); |
703 | X_COND_SIGNAL (reqwait); |
627 | UNLOCK (reqlock); |
704 | X_UNLOCK (reqlock); |
628 | |
705 | |
629 | unblock_sig (); |
706 | unblock_sig (); |
630 | |
707 | |
631 | maybe_start_thread (); |
708 | maybe_start_thread (); |
632 | } |
709 | } |
… | |
… | |
638 | Newz (0, req, 1, aio_cb); |
715 | Newz (0, req, 1, aio_cb); |
639 | |
716 | |
640 | req->type = REQ_QUIT; |
717 | req->type = REQ_QUIT; |
641 | req->pri = PRI_MAX + PRI_BIAS; |
718 | req->pri = PRI_MAX + PRI_BIAS; |
642 | |
719 | |
643 | LOCK (reqlock); |
720 | X_LOCK (reqlock); |
644 | reqq_push (&req_queue, req); |
721 | reqq_push (&req_queue, req); |
645 | COND_SIGNAL (reqwait); |
722 | X_COND_SIGNAL (reqwait); |
646 | UNLOCK (reqlock); |
723 | X_UNLOCK (reqlock); |
647 | |
724 | |
648 | LOCK (wrklock); |
725 | X_LOCK (wrklock); |
649 | --started; |
726 | --started; |
650 | UNLOCK (wrklock); |
727 | X_UNLOCK (wrklock); |
651 | } |
728 | } |
652 | |
729 | |
653 | static void set_max_idle (int nthreads) |
730 | static void set_max_idle (int nthreads) |
654 | { |
731 | { |
655 | if (WORDACCESS_UNSAFE) LOCK (reqlock); |
732 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
656 | max_idle = nthreads <= 0 ? 1 : nthreads; |
733 | max_idle = nthreads <= 0 ? 1 : nthreads; |
657 | if (WORDACCESS_UNSAFE) UNLOCK (reqlock); |
734 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
658 | } |
735 | } |
659 | |
736 | |
660 | static void min_parallel (int nthreads) |
737 | static void min_parallel (int nthreads) |
661 | { |
738 | { |
662 | if (wanted < nthreads) |
739 | if (wanted < nthreads) |
… | |
… | |
677 | fd_set rfd; |
754 | fd_set rfd; |
678 | |
755 | |
679 | while (nreqs) |
756 | while (nreqs) |
680 | { |
757 | { |
681 | int size; |
758 | int size; |
682 | if (WORDACCESS_UNSAFE) LOCK (reslock); |
759 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
683 | size = res_queue.size; |
760 | size = res_queue.size; |
684 | if (WORDACCESS_UNSAFE) UNLOCK (reslock); |
761 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
685 | |
762 | |
686 | if (size) |
763 | if (size) |
687 | return; |
764 | return; |
688 | |
765 | |
689 | maybe_start_thread (); |
766 | maybe_start_thread (); |
690 | |
767 | |
691 | FD_ZERO(&rfd); |
768 | FD_ZERO (&rfd); |
692 | FD_SET(respipe [0], &rfd); |
769 | FD_SET (respipe [0], &rfd); |
693 | |
770 | |
694 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
771 | PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); |
695 | } |
772 | } |
696 | } |
773 | } |
697 | |
774 | |
698 | static int poll_cb () |
775 | static int poll_cb () |
699 | { |
776 | { |
… | |
… | |
713 | { |
790 | { |
714 | for (;;) |
791 | for (;;) |
715 | { |
792 | { |
716 | maybe_start_thread (); |
793 | maybe_start_thread (); |
717 | |
794 | |
718 | LOCK (reslock); |
795 | X_LOCK (reslock); |
719 | req = reqq_shift (&res_queue); |
796 | req = reqq_shift (&res_queue); |
720 | |
797 | |
721 | if (req) |
798 | if (req) |
722 | { |
799 | { |
723 | --npending; |
800 | --npending; |
724 | |
801 | |
725 | if (!res_queue.size) |
802 | if (!res_queue.size) |
726 | { |
803 | { |
727 | /* read any signals sent by the worker threads */ |
804 | /* read any signals sent by the worker threads */ |
728 | char buf [4]; |
805 | char buf [4]; |
729 | while (read (respipe [0], buf, 4) == 4) |
806 | while (PerlSock_recv (respipe [0], buf, 4, 0) == 4) |
730 | ; |
807 | ; |
731 | } |
808 | } |
732 | } |
809 | } |
733 | |
810 | |
734 | UNLOCK (reslock); |
811 | X_UNLOCK (reslock); |
735 | |
812 | |
736 | if (!req) |
813 | if (!req) |
737 | break; |
814 | break; |
738 | |
815 | |
739 | --nreqs; |
816 | --nreqs; |
… | |
… | |
745 | } |
822 | } |
746 | else |
823 | else |
747 | { |
824 | { |
748 | if (!req_invoke (req)) |
825 | if (!req_invoke (req)) |
749 | { |
826 | { |
750 | req_free (req); |
827 | req_destroy (req); |
751 | unblock_sig (); |
828 | unblock_sig (); |
752 | croak (0); |
829 | croak (0); |
753 | } |
830 | } |
754 | |
831 | |
755 | count++; |
832 | count++; |
756 | } |
833 | } |
757 | |
834 | |
758 | req_free (req); |
835 | req_destroy (req); |
759 | |
836 | |
760 | if (maxreqs && !--maxreqs) |
837 | if (maxreqs && !--maxreqs) |
761 | break; |
838 | break; |
762 | |
839 | |
763 | if (max_poll_time) |
840 | if (max_poll_time) |
… | |
… | |
777 | ++maxreqs; |
854 | ++maxreqs; |
778 | } |
855 | } |
779 | |
856 | |
780 | unblock_sig (); |
857 | unblock_sig (); |
781 | return count; |
858 | 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 | } |
859 | } |
795 | |
860 | |
796 | /*****************************************************************************/ |
861 | /*****************************************************************************/ |
797 | /* work around various missing functions */ |
862 | /* work around various missing functions */ |
798 | |
863 | |
… | |
… | |
803 | /* |
868 | /* |
804 | * make our pread/pwrite safe against themselves, but not against |
869 | * make our pread/pwrite safe against themselves, but not against |
805 | * normal read/write by using a mutex. slows down execution a lot, |
870 | * normal read/write by using a mutex. slows down execution a lot, |
806 | * but that's your problem, not mine. |
871 | * but that's your problem, not mine. |
807 | */ |
872 | */ |
808 | static mutex_t preadwritelock = MUTEX_INIT; |
873 | static mutex_t preadwritelock = X_MUTEX_INIT; |
809 | |
874 | |
810 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
875 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
811 | { |
876 | { |
812 | ssize_t res; |
877 | ssize_t res; |
813 | off_t ooffset; |
878 | off_t ooffset; |
814 | |
879 | |
815 | LOCK (preadwritelock); |
880 | X_LOCK (preadwritelock); |
816 | ooffset = lseek (fd, 0, SEEK_CUR); |
881 | ooffset = lseek (fd, 0, SEEK_CUR); |
817 | lseek (fd, offset, SEEK_SET); |
882 | lseek (fd, offset, SEEK_SET); |
818 | res = read (fd, buf, count); |
883 | res = read (fd, buf, count); |
819 | lseek (fd, ooffset, SEEK_SET); |
884 | lseek (fd, ooffset, SEEK_SET); |
820 | UNLOCK (preadwritelock); |
885 | X_UNLOCK (preadwritelock); |
821 | |
886 | |
822 | return res; |
887 | return res; |
823 | } |
888 | } |
824 | |
889 | |
825 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
890 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
826 | { |
891 | { |
827 | ssize_t res; |
892 | ssize_t res; |
828 | off_t ooffset; |
893 | off_t ooffset; |
829 | |
894 | |
830 | LOCK (preadwritelock); |
895 | X_LOCK (preadwritelock); |
831 | ooffset = lseek (fd, 0, SEEK_CUR); |
896 | ooffset = lseek (fd, 0, SEEK_CUR); |
832 | lseek (fd, offset, SEEK_SET); |
897 | lseek (fd, offset, SEEK_SET); |
833 | res = write (fd, buf, count); |
898 | res = write (fd, buf, count); |
834 | lseek (fd, offset, SEEK_SET); |
899 | lseek (fd, offset, SEEK_SET); |
835 | UNLOCK (preadwritelock); |
900 | X_UNLOCK (preadwritelock); |
836 | |
901 | |
837 | return res; |
902 | return res; |
838 | } |
903 | } |
839 | #endif |
904 | #endif |
840 | |
905 | |
… | |
… | |
873 | #if !HAVE_READAHEAD |
938 | #if !HAVE_READAHEAD |
874 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
939 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
875 | |
940 | |
876 | static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) |
941 | static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) |
877 | { |
942 | { |
|
|
943 | size_t todo = count; |
878 | dBUF; |
944 | dBUF; |
879 | |
945 | |
880 | while (count > 0) |
946 | while (todo > 0) |
881 | { |
947 | { |
882 | size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; |
948 | size_t len = todo < AIO_BUFSIZE ? todo : AIO_BUFSIZE; |
883 | |
949 | |
884 | pread (fd, aio_buf, len, offset); |
950 | pread (fd, aio_buf, len, offset); |
885 | offset += len; |
951 | offset += len; |
886 | count -= len; |
952 | todo -= len; |
887 | } |
953 | } |
888 | |
954 | |
889 | errno = 0; |
955 | errno = 0; |
|
|
956 | return count; |
890 | } |
957 | } |
891 | |
958 | |
892 | #endif |
959 | #endif |
893 | |
960 | |
894 | #if !HAVE_READDIR_R |
961 | #if !HAVE_READDIR_R |
895 | # define readdir_r aio_readdir_r |
962 | # define readdir_r aio_readdir_r |
896 | |
963 | |
897 | static mutex_t readdirlock = MUTEX_INIT; |
964 | static mutex_t readdirlock = X_MUTEX_INIT; |
898 | |
965 | |
899 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
966 | static int readdir_r (DIR *dirp, X_DIRENT *ent, X_DIRENT **res) |
900 | { |
967 | { |
901 | struct dirent *e; |
968 | X_DIRENT *e; |
902 | int errorno; |
969 | int errorno; |
903 | |
970 | |
904 | LOCK (readdirlock); |
971 | X_LOCK (readdirlock); |
905 | |
972 | |
906 | e = readdir (dirp); |
973 | e = readdir (dirp); |
907 | errorno = errno; |
974 | errorno = errno; |
908 | |
975 | |
909 | if (e) |
976 | if (e) |
… | |
… | |
912 | strcpy (ent->d_name, e->d_name); |
979 | strcpy (ent->d_name, e->d_name); |
913 | } |
980 | } |
914 | else |
981 | else |
915 | *res = 0; |
982 | *res = 0; |
916 | |
983 | |
917 | UNLOCK (readdirlock); |
984 | X_UNLOCK (readdirlock); |
918 | |
985 | |
919 | errno = errorno; |
986 | errno = errorno; |
920 | return e ? 0 : -1; |
987 | return e ? 0 : -1; |
921 | } |
988 | } |
922 | #endif |
989 | #endif |
… | |
… | |
1018 | static void scandir_ (aio_req req, worker *self) |
1085 | static void scandir_ (aio_req req, worker *self) |
1019 | { |
1086 | { |
1020 | DIR *dirp; |
1087 | DIR *dirp; |
1021 | union |
1088 | union |
1022 | { |
1089 | { |
1023 | struct dirent d; |
1090 | X_DIRENT d; |
1024 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
1091 | char b [offsetof (X_DIRENT, d_name) + NAME_MAX + 1]; |
1025 | } *u; |
1092 | } *u; |
1026 | struct dirent *entp; |
1093 | X_DIRENT *entp; |
1027 | char *name, *names; |
1094 | char *name, *names; |
1028 | int memlen = 4096; |
1095 | int memlen = 4096; |
1029 | int memofs = 0; |
1096 | int memofs = 0; |
1030 | int res = 0; |
1097 | int res = 0; |
1031 | int errorno; |
|
|
1032 | |
1098 | |
1033 | LOCK (wrklock); |
1099 | X_LOCK (wrklock); |
1034 | self->dirp = dirp = opendir (req->ptr1); |
1100 | self->dirp = dirp = opendir (req->ptr1); |
1035 | self->dbuf = u = malloc (sizeof (*u)); |
1101 | self->dbuf = u = malloc (sizeof (*u)); |
1036 | req->flags |= FLAG_PTR2_FREE; |
1102 | req->flags |= FLAG_PTR2_FREE; |
1037 | req->ptr2 = names = malloc (memlen); |
1103 | req->ptr2 = names = malloc (memlen); |
1038 | UNLOCK (wrklock); |
1104 | X_UNLOCK (wrklock); |
1039 | |
1105 | |
1040 | if (dirp && u && names) |
1106 | if (dirp && u && names) |
1041 | for (;;) |
1107 | for (;;) |
1042 | { |
1108 | { |
1043 | errno = 0; |
1109 | errno = 0; |
… | |
… | |
1055 | res++; |
1121 | res++; |
1056 | |
1122 | |
1057 | while (memofs + len > memlen) |
1123 | while (memofs + len > memlen) |
1058 | { |
1124 | { |
1059 | memlen *= 2; |
1125 | memlen *= 2; |
1060 | LOCK (wrklock); |
1126 | X_LOCK (wrklock); |
1061 | req->ptr2 = names = realloc (names, memlen); |
1127 | req->ptr2 = names = realloc (names, memlen); |
1062 | UNLOCK (wrklock); |
1128 | X_UNLOCK (wrklock); |
1063 | |
1129 | |
1064 | if (!names) |
1130 | if (!names) |
1065 | break; |
1131 | break; |
1066 | } |
1132 | } |
1067 | |
1133 | |
… | |
… | |
1076 | req->result = res; |
1142 | req->result = res; |
1077 | } |
1143 | } |
1078 | |
1144 | |
1079 | /*****************************************************************************/ |
1145 | /*****************************************************************************/ |
1080 | |
1146 | |
1081 | static void *aio_proc (void *thr_arg) |
1147 | X_THREAD_PROC (aio_proc) |
1082 | { |
1148 | { |
|
|
1149 | {//D |
1083 | aio_req req; |
1150 | aio_req req; |
1084 | struct timespec ts; |
1151 | struct timespec ts; |
1085 | worker *self = (worker *)thr_arg; |
1152 | worker *self = (worker *)thr_arg; |
1086 | |
1153 | |
1087 | /* try to distribute timeouts somewhat evenly */ |
1154 | /* try to distribute timeouts somewhat randomly */ |
1088 | ts.tv_nsec = (((unsigned long)self + (unsigned long)ts.tv_sec) & 1023UL) |
1155 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
1089 | * (1000000000UL / 1024UL); |
|
|
1090 | |
1156 | |
1091 | for (;;) |
1157 | for (;;) |
1092 | { |
1158 | { |
1093 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1159 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1094 | |
1160 | |
1095 | LOCK (reqlock); |
1161 | X_LOCK (reqlock); |
1096 | |
1162 | |
1097 | for (;;) |
1163 | for (;;) |
1098 | { |
1164 | { |
1099 | self->req = req = reqq_shift (&req_queue); |
1165 | self->req = req = reqq_shift (&req_queue); |
1100 | |
1166 | |
1101 | if (req) |
1167 | if (req) |
1102 | break; |
1168 | break; |
1103 | |
1169 | |
1104 | ++idle; |
1170 | ++idle; |
1105 | |
1171 | |
1106 | if (COND_TIMEDWAIT (reqwait, reqlock, ts) |
1172 | if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) |
1107 | == ETIMEDOUT) |
1173 | == ETIMEDOUT) |
1108 | { |
1174 | { |
1109 | if (idle > max_idle) |
1175 | if (idle > max_idle) |
1110 | { |
1176 | { |
1111 | --idle; |
1177 | --idle; |
1112 | UNLOCK (reqlock); |
1178 | X_UNLOCK (reqlock); |
1113 | LOCK (wrklock); |
1179 | X_LOCK (wrklock); |
1114 | --started; |
1180 | --started; |
1115 | UNLOCK (wrklock); |
1181 | X_UNLOCK (wrklock); |
1116 | goto quit; |
1182 | goto quit; |
1117 | } |
1183 | } |
1118 | |
1184 | |
1119 | /* we are allowed to idle, so do so without any timeout */ |
1185 | /* we are allowed to idle, so do so without any timeout */ |
1120 | COND_WAIT (reqwait, reqlock); |
1186 | X_COND_WAIT (reqwait, reqlock); |
1121 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1187 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1122 | } |
1188 | } |
1123 | |
1189 | |
1124 | --idle; |
1190 | --idle; |
1125 | } |
1191 | } |
1126 | |
1192 | |
1127 | --nready; |
1193 | --nready; |
1128 | |
1194 | |
1129 | UNLOCK (reqlock); |
1195 | X_UNLOCK (reqlock); |
1130 | |
1196 | |
1131 | errno = 0; /* strictly unnecessary */ |
1197 | errno = 0; /* strictly unnecessary */ |
1132 | |
1198 | |
1133 | if (!(req->flags & FLAG_CANCELLED)) |
1199 | if (!(req->flags & FLAG_CANCELLED)) |
1134 | switch (req->type) |
1200 | switch (req->type) |
1135 | { |
1201 | { |
1136 | case REQ_READ: req->result = pread (req->int1, req->ptr1, req->size, req->offs); break; |
1202 | case REQ_READ: req->result = req->offs >= 0 |
1137 | case REQ_WRITE: req->result = pwrite (req->int1, req->ptr1, req->size, req->offs); break; |
1203 | ? pread (req->int1, req->ptr1, req->size, req->offs) |
|
|
1204 | : read (req->int1, req->ptr1, req->size); break; |
|
|
1205 | case REQ_WRITE: req->result = req->offs >= 0 |
|
|
1206 | ? pwrite (req->int1, req->ptr1, req->size, req->offs) |
|
|
1207 | : write (req->int1, req->ptr1, req->size); break; |
1138 | |
1208 | |
1139 | case REQ_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
1209 | 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; |
1210 | case REQ_SENDFILE: req->result = sendfile_ (req->int1, req->int2, req->offs, req->size, self); break; |
1141 | |
1211 | |
1142 | case REQ_STAT: req->result = stat (req->ptr1, (Stat_t *)req->ptr2); break; |
1212 | case REQ_STAT: req->result = stat (req->ptr1, (Stat_t *)req->ptr2); break; |
… | |
… | |
1145 | |
1215 | |
1146 | case REQ_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break; |
1216 | case REQ_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break; |
1147 | case REQ_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; |
1217 | case REQ_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; |
1148 | case REQ_CHMOD: req->result = chmod (req->ptr1, req->mode); break; |
1218 | case REQ_CHMOD: req->result = chmod (req->ptr1, req->mode); break; |
1149 | case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break; |
1219 | case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break; |
|
|
1220 | case REQ_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break; |
|
|
1221 | case REQ_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; |
1150 | |
1222 | |
1151 | case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break; |
1223 | case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break; |
1152 | case REQ_CLOSE: req->result = close (req->int1); break; |
1224 | case REQ_CLOSE: req->result = close (req->int1); break; |
1153 | case REQ_UNLINK: req->result = unlink (req->ptr1); break; |
1225 | case REQ_UNLINK: req->result = unlink (req->ptr1); break; |
1154 | case REQ_RMDIR: req->result = rmdir (req->ptr1); break; |
1226 | case REQ_RMDIR: req->result = rmdir (req->ptr1); break; |
… | |
… | |
1162 | case REQ_FDATASYNC: req->result = fdatasync (req->int1); break; |
1234 | case REQ_FDATASYNC: req->result = fdatasync (req->int1); break; |
1163 | case REQ_FSYNC: req->result = fsync (req->int1); break; |
1235 | case REQ_FSYNC: req->result = fsync (req->int1); break; |
1164 | case REQ_READDIR: scandir_ (req, self); break; |
1236 | case REQ_READDIR: scandir_ (req, self); break; |
1165 | |
1237 | |
1166 | case REQ_BUSY: |
1238 | case REQ_BUSY: |
|
|
1239 | #ifdef _WIN32 |
|
|
1240 | Sleep (req->nv1 * 1000.); |
|
|
1241 | #else |
1167 | { |
1242 | { |
1168 | struct timeval tv; |
1243 | struct timeval tv; |
1169 | |
1244 | |
1170 | tv.tv_sec = req->nv1; |
1245 | tv.tv_sec = req->nv1; |
1171 | tv.tv_usec = (req->nv1 - tv.tv_usec) * 1000000.; |
1246 | tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.; |
1172 | |
1247 | |
1173 | req->result = select (0, 0, 0, 0, &tv); |
1248 | req->result = select (0, 0, 0, 0, &tv); |
1174 | } |
1249 | } |
|
|
1250 | #endif |
|
|
1251 | break; |
1175 | |
1252 | |
1176 | case REQ_UTIME: |
1253 | case REQ_UTIME: |
1177 | case REQ_FUTIME: |
1254 | case REQ_FUTIME: |
1178 | { |
1255 | { |
1179 | struct timeval tv[2]; |
1256 | struct timeval tv[2]; |
… | |
… | |
1209 | break; |
1286 | break; |
1210 | } |
1287 | } |
1211 | |
1288 | |
1212 | req->errorno = errno; |
1289 | req->errorno = errno; |
1213 | |
1290 | |
1214 | LOCK (reslock); |
1291 | X_LOCK (reslock); |
1215 | |
1292 | |
1216 | ++npending; |
1293 | ++npending; |
1217 | |
1294 | |
1218 | if (!reqq_push (&res_queue, req)) |
1295 | if (!reqq_push (&res_queue, req)) |
1219 | { |
1296 | { |
1220 | /* write a dummy byte to the pipe so fh becomes ready */ |
1297 | /* write a dummy byte to the pipe so fh becomes ready */ |
1221 | write (respipe [1], &respipe, 1); |
1298 | send (respipe_osf [1], (const void *)&respipe_osf, 1, 0); |
1222 | |
1299 | |
1223 | /* optionally signal the main thread asynchronously */ |
1300 | /* optionally signal the main thread asynchronously */ |
1224 | if (main_sig) |
1301 | if (main_sig) |
1225 | pthread_kill (main_tid, main_sig); |
1302 | pthread_kill (main_tid, main_sig); |
1226 | } |
1303 | } |
1227 | |
1304 | |
1228 | self->req = 0; |
1305 | self->req = 0; |
1229 | worker_clear (self); |
1306 | worker_clear (self); |
1230 | |
1307 | |
1231 | UNLOCK (reslock); |
1308 | X_UNLOCK (reslock); |
1232 | } |
1309 | } |
1233 | |
1310 | |
1234 | quit: |
1311 | quit: |
1235 | LOCK (wrklock); |
1312 | X_LOCK (wrklock); |
1236 | worker_free (self); |
1313 | worker_free (self); |
1237 | UNLOCK (wrklock); |
1314 | X_UNLOCK (wrklock); |
1238 | |
1315 | |
1239 | return 0; |
1316 | return 0; |
|
|
1317 | }//D |
1240 | } |
1318 | } |
1241 | |
1319 | |
1242 | /*****************************************************************************/ |
1320 | /*****************************************************************************/ |
1243 | |
1321 | |
1244 | static void atfork_prepare (void) |
1322 | static void atfork_prepare (void) |
1245 | { |
1323 | { |
1246 | LOCK (wrklock); |
1324 | X_LOCK (wrklock); |
1247 | LOCK (reqlock); |
1325 | X_LOCK (reqlock); |
1248 | LOCK (reslock); |
1326 | X_LOCK (reslock); |
1249 | #if !HAVE_PREADWRITE |
1327 | #if !HAVE_PREADWRITE |
1250 | LOCK (preadwritelock); |
1328 | X_LOCK (preadwritelock); |
1251 | #endif |
1329 | #endif |
1252 | #if !HAVE_READDIR_R |
1330 | #if !HAVE_READDIR_R |
1253 | LOCK (readdirlock); |
1331 | X_LOCK (readdirlock); |
1254 | #endif |
1332 | #endif |
1255 | } |
1333 | } |
1256 | |
1334 | |
1257 | static void atfork_parent (void) |
1335 | static void atfork_parent (void) |
1258 | { |
1336 | { |
1259 | #if !HAVE_READDIR_R |
1337 | #if !HAVE_READDIR_R |
1260 | UNLOCK (readdirlock); |
1338 | X_UNLOCK (readdirlock); |
1261 | #endif |
1339 | #endif |
1262 | #if !HAVE_PREADWRITE |
1340 | #if !HAVE_PREADWRITE |
1263 | UNLOCK (preadwritelock); |
1341 | X_UNLOCK (preadwritelock); |
1264 | #endif |
1342 | #endif |
1265 | UNLOCK (reslock); |
1343 | X_UNLOCK (reslock); |
1266 | UNLOCK (reqlock); |
1344 | X_UNLOCK (reqlock); |
1267 | UNLOCK (wrklock); |
1345 | X_UNLOCK (wrklock); |
1268 | } |
1346 | } |
1269 | |
1347 | |
1270 | static void atfork_child (void) |
1348 | static void atfork_child (void) |
1271 | { |
1349 | { |
1272 | aio_req prv; |
1350 | aio_req prv; |
1273 | |
1351 | |
1274 | while (prv = reqq_shift (&req_queue)) |
1352 | while (prv = reqq_shift (&req_queue)) |
1275 | req_free (prv); |
1353 | req_destroy (prv); |
1276 | |
1354 | |
1277 | while (prv = reqq_shift (&res_queue)) |
1355 | while (prv = reqq_shift (&res_queue)) |
1278 | req_free (prv); |
1356 | req_destroy (prv); |
1279 | |
1357 | |
1280 | while (wrk_first.next != &wrk_first) |
1358 | while (wrk_first.next != &wrk_first) |
1281 | { |
1359 | { |
1282 | worker *wrk = wrk_first.next; |
1360 | worker *wrk = wrk_first.next; |
1283 | |
1361 | |
1284 | if (wrk->req) |
1362 | if (wrk->req) |
1285 | req_free (wrk->req); |
1363 | req_destroy (wrk->req); |
1286 | |
1364 | |
1287 | worker_clear (wrk); |
1365 | worker_clear (wrk); |
1288 | worker_free (wrk); |
1366 | worker_free (wrk); |
1289 | } |
1367 | } |
1290 | |
1368 | |
… | |
… | |
1292 | idle = 0; |
1370 | idle = 0; |
1293 | nreqs = 0; |
1371 | nreqs = 0; |
1294 | nready = 0; |
1372 | nready = 0; |
1295 | npending = 0; |
1373 | npending = 0; |
1296 | |
1374 | |
1297 | close (respipe [0]); |
1375 | PerlSock_closesocket (respipe [0]); |
1298 | close (respipe [1]); |
1376 | PerlSock_closesocket (respipe [1]); |
|
|
1377 | |
1299 | create_pipe (); |
1378 | create_pipe (respipe); |
1300 | |
1379 | |
1301 | atfork_parent (); |
1380 | atfork_parent (); |
1302 | } |
1381 | } |
1303 | |
1382 | |
1304 | #define dREQ \ |
1383 | #define dREQ \ |
… | |
… | |
1333 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
1412 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
1334 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
1413 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
1335 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
1414 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
1336 | newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); |
1415 | newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); |
1337 | newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); |
1416 | newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); |
|
|
1417 | #ifdef _WIN32 |
|
|
1418 | X_MUTEX_CHECK (wrklock); |
|
|
1419 | X_MUTEX_CHECK (reslock); |
|
|
1420 | X_MUTEX_CHECK (reqlock); |
|
|
1421 | X_MUTEX_CHECK (reqwait); |
|
|
1422 | X_MUTEX_CHECK (preadwritelock); |
|
|
1423 | X_MUTEX_CHECK (readdirlock); |
|
|
1424 | |
|
|
1425 | X_COND_CHECK (reqwait); |
|
|
1426 | #else |
1338 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
1427 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
1339 | newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); |
1428 | newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); |
|
|
1429 | #endif |
1340 | |
1430 | |
1341 | create_pipe (); |
1431 | create_pipe (respipe); |
|
|
1432 | |
1342 | ATFORK (atfork_prepare, atfork_parent, atfork_child); |
1433 | X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child); |
1343 | } |
1434 | } |
1344 | |
1435 | |
1345 | void |
1436 | void |
1346 | max_poll_reqs (int nreqs) |
1437 | max_poll_reqs (int nreqs) |
1347 | PROTOTYPE: $ |
1438 | PROTOTYPE: $ |
… | |
… | |
1410 | |
1501 | |
1411 | REQ_SEND (req); |
1502 | REQ_SEND (req); |
1412 | } |
1503 | } |
1413 | |
1504 | |
1414 | void |
1505 | void |
1415 | aio_read (SV *fh, UV offset, UV length, SV8 *data, UV dataoffset, SV *callback=&PL_sv_undef) |
1506 | aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef) |
1416 | ALIAS: |
1507 | ALIAS: |
1417 | aio_read = REQ_READ |
1508 | aio_read = REQ_READ |
1418 | aio_write = REQ_WRITE |
1509 | aio_write = REQ_WRITE |
1419 | PROTOTYPE: $$$$$;$ |
1510 | PROTOTYPE: $$$$$;$ |
1420 | PPCODE: |
1511 | PPCODE: |
1421 | { |
1512 | { |
1422 | STRLEN svlen; |
1513 | STRLEN svlen; |
1423 | char *svptr = SvPVbyte (data, svlen); |
1514 | char *svptr = SvPVbyte (data, svlen); |
|
|
1515 | UV len = SvUV (length); |
1424 | |
1516 | |
1425 | SvUPGRADE (data, SVt_PV); |
1517 | SvUPGRADE (data, SVt_PV); |
1426 | SvPOK_on (data); |
1518 | SvPOK_on (data); |
1427 | |
1519 | |
1428 | if (dataoffset < 0) |
1520 | if (dataoffset < 0) |
1429 | dataoffset += svlen; |
1521 | dataoffset += svlen; |
1430 | |
1522 | |
1431 | if (dataoffset < 0 || dataoffset > svlen) |
1523 | if (dataoffset < 0 || dataoffset > svlen) |
1432 | croak ("data offset outside of string"); |
1524 | croak ("dataoffset outside of data scalar"); |
1433 | |
1525 | |
1434 | if (ix == REQ_WRITE) |
1526 | if (ix == REQ_WRITE) |
1435 | { |
1527 | { |
1436 | /* write: check length and adjust. */ |
1528 | /* write: check length and adjust. */ |
1437 | if (length < 0 || length + dataoffset > svlen) |
1529 | if (!SvOK (length) || len + dataoffset > svlen) |
1438 | length = svlen - dataoffset; |
1530 | len = svlen - dataoffset; |
1439 | } |
1531 | } |
1440 | else |
1532 | else |
1441 | { |
1533 | { |
1442 | /* read: grow scalar as necessary */ |
1534 | /* read: grow scalar as necessary */ |
1443 | svptr = SvGROW (data, length + dataoffset + 1); |
1535 | svptr = SvGROW (data, len + dataoffset + 1); |
1444 | } |
1536 | } |
1445 | |
1537 | |
1446 | if (length < 0) |
1538 | if (len < 0) |
1447 | croak ("length must not be negative"); |
1539 | croak ("length must not be negative"); |
1448 | |
1540 | |
1449 | { |
1541 | { |
1450 | dREQ; |
1542 | dREQ; |
1451 | |
1543 | |
1452 | req->type = ix; |
1544 | req->type = ix; |
1453 | req->sv1 = newSVsv (fh); |
1545 | req->sv1 = newSVsv (fh); |
1454 | req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
1546 | req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
1455 | : IoOFP (sv_2io (fh))); |
1547 | : IoOFP (sv_2io (fh))); |
1456 | req->offs = offset; |
1548 | req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; |
1457 | req->size = length; |
1549 | req->size = len; |
1458 | req->sv2 = SvREFCNT_inc (data); |
1550 | req->sv2 = SvREFCNT_inc (data); |
1459 | req->ptr1 = (char *)svptr + dataoffset; |
1551 | req->ptr1 = (char *)svptr + dataoffset; |
1460 | req->stroffset = dataoffset; |
1552 | req->stroffset = dataoffset; |
1461 | |
1553 | |
1462 | if (!SvREADONLY (data)) |
1554 | if (!SvREADONLY (data)) |
… | |
… | |
1488 | |
1580 | |
1489 | REQ_SEND; |
1581 | REQ_SEND; |
1490 | } |
1582 | } |
1491 | |
1583 | |
1492 | void |
1584 | void |
1493 | aio_sendfile (SV *out_fh, SV *in_fh, UV in_offset, UV length, SV *callback=&PL_sv_undef) |
1585 | aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef) |
1494 | PROTOTYPE: $$$$;$ |
1586 | PROTOTYPE: $$$$;$ |
1495 | PPCODE: |
1587 | PPCODE: |
1496 | { |
1588 | { |
1497 | dREQ; |
1589 | dREQ; |
1498 | |
1590 | |
1499 | req->type = REQ_SENDFILE; |
1591 | req->type = REQ_SENDFILE; |
1500 | req->sv1 = newSVsv (out_fh); |
1592 | req->sv1 = newSVsv (out_fh); |
1501 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
1593 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
1502 | req->sv2 = newSVsv (in_fh); |
1594 | req->sv2 = newSVsv (in_fh); |
1503 | req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
1595 | req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
1504 | req->offs = in_offset; |
1596 | req->offs = SvVAL64 (in_offset); |
1505 | req->size = length; |
1597 | req->size = length; |
1506 | |
1598 | |
1507 | REQ_SEND; |
1599 | REQ_SEND; |
1508 | } |
1600 | } |
1509 | |
1601 | |
1510 | void |
1602 | void |
1511 | aio_readahead (SV *fh, UV offset, IV length, SV *callback=&PL_sv_undef) |
1603 | aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef) |
1512 | PROTOTYPE: $$$;$ |
1604 | PROTOTYPE: $$$;$ |
1513 | PPCODE: |
1605 | PPCODE: |
1514 | { |
1606 | { |
1515 | dREQ; |
1607 | dREQ; |
1516 | |
1608 | |
1517 | req->type = REQ_READAHEAD; |
1609 | req->type = REQ_READAHEAD; |
1518 | req->sv1 = newSVsv (fh); |
1610 | req->sv1 = newSVsv (fh); |
1519 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
1611 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
1520 | req->offs = offset; |
1612 | req->offs = SvVAL64 (offset); |
1521 | req->size = length; |
1613 | req->size = length; |
1522 | |
1614 | |
1523 | REQ_SEND; |
1615 | REQ_SEND; |
1524 | } |
1616 | } |
1525 | |
1617 | |
… | |
… | |
1533 | dREQ; |
1625 | dREQ; |
1534 | |
1626 | |
1535 | req->ptr2 = malloc (sizeof (Stat_t)); |
1627 | req->ptr2 = malloc (sizeof (Stat_t)); |
1536 | if (!req->ptr2) |
1628 | if (!req->ptr2) |
1537 | { |
1629 | { |
1538 | req_free (req); |
1630 | req_destroy (req); |
1539 | croak ("out of memory during aio_stat statdata allocation"); |
1631 | croak ("out of memory during aio_stat statdata allocation"); |
1540 | } |
1632 | } |
1541 | |
1633 | |
1542 | req->flags |= FLAG_PTR2_FREE; |
1634 | req->flags |= FLAG_PTR2_FREE; |
1543 | req->sv1 = newSVsv (fh_or_path); |
1635 | req->sv1 = newSVsv (fh_or_path); |
… | |
… | |
1572 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1664 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1573 | } |
1665 | } |
1574 | else |
1666 | else |
1575 | { |
1667 | { |
1576 | req->type = REQ_FUTIME; |
1668 | req->type = REQ_FUTIME; |
|
|
1669 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
1670 | } |
|
|
1671 | |
|
|
1672 | REQ_SEND; |
|
|
1673 | } |
|
|
1674 | |
|
|
1675 | void |
|
|
1676 | aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef) |
|
|
1677 | PPCODE: |
|
|
1678 | { |
|
|
1679 | dREQ; |
|
|
1680 | |
|
|
1681 | req->sv1 = newSVsv (fh_or_path); |
|
|
1682 | req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; |
|
|
1683 | |
|
|
1684 | if (SvPOK (fh_or_path)) |
|
|
1685 | { |
|
|
1686 | req->type = REQ_TRUNCATE; |
|
|
1687 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1688 | } |
|
|
1689 | else |
|
|
1690 | { |
|
|
1691 | req->type = REQ_FTRUNCATE; |
1577 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1692 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1578 | } |
1693 | } |
1579 | |
1694 | |
1580 | REQ_SEND; |
1695 | REQ_SEND; |
1581 | } |
1696 | } |
… | |
… | |
1797 | CODE: |
1912 | CODE: |
1798 | { |
1913 | { |
1799 | if (block_sig_level) |
1914 | if (block_sig_level) |
1800 | croak ("cannot call IO::AIO::setsig from within aio_block/callback"); |
1915 | croak ("cannot call IO::AIO::setsig from within aio_block/callback"); |
1801 | |
1916 | |
1802 | LOCK (reslock); |
1917 | X_LOCK (reslock); |
1803 | main_tid = pthread_self (); |
1918 | main_tid = pthread_self (); |
1804 | main_sig = signum; |
1919 | main_sig = signum; |
1805 | UNLOCK (reslock); |
1920 | X_UNLOCK (reslock); |
1806 | |
1921 | |
1807 | if (main_sig && npending) |
1922 | if (main_sig && npending) |
1808 | pthread_kill (main_tid, main_sig); |
1923 | pthread_kill (main_tid, main_sig); |
1809 | } |
1924 | } |
1810 | |
1925 | |
… | |
… | |
1854 | |
1969 | |
1855 | int |
1970 | int |
1856 | nthreads() |
1971 | nthreads() |
1857 | PROTOTYPE: |
1972 | PROTOTYPE: |
1858 | CODE: |
1973 | CODE: |
1859 | if (WORDACCESS_UNSAFE) LOCK (wrklock); |
1974 | if (WORDACCESS_UNSAFE) X_LOCK (wrklock); |
1860 | RETVAL = started; |
1975 | RETVAL = started; |
1861 | if (WORDACCESS_UNSAFE) UNLOCK (wrklock); |
1976 | if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock); |
1862 | OUTPUT: |
1977 | OUTPUT: |
1863 | RETVAL |
1978 | RETVAL |
1864 | |
1979 | |
1865 | PROTOTYPES: DISABLE |
1980 | PROTOTYPES: DISABLE |
1866 | |
1981 | |