… | |
… | |
6 | |
6 | |
7 | #include "EXTERN.h" |
7 | #include "EXTERN.h" |
8 | #include "perl.h" |
8 | #include "perl.h" |
9 | #include "XSUB.h" |
9 | #include "XSUB.h" |
10 | |
10 | |
|
|
11 | #include "schmorp.h" |
|
|
12 | |
11 | // perl stupidly defines these as macros, breaking |
13 | // perl stupidly defines these as argument-less macros, breaking |
12 | // lots and lots of code. |
14 | // lots and lots of code. |
13 | #undef open |
15 | #undef open |
14 | #undef close |
16 | #undef close |
15 | #undef abort |
17 | #undef abort |
16 | #undef malloc |
18 | #undef malloc |
… | |
… | |
56 | #endif |
58 | #endif |
57 | |
59 | |
58 | typedef char *bdb_filename; |
60 | typedef char *bdb_filename; |
59 | |
61 | |
60 | static SV *prepare_cb; |
62 | static SV *prepare_cb; |
|
|
63 | |
|
|
64 | static HV |
|
|
65 | *bdb_stash, |
|
|
66 | *bdb_env_stash, |
|
|
67 | *bdb_txn_stash, |
|
|
68 | *bdb_cursor_stash, |
|
|
69 | *bdb_db_stash, |
|
|
70 | *bdb_sequence_stash; |
61 | |
71 | |
62 | #if DB_VERSION_MINOR >= 6 |
72 | #if DB_VERSION_MINOR >= 6 |
63 | # define c_close close |
73 | # define c_close close |
64 | # define c_count count |
74 | # define c_count count |
65 | # define c_del del |
75 | # define c_del del |
… | |
… | |
245 | } |
255 | } |
246 | |
256 | |
247 | static volatile unsigned int nreqs, nready, npending; |
257 | static volatile unsigned int nreqs, nready, npending; |
248 | static volatile unsigned int max_idle = 4; |
258 | static volatile unsigned int max_idle = 4; |
249 | static volatile unsigned int max_outstanding = 0xffffffff; |
259 | static volatile unsigned int max_outstanding = 0xffffffff; |
250 | static int respipe_osf [2], respipe [2] = { -1, -1 }; |
260 | static s_epipe respipe; |
251 | |
261 | |
252 | static mutex_t reslock = X_MUTEX_INIT; |
262 | static mutex_t reslock = X_MUTEX_INIT; |
253 | static mutex_t reqlock = X_MUTEX_INIT; |
263 | static mutex_t reqlock = X_MUTEX_INIT; |
254 | static cond_t reqwait = X_COND_INIT; |
264 | static cond_t reqwait = X_COND_INIT; |
255 | |
265 | |
… | |
… | |
456 | free (req->buf3); |
466 | free (req->buf3); |
457 | |
467 | |
458 | Safefree (req); |
468 | Safefree (req); |
459 | } |
469 | } |
460 | |
470 | |
461 | #ifdef USE_SOCKETS_AS_HANDLES |
|
|
462 | # define TO_SOCKET(x) (win32_get_osfhandle (x)) |
|
|
463 | #else |
|
|
464 | # define EV_SELECT_IS_WINSOCKET 1 |
|
|
465 | # define TO_SOCKET(x) (x) |
|
|
466 | #endif |
|
|
467 | |
|
|
468 | #ifdef _WIN32 |
|
|
469 | /* taken verbatim from libev's ev_win32.c */ |
|
|
470 | /* oh, the humanity! */ |
|
|
471 | static int |
|
|
472 | ev_pipe (int filedes [2]) |
|
|
473 | { |
|
|
474 | struct sockaddr_in addr = { 0 }; |
|
|
475 | int addr_size = sizeof (addr); |
|
|
476 | struct sockaddr_in adr2; |
|
|
477 | int adr2_size; |
|
|
478 | SOCKET listener; |
|
|
479 | SOCKET sock [2] = { -1, -1 }; |
|
|
480 | |
|
|
481 | if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
|
|
482 | return -1; |
|
|
483 | |
|
|
484 | addr.sin_family = AF_INET; |
|
|
485 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
|
|
486 | addr.sin_port = 0; |
|
|
487 | |
|
|
488 | if (bind (listener, (struct sockaddr *)&addr, addr_size)) |
|
|
489 | goto fail; |
|
|
490 | |
|
|
491 | if (getsockname (listener, (struct sockaddr *)&addr, &addr_size)) |
|
|
492 | goto fail; |
|
|
493 | |
|
|
494 | if (listen (listener, 1)) |
|
|
495 | goto fail; |
|
|
496 | |
|
|
497 | if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
|
|
498 | goto fail; |
|
|
499 | |
|
|
500 | if (connect (sock [0], (struct sockaddr *)&addr, addr_size)) |
|
|
501 | goto fail; |
|
|
502 | |
|
|
503 | if ((sock [1] = accept (listener, 0, 0)) < 0) |
|
|
504 | goto fail; |
|
|
505 | |
|
|
506 | /* windows vista returns fantasy port numbers for getpeername. |
|
|
507 | * example for two interconnected tcp sockets: |
|
|
508 | * |
|
|
509 | * (Socket::unpack_sockaddr_in getsockname $sock0)[0] == 53364 |
|
|
510 | * (Socket::unpack_sockaddr_in getpeername $sock0)[0] == 53363 |
|
|
511 | * (Socket::unpack_sockaddr_in getsockname $sock1)[0] == 53363 |
|
|
512 | * (Socket::unpack_sockaddr_in getpeername $sock1)[0] == 53365 |
|
|
513 | * |
|
|
514 | * wow! tridirectional sockets! |
|
|
515 | * |
|
|
516 | * this way of checking ports seems to work: |
|
|
517 | */ |
|
|
518 | if (getpeername (sock [0], (struct sockaddr *)&addr, &addr_size)) |
|
|
519 | goto fail; |
|
|
520 | |
|
|
521 | if (getsockname (sock [1], (struct sockaddr *)&adr2, &adr2_size)) |
|
|
522 | goto fail; |
|
|
523 | |
|
|
524 | errno = WSAEINVAL; |
|
|
525 | if (addr_size != adr2_size |
|
|
526 | || addr.sin_addr.s_addr != adr2.sin_addr.s_addr /* just to be sure, I mean, it's windows */ |
|
|
527 | || addr.sin_port != adr2.sin_port) |
|
|
528 | goto fail; |
|
|
529 | |
|
|
530 | closesocket (listener); |
|
|
531 | |
|
|
532 | #if EV_SELECT_IS_WINSOCKET |
|
|
533 | filedes [0] = _open_osfhandle (sock [0], 0); |
|
|
534 | filedes [1] = _open_osfhandle (sock [1], 0); |
|
|
535 | #else |
|
|
536 | /* when select isn't winsocket, we also expect socket, connect, accept etc. |
|
|
537 | * to work on fds */ |
|
|
538 | filedes [0] = sock [0]; |
|
|
539 | filedes [1] = sock [1]; |
|
|
540 | #endif |
|
|
541 | |
|
|
542 | return 0; |
|
|
543 | |
|
|
544 | fail: |
|
|
545 | closesocket (listener); |
|
|
546 | |
|
|
547 | if (sock [0] != INVALID_SOCKET) closesocket (sock [0]); |
|
|
548 | if (sock [1] != INVALID_SOCKET) closesocket (sock [1]); |
|
|
549 | |
|
|
550 | return -1; |
|
|
551 | } |
|
|
552 | |
|
|
553 | #define pipe(filedes) ev_pipe(filedes) |
|
|
554 | #endif |
|
|
555 | |
|
|
556 | static void |
471 | static void |
557 | create_respipe (void) |
472 | create_respipe (void) |
558 | { |
473 | { |
559 | #ifdef _WIN32 |
|
|
560 | int arg; /* argg */ |
|
|
561 | #endif |
|
|
562 | int old_readfd = respipe [0]; |
|
|
563 | |
|
|
564 | if (respipe [1] >= 0) |
|
|
565 | respipe_close (TO_SOCKET (respipe [1])); |
|
|
566 | |
|
|
567 | if (pipe (respipe)) |
474 | if (s_epipe_renew (&respipe)) |
568 | croak ("unable to initialize result pipe"); |
475 | croak ("BDB: unable to create event pipe"); |
569 | |
|
|
570 | if (old_readfd >= 0) |
|
|
571 | { |
|
|
572 | if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0) |
|
|
573 | croak ("unable to initialize result pipe(2)"); |
|
|
574 | |
|
|
575 | respipe_close (respipe [0]); |
|
|
576 | respipe [0] = old_readfd; |
|
|
577 | } |
|
|
578 | |
|
|
579 | #ifdef _WIN32 |
|
|
580 | arg = 1; |
|
|
581 | if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg) |
|
|
582 | || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg)) |
|
|
583 | #else |
|
|
584 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK) |
|
|
585 | || fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
586 | #endif |
|
|
587 | croak ("unable to initialize result pipe(3)"); |
|
|
588 | |
|
|
589 | respipe_osf [0] = TO_SOCKET (respipe [0]); |
|
|
590 | respipe_osf [1] = TO_SOCKET (respipe [1]); |
|
|
591 | } |
476 | } |
592 | |
477 | |
593 | static void bdb_request (bdb_req req); |
478 | static void bdb_request (bdb_req req); |
594 | X_THREAD_PROC (bdb_proc); |
479 | X_THREAD_PROC (bdb_proc); |
595 | |
480 | |
… | |
… | |
731 | end_thread (); |
616 | end_thread (); |
732 | } |
617 | } |
733 | |
618 | |
734 | static void poll_wait (void) |
619 | static void poll_wait (void) |
735 | { |
620 | { |
736 | fd_set rfd; |
|
|
737 | |
|
|
738 | while (nreqs) |
621 | while (nreqs) |
739 | { |
622 | { |
740 | int size; |
623 | int size; |
741 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
624 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
742 | size = res_queue.size; |
625 | size = res_queue.size; |
… | |
… | |
745 | if (size) |
628 | if (size) |
746 | return; |
629 | return; |
747 | |
630 | |
748 | maybe_start_thread (); |
631 | maybe_start_thread (); |
749 | |
632 | |
750 | FD_ZERO (&rfd); |
633 | s_epipe_wait (&respipe); |
751 | FD_SET (respipe [0], &rfd); |
|
|
752 | |
|
|
753 | PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
754 | } |
634 | } |
755 | } |
635 | } |
756 | |
636 | |
757 | static int poll_cb (void) |
637 | static int poll_cb (void) |
758 | { |
638 | { |
… | |
… | |
778 | if (req) |
658 | if (req) |
779 | { |
659 | { |
780 | --npending; |
660 | --npending; |
781 | |
661 | |
782 | if (!res_queue.size) |
662 | if (!res_queue.size) |
783 | { |
|
|
784 | /* read any signals sent by the worker threads */ |
663 | /* read any signals sent by the worker threads */ |
785 | char buf [4]; |
664 | s_epipe_drain (&respipe); |
786 | while (respipe_read (respipe [0], buf, 4) == 4) |
|
|
787 | ; |
|
|
788 | } |
|
|
789 | } |
665 | } |
790 | |
666 | |
791 | X_UNLOCK (reslock); |
667 | X_UNLOCK (reslock); |
792 | |
668 | |
793 | if (!req) |
669 | if (!req) |
… | |
… | |
1009 | /* try to distribute timeouts somewhat evenly */ |
885 | /* try to distribute timeouts somewhat evenly */ |
1010 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
886 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
1011 | |
887 | |
1012 | for (;;) |
888 | for (;;) |
1013 | { |
889 | { |
1014 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
890 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
1015 | |
891 | |
1016 | X_LOCK (reqlock); |
892 | X_LOCK (reqlock); |
1017 | |
893 | |
1018 | for (;;) |
894 | for (;;) |
1019 | { |
895 | { |
… | |
… | |
1064 | X_LOCK (reslock); |
940 | X_LOCK (reslock); |
1065 | |
941 | |
1066 | ++npending; |
942 | ++npending; |
1067 | |
943 | |
1068 | if (!reqq_push (&res_queue, req)) |
944 | if (!reqq_push (&res_queue, req)) |
1069 | /* write a dummy byte to the pipe so fh becomes ready */ |
945 | s_epipe_signal (&respipe); |
1070 | respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1); |
|
|
1071 | |
946 | |
1072 | self->req = 0; |
947 | self->req = 0; |
1073 | worker_clear (self); |
948 | worker_clear (self); |
1074 | |
949 | |
1075 | X_UNLOCK (reslock); |
950 | X_UNLOCK (reslock); |
… | |
… | |
1151 | (void)0; |
1026 | (void)0; |
1152 | |
1027 | |
1153 | #define REQ_SEND \ |
1028 | #define REQ_SEND \ |
1154 | req_send (req) |
1029 | req_send (req) |
1155 | |
1030 | |
1156 | #define SvPTR(var, arg, type, class, nullok) \ |
1031 | #define SvPTR(var, arg, type, stash, class, nullok) \ |
1157 | if (!SvOK (arg)) \ |
1032 | if (!SvOK (arg)) \ |
1158 | { \ |
1033 | { \ |
1159 | if (nullok != 1) \ |
1034 | if (nullok != 1) \ |
1160 | croak (# var " must be a " # class " object, not undef"); \ |
1035 | croak (# var " must be a " # class " object, not undef"); \ |
1161 | \ |
1036 | \ |
1162 | (var) = 0; \ |
1037 | (var) = 0; \ |
1163 | } \ |
1038 | } \ |
1164 | else if (sv_derived_from ((arg), # class)) \ |
1039 | else if (SvSTASH (SvRV (arg)) == stash || sv_derived_from ((arg), # class)) \ |
1165 | { \ |
1040 | { \ |
1166 | IV tmp = SvIV ((SV*) SvRV (arg)); \ |
1041 | IV tmp = SvIV ((SV*) SvRV (arg)); \ |
1167 | (var) = INT2PTR (type, tmp); \ |
1042 | (var) = INT2PTR (type, tmp); \ |
1168 | if (!var && nullok != 2) \ |
1043 | if (!var && nullok != 2) \ |
1169 | croak (# var " is not a valid " # class " object anymore"); \ |
1044 | croak (# var " is not a valid " # class " object anymore"); \ |
… | |
… | |
1172 | croak (# var " is not of type " # class); |
1047 | croak (# var " is not of type " # class); |
1173 | |
1048 | |
1174 | #define ARG_MUTABLE(name) \ |
1049 | #define ARG_MUTABLE(name) \ |
1175 | if (SvREADONLY (name)) \ |
1050 | if (SvREADONLY (name)) \ |
1176 | croak ("argument " #name " is read-only/constant, but the request requires it to be mutable"); |
1051 | croak ("argument " #name " is read-only/constant, but the request requires it to be mutable"); |
|
|
1052 | |
|
|
1053 | static SV * |
|
|
1054 | newSVptr (void *ptr, HV *stash) |
|
|
1055 | { |
|
|
1056 | SV *rv = NEWSV (0, 0); |
|
|
1057 | sv_upgrade (rv, SVt_PVMG); |
|
|
1058 | sv_setiv (rv, PTR2IV (ptr)); |
|
|
1059 | |
|
|
1060 | return sv_bless (newRV_noinc (rv), stash); |
|
|
1061 | } |
1177 | |
1062 | |
1178 | static void |
1063 | static void |
1179 | ptr_nuke (SV *sv) |
1064 | ptr_nuke (SV *sv) |
1180 | { |
1065 | { |
1181 | assert (SvROK (sv)); |
1066 | assert (SvROK (sv)); |
… | |
… | |
1303 | |
1188 | |
1304 | PROTOTYPES: ENABLE |
1189 | PROTOTYPES: ENABLE |
1305 | |
1190 | |
1306 | BOOT: |
1191 | BOOT: |
1307 | { |
1192 | { |
1308 | HV *stash = gv_stashpv ("BDB", 1); |
|
|
1309 | |
|
|
1310 | static const struct { |
1193 | static const struct { |
1311 | const char *name; |
1194 | const char *name; |
1312 | IV iv; |
1195 | IV iv; |
1313 | } *civ, const_iv[] = { |
1196 | } *civ, const_iv[] = { |
1314 | #define const_iv(name) { # name, (IV)DB_ ## name }, |
1197 | #define const_iv(name) { # name, (IV)DB_ ## name }, |
… | |
… | |
1510 | const_iv (LOG_INMEMORY) |
1393 | const_iv (LOG_INMEMORY) |
1511 | # endif |
1394 | # endif |
1512 | #endif |
1395 | #endif |
1513 | }; |
1396 | }; |
1514 | |
1397 | |
|
|
1398 | bdb_stash = gv_stashpv ("BDB" , 1); |
|
|
1399 | bdb_env_stash = gv_stashpv ("BDB::Env" , 1); |
|
|
1400 | bdb_txn_stash = gv_stashpv ("BDB::Txn" , 1); |
|
|
1401 | bdb_cursor_stash = gv_stashpv ("BDB::Cursor" , 1); |
|
|
1402 | bdb_db_stash = gv_stashpv ("BDB::Db" , 1); |
|
|
1403 | bdb_sequence_stash = gv_stashpv ("BDB::Sequence", 1); |
|
|
1404 | |
1515 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
1405 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
1516 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
1406 | newCONSTSUB (bdb_stash, (char *)civ->name, newSViv (civ->iv)); |
1517 | |
1407 | |
1518 | prepare_cb = &PL_sv_undef; |
1408 | prepare_cb = &PL_sv_undef; |
1519 | |
1409 | |
1520 | { |
1410 | { |
1521 | /* we currently only allow version, minor-version and patchlevel to go up to 255 */ |
1411 | /* we currently only allow version, minor-version and patchlevel to go up to 255 */ |
1522 | char vstring[3] = { DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH }; |
1412 | char vstring[3] = { DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH }; |
1523 | |
1413 | |
1524 | newCONSTSUB (stash, "VERSION_v", newSVpvn (vstring, 3)); |
1414 | newCONSTSUB (bdb_stash, "VERSION_v", newSVpvn (vstring, 3)); |
1525 | } |
1415 | } |
1526 | |
1416 | |
1527 | newCONSTSUB (stash, "VERSION_STRING", newSVpv (DB_VERSION_STRING, 0)); |
1417 | newCONSTSUB (bdb_stash, "VERSION_STRING", newSVpv (DB_VERSION_STRING, 0)); |
1528 | |
1418 | |
1529 | create_respipe (); |
1419 | create_respipe (); |
1530 | |
1420 | |
1531 | X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child); |
1421 | X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child); |
1532 | patch_errno (); |
1422 | patch_errno (); |
… | |
… | |
1610 | |
1500 | |
1611 | int |
1501 | int |
1612 | poll_fileno () |
1502 | poll_fileno () |
1613 | PROTOTYPE: |
1503 | PROTOTYPE: |
1614 | CODE: |
1504 | CODE: |
1615 | RETVAL = respipe [0]; |
1505 | RETVAL = s_epipe_fd (&respipe); |
1616 | OUTPUT: |
1506 | OUTPUT: |
1617 | RETVAL |
1507 | RETVAL |
1618 | |
1508 | |
1619 | int |
1509 | int |
1620 | poll_cb (...) |
1510 | poll_cb (...) |