ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/BDB/BDB.xs
(Generate patch)

Comparing BDB/BDB.xs (file contents):
Revision 1.12 by root, Sun Jul 8 09:16:19 2007 UTC vs.
Revision 1.15 by root, Mon Aug 13 11:16:22 2007 UTC

4 4
5#include "EXTERN.h" 5#include "EXTERN.h"
6#include "perl.h" 6#include "perl.h"
7#include "XSUB.h" 7#include "XSUB.h"
8 8
9// perl stupidly defines these as macros, breaking
10// lots and lots of code.
11#undef open
12#undef close
13#undef abort
14#undef malloc
15#undef free
16#undef send
17
9#include <stddef.h> 18#include <stddef.h>
10#include <stdlib.h> 19#include <stdlib.h>
11#include <errno.h> 20#include <errno.h>
12#include <sys/time.h>
13#include <sys/types.h> 21#include <sys/types.h>
14#include <limits.h> 22#include <limits.h>
15#include <unistd.h>
16#include <fcntl.h> 23#include <fcntl.h>
17 24
25#ifndef _WIN32
26# include <sys/time.h>
27# include <unistd.h>
28#endif
29
18#include <db.h> 30#include <db.h>
19 31
20#if DB_VERSION_MAJOR < 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 5) 32#if DB_VERSION_MAJOR < 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 4)
21# error you need Berkeley DB 4.5 or newer installed 33# error you need Berkeley DB 4.4 or newer installed
22#endif 34#endif
23 35
24/* number of seconds after which idle threads exit */ 36/* number of seconds after which idle threads exit */
25#define IDLE_TIMEOUT 10 37#define IDLE_TIMEOUT 10
26 38
33typedef SV SV8; /* byte-sv, used for argument-checking */ 45typedef SV SV8; /* byte-sv, used for argument-checking */
34typedef char *octetstring; 46typedef char *octetstring;
35 47
36static SV *prepare_cb; 48static SV *prepare_cb;
37 49
50static void
51debug_errcall (const DB_ENV *dbenv, const char *errpfx, const char *msg)
52{
53 printf ("err[%s]\n", msg);
54}
55
56static void
57debug_msgcall (const DB_ENV *dbenv, const char *msg)
58{
59 printf ("msg[%s]\n", msg);
60}
61
38static inline char * 62static char *
39strdup_ornull (const char *s) 63strdup_ornull (const char *s)
40{ 64{
41 return s ? strdup (s) : 0; 65 return s ? strdup (s) : 0;
42} 66}
43 67
44static inline void 68static void
45sv_to_dbt (DBT *dbt, SV *sv) 69sv_to_dbt (DBT *dbt, SV *sv)
46{ 70{
47 STRLEN len; 71 STRLEN len;
48 char *data = SvPVbyte (sv, len); 72 char *data = SvPVbyte (sv, len);
49 73
51 memcpy (dbt->data, data, len); 75 memcpy (dbt->data, data, len);
52 dbt->size = len; 76 dbt->size = len;
53 dbt->flags = DB_DBT_REALLOC; 77 dbt->flags = DB_DBT_REALLOC;
54} 78}
55 79
56static inline void 80static void
57dbt_to_sv (SV *sv, DBT *dbt) 81dbt_to_sv (SV *sv, DBT *dbt)
58{ 82{
59 if (sv) 83 if (sv)
60 { 84 {
61 SvREADONLY_off (sv); 85 SvREADONLY_off (sv);
157} 181}
158 182
159static volatile unsigned int nreqs, nready, npending; 183static volatile unsigned int nreqs, nready, npending;
160static volatile unsigned int max_idle = 4; 184static volatile unsigned int max_idle = 4;
161static volatile unsigned int max_outstanding = 0xffffffff; 185static volatile unsigned int max_outstanding = 0xffffffff;
162static int respipe [2]; 186static int respipe [2], respipe_osf [2];
163 187
164static mutex_t reslock = X_MUTEX_INIT; 188static mutex_t reslock = X_MUTEX_INIT;
165static mutex_t reqlock = X_MUTEX_INIT; 189static mutex_t reqlock = X_MUTEX_INIT;
166static cond_t reqwait = X_COND_INIT; 190static cond_t reqwait = X_COND_INIT;
167 191
338 free (req->buf1); 362 free (req->buf1);
339 free (req->buf2); 363 free (req->buf2);
340 Safefree (req); 364 Safefree (req);
341} 365}
342 366
343static void *aio_proc (void *arg); 367#ifdef USE_SOCKETS_AS_HANDLES
368# define TO_SOCKET(x) (win32_get_osfhandle (x))
369#else
370# define TO_SOCKET(x) (x)
371#endif
372
373static void
374create_pipe (int fd[2])
375{
376#ifdef _WIN32
377 int arg = 1;
378 if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, fd)
379 || ioctlsocket (TO_SOCKET (fd [0]), FIONBIO, &arg)
380 || ioctlsocket (TO_SOCKET (fd [1]), FIONBIO, &arg))
381#else
382 if (pipe (fd)
383 || fcntl (fd [0], F_SETFL, O_NONBLOCK)
384 || fcntl (fd [1], F_SETFL, O_NONBLOCK))
385#endif
386 croak ("unable to initialize result pipe");
387
388 respipe_osf [0] = TO_SOCKET (respipe [0]);
389 respipe_osf [1] = TO_SOCKET (respipe [1]);
390}
391
392X_THREAD_PROC (bdb_proc);
344 393
345static void start_thread (void) 394static void start_thread (void)
346{ 395{
347 worker *wrk = calloc (1, sizeof (worker)); 396 worker *wrk = calloc (1, sizeof (worker));
348 397
349 if (!wrk) 398 if (!wrk)
350 croak ("unable to allocate worker thread data"); 399 croak ("unable to allocate worker thread data");
351 400
352 X_LOCK (wrklock); 401 X_LOCK (wrklock);
353 if (thread_create (&wrk->tid, aio_proc, (void *)wrk)) 402 if (thread_create (&wrk->tid, bdb_proc, (void *)wrk))
354 { 403 {
355 wrk->prev = &wrk_first; 404 wrk->prev = &wrk_first;
356 wrk->next = wrk_first.next; 405 wrk->next = wrk_first.next;
357 wrk_first.next->prev = wrk; 406 wrk_first.next->prev = wrk;
358 wrk_first.next = wrk; 407 wrk_first.next = wrk;
381 SV *wait_callback = 0; 430 SV *wait_callback = 0;
382 431
383 // synthesize callback if none given 432 // synthesize callback if none given
384 if (!SvOK (req->callback)) 433 if (!SvOK (req->callback))
385 { 434 {
435 int count;
436
386 dSP; 437 dSP;
387 PUSHMARK (SP); 438 PUSHMARK (SP);
388 PUTBACK; 439 PUTBACK;
389 int count = call_sv (prepare_cb, G_ARRAY); 440 count = call_sv (prepare_cb, G_ARRAY);
390 SPAGAIN; 441 SPAGAIN;
391 442
392 if (count != 2) 443 if (count != 2)
393 croak ("prepare callback must return exactly two values\n"); 444 croak ("prepare callback must return exactly two values\n");
394 445
472 if (size) 523 if (size)
473 return; 524 return;
474 525
475 maybe_start_thread (); 526 maybe_start_thread ();
476 527
477 FD_ZERO(&rfd); 528 FD_ZERO (&rfd);
478 FD_SET(respipe [0], &rfd); 529 FD_SET (respipe [0], &rfd);
479 530
480 select (respipe [0] + 1, &rfd, 0, 0, 0); 531 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
481 } 532 }
482} 533}
483 534
484static int poll_cb () 535static int poll_cb ()
485{ 536{
508 559
509 if (!res_queue.size) 560 if (!res_queue.size)
510 { 561 {
511 /* read any signals sent by the worker threads */ 562 /* read any signals sent by the worker threads */
512 char buf [4]; 563 char buf [4];
513 while (read (respipe [0], buf, 4) == 4) 564 while (respipe_read (respipe [0], buf, 4) == 4)
514 ; 565 ;
515 } 566 }
516 } 567 }
517 568
518 X_UNLOCK (reslock); 569 X_UNLOCK (reslock);
555 return count; 606 return count;
556} 607}
557 608
558/*****************************************************************************/ 609/*****************************************************************************/
559 610
560static void *aio_proc (void *thr_arg) 611X_THREAD_PROC (bdb_proc)
561{ 612{
562 aio_req req; 613 aio_req req;
563 struct timespec ts; 614 struct timespec ts;
564 worker *self = (worker *)thr_arg; 615 worker *self = (worker *)thr_arg;
565 616
566 /* try to distribute timeouts somewhat evenly */ 617 /* try to distribute timeouts somewhat evenly */
567 ts.tv_nsec = (((unsigned long)self + (unsigned long)ts.tv_sec) & 1023UL) 618 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
568 * (1000000000UL / 1024UL);
569 619
570 for (;;) 620 for (;;)
571 { 621 {
572 ts.tv_sec = time (0) + IDLE_TIMEOUT; 622 ts.tv_sec = time (0) + IDLE_TIMEOUT;
573 623
733 783
734 ++npending; 784 ++npending;
735 785
736 if (!reqq_push (&res_queue, req)) 786 if (!reqq_push (&res_queue, req))
737 /* write a dummy byte to the pipe so fh becomes ready */ 787 /* write a dummy byte to the pipe so fh becomes ready */
738 write (respipe [1], &respipe, 1); 788 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
739 789
740 self->req = 0; 790 self->req = 0;
741 worker_clear (self); 791 worker_clear (self);
742 792
743 X_UNLOCK (reslock); 793 X_UNLOCK (reslock);
792 idle = 0; 842 idle = 0;
793 nreqs = 0; 843 nreqs = 0;
794 nready = 0; 844 nready = 0;
795 npending = 0; 845 npending = 0;
796 846
797 close (respipe [0]); 847 respipe_close (respipe [0]);
798 close (respipe [1]); 848 respipe_close (respipe [1]);
799 849
800 if (!create_pipe (respipe)) 850 create_pipe (respipe);
801 croak ("unable to initialize result pipe");
802 851
803 atfork_parent (); 852 atfork_parent ();
804} 853}
805 854
806#define dREQ(reqtype) \ 855#define dREQ(reqtype) \
885 const_iv (DSYNC_DB) 934 const_iv (DSYNC_DB)
886 const_iv (DSYNC_LOG) 935 const_iv (DSYNC_LOG)
887 const_iv (LOG_AUTOREMOVE) 936 const_iv (LOG_AUTOREMOVE)
888 const_iv (LOG_INMEMORY) 937 const_iv (LOG_INMEMORY)
889 const_iv (NOLOCKING) 938 const_iv (NOLOCKING)
890 const_iv (MULTIVERSION)
891 const_iv (NOMMAP) 939 const_iv (NOMMAP)
892 const_iv (NOPANIC) 940 const_iv (NOPANIC)
893 const_iv (OVERWRITE) 941 const_iv (OVERWRITE)
894 const_iv (PANIC_ENVIRONMENT) 942 const_iv (PANIC_ENVIRONMENT)
895 const_iv (REGION_INIT) 943 const_iv (REGION_INIT)
896 const_iv (TIME_NOTGRANTED) 944 const_iv (TIME_NOTGRANTED)
897 const_iv (TXN_NOSYNC) 945 const_iv (TXN_NOSYNC)
898 const_iv (TXN_SNAPSHOT)
899 const_iv (TXN_WRITE_NOSYNC) 946 const_iv (TXN_WRITE_NOSYNC)
900 const_iv (WRITECURSOR) 947 const_iv (WRITECURSOR)
901 const_iv (YIELDCPU) 948 const_iv (YIELDCPU)
902 const_iv (ENCRYPT_AES) 949 const_iv (ENCRYPT_AES)
903 const_iv (XA_CREATE) 950 const_iv (XA_CREATE)
946 const_iv (APPEND) 993 const_iv (APPEND)
947 const_iv (NODUPDATA) 994 const_iv (NODUPDATA)
948 const_iv (NOOVERWRITE) 995 const_iv (NOOVERWRITE)
949 996
950 const_iv (TXN_NOWAIT) 997 const_iv (TXN_NOWAIT)
951 const_iv (TXN_SNAPSHOT)
952 const_iv (TXN_SYNC) 998 const_iv (TXN_SYNC)
953 999
954 const_iv (SET_LOCK_TIMEOUT) 1000 const_iv (SET_LOCK_TIMEOUT)
955 const_iv (SET_TXN_TIMEOUT) 1001 const_iv (SET_TXN_TIMEOUT)
956 1002
984 const_iv (LOCK_YOUNGEST) 1030 const_iv (LOCK_YOUNGEST)
985 1031
986 const_iv (SEQ_DEC) 1032 const_iv (SEQ_DEC)
987 const_iv (SEQ_INC) 1033 const_iv (SEQ_INC)
988 const_iv (SEQ_WRAP) 1034 const_iv (SEQ_WRAP)
1035
1036 const_iv (BUFFER_SMALL)
1037 const_iv (DONOTINDEX)
1038 const_iv (KEYEMPTY )
1039 const_iv (KEYEXIST )
1040 const_iv (LOCK_DEADLOCK)
1041 const_iv (LOCK_NOTGRANTED)
1042 const_iv (LOG_BUFFER_FULL)
1043 const_iv (NOSERVER)
1044 const_iv (NOSERVER_HOME)
1045 const_iv (NOSERVER_ID)
1046 const_iv (NOTFOUND)
1047 const_iv (OLD_VERSION)
1048 const_iv (PAGE_NOTFOUND)
1049 const_iv (REP_DUPMASTER)
1050 const_iv (REP_HANDLE_DEAD)
1051 const_iv (REP_HOLDELECTION)
1052 const_iv (REP_IGNORE)
1053 const_iv (REP_ISPERM)
1054 const_iv (REP_JOIN_FAILURE)
1055 const_iv (REP_LOCKOUT)
1056 const_iv (REP_NEWMASTER)
1057 const_iv (REP_NEWSITE)
1058 const_iv (REP_NOTPERM)
1059 const_iv (REP_UNAVAIL)
1060 const_iv (RUNRECOVERY)
1061 const_iv (SECONDARY_BAD)
1062 const_iv (VERIFY_BAD)
1063 const_iv (VERSION_MISMATCH)
1064
1065 const_iv (VERB_DEADLOCK)
1066 const_iv (VERB_RECOVERY)
1067 const_iv (VERB_REGISTER)
1068 const_iv (VERB_REPLICATION)
1069 const_iv (VERB_WAITSFOR)
1070
1071 const_iv (VERSION_MAJOR)
1072 const_iv (VERSION_MINOR)
1073 const_iv (VERSION_PATCH)
1074#if DB_VERSION_MINOR >= 5
1075 const_iv (MULTIVERSION)
1076 const_iv (TXN_SNAPSHOT)
1077#endif
1078#if DB_VERSION_MINOR >= 6
1079 const_iv (DB_PREV_DUP)
1080#endif
989 }; 1081 };
990 1082
991 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 1083 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
992 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 1084 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
993 1085
1086 newCONSTSUB (stash, "DB_VERSION", newSVnv (DB_VERSION_MAJOR + DB_VERSION_MINOR * .1));
1087 newCONSTSUB (stash, "DB_VERSION_STRING", newSVpv (DB_VERSION_STRING, 0));
1088
994 if (!create_pipe (respipe)) 1089 create_pipe (respipe);
995 croak ("unable to initialize result pipe");
996 1090
997 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child); 1091 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child);
998#ifdef _WIN32 1092#ifdef _WIN32
999 X_MUTEX_CHECK (wrklock); 1093 X_MUTEX_CHECK (wrklock);
1000 X_MUTEX_CHECK (reslock); 1094 X_MUTEX_CHECK (reslock);
1141 PROTOTYPE: & 1235 PROTOTYPE: &
1142 CODE: 1236 CODE:
1143 SvREFCNT_dec (prepare_cb); 1237 SvREFCNT_dec (prepare_cb);
1144 prepare_cb = newSVsv (cb); 1238 prepare_cb = newSVsv (cb);
1145 1239
1240char *
1241strerror (int errorno = errno)
1242 PROTOTYPE: ;$
1243 CODE:
1244 RETVAL = db_strerror (errorno);
1245 OUTPUT:
1246 RETVAL
1146 1247
1147DB_ENV * 1248DB_ENV *
1148db_env_create (U32 env_flags = 0) 1249db_env_create (U32 env_flags = 0)
1149 CODE: 1250 CODE:
1150{ 1251{
1151 errno = db_env_create (&RETVAL, env_flags); 1252 errno = db_env_create (&RETVAL, env_flags);
1152 if (errno) 1253 if (errno)
1153 croak ("db_env_create: %s", db_strerror (errno)); 1254 croak ("db_env_create: %s", db_strerror (errno));
1255
1256 if (0)
1257 {
1258 RETVAL->set_errcall (RETVAL, debug_errcall);
1259 RETVAL->set_msgcall (RETVAL, debug_msgcall);
1260 }
1154} 1261}
1155 OUTPUT: 1262 OUTPUT:
1156 RETVAL 1263 RETVAL
1157 1264
1158void 1265void
1159db_env_open (DB_ENV *env, octetstring db_home, U32 open_flags, int mode, SV *callback = &PL_sv_undef) 1266db_env_open (DB_ENV *env, octetstring db_home, U32 open_flags, int mode, SV *callback = &PL_sv_undef)
1160 CODE: 1267 CODE:
1161{ 1268{
1162 env->set_thread_count (env, get_nthreads ());
1163
1164 dREQ (REQ_ENV_OPEN); 1269 dREQ (REQ_ENV_OPEN);
1270
1271 env->set_thread_count (env, wanted + 2);
1272
1165 req->env = env; 1273 req->env = env;
1166 req->uint1 = open_flags | DB_THREAD; 1274 req->uint1 = open_flags | DB_THREAD;
1167 req->int1 = mode; 1275 req->int1 = mode;
1168 req->buf1 = strdup_ornull (db_home); 1276 req->buf1 = strdup_ornull (db_home);
1169 REQ_SEND; 1277 REQ_SEND;
1561 CODE: 1669 CODE:
1562 RETVAL = env->set_flags (env, flags, onoff); 1670 RETVAL = env->set_flags (env, flags, onoff);
1563 OUTPUT: 1671 OUTPUT:
1564 RETVAL 1672 RETVAL
1565 1673
1674void set_errfile (DB_ENV *env, FILE *errfile)
1675 CODE:
1676 env->set_errfile (env, errfile);
1677
1678void set_msgfile (DB_ENV *env, FILE *msgfile)
1679 CODE:
1680 env->set_msgfile (env, msgfile);
1681
1682int set_verbose (DB_ENV *env, U32 which, int onoff = 1)
1683 CODE:
1684 RETVAL = env->set_verbose (env, which, onoff);
1685 OUTPUT:
1686 RETVAL
1687
1566int set_encrypt (DB_ENV *env, const char *password, U32 flags = 0) 1688int set_encrypt (DB_ENV *env, const char *password, U32 flags = 0)
1567 CODE: 1689 CODE:
1568 RETVAL = env->set_encrypt (env, password, flags); 1690 RETVAL = env->set_encrypt (env, password, flags);
1569 OUTPUT: 1691 OUTPUT:
1570 RETVAL 1692 RETVAL

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines