… | |
… | |
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 | |
|
|
102 | static HV *stash; |
|
|
103 | |
51 | #define dBUF \ |
104 | #define dBUF \ |
52 | char *aio_buf; \ |
105 | char *aio_buf; \ |
53 | LOCK (wrklock); \ |
106 | X_LOCK (wrklock); \ |
54 | self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ |
107 | self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ |
55 | UNLOCK (wrklock); \ |
108 | X_UNLOCK (wrklock); \ |
56 | if (!aio_buf) \ |
109 | if (!aio_buf) \ |
57 | return -1; |
110 | return -1; |
58 | |
111 | |
59 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
112 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
60 | |
113 | |
61 | enum { |
114 | enum { |
62 | REQ_QUIT, |
115 | REQ_QUIT, |
63 | REQ_OPEN, REQ_CLOSE, |
116 | REQ_OPEN, REQ_CLOSE, |
64 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
117 | REQ_READ, REQ_WRITE, |
65 | REQ_SENDFILE, |
118 | REQ_READAHEAD, REQ_SENDFILE, |
66 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
119 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
67 | REQ_UTIME, REQ_FUTIME, /* must be consecutive */ |
120 | REQ_TRUNCATE, REQ_FTRUNCATE, |
68 | REQ_CHMOD, REQ_FCHMOD, /* must be consecutive */ |
121 | REQ_UTIME, REQ_FUTIME, |
69 | REQ_CHOWN, REQ_FCHOWN, /* must be consecutive */ |
122 | REQ_CHMOD, REQ_FCHMOD, |
|
|
123 | REQ_CHOWN, REQ_FCHOWN, |
70 | REQ_FSYNC, REQ_FDATASYNC, |
124 | REQ_FSYNC, REQ_FDATASYNC, |
71 | REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME, |
125 | REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME, |
72 | REQ_MKNOD, REQ_READDIR, |
126 | REQ_MKNOD, REQ_READDIR, |
73 | REQ_LINK, REQ_SYMLINK, REQ_READLINK, |
127 | REQ_LINK, REQ_SYMLINK, REQ_READLINK, |
74 | REQ_GROUP, REQ_NOP, |
128 | REQ_GROUP, REQ_NOP, |
… | |
… | |
103 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
157 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
104 | } aio_cb; |
158 | } aio_cb; |
105 | |
159 | |
106 | enum { |
160 | enum { |
107 | FLAG_CANCELLED = 0x01, /* request was cancelled */ |
161 | FLAG_CANCELLED = 0x01, /* request was cancelled */ |
108 | FLAG_SV1_RO_OFF = 0x40, /* data was set readonly */ |
162 | FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ |
109 | FLAG_PTR2_FREE = 0x80, /* need to free(ptr2) */ |
163 | FLAG_PTR2_FREE = 0x80, /* need to free(ptr2) */ |
110 | }; |
164 | }; |
111 | |
165 | |
112 | typedef aio_cb *aio_req; |
166 | typedef aio_cb *aio_req; |
113 | typedef aio_cb *aio_req_ornot; |
167 | typedef aio_cb *aio_req_ornot; |
… | |
… | |
170 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
224 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
171 | |
225 | |
172 | static unsigned int started, idle, wanted; |
226 | static unsigned int started, idle, wanted; |
173 | |
227 | |
174 | /* worker threads management */ |
228 | /* worker threads management */ |
175 | static mutex_t wrklock = MUTEX_INIT; |
229 | static mutex_t wrklock = X_MUTEX_INIT; |
176 | |
230 | |
177 | typedef struct worker { |
231 | typedef struct worker { |
178 | /* locked by wrklock */ |
232 | /* locked by wrklock */ |
179 | struct worker *prev, *next; |
233 | struct worker *prev, *next; |
180 | |
234 | |
… | |
… | |
212 | } |
266 | } |
213 | |
267 | |
214 | static volatile unsigned int nreqs, nready, npending; |
268 | static volatile unsigned int nreqs, nready, npending; |
215 | static volatile unsigned int max_idle = 4; |
269 | static volatile unsigned int max_idle = 4; |
216 | static volatile unsigned int max_outstanding = 0xffffffff; |
270 | static volatile unsigned int max_outstanding = 0xffffffff; |
217 | static int respipe [2]; |
271 | static int respipe_osf [2], respipe [2] = { -1, -1 }; |
218 | |
272 | |
219 | static mutex_t reslock = MUTEX_INIT; |
273 | static mutex_t reslock = X_MUTEX_INIT; |
220 | static mutex_t reqlock = MUTEX_INIT; |
274 | static mutex_t reqlock = X_MUTEX_INIT; |
221 | static cond_t reqwait = COND_INIT; |
275 | static cond_t reqwait = X_COND_INIT; |
222 | |
276 | |
223 | #if WORDACCESS_UNSAFE |
277 | #if WORDACCESS_UNSAFE |
224 | |
278 | |
225 | static unsigned int get_nready () |
279 | static unsigned int get_nready () |
226 | { |
280 | { |
227 | unsigned int retval; |
281 | unsigned int retval; |
228 | |
282 | |
229 | LOCK (reqlock); |
283 | X_LOCK (reqlock); |
230 | retval = nready; |
284 | retval = nready; |
231 | UNLOCK (reqlock); |
285 | X_UNLOCK (reqlock); |
232 | |
286 | |
233 | return retval; |
287 | return retval; |
234 | } |
288 | } |
235 | |
289 | |
236 | static unsigned int get_npending () |
290 | static unsigned int get_npending () |
237 | { |
291 | { |
238 | unsigned int retval; |
292 | unsigned int retval; |
239 | |
293 | |
240 | LOCK (reslock); |
294 | X_LOCK (reslock); |
241 | retval = npending; |
295 | retval = npending; |
242 | UNLOCK (reslock); |
296 | X_UNLOCK (reslock); |
243 | |
297 | |
244 | return retval; |
298 | return retval; |
245 | } |
299 | } |
246 | |
300 | |
247 | static unsigned int get_nthreads () |
301 | static unsigned int get_nthreads () |
248 | { |
302 | { |
249 | unsigned int retval; |
303 | unsigned int retval; |
250 | |
304 | |
251 | LOCK (wrklock); |
305 | X_LOCK (wrklock); |
252 | retval = started; |
306 | retval = started; |
253 | UNLOCK (wrklock); |
307 | X_UNLOCK (wrklock); |
254 | |
308 | |
255 | return retval; |
309 | return retval; |
256 | } |
310 | } |
257 | |
311 | |
258 | #else |
312 | #else |
… | |
… | |
317 | abort (); |
371 | abort (); |
318 | } |
372 | } |
319 | |
373 | |
320 | static int poll_cb (); |
374 | static int poll_cb (); |
321 | static int req_invoke (aio_req req); |
375 | static int req_invoke (aio_req req); |
322 | static void req_free (aio_req req); |
376 | static void req_destroy (aio_req req); |
323 | static void req_cancel (aio_req req); |
377 | static void req_cancel (aio_req req); |
324 | |
378 | |
325 | /* must be called at most once */ |
379 | /* must be called at most once */ |
326 | static SV *req_sv (aio_req req, const char *klass) |
380 | static SV *req_sv (aio_req req, const char *klass) |
327 | { |
381 | { |
… | |
… | |
393 | { |
447 | { |
394 | block_sig (); |
448 | block_sig (); |
395 | |
449 | |
396 | if (!req_invoke (grp)) |
450 | if (!req_invoke (grp)) |
397 | { |
451 | { |
398 | req_free (grp); |
452 | req_destroy (grp); |
399 | unblock_sig (); |
453 | unblock_sig (); |
400 | croak (0); |
454 | croak (0); |
401 | } |
455 | } |
402 | |
456 | |
403 | req_free (grp); |
457 | req_destroy (grp); |
404 | unblock_sig (); |
458 | unblock_sig (); |
405 | } |
459 | } |
406 | } |
460 | } |
407 | |
461 | |
408 | static int req_invoke (aio_req req) |
462 | static int req_invoke (aio_req req) |
409 | { |
463 | { |
410 | dSP; |
464 | dSP; |
411 | |
465 | |
412 | if (req->flags & FLAG_SV1_RO_OFF) |
466 | if (req->flags & FLAG_SV2_RO_OFF) |
413 | SvREADONLY_off (req->sv1); |
467 | SvREADONLY_off (req->sv2); |
414 | |
468 | |
415 | if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) |
469 | if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) |
416 | { |
470 | { |
417 | ENTER; |
471 | ENTER; |
418 | SAVETMPS; |
472 | SAVETMPS; |
… | |
… | |
449 | break; |
503 | break; |
450 | |
504 | |
451 | case REQ_OPEN: |
505 | case REQ_OPEN: |
452 | { |
506 | { |
453 | /* convert fd to fh */ |
507 | /* convert fd to fh */ |
454 | SV *fh; |
508 | SV *fh = &PL_sv_undef; |
455 | |
509 | |
456 | PUSHs (sv_2mortal (newSViv (req->result))); |
510 | if (req->result >= 0) |
457 | PUTBACK; |
511 | { |
458 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
512 | GV *gv = (GV *)sv_newmortal (); |
459 | SPAGAIN; |
513 | int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); |
|
|
514 | char sym [64]; |
|
|
515 | int symlen; |
|
|
516 | |
|
|
517 | symlen = snprintf (sym, sizeof (sym), "fd#%d", req->result); |
|
|
518 | gv_init (gv, stash, sym, symlen, 0); |
460 | |
519 | |
|
|
520 | symlen = snprintf ( |
|
|
521 | sym, |
|
|
522 | sizeof (sym), |
|
|
523 | "%s&=%d", |
|
|
524 | flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<", |
|
|
525 | req->result |
|
|
526 | ); |
|
|
527 | |
|
|
528 | if (do_open (gv, sym, symlen, 0, 0, 0, 0)) |
461 | fh = POPs; |
529 | fh = gv; |
462 | PUSHMARK (SP); |
530 | } |
|
|
531 | |
463 | XPUSHs (fh); |
532 | XPUSHs (fh); |
464 | } |
533 | } |
465 | break; |
534 | break; |
466 | |
535 | |
467 | case REQ_GROUP: |
536 | case REQ_GROUP: |
… | |
… | |
536 | } |
605 | } |
537 | |
606 | |
538 | return !SvTRUE (ERRSV); |
607 | return !SvTRUE (ERRSV); |
539 | } |
608 | } |
540 | |
609 | |
541 | static void req_free (aio_req req) |
610 | static void req_destroy (aio_req req) |
542 | { |
611 | { |
543 | if (req->self) |
612 | if (req->self) |
544 | { |
613 | { |
545 | sv_unmagic (req->self, PERL_MAGIC_ext); |
614 | sv_unmagic (req->self, PERL_MAGIC_ext); |
546 | SvREFCNT_dec (req->self); |
615 | SvREFCNT_dec (req->self); |
… | |
… | |
575 | req->flags |= FLAG_CANCELLED; |
644 | req->flags |= FLAG_CANCELLED; |
576 | |
645 | |
577 | req_cancel_subs (req); |
646 | req_cancel_subs (req); |
578 | } |
647 | } |
579 | |
648 | |
580 | static void *aio_proc(void *arg); |
649 | #ifdef USE_SOCKETS_AS_HANDLES |
|
|
650 | # define TO_SOCKET(x) (win32_get_osfhandle (x)) |
|
|
651 | #else |
|
|
652 | # define TO_SOCKET(x) (x) |
|
|
653 | #endif |
|
|
654 | |
|
|
655 | static void |
|
|
656 | create_respipe () |
|
|
657 | { |
|
|
658 | int old_readfd = respipe [0]; |
|
|
659 | |
|
|
660 | if (respipe [1] >= 0) |
|
|
661 | respipe_close (TO_SOCKET (respipe [1])); |
|
|
662 | |
|
|
663 | #ifdef _WIN32 |
|
|
664 | if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe)) |
|
|
665 | #else |
|
|
666 | if (pipe (respipe)) |
|
|
667 | #endif |
|
|
668 | croak ("unable to initialize result pipe"); |
|
|
669 | |
|
|
670 | if (old_readfd >= 0) |
|
|
671 | { |
|
|
672 | if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0) |
|
|
673 | croak ("unable to initialize result pipe(2)"); |
|
|
674 | |
|
|
675 | respipe_close (respipe [0]); |
|
|
676 | respipe [0] = old_readfd; |
|
|
677 | } |
|
|
678 | |
|
|
679 | #ifdef _WIN32 |
|
|
680 | int arg = 1; |
|
|
681 | if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg) |
|
|
682 | || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg)) |
|
|
683 | #else |
|
|
684 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK) |
|
|
685 | || fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
686 | #endif |
|
|
687 | croak ("unable to initialize result pipe(3)"); |
|
|
688 | |
|
|
689 | respipe_osf [0] = TO_SOCKET (respipe [0]); |
|
|
690 | respipe_osf [1] = TO_SOCKET (respipe [1]); |
|
|
691 | } |
|
|
692 | |
|
|
693 | X_THREAD_PROC (aio_proc); |
581 | |
694 | |
582 | static void start_thread (void) |
695 | static void start_thread (void) |
583 | { |
696 | { |
584 | worker *wrk = calloc (1, sizeof (worker)); |
697 | worker *wrk = calloc (1, sizeof (worker)); |
585 | |
698 | |
586 | if (!wrk) |
699 | if (!wrk) |
587 | croak ("unable to allocate worker thread data"); |
700 | croak ("unable to allocate worker thread data"); |
588 | |
701 | |
589 | LOCK (wrklock); |
702 | X_LOCK (wrklock); |
590 | |
703 | |
591 | if (thread_create (&wrk->tid, aio_proc, (void *)wrk)) |
704 | if (thread_create (&wrk->tid, aio_proc, (void *)wrk)) |
592 | { |
705 | { |
593 | wrk->prev = &wrk_first; |
706 | wrk->prev = &wrk_first; |
594 | wrk->next = wrk_first.next; |
707 | wrk->next = wrk_first.next; |
… | |
… | |
597 | ++started; |
710 | ++started; |
598 | } |
711 | } |
599 | else |
712 | else |
600 | free (wrk); |
713 | free (wrk); |
601 | |
714 | |
602 | UNLOCK (wrklock); |
715 | X_UNLOCK (wrklock); |
603 | } |
716 | } |
604 | |
717 | |
605 | static void maybe_start_thread () |
718 | static void maybe_start_thread () |
606 | { |
719 | { |
607 | if (get_nthreads () >= wanted) |
720 | if (get_nthreads () >= wanted) |
… | |
… | |
618 | { |
731 | { |
619 | block_sig (); |
732 | block_sig (); |
620 | |
733 | |
621 | ++nreqs; |
734 | ++nreqs; |
622 | |
735 | |
623 | LOCK (reqlock); |
736 | X_LOCK (reqlock); |
624 | ++nready; |
737 | ++nready; |
625 | reqq_push (&req_queue, req); |
738 | reqq_push (&req_queue, req); |
626 | COND_SIGNAL (reqwait); |
739 | X_COND_SIGNAL (reqwait); |
627 | UNLOCK (reqlock); |
740 | X_UNLOCK (reqlock); |
628 | |
741 | |
629 | unblock_sig (); |
742 | unblock_sig (); |
630 | |
743 | |
631 | maybe_start_thread (); |
744 | maybe_start_thread (); |
632 | } |
745 | } |
… | |
… | |
638 | Newz (0, req, 1, aio_cb); |
751 | Newz (0, req, 1, aio_cb); |
639 | |
752 | |
640 | req->type = REQ_QUIT; |
753 | req->type = REQ_QUIT; |
641 | req->pri = PRI_MAX + PRI_BIAS; |
754 | req->pri = PRI_MAX + PRI_BIAS; |
642 | |
755 | |
643 | LOCK (reqlock); |
756 | X_LOCK (reqlock); |
644 | reqq_push (&req_queue, req); |
757 | reqq_push (&req_queue, req); |
645 | COND_SIGNAL (reqwait); |
758 | X_COND_SIGNAL (reqwait); |
646 | UNLOCK (reqlock); |
759 | X_UNLOCK (reqlock); |
647 | |
760 | |
648 | LOCK (wrklock); |
761 | X_LOCK (wrklock); |
649 | --started; |
762 | --started; |
650 | UNLOCK (wrklock); |
763 | X_UNLOCK (wrklock); |
651 | } |
764 | } |
652 | |
765 | |
653 | static void set_max_idle (int nthreads) |
766 | static void set_max_idle (int nthreads) |
654 | { |
767 | { |
655 | if (WORDACCESS_UNSAFE) LOCK (reqlock); |
768 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
656 | max_idle = nthreads <= 0 ? 1 : nthreads; |
769 | max_idle = nthreads <= 0 ? 1 : nthreads; |
657 | if (WORDACCESS_UNSAFE) UNLOCK (reqlock); |
770 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
658 | } |
771 | } |
659 | |
772 | |
660 | static void min_parallel (int nthreads) |
773 | static void min_parallel (int nthreads) |
661 | { |
774 | { |
662 | if (wanted < nthreads) |
775 | if (wanted < nthreads) |
… | |
… | |
677 | fd_set rfd; |
790 | fd_set rfd; |
678 | |
791 | |
679 | while (nreqs) |
792 | while (nreqs) |
680 | { |
793 | { |
681 | int size; |
794 | int size; |
682 | if (WORDACCESS_UNSAFE) LOCK (reslock); |
795 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
683 | size = res_queue.size; |
796 | size = res_queue.size; |
684 | if (WORDACCESS_UNSAFE) UNLOCK (reslock); |
797 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
685 | |
798 | |
686 | if (size) |
799 | if (size) |
687 | return; |
800 | return; |
688 | |
801 | |
689 | maybe_start_thread (); |
802 | maybe_start_thread (); |
690 | |
803 | |
691 | FD_ZERO(&rfd); |
804 | FD_ZERO (&rfd); |
692 | FD_SET(respipe [0], &rfd); |
805 | FD_SET (respipe [0], &rfd); |
693 | |
806 | |
694 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
807 | PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); |
695 | } |
808 | } |
696 | } |
809 | } |
697 | |
810 | |
698 | static int poll_cb () |
811 | static int poll_cb () |
699 | { |
812 | { |
… | |
… | |
713 | { |
826 | { |
714 | for (;;) |
827 | for (;;) |
715 | { |
828 | { |
716 | maybe_start_thread (); |
829 | maybe_start_thread (); |
717 | |
830 | |
718 | LOCK (reslock); |
831 | X_LOCK (reslock); |
719 | req = reqq_shift (&res_queue); |
832 | req = reqq_shift (&res_queue); |
720 | |
833 | |
721 | if (req) |
834 | if (req) |
722 | { |
835 | { |
723 | --npending; |
836 | --npending; |
724 | |
837 | |
725 | if (!res_queue.size) |
838 | if (!res_queue.size) |
726 | { |
839 | { |
727 | /* read any signals sent by the worker threads */ |
840 | /* read any signals sent by the worker threads */ |
728 | char buf [4]; |
841 | char buf [4]; |
729 | while (read (respipe [0], buf, 4) == 4) |
842 | while (respipe_read (respipe [0], buf, 4) == 4) |
730 | ; |
843 | ; |
731 | } |
844 | } |
732 | } |
845 | } |
733 | |
846 | |
734 | UNLOCK (reslock); |
847 | X_UNLOCK (reslock); |
735 | |
848 | |
736 | if (!req) |
849 | if (!req) |
737 | break; |
850 | break; |
738 | |
851 | |
739 | --nreqs; |
852 | --nreqs; |
… | |
… | |
745 | } |
858 | } |
746 | else |
859 | else |
747 | { |
860 | { |
748 | if (!req_invoke (req)) |
861 | if (!req_invoke (req)) |
749 | { |
862 | { |
750 | req_free (req); |
863 | req_destroy (req); |
751 | unblock_sig (); |
864 | unblock_sig (); |
752 | croak (0); |
865 | croak (0); |
753 | } |
866 | } |
754 | |
867 | |
755 | count++; |
868 | count++; |
756 | } |
869 | } |
757 | |
870 | |
758 | req_free (req); |
871 | req_destroy (req); |
759 | |
872 | |
760 | if (maxreqs && !--maxreqs) |
873 | if (maxreqs && !--maxreqs) |
761 | break; |
874 | break; |
762 | |
875 | |
763 | if (max_poll_time) |
876 | if (max_poll_time) |
… | |
… | |
777 | ++maxreqs; |
890 | ++maxreqs; |
778 | } |
891 | } |
779 | |
892 | |
780 | unblock_sig (); |
893 | unblock_sig (); |
781 | return count; |
894 | 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 | } |
895 | } |
795 | |
896 | |
796 | /*****************************************************************************/ |
897 | /*****************************************************************************/ |
797 | /* work around various missing functions */ |
898 | /* work around various missing functions */ |
798 | |
899 | |
… | |
… | |
803 | /* |
904 | /* |
804 | * make our pread/pwrite safe against themselves, but not against |
905 | * make our pread/pwrite safe against themselves, but not against |
805 | * normal read/write by using a mutex. slows down execution a lot, |
906 | * normal read/write by using a mutex. slows down execution a lot, |
806 | * but that's your problem, not mine. |
907 | * but that's your problem, not mine. |
807 | */ |
908 | */ |
808 | static mutex_t preadwritelock = MUTEX_INIT; |
909 | static mutex_t preadwritelock = X_MUTEX_INIT; |
809 | |
910 | |
810 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
911 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
811 | { |
912 | { |
812 | ssize_t res; |
913 | ssize_t res; |
813 | off_t ooffset; |
914 | off_t ooffset; |
814 | |
915 | |
815 | LOCK (preadwritelock); |
916 | X_LOCK (preadwritelock); |
816 | ooffset = lseek (fd, 0, SEEK_CUR); |
917 | ooffset = lseek (fd, 0, SEEK_CUR); |
817 | lseek (fd, offset, SEEK_SET); |
918 | lseek (fd, offset, SEEK_SET); |
818 | res = read (fd, buf, count); |
919 | res = read (fd, buf, count); |
819 | lseek (fd, ooffset, SEEK_SET); |
920 | lseek (fd, ooffset, SEEK_SET); |
820 | UNLOCK (preadwritelock); |
921 | X_UNLOCK (preadwritelock); |
821 | |
922 | |
822 | return res; |
923 | return res; |
823 | } |
924 | } |
824 | |
925 | |
825 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
926 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
826 | { |
927 | { |
827 | ssize_t res; |
928 | ssize_t res; |
828 | off_t ooffset; |
929 | off_t ooffset; |
829 | |
930 | |
830 | LOCK (preadwritelock); |
931 | X_LOCK (preadwritelock); |
831 | ooffset = lseek (fd, 0, SEEK_CUR); |
932 | ooffset = lseek (fd, 0, SEEK_CUR); |
832 | lseek (fd, offset, SEEK_SET); |
933 | lseek (fd, offset, SEEK_SET); |
833 | res = write (fd, buf, count); |
934 | res = write (fd, buf, count); |
834 | lseek (fd, offset, SEEK_SET); |
935 | lseek (fd, offset, SEEK_SET); |
835 | UNLOCK (preadwritelock); |
936 | X_UNLOCK (preadwritelock); |
836 | |
937 | |
837 | return res; |
938 | return res; |
838 | } |
939 | } |
839 | #endif |
940 | #endif |
840 | |
941 | |
… | |
… | |
873 | #if !HAVE_READAHEAD |
974 | #if !HAVE_READAHEAD |
874 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
975 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
875 | |
976 | |
876 | static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) |
977 | static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) |
877 | { |
978 | { |
|
|
979 | size_t todo = count; |
878 | dBUF; |
980 | dBUF; |
879 | |
981 | |
880 | while (count > 0) |
982 | while (todo > 0) |
881 | { |
983 | { |
882 | size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; |
984 | size_t len = todo < AIO_BUFSIZE ? todo : AIO_BUFSIZE; |
883 | |
985 | |
884 | pread (fd, aio_buf, len, offset); |
986 | pread (fd, aio_buf, len, offset); |
885 | offset += len; |
987 | offset += len; |
886 | count -= len; |
988 | todo -= len; |
887 | } |
989 | } |
888 | |
990 | |
889 | errno = 0; |
991 | errno = 0; |
|
|
992 | return count; |
890 | } |
993 | } |
891 | |
994 | |
892 | #endif |
995 | #endif |
893 | |
996 | |
894 | #if !HAVE_READDIR_R |
997 | #if !HAVE_READDIR_R |
895 | # define readdir_r aio_readdir_r |
998 | # define readdir_r aio_readdir_r |
896 | |
999 | |
897 | static mutex_t readdirlock = MUTEX_INIT; |
1000 | static mutex_t readdirlock = X_MUTEX_INIT; |
898 | |
1001 | |
899 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
1002 | static int readdir_r (DIR *dirp, X_DIRENT *ent, X_DIRENT **res) |
900 | { |
1003 | { |
901 | struct dirent *e; |
1004 | X_DIRENT *e; |
902 | int errorno; |
1005 | int errorno; |
903 | |
1006 | |
904 | LOCK (readdirlock); |
1007 | X_LOCK (readdirlock); |
905 | |
1008 | |
906 | e = readdir (dirp); |
1009 | e = readdir (dirp); |
907 | errorno = errno; |
1010 | errorno = errno; |
908 | |
1011 | |
909 | if (e) |
1012 | if (e) |
… | |
… | |
912 | strcpy (ent->d_name, e->d_name); |
1015 | strcpy (ent->d_name, e->d_name); |
913 | } |
1016 | } |
914 | else |
1017 | else |
915 | *res = 0; |
1018 | *res = 0; |
916 | |
1019 | |
917 | UNLOCK (readdirlock); |
1020 | X_UNLOCK (readdirlock); |
918 | |
1021 | |
919 | errno = errorno; |
1022 | errno = errorno; |
920 | return e ? 0 : -1; |
1023 | return e ? 0 : -1; |
921 | } |
1024 | } |
922 | #endif |
1025 | #endif |
… | |
… | |
1018 | static void scandir_ (aio_req req, worker *self) |
1121 | static void scandir_ (aio_req req, worker *self) |
1019 | { |
1122 | { |
1020 | DIR *dirp; |
1123 | DIR *dirp; |
1021 | union |
1124 | union |
1022 | { |
1125 | { |
1023 | struct dirent d; |
1126 | X_DIRENT d; |
1024 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
1127 | char b [offsetof (X_DIRENT, d_name) + NAME_MAX + 1]; |
1025 | } *u; |
1128 | } *u; |
1026 | struct dirent *entp; |
1129 | X_DIRENT *entp; |
1027 | char *name, *names; |
1130 | char *name, *names; |
1028 | int memlen = 4096; |
1131 | int memlen = 4096; |
1029 | int memofs = 0; |
1132 | int memofs = 0; |
1030 | int res = 0; |
1133 | int res = 0; |
1031 | int errorno; |
|
|
1032 | |
1134 | |
1033 | LOCK (wrklock); |
1135 | X_LOCK (wrklock); |
1034 | self->dirp = dirp = opendir (req->ptr1); |
1136 | self->dirp = dirp = opendir (req->ptr1); |
1035 | self->dbuf = u = malloc (sizeof (*u)); |
1137 | self->dbuf = u = malloc (sizeof (*u)); |
1036 | req->flags |= FLAG_PTR2_FREE; |
1138 | req->flags |= FLAG_PTR2_FREE; |
1037 | req->ptr2 = names = malloc (memlen); |
1139 | req->ptr2 = names = malloc (memlen); |
1038 | UNLOCK (wrklock); |
1140 | X_UNLOCK (wrklock); |
1039 | |
1141 | |
1040 | if (dirp && u && names) |
1142 | if (dirp && u && names) |
1041 | for (;;) |
1143 | for (;;) |
1042 | { |
1144 | { |
1043 | errno = 0; |
1145 | errno = 0; |
… | |
… | |
1055 | res++; |
1157 | res++; |
1056 | |
1158 | |
1057 | while (memofs + len > memlen) |
1159 | while (memofs + len > memlen) |
1058 | { |
1160 | { |
1059 | memlen *= 2; |
1161 | memlen *= 2; |
1060 | LOCK (wrklock); |
1162 | X_LOCK (wrklock); |
1061 | req->ptr2 = names = realloc (names, memlen); |
1163 | req->ptr2 = names = realloc (names, memlen); |
1062 | UNLOCK (wrklock); |
1164 | X_UNLOCK (wrklock); |
1063 | |
1165 | |
1064 | if (!names) |
1166 | if (!names) |
1065 | break; |
1167 | break; |
1066 | } |
1168 | } |
1067 | |
1169 | |
… | |
… | |
1076 | req->result = res; |
1178 | req->result = res; |
1077 | } |
1179 | } |
1078 | |
1180 | |
1079 | /*****************************************************************************/ |
1181 | /*****************************************************************************/ |
1080 | |
1182 | |
1081 | static void *aio_proc (void *thr_arg) |
1183 | X_THREAD_PROC (aio_proc) |
1082 | { |
1184 | { |
|
|
1185 | {//D |
1083 | aio_req req; |
1186 | aio_req req; |
1084 | struct timespec ts; |
1187 | struct timespec ts; |
1085 | worker *self = (worker *)thr_arg; |
1188 | worker *self = (worker *)thr_arg; |
1086 | |
1189 | |
1087 | /* try to distribute timeouts somewhat evenly */ |
1190 | /* try to distribute timeouts somewhat randomly */ |
1088 | ts.tv_nsec = (((unsigned long)self + (unsigned long)ts.tv_sec) & 1023UL) |
1191 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
1089 | * (1000000000UL / 1024UL); |
|
|
1090 | |
1192 | |
1091 | for (;;) |
1193 | for (;;) |
1092 | { |
1194 | { |
1093 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1195 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1094 | |
1196 | |
1095 | LOCK (reqlock); |
1197 | X_LOCK (reqlock); |
1096 | |
1198 | |
1097 | for (;;) |
1199 | for (;;) |
1098 | { |
1200 | { |
1099 | self->req = req = reqq_shift (&req_queue); |
1201 | self->req = req = reqq_shift (&req_queue); |
1100 | |
1202 | |
1101 | if (req) |
1203 | if (req) |
1102 | break; |
1204 | break; |
1103 | |
1205 | |
1104 | ++idle; |
1206 | ++idle; |
1105 | |
1207 | |
1106 | if (COND_TIMEDWAIT (reqwait, reqlock, ts) |
1208 | if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) |
1107 | == ETIMEDOUT) |
1209 | == ETIMEDOUT) |
1108 | { |
1210 | { |
1109 | if (idle > max_idle) |
1211 | if (idle > max_idle) |
1110 | { |
1212 | { |
1111 | --idle; |
1213 | --idle; |
1112 | UNLOCK (reqlock); |
1214 | X_UNLOCK (reqlock); |
1113 | LOCK (wrklock); |
1215 | X_LOCK (wrklock); |
1114 | --started; |
1216 | --started; |
1115 | UNLOCK (wrklock); |
1217 | X_UNLOCK (wrklock); |
1116 | goto quit; |
1218 | goto quit; |
1117 | } |
1219 | } |
1118 | |
1220 | |
1119 | /* we are allowed to idle, so do so without any timeout */ |
1221 | /* we are allowed to idle, so do so without any timeout */ |
1120 | COND_WAIT (reqwait, reqlock); |
1222 | X_COND_WAIT (reqwait, reqlock); |
1121 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1223 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1122 | } |
1224 | } |
1123 | |
1225 | |
1124 | --idle; |
1226 | --idle; |
1125 | } |
1227 | } |
1126 | |
1228 | |
1127 | --nready; |
1229 | --nready; |
1128 | |
1230 | |
1129 | UNLOCK (reqlock); |
1231 | X_UNLOCK (reqlock); |
1130 | |
1232 | |
1131 | errno = 0; /* strictly unnecessary */ |
1233 | errno = 0; /* strictly unnecessary */ |
1132 | |
1234 | |
1133 | if (!(req->flags & FLAG_CANCELLED)) |
1235 | if (!(req->flags & FLAG_CANCELLED)) |
1134 | switch (req->type) |
1236 | switch (req->type) |
1135 | { |
1237 | { |
1136 | case REQ_READ: req->result = pread (req->int1, req->ptr1, req->size, req->offs); break; |
1238 | case REQ_READ: req->result = req->offs >= 0 |
1137 | case REQ_WRITE: req->result = pwrite (req->int1, req->ptr1, req->size, req->offs); break; |
1239 | ? pread (req->int1, req->ptr1, req->size, req->offs) |
|
|
1240 | : read (req->int1, req->ptr1, req->size); break; |
|
|
1241 | case REQ_WRITE: req->result = req->offs >= 0 |
|
|
1242 | ? pwrite (req->int1, req->ptr1, req->size, req->offs) |
|
|
1243 | : write (req->int1, req->ptr1, req->size); break; |
1138 | |
1244 | |
1139 | case REQ_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
1245 | 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; |
1246 | case REQ_SENDFILE: req->result = sendfile_ (req->int1, req->int2, req->offs, req->size, self); break; |
1141 | |
1247 | |
1142 | case REQ_STAT: req->result = stat (req->ptr1, (Stat_t *)req->ptr2); break; |
1248 | case REQ_STAT: req->result = stat (req->ptr1, (Stat_t *)req->ptr2); break; |
… | |
… | |
1145 | |
1251 | |
1146 | case REQ_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break; |
1252 | 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; |
1253 | 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; |
1254 | case REQ_CHMOD: req->result = chmod (req->ptr1, req->mode); break; |
1149 | case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break; |
1255 | case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break; |
|
|
1256 | case REQ_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break; |
|
|
1257 | case REQ_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; |
1150 | |
1258 | |
1151 | case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break; |
1259 | case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break; |
1152 | case REQ_CLOSE: req->result = close (req->int1); break; |
1260 | case REQ_CLOSE: req->result = PerlIO_close ((PerlIO *)req->ptr1); break; |
1153 | case REQ_UNLINK: req->result = unlink (req->ptr1); break; |
1261 | case REQ_UNLINK: req->result = unlink (req->ptr1); break; |
1154 | case REQ_RMDIR: req->result = rmdir (req->ptr1); break; |
1262 | case REQ_RMDIR: req->result = rmdir (req->ptr1); break; |
1155 | case REQ_MKDIR: req->result = mkdir (req->ptr1, req->mode); break; |
1263 | case REQ_MKDIR: req->result = mkdir (req->ptr1, req->mode); break; |
1156 | case REQ_RENAME: req->result = rename (req->ptr2, req->ptr1); break; |
1264 | case REQ_RENAME: req->result = rename (req->ptr2, req->ptr1); break; |
1157 | case REQ_LINK: req->result = link (req->ptr2, req->ptr1); break; |
1265 | case REQ_LINK: req->result = link (req->ptr2, req->ptr1); break; |
… | |
… | |
1162 | case REQ_FDATASYNC: req->result = fdatasync (req->int1); break; |
1270 | case REQ_FDATASYNC: req->result = fdatasync (req->int1); break; |
1163 | case REQ_FSYNC: req->result = fsync (req->int1); break; |
1271 | case REQ_FSYNC: req->result = fsync (req->int1); break; |
1164 | case REQ_READDIR: scandir_ (req, self); break; |
1272 | case REQ_READDIR: scandir_ (req, self); break; |
1165 | |
1273 | |
1166 | case REQ_BUSY: |
1274 | case REQ_BUSY: |
|
|
1275 | #ifdef _WIN32 |
|
|
1276 | Sleep (req->nv1 * 1000.); |
|
|
1277 | #else |
1167 | { |
1278 | { |
1168 | struct timeval tv; |
1279 | struct timeval tv; |
1169 | |
1280 | |
1170 | tv.tv_sec = req->nv1; |
1281 | tv.tv_sec = req->nv1; |
1171 | tv.tv_usec = (req->nv1 - tv.tv_usec) * 1000000.; |
1282 | tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.; |
1172 | |
1283 | |
1173 | req->result = select (0, 0, 0, 0, &tv); |
1284 | req->result = select (0, 0, 0, 0, &tv); |
1174 | } |
1285 | } |
|
|
1286 | #endif |
|
|
1287 | break; |
1175 | |
1288 | |
1176 | case REQ_UTIME: |
1289 | case REQ_UTIME: |
1177 | case REQ_FUTIME: |
1290 | case REQ_FUTIME: |
1178 | { |
1291 | { |
1179 | struct timeval tv[2]; |
1292 | struct timeval tv[2]; |
… | |
… | |
1209 | break; |
1322 | break; |
1210 | } |
1323 | } |
1211 | |
1324 | |
1212 | req->errorno = errno; |
1325 | req->errorno = errno; |
1213 | |
1326 | |
1214 | LOCK (reslock); |
1327 | X_LOCK (reslock); |
1215 | |
1328 | |
1216 | ++npending; |
1329 | ++npending; |
1217 | |
1330 | |
1218 | if (!reqq_push (&res_queue, req)) |
1331 | if (!reqq_push (&res_queue, req)) |
1219 | { |
1332 | { |
1220 | /* write a dummy byte to the pipe so fh becomes ready */ |
1333 | /* write a dummy byte to the pipe so fh becomes ready */ |
1221 | write (respipe [1], &respipe, 1); |
1334 | respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1); |
1222 | |
1335 | |
1223 | /* optionally signal the main thread asynchronously */ |
1336 | /* optionally signal the main thread asynchronously */ |
1224 | if (main_sig) |
1337 | if (main_sig) |
1225 | pthread_kill (main_tid, main_sig); |
1338 | pthread_kill (main_tid, main_sig); |
1226 | } |
1339 | } |
1227 | |
1340 | |
1228 | self->req = 0; |
1341 | self->req = 0; |
1229 | worker_clear (self); |
1342 | worker_clear (self); |
1230 | |
1343 | |
1231 | UNLOCK (reslock); |
1344 | X_UNLOCK (reslock); |
1232 | } |
1345 | } |
1233 | |
1346 | |
1234 | quit: |
1347 | quit: |
1235 | LOCK (wrklock); |
1348 | X_LOCK (wrklock); |
1236 | worker_free (self); |
1349 | worker_free (self); |
1237 | UNLOCK (wrklock); |
1350 | X_UNLOCK (wrklock); |
1238 | |
1351 | |
1239 | return 0; |
1352 | return 0; |
|
|
1353 | }//D |
1240 | } |
1354 | } |
1241 | |
1355 | |
1242 | /*****************************************************************************/ |
1356 | /*****************************************************************************/ |
1243 | |
1357 | |
1244 | static void atfork_prepare (void) |
1358 | static void atfork_prepare (void) |
1245 | { |
1359 | { |
1246 | LOCK (wrklock); |
1360 | X_LOCK (wrklock); |
1247 | LOCK (reqlock); |
1361 | X_LOCK (reqlock); |
1248 | LOCK (reslock); |
1362 | X_LOCK (reslock); |
1249 | #if !HAVE_PREADWRITE |
1363 | #if !HAVE_PREADWRITE |
1250 | LOCK (preadwritelock); |
1364 | X_LOCK (preadwritelock); |
1251 | #endif |
1365 | #endif |
1252 | #if !HAVE_READDIR_R |
1366 | #if !HAVE_READDIR_R |
1253 | LOCK (readdirlock); |
1367 | X_LOCK (readdirlock); |
1254 | #endif |
1368 | #endif |
1255 | } |
1369 | } |
1256 | |
1370 | |
1257 | static void atfork_parent (void) |
1371 | static void atfork_parent (void) |
1258 | { |
1372 | { |
1259 | #if !HAVE_READDIR_R |
1373 | #if !HAVE_READDIR_R |
1260 | UNLOCK (readdirlock); |
1374 | X_UNLOCK (readdirlock); |
1261 | #endif |
1375 | #endif |
1262 | #if !HAVE_PREADWRITE |
1376 | #if !HAVE_PREADWRITE |
1263 | UNLOCK (preadwritelock); |
1377 | X_UNLOCK (preadwritelock); |
1264 | #endif |
1378 | #endif |
1265 | UNLOCK (reslock); |
1379 | X_UNLOCK (reslock); |
1266 | UNLOCK (reqlock); |
1380 | X_UNLOCK (reqlock); |
1267 | UNLOCK (wrklock); |
1381 | X_UNLOCK (wrklock); |
1268 | } |
1382 | } |
1269 | |
1383 | |
1270 | static void atfork_child (void) |
1384 | static void atfork_child (void) |
1271 | { |
1385 | { |
1272 | aio_req prv; |
1386 | aio_req prv; |
1273 | |
1387 | |
1274 | while (prv = reqq_shift (&req_queue)) |
1388 | while (prv = reqq_shift (&req_queue)) |
1275 | req_free (prv); |
1389 | req_destroy (prv); |
1276 | |
1390 | |
1277 | while (prv = reqq_shift (&res_queue)) |
1391 | while (prv = reqq_shift (&res_queue)) |
1278 | req_free (prv); |
1392 | req_destroy (prv); |
1279 | |
1393 | |
1280 | while (wrk_first.next != &wrk_first) |
1394 | while (wrk_first.next != &wrk_first) |
1281 | { |
1395 | { |
1282 | worker *wrk = wrk_first.next; |
1396 | worker *wrk = wrk_first.next; |
1283 | |
1397 | |
1284 | if (wrk->req) |
1398 | if (wrk->req) |
1285 | req_free (wrk->req); |
1399 | req_destroy (wrk->req); |
1286 | |
1400 | |
1287 | worker_clear (wrk); |
1401 | worker_clear (wrk); |
1288 | worker_free (wrk); |
1402 | worker_free (wrk); |
1289 | } |
1403 | } |
1290 | |
1404 | |
… | |
… | |
1292 | idle = 0; |
1406 | idle = 0; |
1293 | nreqs = 0; |
1407 | nreqs = 0; |
1294 | nready = 0; |
1408 | nready = 0; |
1295 | npending = 0; |
1409 | npending = 0; |
1296 | |
1410 | |
1297 | close (respipe [0]); |
|
|
1298 | close (respipe [1]); |
|
|
1299 | create_pipe (); |
1411 | create_respipe (); |
1300 | |
1412 | |
1301 | atfork_parent (); |
1413 | atfork_parent (); |
1302 | } |
1414 | } |
1303 | |
1415 | |
1304 | #define dREQ \ |
1416 | #define dREQ \ |
… | |
… | |
1326 | |
1438 | |
1327 | PROTOTYPES: ENABLE |
1439 | PROTOTYPES: ENABLE |
1328 | |
1440 | |
1329 | BOOT: |
1441 | BOOT: |
1330 | { |
1442 | { |
1331 | HV *stash = gv_stashpv ("IO::AIO", 1); |
1443 | stash = gv_stashpv ("IO::AIO", 1); |
1332 | |
1444 | |
1333 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
1445 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
1334 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
1446 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
1335 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
1447 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
1336 | newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); |
1448 | newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); |
1337 | newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); |
1449 | newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); |
|
|
1450 | #ifdef _WIN32 |
|
|
1451 | X_MUTEX_CHECK (wrklock); |
|
|
1452 | X_MUTEX_CHECK (reslock); |
|
|
1453 | X_MUTEX_CHECK (reqlock); |
|
|
1454 | X_MUTEX_CHECK (reqwait); |
|
|
1455 | X_MUTEX_CHECK (preadwritelock); |
|
|
1456 | X_MUTEX_CHECK (readdirlock); |
|
|
1457 | |
|
|
1458 | X_COND_CHECK (reqwait); |
|
|
1459 | #else |
1338 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
1460 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
1339 | newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); |
1461 | newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); |
|
|
1462 | #endif |
1340 | |
1463 | |
1341 | create_pipe (); |
1464 | create_respipe (); |
|
|
1465 | |
1342 | ATFORK (atfork_prepare, atfork_parent, atfork_child); |
1466 | X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child); |
1343 | } |
1467 | } |
1344 | |
1468 | |
1345 | void |
1469 | void |
1346 | max_poll_reqs (int nreqs) |
1470 | max_poll_reqs (int nreqs) |
1347 | PROTOTYPE: $ |
1471 | PROTOTYPE: $ |
… | |
… | |
1392 | |
1516 | |
1393 | REQ_SEND; |
1517 | REQ_SEND; |
1394 | } |
1518 | } |
1395 | |
1519 | |
1396 | void |
1520 | void |
1397 | aio_close (SV *fh, SV *callback=&PL_sv_undef) |
1521 | aio_fsync (SV *fh, SV *callback=&PL_sv_undef) |
1398 | PROTOTYPE: $;$ |
1522 | PROTOTYPE: $;$ |
1399 | ALIAS: |
1523 | ALIAS: |
1400 | aio_close = REQ_CLOSE |
|
|
1401 | aio_fsync = REQ_FSYNC |
1524 | aio_fsync = REQ_FSYNC |
1402 | aio_fdatasync = REQ_FDATASYNC |
1525 | aio_fdatasync = REQ_FDATASYNC |
1403 | PPCODE: |
1526 | PPCODE: |
1404 | { |
1527 | { |
1405 | dREQ; |
1528 | dREQ; |
… | |
… | |
1410 | |
1533 | |
1411 | REQ_SEND (req); |
1534 | REQ_SEND (req); |
1412 | } |
1535 | } |
1413 | |
1536 | |
1414 | void |
1537 | void |
|
|
1538 | aio_close (SV *fh, SV *callback=&PL_sv_undef) |
|
|
1539 | PROTOTYPE: $;$ |
|
|
1540 | PPCODE: |
|
|
1541 | { |
|
|
1542 | PerlIO *io = IoIFP (sv_2io (fh)); |
|
|
1543 | int fd = PerlIO_fileno (io); |
|
|
1544 | |
|
|
1545 | if (fd < 0) |
|
|
1546 | croak ("aio_close called with fd-less filehandle"); |
|
|
1547 | |
|
|
1548 | PerlIO_binmode (io, 0, 0, 0); |
|
|
1549 | |
|
|
1550 | { |
|
|
1551 | dREQ; |
|
|
1552 | |
|
|
1553 | req->type = REQ_CLOSE; |
|
|
1554 | req->sv1 = newSVsv (fh); |
|
|
1555 | req->ptr1 = (void *)io; |
|
|
1556 | |
|
|
1557 | REQ_SEND (req); |
|
|
1558 | } |
|
|
1559 | } |
|
|
1560 | |
|
|
1561 | void |
1415 | aio_read (SV *fh, UV offset, UV length, SV8 *data, UV dataoffset, SV *callback=&PL_sv_undef) |
1562 | aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef) |
1416 | ALIAS: |
1563 | ALIAS: |
1417 | aio_read = REQ_READ |
1564 | aio_read = REQ_READ |
1418 | aio_write = REQ_WRITE |
1565 | aio_write = REQ_WRITE |
1419 | PROTOTYPE: $$$$$;$ |
1566 | PROTOTYPE: $$$$$;$ |
1420 | PPCODE: |
1567 | PPCODE: |
1421 | { |
1568 | { |
1422 | STRLEN svlen; |
1569 | STRLEN svlen; |
1423 | char *svptr = SvPVbyte (data, svlen); |
1570 | char *svptr = SvPVbyte (data, svlen); |
|
|
1571 | UV len = SvUV (length); |
1424 | |
1572 | |
1425 | SvUPGRADE (data, SVt_PV); |
1573 | SvUPGRADE (data, SVt_PV); |
1426 | SvPOK_on (data); |
1574 | SvPOK_on (data); |
1427 | |
1575 | |
1428 | if (dataoffset < 0) |
1576 | if (dataoffset < 0) |
1429 | dataoffset += svlen; |
1577 | dataoffset += svlen; |
1430 | |
1578 | |
1431 | if (dataoffset < 0 || dataoffset > svlen) |
1579 | if (dataoffset < 0 || dataoffset > svlen) |
1432 | croak ("data offset outside of string"); |
1580 | croak ("dataoffset outside of data scalar"); |
1433 | |
1581 | |
1434 | if (ix == REQ_WRITE) |
1582 | if (ix == REQ_WRITE) |
1435 | { |
1583 | { |
1436 | /* write: check length and adjust. */ |
1584 | /* write: check length and adjust. */ |
1437 | if (length < 0 || length + dataoffset > svlen) |
1585 | if (!SvOK (length) || len + dataoffset > svlen) |
1438 | length = svlen - dataoffset; |
1586 | len = svlen - dataoffset; |
1439 | } |
1587 | } |
1440 | else |
1588 | else |
1441 | { |
1589 | { |
1442 | /* read: grow scalar as necessary */ |
1590 | /* read: grow scalar as necessary */ |
1443 | svptr = SvGROW (data, length + dataoffset + 1); |
1591 | svptr = SvGROW (data, len + dataoffset + 1); |
1444 | } |
1592 | } |
1445 | |
1593 | |
1446 | if (length < 0) |
1594 | if (len < 0) |
1447 | croak ("length must not be negative"); |
1595 | croak ("length must not be negative"); |
1448 | |
1596 | |
1449 | { |
1597 | { |
1450 | dREQ; |
1598 | dREQ; |
1451 | |
1599 | |
1452 | req->type = ix; |
1600 | req->type = ix; |
1453 | req->sv1 = newSVsv (fh); |
1601 | req->sv1 = newSVsv (fh); |
1454 | req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
1602 | req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
1455 | : IoOFP (sv_2io (fh))); |
1603 | : IoOFP (sv_2io (fh))); |
1456 | req->offs = offset; |
1604 | req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; |
1457 | req->size = length; |
1605 | req->size = len; |
1458 | req->sv2 = SvREFCNT_inc (data); |
1606 | req->sv2 = SvREFCNT_inc (data); |
1459 | req->ptr1 = (char *)svptr + dataoffset; |
1607 | req->ptr1 = (char *)svptr + dataoffset; |
1460 | req->stroffset = dataoffset; |
1608 | req->stroffset = dataoffset; |
1461 | |
1609 | |
1462 | if (!SvREADONLY (data)) |
1610 | if (!SvREADONLY (data)) |
1463 | { |
1611 | { |
1464 | SvREADONLY_on (data); |
1612 | SvREADONLY_on (data); |
1465 | req->flags |= FLAG_SV1_RO_OFF; |
1613 | req->flags |= FLAG_SV2_RO_OFF; |
1466 | } |
1614 | } |
1467 | |
1615 | |
1468 | REQ_SEND; |
1616 | REQ_SEND; |
1469 | } |
1617 | } |
1470 | } |
1618 | } |
… | |
… | |
1488 | |
1636 | |
1489 | REQ_SEND; |
1637 | REQ_SEND; |
1490 | } |
1638 | } |
1491 | |
1639 | |
1492 | void |
1640 | void |
1493 | aio_sendfile (SV *out_fh, SV *in_fh, UV in_offset, UV length, SV *callback=&PL_sv_undef) |
1641 | aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef) |
1494 | PROTOTYPE: $$$$;$ |
1642 | PROTOTYPE: $$$$;$ |
1495 | PPCODE: |
1643 | PPCODE: |
1496 | { |
1644 | { |
1497 | dREQ; |
1645 | dREQ; |
1498 | |
1646 | |
1499 | req->type = REQ_SENDFILE; |
1647 | req->type = REQ_SENDFILE; |
1500 | req->sv1 = newSVsv (out_fh); |
1648 | req->sv1 = newSVsv (out_fh); |
1501 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
1649 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
1502 | req->sv2 = newSVsv (in_fh); |
1650 | req->sv2 = newSVsv (in_fh); |
1503 | req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
1651 | req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
1504 | req->offs = in_offset; |
1652 | req->offs = SvVAL64 (in_offset); |
1505 | req->size = length; |
1653 | req->size = length; |
1506 | |
1654 | |
1507 | REQ_SEND; |
1655 | REQ_SEND; |
1508 | } |
1656 | } |
1509 | |
1657 | |
1510 | void |
1658 | void |
1511 | aio_readahead (SV *fh, UV offset, IV length, SV *callback=&PL_sv_undef) |
1659 | aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef) |
1512 | PROTOTYPE: $$$;$ |
1660 | PROTOTYPE: $$$;$ |
1513 | PPCODE: |
1661 | PPCODE: |
1514 | { |
1662 | { |
1515 | dREQ; |
1663 | dREQ; |
1516 | |
1664 | |
1517 | req->type = REQ_READAHEAD; |
1665 | req->type = REQ_READAHEAD; |
1518 | req->sv1 = newSVsv (fh); |
1666 | req->sv1 = newSVsv (fh); |
1519 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
1667 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
1520 | req->offs = offset; |
1668 | req->offs = SvVAL64 (offset); |
1521 | req->size = length; |
1669 | req->size = length; |
1522 | |
1670 | |
1523 | REQ_SEND; |
1671 | REQ_SEND; |
1524 | } |
1672 | } |
1525 | |
1673 | |
… | |
… | |
1533 | dREQ; |
1681 | dREQ; |
1534 | |
1682 | |
1535 | req->ptr2 = malloc (sizeof (Stat_t)); |
1683 | req->ptr2 = malloc (sizeof (Stat_t)); |
1536 | if (!req->ptr2) |
1684 | if (!req->ptr2) |
1537 | { |
1685 | { |
1538 | req_free (req); |
1686 | req_destroy (req); |
1539 | croak ("out of memory during aio_stat statdata allocation"); |
1687 | croak ("out of memory during aio_stat statdata allocation"); |
1540 | } |
1688 | } |
1541 | |
1689 | |
1542 | req->flags |= FLAG_PTR2_FREE; |
1690 | req->flags |= FLAG_PTR2_FREE; |
1543 | req->sv1 = newSVsv (fh_or_path); |
1691 | req->sv1 = newSVsv (fh_or_path); |
… | |
… | |
1572 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1720 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1573 | } |
1721 | } |
1574 | else |
1722 | else |
1575 | { |
1723 | { |
1576 | req->type = REQ_FUTIME; |
1724 | req->type = REQ_FUTIME; |
|
|
1725 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
1726 | } |
|
|
1727 | |
|
|
1728 | REQ_SEND; |
|
|
1729 | } |
|
|
1730 | |
|
|
1731 | void |
|
|
1732 | aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef) |
|
|
1733 | PPCODE: |
|
|
1734 | { |
|
|
1735 | dREQ; |
|
|
1736 | |
|
|
1737 | req->sv1 = newSVsv (fh_or_path); |
|
|
1738 | req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; |
|
|
1739 | |
|
|
1740 | if (SvPOK (fh_or_path)) |
|
|
1741 | { |
|
|
1742 | req->type = REQ_TRUNCATE; |
|
|
1743 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1744 | } |
|
|
1745 | else |
|
|
1746 | { |
|
|
1747 | req->type = REQ_FTRUNCATE; |
1577 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1748 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1578 | } |
1749 | } |
1579 | |
1750 | |
1580 | REQ_SEND; |
1751 | REQ_SEND; |
1581 | } |
1752 | } |
… | |
… | |
1797 | CODE: |
1968 | CODE: |
1798 | { |
1969 | { |
1799 | if (block_sig_level) |
1970 | if (block_sig_level) |
1800 | croak ("cannot call IO::AIO::setsig from within aio_block/callback"); |
1971 | croak ("cannot call IO::AIO::setsig from within aio_block/callback"); |
1801 | |
1972 | |
1802 | LOCK (reslock); |
1973 | X_LOCK (reslock); |
1803 | main_tid = pthread_self (); |
1974 | main_tid = pthread_self (); |
1804 | main_sig = signum; |
1975 | main_sig = signum; |
1805 | UNLOCK (reslock); |
1976 | X_UNLOCK (reslock); |
1806 | |
1977 | |
1807 | if (main_sig && npending) |
1978 | if (main_sig && npending) |
1808 | pthread_kill (main_tid, main_sig); |
1979 | pthread_kill (main_tid, main_sig); |
1809 | } |
1980 | } |
1810 | |
1981 | |
… | |
… | |
1854 | |
2025 | |
1855 | int |
2026 | int |
1856 | nthreads() |
2027 | nthreads() |
1857 | PROTOTYPE: |
2028 | PROTOTYPE: |
1858 | CODE: |
2029 | CODE: |
1859 | if (WORDACCESS_UNSAFE) LOCK (wrklock); |
2030 | if (WORDACCESS_UNSAFE) X_LOCK (wrklock); |
1860 | RETVAL = started; |
2031 | RETVAL = started; |
1861 | if (WORDACCESS_UNSAFE) UNLOCK (wrklock); |
2032 | if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock); |
1862 | OUTPUT: |
2033 | OUTPUT: |
1863 | RETVAL |
2034 | RETVAL |
1864 | |
2035 | |
1865 | PROTOTYPES: DISABLE |
2036 | PROTOTYPES: DISABLE |
1866 | |
2037 | |