… | |
… | |
4 | |
4 | |
5 | #include "EXTERN.h" |
5 | #include "EXTERN.h" |
6 | #include "perl.h" |
6 | #include "perl.h" |
7 | #include "XSUB.h" |
7 | #include "XSUB.h" |
8 | |
8 | |
9 | #include "autoconf/config.h" |
|
|
10 | |
|
|
11 | #include <stddef.h> |
9 | #include <stddef.h> |
12 | #include <stdlib.h> |
10 | #include <stdlib.h> |
13 | #include <errno.h> |
11 | #include <errno.h> |
14 | #include <sys/time.h> |
|
|
15 | #include <sys/select.h> |
|
|
16 | #include <sys/types.h> |
12 | #include <sys/types.h> |
17 | #include <sys/stat.h> |
13 | #include <sys/stat.h> |
18 | #include <limits.h> |
14 | #include <limits.h> |
19 | #include <unistd.h> |
|
|
20 | #include <fcntl.h> |
15 | #include <fcntl.h> |
21 | #include <signal.h> |
|
|
22 | #include <sched.h> |
16 | #include <sched.h> |
|
|
17 | |
|
|
18 | #ifdef _WIN32 |
|
|
19 | |
|
|
20 | # define SIGIO 0 |
|
|
21 | typedef Direntry_t X_DIRENT; |
|
|
22 | #undef malloc |
|
|
23 | #undef free |
|
|
24 | |
|
|
25 | // perl overrides all those nice win32 functions |
|
|
26 | # undef open |
|
|
27 | # undef read |
|
|
28 | # undef write |
|
|
29 | # undef stat |
|
|
30 | # undef fstat |
|
|
31 | # define lstat stat |
|
|
32 | # undef truncate |
|
|
33 | # undef ftruncate |
|
|
34 | # undef open |
|
|
35 | # undef close |
|
|
36 | # undef unlink |
|
|
37 | # undef rmdir |
|
|
38 | # undef rename |
|
|
39 | # undef lseek |
|
|
40 | |
|
|
41 | # define chown(a,b,c) (errno = ENOSYS, -1) |
|
|
42 | # define fchown(a,b,c) (errno = ENOSYS, -1) |
|
|
43 | # define fchmod(a,b) (errno = ENOSYS, -1) |
|
|
44 | # define symlink(a,b) (errno = ENOSYS, -1) |
|
|
45 | # define readlink(a,b,c) (errno = ENOSYS, -1) |
|
|
46 | # define mknod(a,b,c) (errno = ENOSYS, -1) |
|
|
47 | # define truncate(a,b) (errno = ENOSYS, -1) |
|
|
48 | # define ftruncate(fd,o) chsize ((fd), (o)) |
|
|
49 | # define fsync(fd) _commit (fd) |
|
|
50 | # define opendir(fd) (errno = ENOSYS, 0) |
|
|
51 | # define readdir(fd) (errno = ENOSYS, -1) |
|
|
52 | # define closedir(fd) (errno = ENOSYS, -1) |
|
|
53 | # define mkdir(a,b) mkdir (a) |
|
|
54 | |
|
|
55 | #else |
|
|
56 | |
|
|
57 | # include "autoconf/config.h" |
|
|
58 | # include <sys/time.h> |
|
|
59 | # include <sys/select.h> |
|
|
60 | # include <unistd.h> |
23 | #include <utime.h> |
61 | # include <utime.h> |
|
|
62 | # include <signal.h> |
|
|
63 | typedef struct dirent X_DIRENT; |
|
|
64 | |
|
|
65 | #endif |
24 | |
66 | |
25 | #if HAVE_SENDFILE |
67 | #if HAVE_SENDFILE |
26 | # if __linux |
68 | # if __linux |
27 | # include <sys/sendfile.h> |
69 | # include <sys/sendfile.h> |
28 | # elif __freebsd |
70 | # elif __freebsd |
… | |
… | |
46 | #endif |
88 | #endif |
47 | |
89 | |
48 | /* buffer size for various temporary buffers */ |
90 | /* buffer size for various temporary buffers */ |
49 | #define AIO_BUFSIZE 65536 |
91 | #define AIO_BUFSIZE 65536 |
50 | |
92 | |
|
|
93 | /* use NV for 32 bit perls as it allows larger offsets */ |
|
|
94 | #if IVSIZE >= 8 |
|
|
95 | # define SvVAL64 SvIV |
|
|
96 | #else |
|
|
97 | # define SvVAL64 SvNV |
|
|
98 | #endif |
|
|
99 | |
51 | #define dBUF \ |
100 | #define dBUF \ |
52 | char *aio_buf; \ |
101 | char *aio_buf; \ |
53 | LOCK (wrklock); \ |
102 | X_LOCK (wrklock); \ |
54 | self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ |
103 | self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ |
55 | UNLOCK (wrklock); \ |
104 | X_UNLOCK (wrklock); \ |
56 | if (!aio_buf) \ |
105 | if (!aio_buf) \ |
57 | return -1; |
106 | return -1; |
58 | |
107 | |
59 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
108 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
60 | |
109 | |
61 | enum { |
110 | enum { |
62 | REQ_QUIT, |
111 | REQ_QUIT, |
63 | REQ_OPEN, REQ_CLOSE, |
112 | REQ_OPEN, REQ_CLOSE, |
64 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
113 | REQ_READ, REQ_WRITE, |
65 | REQ_SENDFILE, |
114 | REQ_READAHEAD, REQ_SENDFILE, |
66 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
115 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
67 | REQ_UTIME, REQ_FUTIME, /* must be consecutive */ |
116 | REQ_TRUNCATE, REQ_FTRUNCATE, |
68 | REQ_CHMOD, REQ_FCHMOD, /* must be consecutive */ |
117 | REQ_UTIME, REQ_FUTIME, |
69 | REQ_CHOWN, REQ_FCHOWN, /* must be consecutive */ |
118 | REQ_CHMOD, REQ_FCHMOD, |
|
|
119 | REQ_CHOWN, REQ_FCHOWN, |
70 | REQ_FSYNC, REQ_FDATASYNC, |
120 | REQ_FSYNC, REQ_FDATASYNC, |
71 | REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME, |
121 | REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME, |
72 | REQ_MKNOD, REQ_READDIR, |
122 | REQ_MKNOD, REQ_READDIR, |
73 | REQ_LINK, REQ_SYMLINK, REQ_READLINK, |
123 | REQ_LINK, REQ_SYMLINK, REQ_READLINK, |
74 | REQ_GROUP, REQ_NOP, |
124 | REQ_GROUP, REQ_NOP, |
… | |
… | |
103 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
153 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
104 | } aio_cb; |
154 | } aio_cb; |
105 | |
155 | |
106 | enum { |
156 | enum { |
107 | FLAG_CANCELLED = 0x01, /* request was cancelled */ |
157 | FLAG_CANCELLED = 0x01, /* request was cancelled */ |
108 | FLAG_SV1_RO_OFF = 0x40, /* data was set readonly */ |
158 | FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ |
109 | FLAG_PTR2_FREE = 0x80, /* need to free(ptr2) */ |
159 | FLAG_PTR2_FREE = 0x80, /* need to free(ptr2) */ |
110 | }; |
160 | }; |
111 | |
161 | |
112 | typedef aio_cb *aio_req; |
162 | typedef aio_cb *aio_req; |
113 | typedef aio_cb *aio_req_ornot; |
163 | typedef aio_cb *aio_req_ornot; |
… | |
… | |
170 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
220 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
171 | |
221 | |
172 | static unsigned int started, idle, wanted; |
222 | static unsigned int started, idle, wanted; |
173 | |
223 | |
174 | /* worker threads management */ |
224 | /* worker threads management */ |
175 | static mutex_t wrklock = MUTEX_INIT; |
225 | static mutex_t wrklock = X_MUTEX_INIT; |
176 | |
226 | |
177 | typedef struct worker { |
227 | typedef struct worker { |
178 | /* locked by wrklock */ |
228 | /* locked by wrklock */ |
179 | struct worker *prev, *next; |
229 | struct worker *prev, *next; |
180 | |
230 | |
… | |
… | |
214 | static volatile unsigned int nreqs, nready, npending; |
264 | static volatile unsigned int nreqs, nready, npending; |
215 | static volatile unsigned int max_idle = 4; |
265 | static volatile unsigned int max_idle = 4; |
216 | static volatile unsigned int max_outstanding = 0xffffffff; |
266 | static volatile unsigned int max_outstanding = 0xffffffff; |
217 | static int respipe [2]; |
267 | static int respipe [2]; |
218 | |
268 | |
219 | static mutex_t reslock = MUTEX_INIT; |
269 | static mutex_t reslock = X_MUTEX_INIT; |
220 | static mutex_t reqlock = MUTEX_INIT; |
270 | static mutex_t reqlock = X_MUTEX_INIT; |
221 | static cond_t reqwait = COND_INIT; |
271 | static cond_t reqwait = X_COND_INIT; |
222 | |
272 | |
223 | #if WORDACCESS_UNSAFE |
273 | #if WORDACCESS_UNSAFE |
224 | |
274 | |
225 | static unsigned int get_nready () |
275 | static unsigned int get_nready () |
226 | { |
276 | { |
227 | unsigned int retval; |
277 | unsigned int retval; |
228 | |
278 | |
229 | LOCK (reqlock); |
279 | X_LOCK (reqlock); |
230 | retval = nready; |
280 | retval = nready; |
231 | UNLOCK (reqlock); |
281 | X_UNLOCK (reqlock); |
232 | |
282 | |
233 | return retval; |
283 | return retval; |
234 | } |
284 | } |
235 | |
285 | |
236 | static unsigned int get_npending () |
286 | static unsigned int get_npending () |
237 | { |
287 | { |
238 | unsigned int retval; |
288 | unsigned int retval; |
239 | |
289 | |
240 | LOCK (reslock); |
290 | X_LOCK (reslock); |
241 | retval = npending; |
291 | retval = npending; |
242 | UNLOCK (reslock); |
292 | X_UNLOCK (reslock); |
243 | |
293 | |
244 | return retval; |
294 | return retval; |
245 | } |
295 | } |
246 | |
296 | |
247 | static unsigned int get_nthreads () |
297 | static unsigned int get_nthreads () |
248 | { |
298 | { |
249 | unsigned int retval; |
299 | unsigned int retval; |
250 | |
300 | |
251 | LOCK (wrklock); |
301 | X_LOCK (wrklock); |
252 | retval = started; |
302 | retval = started; |
253 | UNLOCK (wrklock); |
303 | X_UNLOCK (wrklock); |
254 | |
304 | |
255 | return retval; |
305 | return retval; |
256 | } |
306 | } |
257 | |
307 | |
258 | #else |
308 | #else |
… | |
… | |
317 | abort (); |
367 | abort (); |
318 | } |
368 | } |
319 | |
369 | |
320 | static int poll_cb (); |
370 | static int poll_cb (); |
321 | static int req_invoke (aio_req req); |
371 | static int req_invoke (aio_req req); |
322 | static void req_free (aio_req req); |
372 | static void req_destroy (aio_req req); |
323 | static void req_cancel (aio_req req); |
373 | static void req_cancel (aio_req req); |
324 | |
374 | |
325 | /* must be called at most once */ |
375 | /* must be called at most once */ |
326 | static SV *req_sv (aio_req req, const char *klass) |
376 | static SV *req_sv (aio_req req, const char *klass) |
327 | { |
377 | { |
… | |
… | |
393 | { |
443 | { |
394 | block_sig (); |
444 | block_sig (); |
395 | |
445 | |
396 | if (!req_invoke (grp)) |
446 | if (!req_invoke (grp)) |
397 | { |
447 | { |
398 | req_free (grp); |
448 | req_destroy (grp); |
399 | unblock_sig (); |
449 | unblock_sig (); |
400 | croak (0); |
450 | croak (0); |
401 | } |
451 | } |
402 | |
452 | |
403 | req_free (grp); |
453 | req_destroy (grp); |
404 | unblock_sig (); |
454 | unblock_sig (); |
405 | } |
455 | } |
406 | } |
456 | } |
407 | |
457 | |
408 | static int req_invoke (aio_req req) |
458 | static int req_invoke (aio_req req) |
409 | { |
459 | { |
410 | dSP; |
460 | dSP; |
411 | |
461 | |
412 | if (req->flags & FLAG_SV1_RO_OFF) |
462 | if (req->flags & FLAG_SV2_RO_OFF) |
413 | SvREADONLY_off (req->sv1); |
463 | SvREADONLY_off (req->sv2); |
414 | |
464 | |
415 | if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) |
465 | if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) |
416 | { |
466 | { |
417 | ENTER; |
467 | ENTER; |
418 | SAVETMPS; |
468 | SAVETMPS; |
… | |
… | |
536 | } |
586 | } |
537 | |
587 | |
538 | return !SvTRUE (ERRSV); |
588 | return !SvTRUE (ERRSV); |
539 | } |
589 | } |
540 | |
590 | |
541 | static void req_free (aio_req req) |
591 | static void req_destroy (aio_req req) |
542 | { |
592 | { |
543 | if (req->self) |
593 | if (req->self) |
544 | { |
594 | { |
545 | sv_unmagic (req->self, PERL_MAGIC_ext); |
595 | sv_unmagic (req->self, PERL_MAGIC_ext); |
546 | SvREFCNT_dec (req->self); |
596 | SvREFCNT_dec (req->self); |
… | |
… | |
575 | req->flags |= FLAG_CANCELLED; |
625 | req->flags |= FLAG_CANCELLED; |
576 | |
626 | |
577 | req_cancel_subs (req); |
627 | req_cancel_subs (req); |
578 | } |
628 | } |
579 | |
629 | |
580 | static void *aio_proc(void *arg); |
630 | X_THREAD_PROC (aio_proc); |
581 | |
631 | |
582 | static void start_thread (void) |
632 | static void start_thread (void) |
583 | { |
633 | { |
584 | worker *wrk = calloc (1, sizeof (worker)); |
634 | worker *wrk = calloc (1, sizeof (worker)); |
585 | |
635 | |
586 | if (!wrk) |
636 | if (!wrk) |
587 | croak ("unable to allocate worker thread data"); |
637 | croak ("unable to allocate worker thread data"); |
588 | |
638 | |
589 | LOCK (wrklock); |
639 | X_LOCK (wrklock); |
590 | |
640 | |
591 | if (thread_create (&wrk->tid, aio_proc, (void *)wrk)) |
641 | if (thread_create (&wrk->tid, aio_proc, (void *)wrk)) |
592 | { |
642 | { |
593 | wrk->prev = &wrk_first; |
643 | wrk->prev = &wrk_first; |
594 | wrk->next = wrk_first.next; |
644 | wrk->next = wrk_first.next; |
… | |
… | |
597 | ++started; |
647 | ++started; |
598 | } |
648 | } |
599 | else |
649 | else |
600 | free (wrk); |
650 | free (wrk); |
601 | |
651 | |
602 | UNLOCK (wrklock); |
652 | X_UNLOCK (wrklock); |
603 | } |
653 | } |
604 | |
654 | |
605 | static void maybe_start_thread () |
655 | static void maybe_start_thread () |
606 | { |
656 | { |
607 | if (get_nthreads () >= wanted) |
657 | if (get_nthreads () >= wanted) |
… | |
… | |
618 | { |
668 | { |
619 | block_sig (); |
669 | block_sig (); |
620 | |
670 | |
621 | ++nreqs; |
671 | ++nreqs; |
622 | |
672 | |
623 | LOCK (reqlock); |
673 | X_LOCK (reqlock); |
624 | ++nready; |
674 | ++nready; |
625 | reqq_push (&req_queue, req); |
675 | reqq_push (&req_queue, req); |
626 | COND_SIGNAL (reqwait); |
676 | X_COND_SIGNAL (reqwait); |
627 | UNLOCK (reqlock); |
677 | X_UNLOCK (reqlock); |
628 | |
678 | |
629 | unblock_sig (); |
679 | unblock_sig (); |
630 | |
680 | |
631 | maybe_start_thread (); |
681 | maybe_start_thread (); |
632 | } |
682 | } |
… | |
… | |
638 | Newz (0, req, 1, aio_cb); |
688 | Newz (0, req, 1, aio_cb); |
639 | |
689 | |
640 | req->type = REQ_QUIT; |
690 | req->type = REQ_QUIT; |
641 | req->pri = PRI_MAX + PRI_BIAS; |
691 | req->pri = PRI_MAX + PRI_BIAS; |
642 | |
692 | |
643 | LOCK (reqlock); |
693 | X_LOCK (reqlock); |
644 | reqq_push (&req_queue, req); |
694 | reqq_push (&req_queue, req); |
645 | COND_SIGNAL (reqwait); |
695 | X_COND_SIGNAL (reqwait); |
646 | UNLOCK (reqlock); |
696 | X_UNLOCK (reqlock); |
647 | |
697 | |
648 | LOCK (wrklock); |
698 | X_LOCK (wrklock); |
649 | --started; |
699 | --started; |
650 | UNLOCK (wrklock); |
700 | X_UNLOCK (wrklock); |
651 | } |
701 | } |
652 | |
702 | |
653 | static void set_max_idle (int nthreads) |
703 | static void set_max_idle (int nthreads) |
654 | { |
704 | { |
655 | if (WORDACCESS_UNSAFE) LOCK (reqlock); |
705 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
656 | max_idle = nthreads <= 0 ? 1 : nthreads; |
706 | max_idle = nthreads <= 0 ? 1 : nthreads; |
657 | if (WORDACCESS_UNSAFE) UNLOCK (reqlock); |
707 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
658 | } |
708 | } |
659 | |
709 | |
660 | static void min_parallel (int nthreads) |
710 | static void min_parallel (int nthreads) |
661 | { |
711 | { |
662 | if (wanted < nthreads) |
712 | if (wanted < nthreads) |
… | |
… | |
677 | fd_set rfd; |
727 | fd_set rfd; |
678 | |
728 | |
679 | while (nreqs) |
729 | while (nreqs) |
680 | { |
730 | { |
681 | int size; |
731 | int size; |
682 | if (WORDACCESS_UNSAFE) LOCK (reslock); |
732 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
683 | size = res_queue.size; |
733 | size = res_queue.size; |
684 | if (WORDACCESS_UNSAFE) UNLOCK (reslock); |
734 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
685 | |
735 | |
686 | if (size) |
736 | if (size) |
687 | return; |
737 | return; |
688 | |
738 | |
689 | maybe_start_thread (); |
739 | maybe_start_thread (); |
… | |
… | |
713 | { |
763 | { |
714 | for (;;) |
764 | for (;;) |
715 | { |
765 | { |
716 | maybe_start_thread (); |
766 | maybe_start_thread (); |
717 | |
767 | |
718 | LOCK (reslock); |
768 | X_LOCK (reslock); |
719 | req = reqq_shift (&res_queue); |
769 | req = reqq_shift (&res_queue); |
720 | |
770 | |
721 | if (req) |
771 | if (req) |
722 | { |
772 | { |
723 | --npending; |
773 | --npending; |
… | |
… | |
729 | while (read (respipe [0], buf, 4) == 4) |
779 | while (read (respipe [0], buf, 4) == 4) |
730 | ; |
780 | ; |
731 | } |
781 | } |
732 | } |
782 | } |
733 | |
783 | |
734 | UNLOCK (reslock); |
784 | X_UNLOCK (reslock); |
735 | |
785 | |
736 | if (!req) |
786 | if (!req) |
737 | break; |
787 | break; |
738 | |
788 | |
739 | --nreqs; |
789 | --nreqs; |
… | |
… | |
745 | } |
795 | } |
746 | else |
796 | else |
747 | { |
797 | { |
748 | if (!req_invoke (req)) |
798 | if (!req_invoke (req)) |
749 | { |
799 | { |
750 | req_free (req); |
800 | req_destroy (req); |
751 | unblock_sig (); |
801 | unblock_sig (); |
752 | croak (0); |
802 | croak (0); |
753 | } |
803 | } |
754 | |
804 | |
755 | count++; |
805 | count++; |
756 | } |
806 | } |
757 | |
807 | |
758 | req_free (req); |
808 | req_destroy (req); |
759 | |
809 | |
760 | if (maxreqs && !--maxreqs) |
810 | if (maxreqs && !--maxreqs) |
761 | break; |
811 | break; |
762 | |
812 | |
763 | if (max_poll_time) |
813 | if (max_poll_time) |
… | |
… | |
777 | ++maxreqs; |
827 | ++maxreqs; |
778 | } |
828 | } |
779 | |
829 | |
780 | unblock_sig (); |
830 | unblock_sig (); |
781 | return count; |
831 | 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 | } |
832 | } |
795 | |
833 | |
796 | /*****************************************************************************/ |
834 | /*****************************************************************************/ |
797 | /* work around various missing functions */ |
835 | /* work around various missing functions */ |
798 | |
836 | |
… | |
… | |
803 | /* |
841 | /* |
804 | * make our pread/pwrite safe against themselves, but not against |
842 | * make our pread/pwrite safe against themselves, but not against |
805 | * normal read/write by using a mutex. slows down execution a lot, |
843 | * normal read/write by using a mutex. slows down execution a lot, |
806 | * but that's your problem, not mine. |
844 | * but that's your problem, not mine. |
807 | */ |
845 | */ |
808 | static mutex_t preadwritelock = MUTEX_INIT; |
846 | static mutex_t preadwritelock = X_MUTEX_INIT; |
809 | |
847 | |
810 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
848 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
811 | { |
849 | { |
812 | ssize_t res; |
850 | ssize_t res; |
813 | off_t ooffset; |
851 | off_t ooffset; |
814 | |
852 | |
815 | LOCK (preadwritelock); |
853 | X_LOCK (preadwritelock); |
816 | ooffset = lseek (fd, 0, SEEK_CUR); |
854 | ooffset = lseek (fd, 0, SEEK_CUR); |
817 | lseek (fd, offset, SEEK_SET); |
855 | lseek (fd, offset, SEEK_SET); |
818 | res = read (fd, buf, count); |
856 | res = read (fd, buf, count); |
819 | lseek (fd, ooffset, SEEK_SET); |
857 | lseek (fd, ooffset, SEEK_SET); |
820 | UNLOCK (preadwritelock); |
858 | X_UNLOCK (preadwritelock); |
821 | |
859 | |
822 | return res; |
860 | return res; |
823 | } |
861 | } |
824 | |
862 | |
825 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
863 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
826 | { |
864 | { |
827 | ssize_t res; |
865 | ssize_t res; |
828 | off_t ooffset; |
866 | off_t ooffset; |
829 | |
867 | |
830 | LOCK (preadwritelock); |
868 | X_LOCK (preadwritelock); |
831 | ooffset = lseek (fd, 0, SEEK_CUR); |
869 | ooffset = lseek (fd, 0, SEEK_CUR); |
832 | lseek (fd, offset, SEEK_SET); |
870 | lseek (fd, offset, SEEK_SET); |
833 | res = write (fd, buf, count); |
871 | res = write (fd, buf, count); |
834 | lseek (fd, offset, SEEK_SET); |
872 | lseek (fd, offset, SEEK_SET); |
835 | UNLOCK (preadwritelock); |
873 | X_UNLOCK (preadwritelock); |
836 | |
874 | |
837 | return res; |
875 | return res; |
838 | } |
876 | } |
839 | #endif |
877 | #endif |
840 | |
878 | |
… | |
… | |
873 | #if !HAVE_READAHEAD |
911 | #if !HAVE_READAHEAD |
874 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
912 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
875 | |
913 | |
876 | static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) |
914 | static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) |
877 | { |
915 | { |
|
|
916 | size_t todo = count; |
878 | dBUF; |
917 | dBUF; |
879 | |
918 | |
880 | while (count > 0) |
919 | while (todo > 0) |
881 | { |
920 | { |
882 | size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; |
921 | size_t len = todo < AIO_BUFSIZE ? todo : AIO_BUFSIZE; |
883 | |
922 | |
884 | pread (fd, aio_buf, len, offset); |
923 | pread (fd, aio_buf, len, offset); |
885 | offset += len; |
924 | offset += len; |
886 | count -= len; |
925 | todo -= len; |
887 | } |
926 | } |
888 | |
927 | |
889 | errno = 0; |
928 | errno = 0; |
|
|
929 | return count; |
890 | } |
930 | } |
891 | |
931 | |
892 | #endif |
932 | #endif |
893 | |
933 | |
894 | #if !HAVE_READDIR_R |
934 | #if !HAVE_READDIR_R |
895 | # define readdir_r aio_readdir_r |
935 | # define readdir_r aio_readdir_r |
896 | |
936 | |
897 | static mutex_t readdirlock = MUTEX_INIT; |
937 | static mutex_t readdirlock = X_MUTEX_INIT; |
898 | |
938 | |
899 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
939 | static int readdir_r (DIR *dirp, X_DIRENT *ent, X_DIRENT **res) |
900 | { |
940 | { |
901 | struct dirent *e; |
941 | X_DIRENT *e; |
902 | int errorno; |
942 | int errorno; |
903 | |
943 | |
904 | LOCK (readdirlock); |
944 | X_LOCK (readdirlock); |
905 | |
945 | |
906 | e = readdir (dirp); |
946 | e = readdir (dirp); |
907 | errorno = errno; |
947 | errorno = errno; |
908 | |
948 | |
909 | if (e) |
949 | if (e) |
… | |
… | |
912 | strcpy (ent->d_name, e->d_name); |
952 | strcpy (ent->d_name, e->d_name); |
913 | } |
953 | } |
914 | else |
954 | else |
915 | *res = 0; |
955 | *res = 0; |
916 | |
956 | |
917 | UNLOCK (readdirlock); |
957 | X_UNLOCK (readdirlock); |
918 | |
958 | |
919 | errno = errorno; |
959 | errno = errorno; |
920 | return e ? 0 : -1; |
960 | return e ? 0 : -1; |
921 | } |
961 | } |
922 | #endif |
962 | #endif |
… | |
… | |
1018 | static void scandir_ (aio_req req, worker *self) |
1058 | static void scandir_ (aio_req req, worker *self) |
1019 | { |
1059 | { |
1020 | DIR *dirp; |
1060 | DIR *dirp; |
1021 | union |
1061 | union |
1022 | { |
1062 | { |
1023 | struct dirent d; |
1063 | X_DIRENT d; |
1024 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
1064 | char b [offsetof (X_DIRENT, d_name) + NAME_MAX + 1]; |
1025 | } *u; |
1065 | } *u; |
1026 | struct dirent *entp; |
1066 | X_DIRENT *entp; |
1027 | char *name, *names; |
1067 | char *name, *names; |
1028 | int memlen = 4096; |
1068 | int memlen = 4096; |
1029 | int memofs = 0; |
1069 | int memofs = 0; |
1030 | int res = 0; |
1070 | int res = 0; |
1031 | int errorno; |
|
|
1032 | |
1071 | |
1033 | LOCK (wrklock); |
1072 | X_LOCK (wrklock); |
1034 | self->dirp = dirp = opendir (req->ptr1); |
1073 | self->dirp = dirp = opendir (req->ptr1); |
1035 | self->dbuf = u = malloc (sizeof (*u)); |
1074 | self->dbuf = u = malloc (sizeof (*u)); |
1036 | req->flags |= FLAG_PTR2_FREE; |
1075 | req->flags |= FLAG_PTR2_FREE; |
1037 | req->ptr2 = names = malloc (memlen); |
1076 | req->ptr2 = names = malloc (memlen); |
1038 | UNLOCK (wrklock); |
1077 | X_UNLOCK (wrklock); |
1039 | |
1078 | |
1040 | if (dirp && u && names) |
1079 | if (dirp && u && names) |
1041 | for (;;) |
1080 | for (;;) |
1042 | { |
1081 | { |
1043 | errno = 0; |
1082 | errno = 0; |
… | |
… | |
1055 | res++; |
1094 | res++; |
1056 | |
1095 | |
1057 | while (memofs + len > memlen) |
1096 | while (memofs + len > memlen) |
1058 | { |
1097 | { |
1059 | memlen *= 2; |
1098 | memlen *= 2; |
1060 | LOCK (wrklock); |
1099 | X_LOCK (wrklock); |
1061 | req->ptr2 = names = realloc (names, memlen); |
1100 | req->ptr2 = names = realloc (names, memlen); |
1062 | UNLOCK (wrklock); |
1101 | X_UNLOCK (wrklock); |
1063 | |
1102 | |
1064 | if (!names) |
1103 | if (!names) |
1065 | break; |
1104 | break; |
1066 | } |
1105 | } |
1067 | |
1106 | |
… | |
… | |
1076 | req->result = res; |
1115 | req->result = res; |
1077 | } |
1116 | } |
1078 | |
1117 | |
1079 | /*****************************************************************************/ |
1118 | /*****************************************************************************/ |
1080 | |
1119 | |
1081 | static void *aio_proc (void *thr_arg) |
1120 | X_THREAD_PROC (aio_proc) |
1082 | { |
1121 | { |
|
|
1122 | {//D |
1083 | aio_req req; |
1123 | aio_req req; |
1084 | struct timespec ts; |
1124 | struct timespec ts; |
1085 | worker *self = (worker *)thr_arg; |
1125 | worker *self = (worker *)thr_arg; |
1086 | |
1126 | |
1087 | /* try to distribute timeouts somewhat evenly */ |
1127 | /* try to distribute timeouts somewhat randomly */ |
1088 | ts.tv_nsec = (((unsigned long)self + (unsigned long)ts.tv_sec) & 1023UL) |
1128 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
1089 | * (1000000000UL / 1024UL); |
|
|
1090 | |
1129 | |
1091 | for (;;) |
1130 | for (;;) |
1092 | { |
1131 | { |
1093 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1132 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1094 | |
1133 | |
1095 | LOCK (reqlock); |
1134 | X_LOCK (reqlock); |
1096 | |
1135 | |
1097 | for (;;) |
1136 | for (;;) |
1098 | { |
1137 | { |
1099 | self->req = req = reqq_shift (&req_queue); |
1138 | self->req = req = reqq_shift (&req_queue); |
1100 | |
1139 | |
1101 | if (req) |
1140 | if (req) |
1102 | break; |
1141 | break; |
1103 | |
1142 | |
1104 | ++idle; |
1143 | ++idle; |
1105 | |
1144 | |
1106 | if (COND_TIMEDWAIT (reqwait, reqlock, ts) |
1145 | if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) |
1107 | == ETIMEDOUT) |
1146 | == ETIMEDOUT) |
1108 | { |
1147 | { |
1109 | if (idle > max_idle) |
1148 | if (idle > max_idle) |
1110 | { |
1149 | { |
1111 | --idle; |
1150 | --idle; |
1112 | UNLOCK (reqlock); |
1151 | X_UNLOCK (reqlock); |
1113 | LOCK (wrklock); |
1152 | X_LOCK (wrklock); |
1114 | --started; |
1153 | --started; |
1115 | UNLOCK (wrklock); |
1154 | X_UNLOCK (wrklock); |
1116 | goto quit; |
1155 | goto quit; |
1117 | } |
1156 | } |
1118 | |
1157 | |
1119 | /* we are allowed to idle, so do so without any timeout */ |
1158 | /* we are allowed to idle, so do so without any timeout */ |
1120 | COND_WAIT (reqwait, reqlock); |
1159 | X_COND_WAIT (reqwait, reqlock); |
1121 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1160 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1122 | } |
1161 | } |
1123 | |
1162 | |
1124 | --idle; |
1163 | --idle; |
1125 | } |
1164 | } |
1126 | |
1165 | |
1127 | --nready; |
1166 | --nready; |
1128 | |
1167 | |
1129 | UNLOCK (reqlock); |
1168 | X_UNLOCK (reqlock); |
1130 | |
1169 | |
1131 | errno = 0; /* strictly unnecessary */ |
1170 | errno = 0; /* strictly unnecessary */ |
1132 | |
1171 | |
1133 | if (!(req->flags & FLAG_CANCELLED)) |
1172 | if (!(req->flags & FLAG_CANCELLED)) |
1134 | switch (req->type) |
1173 | switch (req->type) |
1135 | { |
1174 | { |
1136 | case REQ_READ: req->result = pread (req->int1, req->ptr1, req->size, req->offs); break; |
1175 | case REQ_READ: req->result = req->offs >= 0 |
1137 | case REQ_WRITE: req->result = pwrite (req->int1, req->ptr1, req->size, req->offs); break; |
1176 | ? pread (req->int1, req->ptr1, req->size, req->offs) |
|
|
1177 | : read (req->int1, req->ptr1, req->size); break; |
|
|
1178 | case REQ_WRITE: req->result = req->offs >= 0 |
|
|
1179 | ? pwrite (req->int1, req->ptr1, req->size, req->offs) |
|
|
1180 | : write (req->int1, req->ptr1, req->size); break; |
1138 | |
1181 | |
1139 | case REQ_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
1182 | 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; |
1183 | case REQ_SENDFILE: req->result = sendfile_ (req->int1, req->int2, req->offs, req->size, self); break; |
1141 | |
1184 | |
1142 | case REQ_STAT: req->result = stat (req->ptr1, (Stat_t *)req->ptr2); break; |
1185 | case REQ_STAT: req->result = stat (req->ptr1, (Stat_t *)req->ptr2); break; |
… | |
… | |
1145 | |
1188 | |
1146 | case REQ_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break; |
1189 | case REQ_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break; |
1147 | case REQ_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; |
1190 | case REQ_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; |
1148 | case REQ_CHMOD: req->result = chmod (req->ptr1, req->mode); break; |
1191 | case REQ_CHMOD: req->result = chmod (req->ptr1, req->mode); break; |
1149 | case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break; |
1192 | case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break; |
|
|
1193 | case REQ_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break; |
|
|
1194 | case REQ_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; |
1150 | |
1195 | |
1151 | case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break; |
1196 | case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break; |
1152 | case REQ_CLOSE: req->result = close (req->int1); break; |
1197 | case REQ_CLOSE: req->result = close (req->int1); break; |
1153 | case REQ_UNLINK: req->result = unlink (req->ptr1); break; |
1198 | case REQ_UNLINK: req->result = unlink (req->ptr1); break; |
1154 | case REQ_RMDIR: req->result = rmdir (req->ptr1); break; |
1199 | case REQ_RMDIR: req->result = rmdir (req->ptr1); break; |
… | |
… | |
1162 | case REQ_FDATASYNC: req->result = fdatasync (req->int1); break; |
1207 | case REQ_FDATASYNC: req->result = fdatasync (req->int1); break; |
1163 | case REQ_FSYNC: req->result = fsync (req->int1); break; |
1208 | case REQ_FSYNC: req->result = fsync (req->int1); break; |
1164 | case REQ_READDIR: scandir_ (req, self); break; |
1209 | case REQ_READDIR: scandir_ (req, self); break; |
1165 | |
1210 | |
1166 | case REQ_BUSY: |
1211 | case REQ_BUSY: |
|
|
1212 | #ifdef _WIN32 |
|
|
1213 | Sleep (req->nv1 * 1000.); |
|
|
1214 | #else |
1167 | { |
1215 | { |
1168 | struct timeval tv; |
1216 | struct timeval tv; |
1169 | |
1217 | |
1170 | tv.tv_sec = req->nv1; |
1218 | tv.tv_sec = req->nv1; |
1171 | tv.tv_usec = (req->nv1 - tv.tv_usec) * 1000000.; |
1219 | tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.; |
1172 | |
1220 | |
1173 | req->result = select (0, 0, 0, 0, &tv); |
1221 | req->result = select (0, 0, 0, 0, &tv); |
1174 | } |
1222 | } |
|
|
1223 | #endif |
|
|
1224 | break; |
1175 | |
1225 | |
1176 | case REQ_UTIME: |
1226 | case REQ_UTIME: |
1177 | case REQ_FUTIME: |
1227 | case REQ_FUTIME: |
1178 | { |
1228 | { |
1179 | struct timeval tv[2]; |
1229 | struct timeval tv[2]; |
… | |
… | |
1209 | break; |
1259 | break; |
1210 | } |
1260 | } |
1211 | |
1261 | |
1212 | req->errorno = errno; |
1262 | req->errorno = errno; |
1213 | |
1263 | |
1214 | LOCK (reslock); |
1264 | X_LOCK (reslock); |
1215 | |
1265 | |
1216 | ++npending; |
1266 | ++npending; |
1217 | |
1267 | |
1218 | if (!reqq_push (&res_queue, req)) |
1268 | if (!reqq_push (&res_queue, req)) |
1219 | { |
1269 | { |
… | |
… | |
1226 | } |
1276 | } |
1227 | |
1277 | |
1228 | self->req = 0; |
1278 | self->req = 0; |
1229 | worker_clear (self); |
1279 | worker_clear (self); |
1230 | |
1280 | |
1231 | UNLOCK (reslock); |
1281 | X_UNLOCK (reslock); |
1232 | } |
1282 | } |
1233 | |
1283 | |
1234 | quit: |
1284 | quit: |
1235 | LOCK (wrklock); |
1285 | X_LOCK (wrklock); |
1236 | worker_free (self); |
1286 | worker_free (self); |
1237 | UNLOCK (wrklock); |
1287 | X_UNLOCK (wrklock); |
1238 | |
1288 | |
1239 | return 0; |
1289 | return 0; |
|
|
1290 | }//D |
1240 | } |
1291 | } |
1241 | |
1292 | |
1242 | /*****************************************************************************/ |
1293 | /*****************************************************************************/ |
1243 | |
1294 | |
1244 | static void atfork_prepare (void) |
1295 | static void atfork_prepare (void) |
1245 | { |
1296 | { |
1246 | LOCK (wrklock); |
1297 | X_LOCK (wrklock); |
1247 | LOCK (reqlock); |
1298 | X_LOCK (reqlock); |
1248 | LOCK (reslock); |
1299 | X_LOCK (reslock); |
1249 | #if !HAVE_PREADWRITE |
1300 | #if !HAVE_PREADWRITE |
1250 | LOCK (preadwritelock); |
1301 | X_LOCK (preadwritelock); |
1251 | #endif |
1302 | #endif |
1252 | #if !HAVE_READDIR_R |
1303 | #if !HAVE_READDIR_R |
1253 | LOCK (readdirlock); |
1304 | X_LOCK (readdirlock); |
1254 | #endif |
1305 | #endif |
1255 | } |
1306 | } |
1256 | |
1307 | |
1257 | static void atfork_parent (void) |
1308 | static void atfork_parent (void) |
1258 | { |
1309 | { |
1259 | #if !HAVE_READDIR_R |
1310 | #if !HAVE_READDIR_R |
1260 | UNLOCK (readdirlock); |
1311 | X_UNLOCK (readdirlock); |
1261 | #endif |
1312 | #endif |
1262 | #if !HAVE_PREADWRITE |
1313 | #if !HAVE_PREADWRITE |
1263 | UNLOCK (preadwritelock); |
1314 | X_UNLOCK (preadwritelock); |
1264 | #endif |
1315 | #endif |
1265 | UNLOCK (reslock); |
1316 | X_UNLOCK (reslock); |
1266 | UNLOCK (reqlock); |
1317 | X_UNLOCK (reqlock); |
1267 | UNLOCK (wrklock); |
1318 | X_UNLOCK (wrklock); |
1268 | } |
1319 | } |
1269 | |
1320 | |
1270 | static void atfork_child (void) |
1321 | static void atfork_child (void) |
1271 | { |
1322 | { |
1272 | aio_req prv; |
1323 | aio_req prv; |
1273 | |
1324 | |
1274 | while (prv = reqq_shift (&req_queue)) |
1325 | while (prv = reqq_shift (&req_queue)) |
1275 | req_free (prv); |
1326 | req_destroy (prv); |
1276 | |
1327 | |
1277 | while (prv = reqq_shift (&res_queue)) |
1328 | while (prv = reqq_shift (&res_queue)) |
1278 | req_free (prv); |
1329 | req_destroy (prv); |
1279 | |
1330 | |
1280 | while (wrk_first.next != &wrk_first) |
1331 | while (wrk_first.next != &wrk_first) |
1281 | { |
1332 | { |
1282 | worker *wrk = wrk_first.next; |
1333 | worker *wrk = wrk_first.next; |
1283 | |
1334 | |
1284 | if (wrk->req) |
1335 | if (wrk->req) |
1285 | req_free (wrk->req); |
1336 | req_destroy (wrk->req); |
1286 | |
1337 | |
1287 | worker_clear (wrk); |
1338 | worker_clear (wrk); |
1288 | worker_free (wrk); |
1339 | worker_free (wrk); |
1289 | } |
1340 | } |
1290 | |
1341 | |
… | |
… | |
1294 | nready = 0; |
1345 | nready = 0; |
1295 | npending = 0; |
1346 | npending = 0; |
1296 | |
1347 | |
1297 | close (respipe [0]); |
1348 | close (respipe [0]); |
1298 | close (respipe [1]); |
1349 | close (respipe [1]); |
1299 | create_pipe (); |
1350 | |
|
|
1351 | if (!create_pipe (respipe)) |
|
|
1352 | croak ("cannot set result pipe to nonblocking mode"); |
1300 | |
1353 | |
1301 | atfork_parent (); |
1354 | atfork_parent (); |
1302 | } |
1355 | } |
1303 | |
1356 | |
1304 | #define dREQ \ |
1357 | #define dREQ \ |
… | |
… | |
1333 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
1386 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
1334 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
1387 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
1335 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
1388 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
1336 | newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); |
1389 | newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); |
1337 | newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); |
1390 | newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); |
|
|
1391 | #ifdef _WIN32 |
|
|
1392 | X_MUTEX_CHECK (wrklock); |
|
|
1393 | X_MUTEX_CHECK (reslock); |
|
|
1394 | X_MUTEX_CHECK (reqlock); |
|
|
1395 | X_MUTEX_CHECK (reqwait); |
|
|
1396 | X_MUTEX_CHECK (preadwritelock); |
|
|
1397 | X_MUTEX_CHECK (readdirlock); |
|
|
1398 | #else |
1338 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
1399 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
1339 | newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); |
1400 | newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); |
|
|
1401 | #endif |
1340 | |
1402 | |
1341 | create_pipe (); |
1403 | if (!create_pipe (respipe)) |
|
|
1404 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
1405 | |
1342 | ATFORK (atfork_prepare, atfork_parent, atfork_child); |
1406 | X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child); |
1343 | } |
1407 | } |
1344 | |
1408 | |
1345 | void |
1409 | void |
1346 | max_poll_reqs (int nreqs) |
1410 | max_poll_reqs (int nreqs) |
1347 | PROTOTYPE: $ |
1411 | PROTOTYPE: $ |
… | |
… | |
1410 | |
1474 | |
1411 | REQ_SEND (req); |
1475 | REQ_SEND (req); |
1412 | } |
1476 | } |
1413 | |
1477 | |
1414 | void |
1478 | void |
1415 | aio_read (SV *fh, UV offset, UV length, SV8 *data, UV dataoffset, SV *callback=&PL_sv_undef) |
1479 | aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef) |
1416 | ALIAS: |
1480 | ALIAS: |
1417 | aio_read = REQ_READ |
1481 | aio_read = REQ_READ |
1418 | aio_write = REQ_WRITE |
1482 | aio_write = REQ_WRITE |
1419 | PROTOTYPE: $$$$$;$ |
1483 | PROTOTYPE: $$$$$;$ |
1420 | PPCODE: |
1484 | PPCODE: |
1421 | { |
1485 | { |
1422 | STRLEN svlen; |
1486 | STRLEN svlen; |
1423 | char *svptr = SvPVbyte (data, svlen); |
1487 | char *svptr = SvPVbyte (data, svlen); |
|
|
1488 | UV len = SvUV (length); |
1424 | |
1489 | |
1425 | SvUPGRADE (data, SVt_PV); |
1490 | SvUPGRADE (data, SVt_PV); |
1426 | SvPOK_on (data); |
1491 | SvPOK_on (data); |
1427 | |
1492 | |
1428 | if (dataoffset < 0) |
1493 | if (dataoffset < 0) |
1429 | dataoffset += svlen; |
1494 | dataoffset += svlen; |
1430 | |
1495 | |
1431 | if (dataoffset < 0 || dataoffset > svlen) |
1496 | if (dataoffset < 0 || dataoffset > svlen) |
1432 | croak ("data offset outside of string"); |
1497 | croak ("dataoffset outside of data scalar"); |
1433 | |
1498 | |
1434 | if (ix == REQ_WRITE) |
1499 | if (ix == REQ_WRITE) |
1435 | { |
1500 | { |
1436 | /* write: check length and adjust. */ |
1501 | /* write: check length and adjust. */ |
1437 | if (length < 0 || length + dataoffset > svlen) |
1502 | if (!SvOK (length) || len + dataoffset > svlen) |
1438 | length = svlen - dataoffset; |
1503 | len = svlen - dataoffset; |
1439 | } |
1504 | } |
1440 | else |
1505 | else |
1441 | { |
1506 | { |
1442 | /* read: grow scalar as necessary */ |
1507 | /* read: grow scalar as necessary */ |
1443 | svptr = SvGROW (data, length + dataoffset + 1); |
1508 | svptr = SvGROW (data, len + dataoffset + 1); |
1444 | } |
1509 | } |
1445 | |
1510 | |
1446 | if (length < 0) |
1511 | if (len < 0) |
1447 | croak ("length must not be negative"); |
1512 | croak ("length must not be negative"); |
1448 | |
1513 | |
1449 | { |
1514 | { |
1450 | dREQ; |
1515 | dREQ; |
1451 | |
1516 | |
1452 | req->type = ix; |
1517 | req->type = ix; |
1453 | req->sv1 = newSVsv (fh); |
1518 | req->sv1 = newSVsv (fh); |
1454 | req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
1519 | req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
1455 | : IoOFP (sv_2io (fh))); |
1520 | : IoOFP (sv_2io (fh))); |
1456 | req->offs = offset; |
1521 | req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; |
1457 | req->size = length; |
1522 | req->size = len; |
1458 | req->sv2 = SvREFCNT_inc (data); |
1523 | req->sv2 = SvREFCNT_inc (data); |
1459 | req->ptr1 = (char *)svptr + dataoffset; |
1524 | req->ptr1 = (char *)svptr + dataoffset; |
1460 | req->stroffset = dataoffset; |
1525 | req->stroffset = dataoffset; |
1461 | |
1526 | |
1462 | if (!SvREADONLY (data)) |
1527 | if (!SvREADONLY (data)) |
1463 | { |
1528 | { |
1464 | SvREADONLY_on (data); |
1529 | SvREADONLY_on (data); |
1465 | req->flags |= FLAG_SV1_RO_OFF; |
1530 | req->flags |= FLAG_SV2_RO_OFF; |
1466 | } |
1531 | } |
1467 | |
1532 | |
1468 | REQ_SEND; |
1533 | REQ_SEND; |
1469 | } |
1534 | } |
1470 | } |
1535 | } |
… | |
… | |
1488 | |
1553 | |
1489 | REQ_SEND; |
1554 | REQ_SEND; |
1490 | } |
1555 | } |
1491 | |
1556 | |
1492 | void |
1557 | void |
1493 | aio_sendfile (SV *out_fh, SV *in_fh, UV in_offset, UV length, SV *callback=&PL_sv_undef) |
1558 | aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef) |
1494 | PROTOTYPE: $$$$;$ |
1559 | PROTOTYPE: $$$$;$ |
1495 | PPCODE: |
1560 | PPCODE: |
1496 | { |
1561 | { |
1497 | dREQ; |
1562 | dREQ; |
1498 | |
1563 | |
1499 | req->type = REQ_SENDFILE; |
1564 | req->type = REQ_SENDFILE; |
1500 | req->sv1 = newSVsv (out_fh); |
1565 | req->sv1 = newSVsv (out_fh); |
1501 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
1566 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
1502 | req->sv2 = newSVsv (in_fh); |
1567 | req->sv2 = newSVsv (in_fh); |
1503 | req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
1568 | req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
1504 | req->offs = in_offset; |
1569 | req->offs = SvVAL64 (in_offset); |
1505 | req->size = length; |
1570 | req->size = length; |
1506 | |
1571 | |
1507 | REQ_SEND; |
1572 | REQ_SEND; |
1508 | } |
1573 | } |
1509 | |
1574 | |
1510 | void |
1575 | void |
1511 | aio_readahead (SV *fh, UV offset, IV length, SV *callback=&PL_sv_undef) |
1576 | aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef) |
1512 | PROTOTYPE: $$$;$ |
1577 | PROTOTYPE: $$$;$ |
1513 | PPCODE: |
1578 | PPCODE: |
1514 | { |
1579 | { |
1515 | dREQ; |
1580 | dREQ; |
1516 | |
1581 | |
1517 | req->type = REQ_READAHEAD; |
1582 | req->type = REQ_READAHEAD; |
1518 | req->sv1 = newSVsv (fh); |
1583 | req->sv1 = newSVsv (fh); |
1519 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
1584 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
1520 | req->offs = offset; |
1585 | req->offs = SvVAL64 (offset); |
1521 | req->size = length; |
1586 | req->size = length; |
1522 | |
1587 | |
1523 | REQ_SEND; |
1588 | REQ_SEND; |
1524 | } |
1589 | } |
1525 | |
1590 | |
… | |
… | |
1533 | dREQ; |
1598 | dREQ; |
1534 | |
1599 | |
1535 | req->ptr2 = malloc (sizeof (Stat_t)); |
1600 | req->ptr2 = malloc (sizeof (Stat_t)); |
1536 | if (!req->ptr2) |
1601 | if (!req->ptr2) |
1537 | { |
1602 | { |
1538 | req_free (req); |
1603 | req_destroy (req); |
1539 | croak ("out of memory during aio_stat statdata allocation"); |
1604 | croak ("out of memory during aio_stat statdata allocation"); |
1540 | } |
1605 | } |
1541 | |
1606 | |
1542 | req->flags |= FLAG_PTR2_FREE; |
1607 | req->flags |= FLAG_PTR2_FREE; |
1543 | req->sv1 = newSVsv (fh_or_path); |
1608 | req->sv1 = newSVsv (fh_or_path); |
… | |
… | |
1572 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1637 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1573 | } |
1638 | } |
1574 | else |
1639 | else |
1575 | { |
1640 | { |
1576 | req->type = REQ_FUTIME; |
1641 | req->type = REQ_FUTIME; |
|
|
1642 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
1643 | } |
|
|
1644 | |
|
|
1645 | REQ_SEND; |
|
|
1646 | } |
|
|
1647 | |
|
|
1648 | void |
|
|
1649 | aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef) |
|
|
1650 | PPCODE: |
|
|
1651 | { |
|
|
1652 | dREQ; |
|
|
1653 | |
|
|
1654 | req->sv1 = newSVsv (fh_or_path); |
|
|
1655 | req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; |
|
|
1656 | |
|
|
1657 | if (SvPOK (fh_or_path)) |
|
|
1658 | { |
|
|
1659 | req->type = REQ_TRUNCATE; |
|
|
1660 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1661 | } |
|
|
1662 | else |
|
|
1663 | { |
|
|
1664 | req->type = REQ_FTRUNCATE; |
1577 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1665 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1578 | } |
1666 | } |
1579 | |
1667 | |
1580 | REQ_SEND; |
1668 | REQ_SEND; |
1581 | } |
1669 | } |
… | |
… | |
1797 | CODE: |
1885 | CODE: |
1798 | { |
1886 | { |
1799 | if (block_sig_level) |
1887 | if (block_sig_level) |
1800 | croak ("cannot call IO::AIO::setsig from within aio_block/callback"); |
1888 | croak ("cannot call IO::AIO::setsig from within aio_block/callback"); |
1801 | |
1889 | |
1802 | LOCK (reslock); |
1890 | X_LOCK (reslock); |
1803 | main_tid = pthread_self (); |
1891 | main_tid = pthread_self (); |
1804 | main_sig = signum; |
1892 | main_sig = signum; |
1805 | UNLOCK (reslock); |
1893 | X_UNLOCK (reslock); |
1806 | |
1894 | |
1807 | if (main_sig && npending) |
1895 | if (main_sig && npending) |
1808 | pthread_kill (main_tid, main_sig); |
1896 | pthread_kill (main_tid, main_sig); |
1809 | } |
1897 | } |
1810 | |
1898 | |
… | |
… | |
1854 | |
1942 | |
1855 | int |
1943 | int |
1856 | nthreads() |
1944 | nthreads() |
1857 | PROTOTYPE: |
1945 | PROTOTYPE: |
1858 | CODE: |
1946 | CODE: |
1859 | if (WORDACCESS_UNSAFE) LOCK (wrklock); |
1947 | if (WORDACCESS_UNSAFE) X_LOCK (wrklock); |
1860 | RETVAL = started; |
1948 | RETVAL = started; |
1861 | if (WORDACCESS_UNSAFE) UNLOCK (wrklock); |
1949 | if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock); |
1862 | OUTPUT: |
1950 | OUTPUT: |
1863 | RETVAL |
1951 | RETVAL |
1864 | |
1952 | |
1865 | PROTOTYPES: DISABLE |
1953 | PROTOTYPES: DISABLE |
1866 | |
1954 | |