… | |
… | |
8 | #include "perl.h" |
8 | #include "perl.h" |
9 | #include "XSUB.h" |
9 | #include "XSUB.h" |
10 | |
10 | |
11 | #include "schmorp.h" |
11 | #include "schmorp.h" |
12 | |
12 | |
13 | // perl stupidly defines these as macros, breaking |
13 | // perl stupidly defines these as argument-less macros, breaking |
14 | // lots and lots of code. |
14 | // lots and lots of code. |
15 | #undef open |
15 | #undef open |
16 | #undef close |
16 | #undef close |
17 | #undef abort |
17 | #undef abort |
18 | #undef malloc |
18 | #undef malloc |
… | |
… | |
247 | } |
247 | } |
248 | |
248 | |
249 | static volatile unsigned int nreqs, nready, npending; |
249 | static volatile unsigned int nreqs, nready, npending; |
250 | static volatile unsigned int max_idle = 4; |
250 | static volatile unsigned int max_idle = 4; |
251 | static volatile unsigned int max_outstanding = 0xffffffff; |
251 | static volatile unsigned int max_outstanding = 0xffffffff; |
252 | static int respipe_osf [2], respipe [2] = { -1, -1 }; |
252 | static s_epipe respipe; |
253 | |
253 | |
254 | static mutex_t reslock = X_MUTEX_INIT; |
254 | static mutex_t reslock = X_MUTEX_INIT; |
255 | static mutex_t reqlock = X_MUTEX_INIT; |
255 | static mutex_t reqlock = X_MUTEX_INIT; |
256 | static cond_t reqwait = X_COND_INIT; |
256 | static cond_t reqwait = X_COND_INIT; |
257 | |
257 | |
… | |
… | |
461 | } |
461 | } |
462 | |
462 | |
463 | static void |
463 | static void |
464 | create_respipe (void) |
464 | create_respipe (void) |
465 | { |
465 | { |
466 | #ifdef _WIN32 |
|
|
467 | int arg; /* argg */ |
|
|
468 | #endif |
|
|
469 | int old_readfd = respipe [0]; |
|
|
470 | |
|
|
471 | if (respipe [1] >= 0) |
|
|
472 | respipe_close (S_TO_SOCKET (respipe [1])); |
|
|
473 | |
|
|
474 | if (s_pipe (respipe)) |
466 | if (s_epipe_renew (&respipe)) |
475 | croak ("unable to initialize result pipe"); |
467 | croak ("BDB: unable to create event pipe"); |
476 | |
|
|
477 | if (old_readfd >= 0) |
|
|
478 | { |
|
|
479 | if (dup2 (S_TO_SOCKET (respipe [0]), S_TO_SOCKET (old_readfd)) < 0) |
|
|
480 | croak ("unable to initialize result pipe(2)"); |
|
|
481 | |
|
|
482 | respipe_close (respipe [0]); |
|
|
483 | respipe [0] = old_readfd; |
|
|
484 | } |
|
|
485 | |
|
|
486 | #ifdef _WIN32 |
|
|
487 | arg = 1; |
|
|
488 | if (ioctlsocket (S_TO_SOCKET (respipe [0]), FIONBIO, &arg) |
|
|
489 | || ioctlsocket (S_TO_SOCKET (respipe [1]), FIONBIO, &arg)) |
|
|
490 | #else |
|
|
491 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK) |
|
|
492 | || fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
493 | #endif |
|
|
494 | croak ("unable to initialize result pipe(3)"); |
|
|
495 | |
|
|
496 | respipe_osf [0] = S_TO_SOCKET (respipe [0]); |
|
|
497 | respipe_osf [1] = S_TO_SOCKET (respipe [1]); |
|
|
498 | } |
468 | } |
499 | |
469 | |
500 | static void bdb_request (bdb_req req); |
470 | static void bdb_request (bdb_req req); |
501 | X_THREAD_PROC (bdb_proc); |
471 | X_THREAD_PROC (bdb_proc); |
502 | |
472 | |
… | |
… | |
652 | if (size) |
622 | if (size) |
653 | return; |
623 | return; |
654 | |
624 | |
655 | maybe_start_thread (); |
625 | maybe_start_thread (); |
656 | |
626 | |
657 | FD_ZERO (&rfd); |
627 | s_epipe_wait (&respipe); |
658 | FD_SET (respipe [0], &rfd); |
|
|
659 | |
|
|
660 | PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
661 | } |
628 | } |
662 | } |
629 | } |
663 | |
630 | |
664 | static int poll_cb (void) |
631 | static int poll_cb (void) |
665 | { |
632 | { |
… | |
… | |
685 | if (req) |
652 | if (req) |
686 | { |
653 | { |
687 | --npending; |
654 | --npending; |
688 | |
655 | |
689 | if (!res_queue.size) |
656 | if (!res_queue.size) |
690 | { |
|
|
691 | /* read any signals sent by the worker threads */ |
657 | /* read any signals sent by the worker threads */ |
692 | char buf [4]; |
658 | s_epipe_drain (&respipe); |
693 | while (respipe_read (respipe [0], buf, 4) == 4) |
|
|
694 | ; |
|
|
695 | } |
|
|
696 | } |
659 | } |
697 | |
660 | |
698 | X_UNLOCK (reslock); |
661 | X_UNLOCK (reslock); |
699 | |
662 | |
700 | if (!req) |
663 | if (!req) |
… | |
… | |
916 | /* try to distribute timeouts somewhat evenly */ |
879 | /* try to distribute timeouts somewhat evenly */ |
917 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
880 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
918 | |
881 | |
919 | for (;;) |
882 | for (;;) |
920 | { |
883 | { |
921 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
884 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
922 | |
885 | |
923 | X_LOCK (reqlock); |
886 | X_LOCK (reqlock); |
924 | |
887 | |
925 | for (;;) |
888 | for (;;) |
926 | { |
889 | { |
… | |
… | |
971 | X_LOCK (reslock); |
934 | X_LOCK (reslock); |
972 | |
935 | |
973 | ++npending; |
936 | ++npending; |
974 | |
937 | |
975 | if (!reqq_push (&res_queue, req)) |
938 | if (!reqq_push (&res_queue, req)) |
976 | /* write a dummy byte to the pipe so fh becomes ready */ |
939 | s_epipe_signal (&respipe); |
977 | respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1); |
|
|
978 | |
940 | |
979 | self->req = 0; |
941 | self->req = 0; |
980 | worker_clear (self); |
942 | worker_clear (self); |
981 | |
943 | |
982 | X_UNLOCK (reslock); |
944 | X_UNLOCK (reslock); |
… | |
… | |
1517 | |
1479 | |
1518 | int |
1480 | int |
1519 | poll_fileno () |
1481 | poll_fileno () |
1520 | PROTOTYPE: |
1482 | PROTOTYPE: |
1521 | CODE: |
1483 | CODE: |
1522 | RETVAL = respipe [0]; |
1484 | RETVAL = s_epipe_fd (&respipe); |
1523 | OUTPUT: |
1485 | OUTPUT: |
1524 | RETVAL |
1486 | RETVAL |
1525 | |
1487 | |
1526 | int |
1488 | int |
1527 | poll_cb (...) |
1489 | poll_cb (...) |