1 | /* solaris */ |
1 | /* solaris */ |
2 | #define _POSIX_PTHREAD_SEMANTICS 1 |
2 | #define _POSIX_PTHREAD_SEMANTICS 1 |
3 | |
3 | |
4 | #if __linux |
4 | #if __linux && !defined(_GNU_SOURCE) |
5 | # define _GNU_SOURCE |
5 | # define _GNU_SOURCE |
6 | #endif |
6 | #endif |
7 | |
7 | |
|
|
8 | /* just in case */ |
8 | #define _REENTRANT 1 |
9 | #define _REENTRANT 1 |
9 | |
10 | |
10 | #include <errno.h> |
11 | #include <errno.h> |
11 | |
12 | |
12 | #include "EXTERN.h" |
13 | #include "EXTERN.h" |
… | |
… | |
47 | /* used for struct dirent, AIX doesn't provide it */ |
48 | /* used for struct dirent, AIX doesn't provide it */ |
48 | #ifndef NAME_MAX |
49 | #ifndef NAME_MAX |
49 | # define NAME_MAX 4096 |
50 | # define NAME_MAX 4096 |
50 | #endif |
51 | #endif |
51 | |
52 | |
|
|
53 | #ifndef PTHREAD_STACK_MIN |
|
|
54 | /* care for broken platforms, e.g. windows */ |
|
|
55 | # define PTHREAD_STACK_MIN 16384 |
|
|
56 | #endif |
|
|
57 | |
52 | #if __ia64 |
58 | #if __ia64 |
53 | # define STACKSIZE 65536 |
59 | # define STACKSIZE 65536 |
54 | #elif __i386 || __x86_64 /* 16k is unreasonably high :( */ |
60 | #elif __i386 || __x86_64 /* 16k is unreasonably high :( */ |
55 | # define STACKSIZE PTHREAD_STACK_MIN |
61 | # define STACKSIZE PTHREAD_STACK_MIN |
56 | #else |
62 | #else |
… | |
… | |
126 | NUM_PRI = PRI_MAX + PRI_BIAS + 1, |
132 | NUM_PRI = PRI_MAX + PRI_BIAS + 1, |
127 | }; |
133 | }; |
128 | |
134 | |
129 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
135 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
130 | |
136 | |
131 | static int started, wanted; |
137 | static unsigned int started, wanted; |
132 | static volatile int nreqs; |
138 | static volatile unsigned int nreqs; |
133 | static int max_outstanding = 1<<30; |
139 | static volatile unsigned int max_outstanding = 0xffffffff; |
134 | static int respipe [2]; |
140 | static int respipe [2]; |
135 | |
141 | |
136 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
142 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
137 | # define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
143 | # define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
138 | #else |
144 | #else |
… | |
… | |
140 | #endif |
146 | #endif |
141 | |
147 | |
142 | #define LOCK(mutex) pthread_mutex_lock (&(mutex)) |
148 | #define LOCK(mutex) pthread_mutex_lock (&(mutex)) |
143 | #define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
149 | #define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
144 | |
150 | |
145 | /* worker threasd management */ |
151 | /* worker threads management */ |
146 | static pthread_mutex_t wrklock = AIO_MUTEX_INIT; |
152 | static pthread_mutex_t wrklock = AIO_MUTEX_INIT; |
147 | |
153 | |
148 | typedef struct worker { |
154 | typedef struct worker { |
149 | /* locked by wrklock */ |
155 | /* locked by wrklock */ |
150 | struct worker *prev, *next; |
156 | struct worker *prev, *next; |
… | |
… | |
238 | } |
244 | } |
239 | |
245 | |
240 | abort (); |
246 | abort (); |
241 | } |
247 | } |
242 | |
248 | |
|
|
249 | static int poll_cb (int max); |
243 | static void req_invoke (aio_req req); |
250 | static void req_invoke (aio_req req); |
244 | static void req_free (aio_req req); |
251 | static void req_free (aio_req req); |
245 | static void req_cancel (aio_req req); |
252 | static void req_cancel (aio_req req); |
246 | |
253 | |
247 | /* must be called at most once */ |
254 | /* must be called at most once */ |
… | |
… | |
490 | req->flags |= FLAG_CANCELLED; |
497 | req->flags |= FLAG_CANCELLED; |
491 | |
498 | |
492 | req_cancel_subs (req); |
499 | req_cancel_subs (req); |
493 | } |
500 | } |
494 | |
501 | |
495 | static int poll_cb () |
502 | static int poll_cb (int max) |
496 | { |
503 | { |
497 | dSP; |
504 | dSP; |
498 | int count = 0; |
505 | int count = 0; |
499 | int do_croak = 0; |
506 | int do_croak = 0; |
500 | aio_req req; |
507 | aio_req req; |
501 | |
508 | |
502 | for (;;) |
509 | for (;;) |
503 | { |
510 | { |
504 | LOCK (reslock); |
511 | while (max <= 0 || count < max) |
505 | req = reqq_shift (&res_queue); |
|
|
506 | |
|
|
507 | if (req) |
|
|
508 | { |
512 | { |
|
|
513 | LOCK (reslock); |
|
|
514 | req = reqq_shift (&res_queue); |
|
|
515 | |
509 | if (!res_queue.size) |
516 | if (req) |
510 | { |
517 | { |
|
|
518 | if (!res_queue.size) |
|
|
519 | { |
511 | /* read any signals sent by the worker threads */ |
520 | /* read any signals sent by the worker threads */ |
512 | char buf [32]; |
521 | char buf [32]; |
513 | while (read (respipe [0], buf, 32) == 32) |
522 | while (read (respipe [0], buf, 32) == 32) |
|
|
523 | ; |
514 | ; |
524 | } |
515 | } |
525 | } |
|
|
526 | |
|
|
527 | UNLOCK (reslock); |
|
|
528 | |
|
|
529 | if (!req) |
|
|
530 | break; |
|
|
531 | |
|
|
532 | --nreqs; |
|
|
533 | |
|
|
534 | if (req->type == REQ_QUIT) |
|
|
535 | --started; |
|
|
536 | else if (req->type == REQ_GROUP && req->length) |
|
|
537 | { |
|
|
538 | req->fd = 1; /* mark request as delayed */ |
|
|
539 | continue; |
|
|
540 | } |
|
|
541 | else |
|
|
542 | { |
|
|
543 | if (req->type == REQ_READ) |
|
|
544 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
|
|
545 | |
|
|
546 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
547 | SvREADONLY_off (req->data); |
|
|
548 | |
|
|
549 | if (req->statdata) |
|
|
550 | { |
|
|
551 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
552 | PL_laststatval = req->result; |
|
|
553 | PL_statcache = *(req->statdata); |
|
|
554 | } |
|
|
555 | |
|
|
556 | req_invoke (req); |
|
|
557 | |
|
|
558 | count++; |
|
|
559 | } |
|
|
560 | |
|
|
561 | req_free (req); |
516 | } |
562 | } |
517 | |
563 | |
518 | UNLOCK (reslock); |
564 | if (nreqs <= max_outstanding) |
519 | |
|
|
520 | if (!req) |
|
|
521 | break; |
565 | break; |
522 | |
566 | |
523 | --nreqs; |
567 | poll_wait (); |
524 | |
568 | |
525 | if (req->type == REQ_QUIT) |
569 | max = 0; |
526 | started--; |
|
|
527 | else if (req->type == REQ_GROUP && req->length) |
|
|
528 | { |
|
|
529 | req->fd = 1; /* mark request as delayed */ |
|
|
530 | continue; |
|
|
531 | } |
|
|
532 | else |
|
|
533 | { |
|
|
534 | if (req->type == REQ_READ) |
|
|
535 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
|
|
536 | |
|
|
537 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
538 | SvREADONLY_off (req->data); |
|
|
539 | |
|
|
540 | if (req->statdata) |
|
|
541 | { |
|
|
542 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
543 | PL_laststatval = req->result; |
|
|
544 | PL_statcache = *(req->statdata); |
|
|
545 | } |
|
|
546 | |
|
|
547 | req_invoke (req); |
|
|
548 | |
|
|
549 | count++; |
|
|
550 | } |
|
|
551 | |
|
|
552 | req_free (req); |
|
|
553 | } |
570 | } |
554 | |
571 | |
555 | return count; |
572 | return count; |
556 | } |
573 | } |
557 | |
574 | |
558 | static void *aio_proc(void *arg); |
575 | static void *aio_proc(void *arg); |
559 | |
576 | |
560 | static void start_thread (void) |
577 | static void start_thread (void) |
561 | { |
578 | { |
|
|
579 | sigset_t fullsigset, oldsigset; |
|
|
580 | pthread_attr_t attr; |
|
|
581 | |
562 | worker *wrk = calloc (1, sizeof (worker)); |
582 | worker *wrk = calloc (1, sizeof (worker)); |
563 | |
583 | |
564 | if (!wrk) |
584 | if (!wrk) |
565 | croak ("unable to allocate worker thread data"); |
585 | croak ("unable to allocate worker thread data"); |
566 | |
|
|
567 | sigset_t fullsigset, oldsigset; |
|
|
568 | pthread_attr_t attr; |
|
|
569 | |
586 | |
570 | pthread_attr_init (&attr); |
587 | pthread_attr_init (&attr); |
571 | pthread_attr_setstacksize (&attr, STACKSIZE); |
588 | pthread_attr_setstacksize (&attr, STACKSIZE); |
572 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
589 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
573 | |
590 | |
… | |
… | |
580 | { |
597 | { |
581 | wrk->prev = &wrk_first; |
598 | wrk->prev = &wrk_first; |
582 | wrk->next = wrk_first.next; |
599 | wrk->next = wrk_first.next; |
583 | wrk_first.next->prev = wrk; |
600 | wrk_first.next->prev = wrk; |
584 | wrk_first.next = wrk; |
601 | wrk_first.next = wrk; |
585 | started++; |
602 | ++started; |
586 | } |
603 | } |
587 | else |
604 | else |
588 | free (wrk); |
605 | free (wrk); |
589 | |
606 | |
590 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
607 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
… | |
… | |
600 | |
617 | |
601 | LOCK (reqlock); |
618 | LOCK (reqlock); |
602 | reqq_push (&req_queue, req); |
619 | reqq_push (&req_queue, req); |
603 | pthread_cond_signal (&reqwait); |
620 | pthread_cond_signal (&reqwait); |
604 | UNLOCK (reqlock); |
621 | UNLOCK (reqlock); |
605 | |
|
|
606 | if (nreqs > max_outstanding) |
|
|
607 | for (;;) |
|
|
608 | { |
|
|
609 | poll_cb (); |
|
|
610 | |
|
|
611 | if (nreqs <= max_outstanding) |
|
|
612 | break; |
|
|
613 | |
|
|
614 | poll_wait (); |
|
|
615 | } |
|
|
616 | } |
622 | } |
617 | |
623 | |
618 | static void end_thread (void) |
624 | static void end_thread (void) |
619 | { |
625 | { |
620 | aio_req req; |
626 | aio_req req; |
… | |
… | |
647 | } |
653 | } |
648 | |
654 | |
649 | while (started > wanted) |
655 | while (started > wanted) |
650 | { |
656 | { |
651 | poll_wait (); |
657 | poll_wait (); |
652 | poll_cb (); |
658 | poll_cb (0); |
653 | } |
659 | } |
654 | } |
660 | } |
655 | |
661 | |
656 | static void create_pipe () |
662 | static void create_pipe () |
657 | { |
663 | { |
… | |
… | |
713 | #if !HAVE_FDATASYNC |
719 | #if !HAVE_FDATASYNC |
714 | # define fdatasync fsync |
720 | # define fdatasync fsync |
715 | #endif |
721 | #endif |
716 | |
722 | |
717 | #if !HAVE_READAHEAD |
723 | #if !HAVE_READAHEAD |
718 | # define readahead aio_readahead |
724 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
719 | |
725 | |
720 | static ssize_t readahead (int fd, off_t offset, size_t count) |
726 | static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) |
721 | { |
727 | { |
722 | dBUF; |
728 | dBUF; |
723 | |
729 | |
724 | while (count > 0) |
730 | while (count > 0) |
725 | { |
731 | { |
… | |
… | |
730 | count -= len; |
736 | count -= len; |
731 | } |
737 | } |
732 | |
738 | |
733 | errno = 0; |
739 | errno = 0; |
734 | } |
740 | } |
|
|
741 | |
735 | #endif |
742 | #endif |
736 | |
743 | |
737 | #if !HAVE_READDIR_R |
744 | #if !HAVE_READDIR_R |
738 | # define readdir_r aio_readdir_r |
745 | # define readdir_r aio_readdir_r |
739 | |
746 | |
… | |
… | |
784 | { |
791 | { |
785 | off_t sbytes; |
792 | off_t sbytes; |
786 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
793 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
787 | |
794 | |
788 | if (res < 0 && sbytes) |
795 | if (res < 0 && sbytes) |
789 | /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ |
796 | /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */ |
790 | res = sbytes; |
797 | res = sbytes; |
791 | } |
798 | } |
792 | |
799 | |
793 | # elif __hpux |
800 | # elif __hpux |
794 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
801 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
… | |
… | |
874 | int errorno; |
881 | int errorno; |
875 | |
882 | |
876 | LOCK (wrklock); |
883 | LOCK (wrklock); |
877 | self->dirp = dirp = opendir (req->dataptr); |
884 | self->dirp = dirp = opendir (req->dataptr); |
878 | self->dbuf = u = malloc (sizeof (*u)); |
885 | self->dbuf = u = malloc (sizeof (*u)); |
|
|
886 | req->data2ptr = names = malloc (memlen); |
879 | UNLOCK (wrklock); |
887 | UNLOCK (wrklock); |
880 | |
|
|
881 | req->data2ptr = names = malloc (memlen); |
|
|
882 | |
888 | |
883 | if (dirp && u && names) |
889 | if (dirp && u && names) |
884 | for (;;) |
890 | for (;;) |
885 | { |
891 | { |
886 | errno = 0; |
892 | errno = 0; |
… | |
… | |
1107 | create_pipe (); |
1113 | create_pipe (); |
1108 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
1114 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
1109 | } |
1115 | } |
1110 | |
1116 | |
1111 | void |
1117 | void |
1112 | min_parallel (nthreads) |
1118 | min_parallel (int nthreads) |
1113 | int nthreads |
|
|
1114 | PROTOTYPE: $ |
1119 | PROTOTYPE: $ |
1115 | |
1120 | |
1116 | void |
1121 | void |
1117 | max_parallel (nthreads) |
1122 | max_parallel (int nthreads) |
1118 | int nthreads |
|
|
1119 | PROTOTYPE: $ |
1123 | PROTOTYPE: $ |
1120 | |
1124 | |
1121 | int |
1125 | int |
1122 | max_outstanding (nreqs) |
1126 | max_outstanding (int maxreqs) |
1123 | int nreqs |
1127 | PROTOTYPE: $ |
1124 | PROTOTYPE: $ |
|
|
1125 | CODE: |
1128 | CODE: |
1126 | RETVAL = max_outstanding; |
1129 | RETVAL = max_outstanding; |
1127 | max_outstanding = nreqs; |
1130 | max_outstanding = maxreqs; |
|
|
1131 | OUTPUT: |
|
|
1132 | RETVAL |
1128 | |
1133 | |
1129 | void |
1134 | void |
1130 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
1135 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
1131 | SV * pathname |
1136 | SV * pathname |
1132 | int flags |
1137 | int flags |
… | |
… | |
1409 | PROTOTYPE: |
1414 | PROTOTYPE: |
1410 | CODE: |
1415 | CODE: |
1411 | while (nreqs) |
1416 | while (nreqs) |
1412 | { |
1417 | { |
1413 | poll_wait (); |
1418 | poll_wait (); |
1414 | poll_cb (); |
1419 | poll_cb (0); |
1415 | } |
1420 | } |
1416 | |
1421 | |
1417 | void |
1422 | void |
1418 | poll() |
1423 | poll() |
1419 | PROTOTYPE: |
1424 | PROTOTYPE: |
1420 | CODE: |
1425 | CODE: |
1421 | if (nreqs) |
1426 | if (nreqs) |
1422 | { |
1427 | { |
1423 | poll_wait (); |
1428 | poll_wait (); |
1424 | poll_cb (); |
1429 | poll_cb (0); |
1425 | } |
1430 | } |
1426 | |
1431 | |
1427 | int |
1432 | int |
1428 | poll_fileno() |
1433 | poll_fileno() |
1429 | PROTOTYPE: |
1434 | PROTOTYPE: |
… | |
… | |
1434 | |
1439 | |
1435 | int |
1440 | int |
1436 | poll_cb(...) |
1441 | poll_cb(...) |
1437 | PROTOTYPE: |
1442 | PROTOTYPE: |
1438 | CODE: |
1443 | CODE: |
1439 | RETVAL = poll_cb (); |
1444 | RETVAL = poll_cb (0); |
|
|
1445 | OUTPUT: |
|
|
1446 | RETVAL |
|
|
1447 | |
|
|
1448 | int |
|
|
1449 | poll_some(int max = 0) |
|
|
1450 | PROTOTYPE: $ |
|
|
1451 | CODE: |
|
|
1452 | RETVAL = poll_cb (max); |
1440 | OUTPUT: |
1453 | OUTPUT: |
1441 | RETVAL |
1454 | RETVAL |
1442 | |
1455 | |
1443 | void |
1456 | void |
1444 | poll_wait() |
1457 | poll_wait() |