… | |
… | |
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 |
… | |
… | |
150 | enum { |
152 | enum { |
151 | REQ_QUIT, |
153 | REQ_QUIT, |
152 | REQ_ENV_OPEN, REQ_ENV_CLOSE, REQ_ENV_TXN_CHECKPOINT, REQ_ENV_LOCK_DETECT, |
154 | REQ_ENV_OPEN, REQ_ENV_CLOSE, REQ_ENV_TXN_CHECKPOINT, REQ_ENV_LOCK_DETECT, |
153 | REQ_ENV_MEMP_SYNC, REQ_ENV_MEMP_TRICKLE, REQ_ENV_DBREMOVE, REQ_ENV_DBRENAME, |
155 | REQ_ENV_MEMP_SYNC, REQ_ENV_MEMP_TRICKLE, REQ_ENV_DBREMOVE, REQ_ENV_DBRENAME, |
154 | REQ_ENV_LOG_ARCHIVE, |
156 | REQ_ENV_LOG_ARCHIVE, |
155 | REQ_DB_OPEN, REQ_DB_CLOSE, REQ_DB_COMPACT, REQ_DB_SYNC, REQ_DB_UPGRADE, |
157 | REQ_DB_OPEN, REQ_DB_CLOSE, REQ_DB_COMPACT, REQ_DB_SYNC, REQ_DB_VERIFY, REQ_DB_UPGRADE, |
156 | REQ_DB_PUT, REQ_DB_EXISTS, REQ_DB_GET, REQ_DB_PGET, REQ_DB_DEL, REQ_DB_KEY_RANGE, |
158 | REQ_DB_PUT, REQ_DB_EXISTS, REQ_DB_GET, REQ_DB_PGET, REQ_DB_DEL, REQ_DB_KEY_RANGE, |
157 | REQ_TXN_COMMIT, REQ_TXN_ABORT, REQ_TXN_FINISH, |
159 | REQ_TXN_COMMIT, REQ_TXN_ABORT, REQ_TXN_FINISH, |
158 | REQ_C_CLOSE, REQ_C_COUNT, REQ_C_PUT, REQ_C_GET, REQ_C_PGET, REQ_C_DEL, |
160 | REQ_C_CLOSE, REQ_C_COUNT, REQ_C_PUT, REQ_C_GET, REQ_C_PGET, REQ_C_DEL, |
159 | REQ_SEQ_OPEN, REQ_SEQ_CLOSE, REQ_SEQ_GET, REQ_SEQ_REMOVE, |
161 | REQ_SEQ_OPEN, REQ_SEQ_CLOSE, REQ_SEQ_GET, REQ_SEQ_REMOVE, |
160 | }; |
162 | }; |
… | |
… | |
245 | } |
247 | } |
246 | |
248 | |
247 | static volatile unsigned int nreqs, nready, npending; |
249 | static volatile unsigned int nreqs, nready, npending; |
248 | static volatile unsigned int max_idle = 4; |
250 | static volatile unsigned int max_idle = 4; |
249 | static volatile unsigned int max_outstanding = 0xffffffff; |
251 | static volatile unsigned int max_outstanding = 0xffffffff; |
250 | static int respipe_osf [2], respipe [2] = { -1, -1 }; |
252 | static s_epipe respipe; |
251 | |
253 | |
252 | static mutex_t reslock = X_MUTEX_INIT; |
254 | static mutex_t reslock = X_MUTEX_INIT; |
253 | static mutex_t reqlock = X_MUTEX_INIT; |
255 | static mutex_t reqlock = X_MUTEX_INIT; |
254 | static cond_t reqwait = X_COND_INIT; |
256 | static cond_t reqwait = X_COND_INIT; |
255 | |
257 | |
… | |
… | |
456 | free (req->buf3); |
458 | free (req->buf3); |
457 | |
459 | |
458 | Safefree (req); |
460 | Safefree (req); |
459 | } |
461 | } |
460 | |
462 | |
461 | #ifdef USE_SOCKETS_AS_HANDLES |
|
|
462 | # define TO_SOCKET(x) (win32_get_osfhandle (x)) |
|
|
463 | #else |
|
|
464 | # define TO_SOCKET(x) (x) |
|
|
465 | #endif |
|
|
466 | |
|
|
467 | static void |
463 | static void |
468 | create_respipe (void) |
464 | create_respipe (void) |
469 | { |
465 | { |
470 | #ifdef _WIN32 |
|
|
471 | int arg; /* argg */ |
|
|
472 | #endif |
|
|
473 | int old_readfd = respipe [0]; |
|
|
474 | |
|
|
475 | if (respipe [1] >= 0) |
|
|
476 | respipe_close (TO_SOCKET (respipe [1])); |
|
|
477 | |
|
|
478 | #ifdef _WIN32 |
|
|
479 | if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe)) |
|
|
480 | #else |
|
|
481 | if (pipe (respipe)) |
466 | if (s_epipe_renew (&respipe)) |
482 | #endif |
467 | croak ("BDB: unable to create event pipe"); |
483 | croak ("unable to initialize result pipe"); |
|
|
484 | |
|
|
485 | if (old_readfd >= 0) |
|
|
486 | { |
|
|
487 | if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0) |
|
|
488 | croak ("unable to initialize result pipe(2)"); |
|
|
489 | |
|
|
490 | respipe_close (respipe [0]); |
|
|
491 | respipe [0] = old_readfd; |
|
|
492 | } |
|
|
493 | |
|
|
494 | #ifdef _WIN32 |
|
|
495 | arg = 1; |
|
|
496 | if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg) |
|
|
497 | || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg)) |
|
|
498 | #else |
|
|
499 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK) |
|
|
500 | || fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
501 | #endif |
|
|
502 | croak ("unable to initialize result pipe(3)"); |
|
|
503 | |
|
|
504 | respipe_osf [0] = TO_SOCKET (respipe [0]); |
|
|
505 | respipe_osf [1] = TO_SOCKET (respipe [1]); |
|
|
506 | } |
468 | } |
507 | |
469 | |
508 | static void bdb_request (bdb_req req); |
470 | static void bdb_request (bdb_req req); |
509 | X_THREAD_PROC (bdb_proc); |
471 | X_THREAD_PROC (bdb_proc); |
510 | |
472 | |
… | |
… | |
660 | if (size) |
622 | if (size) |
661 | return; |
623 | return; |
662 | |
624 | |
663 | maybe_start_thread (); |
625 | maybe_start_thread (); |
664 | |
626 | |
665 | FD_ZERO (&rfd); |
627 | s_epipe_wait (&respipe); |
666 | FD_SET (respipe [0], &rfd); |
|
|
667 | |
|
|
668 | PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
669 | } |
628 | } |
670 | } |
629 | } |
671 | |
630 | |
672 | static int poll_cb (void) |
631 | static int poll_cb (void) |
673 | { |
632 | { |
… | |
… | |
693 | if (req) |
652 | if (req) |
694 | { |
653 | { |
695 | --npending; |
654 | --npending; |
696 | |
655 | |
697 | if (!res_queue.size) |
656 | if (!res_queue.size) |
698 | { |
|
|
699 | /* read any signals sent by the worker threads */ |
657 | /* read any signals sent by the worker threads */ |
700 | char buf [4]; |
658 | s_epipe_drain (&respipe); |
701 | while (respipe_read (respipe [0], buf, 4) == 4) |
|
|
702 | ; |
|
|
703 | } |
|
|
704 | } |
659 | } |
705 | |
660 | |
706 | X_UNLOCK (reslock); |
661 | X_UNLOCK (reslock); |
707 | |
662 | |
708 | if (!req) |
663 | if (!req) |
… | |
… | |
796 | break; |
751 | break; |
797 | #endif |
752 | #endif |
798 | |
753 | |
799 | case REQ_DB_SYNC: |
754 | case REQ_DB_SYNC: |
800 | req->result = req->db->sync (req->db, req->uint1); |
755 | req->result = req->db->sync (req->db, req->uint1); |
|
|
756 | break; |
|
|
757 | |
|
|
758 | case REQ_DB_VERIFY: |
|
|
759 | req->result = req->db->verify (req->db, req->buf1, req->buf2, 0, req->uint1); |
801 | break; |
760 | break; |
802 | |
761 | |
803 | case REQ_DB_UPGRADE: |
762 | case REQ_DB_UPGRADE: |
804 | req->result = req->db->upgrade (req->db, req->buf1, req->uint1); |
763 | req->result = req->db->upgrade (req->db, req->buf1, req->uint1); |
805 | break; |
764 | break; |
… | |
… | |
920 | /* try to distribute timeouts somewhat evenly */ |
879 | /* try to distribute timeouts somewhat evenly */ |
921 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
880 | ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
922 | |
881 | |
923 | for (;;) |
882 | for (;;) |
924 | { |
883 | { |
925 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
884 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
926 | |
885 | |
927 | X_LOCK (reqlock); |
886 | X_LOCK (reqlock); |
928 | |
887 | |
929 | for (;;) |
888 | for (;;) |
930 | { |
889 | { |
… | |
… | |
975 | X_LOCK (reslock); |
934 | X_LOCK (reslock); |
976 | |
935 | |
977 | ++npending; |
936 | ++npending; |
978 | |
937 | |
979 | if (!reqq_push (&res_queue, req)) |
938 | if (!reqq_push (&res_queue, req)) |
980 | /* write a dummy byte to the pipe so fh becomes ready */ |
939 | s_epipe_signal (&respipe); |
981 | respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1); |
|
|
982 | |
940 | |
983 | self->req = 0; |
941 | self->req = 0; |
984 | worker_clear (self); |
942 | worker_clear (self); |
985 | |
943 | |
986 | X_UNLOCK (reslock); |
944 | X_UNLOCK (reslock); |
… | |
… | |
1162 | } |
1120 | } |
1163 | } |
1121 | } |
1164 | |
1122 | |
1165 | return 0; |
1123 | return 0; |
1166 | } |
1124 | } |
|
|
1125 | |
|
|
1126 | /*****************************************************************************/ |
|
|
1127 | |
|
|
1128 | #if 0 |
|
|
1129 | static int |
|
|
1130 | bt_pfxc_compare (DB *db, const DBT *dbt1, const DBT *dbt2) |
|
|
1131 | { |
|
|
1132 | ssize_t size1 = dbt1->size; |
|
|
1133 | ssize_t size2 = dbt2->size; |
|
|
1134 | int res = memcmp ((void *)dbt1->data, (void *)dbt2->data, |
|
|
1135 | size1 <= size2 ? size1 : size2); |
|
|
1136 | |
|
|
1137 | if (res) |
|
|
1138 | return res; |
|
|
1139 | else if (size1 - size2) |
|
|
1140 | return size1 - size2; |
|
|
1141 | else |
|
|
1142 | return 0; |
|
|
1143 | } |
|
|
1144 | |
|
|
1145 | static size_t |
|
|
1146 | bt_pfxc_prefix_x (DB *db, const DBT *dbt1, const DBT *dbt2) |
|
|
1147 | { |
|
|
1148 | ssize_t size1 = dbt1->size; |
|
|
1149 | ssize_t size2 = dbt2->size; |
|
|
1150 | u_int8_t *p1 = (u_int8_t *)dbt1->data; |
|
|
1151 | u_int8_t *p2 = (u_int8_t *)dbt2->data; |
|
|
1152 | u_int8_t *pe = p1 + (size1 <= size2 ? size1 : size2); |
|
|
1153 | |
|
|
1154 | while (p1 < pe) |
|
|
1155 | if (*p1++ != *p2++) |
|
|
1156 | return p1 - (u_int8_t *)dbt1->data - 1; |
|
|
1157 | |
|
|
1158 | if (size1 < size2) return size1 + 1; |
|
|
1159 | if (size1 > size2) return size2 + 1; |
|
|
1160 | |
|
|
1161 | return size1; |
|
|
1162 | } |
|
|
1163 | #endif |
|
|
1164 | |
|
|
1165 | /*****************************************************************************/ |
1167 | |
1166 | |
1168 | /* stupid windows defines CALLBACK as well */ |
1167 | /* stupid windows defines CALLBACK as well */ |
1169 | #undef CALLBACK |
1168 | #undef CALLBACK |
1170 | #define CALLBACK SV *cb = pop_callback (&items, ST (items - 1)); |
1169 | #define CALLBACK SV *cb = pop_callback (&items, ST (items - 1)); |
1171 | |
1170 | |
… | |
… | |
1308 | const_iv (REP_UNAVAIL) |
1307 | const_iv (REP_UNAVAIL) |
1309 | const_iv (RUNRECOVERY) |
1308 | const_iv (RUNRECOVERY) |
1310 | const_iv (SECONDARY_BAD) |
1309 | const_iv (SECONDARY_BAD) |
1311 | const_iv (VERIFY_BAD) |
1310 | const_iv (VERIFY_BAD) |
1312 | |
1311 | |
|
|
1312 | const_iv (SALVAGE) |
|
|
1313 | const_iv (AGGRESSIVE) |
|
|
1314 | const_iv (PRINTABLE) |
|
|
1315 | const_iv (NOORDERCHK) |
|
|
1316 | const_iv (ORDERCHKONLY) |
|
|
1317 | |
1313 | const_iv (ARCH_ABS) |
1318 | const_iv (ARCH_ABS) |
1314 | const_iv (ARCH_DATA) |
1319 | const_iv (ARCH_DATA) |
1315 | const_iv (ARCH_LOG) |
1320 | const_iv (ARCH_LOG) |
1316 | const_iv (ARCH_REMOVE) |
1321 | const_iv (ARCH_REMOVE) |
1317 | |
1322 | |
… | |
… | |
1474 | |
1479 | |
1475 | int |
1480 | int |
1476 | poll_fileno () |
1481 | poll_fileno () |
1477 | PROTOTYPE: |
1482 | PROTOTYPE: |
1478 | CODE: |
1483 | CODE: |
1479 | RETVAL = respipe [0]; |
1484 | RETVAL = s_epipe_fd (&respipe); |
1480 | OUTPUT: |
1485 | OUTPUT: |
1481 | RETVAL |
1486 | RETVAL |
1482 | |
1487 | |
1483 | int |
1488 | int |
1484 | poll_cb (...) |
1489 | poll_cb (...) |
… | |
… | |
1762 | req->uint1 = flags; |
1767 | req->uint1 = flags; |
1763 | REQ_SEND; |
1768 | REQ_SEND; |
1764 | } |
1769 | } |
1765 | |
1770 | |
1766 | void |
1771 | void |
|
|
1772 | db_verify (DB *db, bdb_filename file, bdb_filename database = 0, SV *dummy = 0, U32 flags = 0, SV *callback = 0) |
|
|
1773 | PREINIT: |
|
|
1774 | CALLBACK |
|
|
1775 | CODE: |
|
|
1776 | { |
|
|
1777 | dREQ (REQ_DB_VERIFY, 1); |
|
|
1778 | ptr_nuke (ST (0)); /* verify destroys the database handle, hopefully it is freed as well */ |
|
|
1779 | req->db = db; |
|
|
1780 | req->buf1 = strdup (file); |
|
|
1781 | req->buf2 = strdup_ornull (database); |
|
|
1782 | req->uint1 = flags; |
|
|
1783 | REQ_SEND; |
|
|
1784 | } |
|
|
1785 | |
|
|
1786 | void |
1767 | db_upgrade (DB *db, bdb_filename file, U32 flags = 0, SV *callback = 0) |
1787 | db_upgrade (DB *db, bdb_filename file, U32 flags = 0, SV *callback = 0) |
1768 | PREINIT: |
1788 | PREINIT: |
1769 | CALLBACK |
1789 | CALLBACK |
1770 | CODE: |
1790 | CODE: |
1771 | { |
1791 | { |
1772 | dREQ (REQ_DB_SYNC, 1); |
1792 | dREQ (REQ_DB_UPGRADE, 1); |
1773 | req->db = db; |
1793 | req->db = db; |
1774 | req->buf1 = strdup (file); |
1794 | req->buf1 = strdup (file); |
1775 | req->uint1 = flags; |
1795 | req->uint1 = flags; |
1776 | REQ_SEND; |
1796 | REQ_SEND; |
1777 | } |
1797 | } |