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; |
|
|
139 | static volatile unsigned int max_outstanding = 0xffffffff; |
133 | static int respipe [2]; |
140 | static int respipe [2]; |
134 | |
141 | |
135 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
142 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
136 | # define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
143 | # define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
137 | #else |
144 | #else |
… | |
… | |
139 | #endif |
146 | #endif |
140 | |
147 | |
141 | #define LOCK(mutex) pthread_mutex_lock (&(mutex)) |
148 | #define LOCK(mutex) pthread_mutex_lock (&(mutex)) |
142 | #define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
149 | #define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
143 | |
150 | |
144 | /* worker threasd management */ |
151 | /* worker threads management */ |
145 | static pthread_mutex_t wrklock = AIO_MUTEX_INIT; |
152 | static pthread_mutex_t wrklock = AIO_MUTEX_INIT; |
146 | |
153 | |
147 | typedef struct worker { |
154 | typedef struct worker { |
148 | /* locked by wrklock */ |
155 | /* locked by wrklock */ |
149 | struct worker *prev, *next; |
156 | struct worker *prev, *next; |
… | |
… | |
237 | } |
244 | } |
238 | |
245 | |
239 | abort (); |
246 | abort (); |
240 | } |
247 | } |
241 | |
248 | |
242 | static int poll_cb (); |
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 | |
… | |
… | |
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); |
… | |
… | |
636 | } |
653 | } |
637 | |
654 | |
638 | while (started > wanted) |
655 | while (started > wanted) |
639 | { |
656 | { |
640 | poll_wait (); |
657 | poll_wait (); |
641 | poll_cb (); |
658 | poll_cb (0); |
642 | } |
659 | } |
643 | } |
660 | } |
644 | |
661 | |
645 | static void create_pipe () |
662 | static void create_pipe () |
646 | { |
663 | { |
… | |
… | |
702 | #if !HAVE_FDATASYNC |
719 | #if !HAVE_FDATASYNC |
703 | # define fdatasync fsync |
720 | # define fdatasync fsync |
704 | #endif |
721 | #endif |
705 | |
722 | |
706 | #if !HAVE_READAHEAD |
723 | #if !HAVE_READAHEAD |
707 | # define readahead aio_readahead |
724 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
708 | |
725 | |
709 | 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) |
710 | { |
727 | { |
711 | dBUF; |
728 | dBUF; |
712 | |
729 | |
713 | while (count > 0) |
730 | while (count > 0) |
714 | { |
731 | { |
… | |
… | |
719 | count -= len; |
736 | count -= len; |
720 | } |
737 | } |
721 | |
738 | |
722 | errno = 0; |
739 | errno = 0; |
723 | } |
740 | } |
|
|
741 | |
724 | #endif |
742 | #endif |
725 | |
743 | |
726 | #if !HAVE_READDIR_R |
744 | #if !HAVE_READDIR_R |
727 | # define readdir_r aio_readdir_r |
745 | # define readdir_r aio_readdir_r |
728 | |
746 | |
… | |
… | |
773 | { |
791 | { |
774 | off_t sbytes; |
792 | off_t sbytes; |
775 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
793 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
776 | |
794 | |
777 | if (res < 0 && sbytes) |
795 | if (res < 0 && sbytes) |
778 | /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ |
796 | /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */ |
779 | res = sbytes; |
797 | res = sbytes; |
780 | } |
798 | } |
781 | |
799 | |
782 | # elif __hpux |
800 | # elif __hpux |
783 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
801 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
… | |
… | |
863 | int errorno; |
881 | int errorno; |
864 | |
882 | |
865 | LOCK (wrklock); |
883 | LOCK (wrklock); |
866 | self->dirp = dirp = opendir (req->dataptr); |
884 | self->dirp = dirp = opendir (req->dataptr); |
867 | self->dbuf = u = malloc (sizeof (*u)); |
885 | self->dbuf = u = malloc (sizeof (*u)); |
|
|
886 | req->data2ptr = names = malloc (memlen); |
868 | UNLOCK (wrklock); |
887 | UNLOCK (wrklock); |
869 | |
|
|
870 | req->data2ptr = names = malloc (memlen); |
|
|
871 | |
888 | |
872 | if (dirp && u && names) |
889 | if (dirp && u && names) |
873 | for (;;) |
890 | for (;;) |
874 | { |
891 | { |
875 | errno = 0; |
892 | errno = 0; |
… | |
… | |
1096 | create_pipe (); |
1113 | create_pipe (); |
1097 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
1114 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
1098 | } |
1115 | } |
1099 | |
1116 | |
1100 | void |
1117 | void |
1101 | min_parallel (nthreads) |
1118 | min_parallel (int nthreads) |
1102 | int nthreads |
|
|
1103 | PROTOTYPE: $ |
1119 | PROTOTYPE: $ |
1104 | |
1120 | |
1105 | void |
1121 | void |
1106 | max_parallel (nthreads) |
1122 | max_parallel (int nthreads) |
1107 | int nthreads |
|
|
1108 | PROTOTYPE: $ |
1123 | PROTOTYPE: $ |
|
|
1124 | |
|
|
1125 | int |
|
|
1126 | max_outstanding (int maxreqs) |
|
|
1127 | PROTOTYPE: $ |
|
|
1128 | CODE: |
|
|
1129 | RETVAL = max_outstanding; |
|
|
1130 | max_outstanding = maxreqs; |
|
|
1131 | OUTPUT: |
|
|
1132 | RETVAL |
1109 | |
1133 | |
1110 | void |
1134 | void |
1111 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
1135 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
1112 | SV * pathname |
1136 | SV * pathname |
1113 | int flags |
1137 | int flags |
… | |
… | |
1390 | PROTOTYPE: |
1414 | PROTOTYPE: |
1391 | CODE: |
1415 | CODE: |
1392 | while (nreqs) |
1416 | while (nreqs) |
1393 | { |
1417 | { |
1394 | poll_wait (); |
1418 | poll_wait (); |
1395 | poll_cb (); |
1419 | poll_cb (0); |
1396 | } |
1420 | } |
1397 | |
1421 | |
1398 | void |
1422 | void |
1399 | poll() |
1423 | poll() |
1400 | PROTOTYPE: |
1424 | PROTOTYPE: |
1401 | CODE: |
1425 | CODE: |
1402 | if (nreqs) |
1426 | if (nreqs) |
1403 | { |
1427 | { |
1404 | poll_wait (); |
1428 | poll_wait (); |
1405 | poll_cb (); |
1429 | poll_cb (0); |
1406 | } |
1430 | } |
1407 | |
1431 | |
1408 | int |
1432 | int |
1409 | poll_fileno() |
1433 | poll_fileno() |
1410 | PROTOTYPE: |
1434 | PROTOTYPE: |
… | |
… | |
1415 | |
1439 | |
1416 | int |
1440 | int |
1417 | poll_cb(...) |
1441 | poll_cb(...) |
1418 | PROTOTYPE: |
1442 | PROTOTYPE: |
1419 | CODE: |
1443 | CODE: |
1420 | 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); |
1421 | OUTPUT: |
1453 | OUTPUT: |
1422 | RETVAL |
1454 | RETVAL |
1423 | |
1455 | |
1424 | void |
1456 | void |
1425 | poll_wait() |
1457 | poll_wait() |