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.13 by root, Sun Jul 8 11:12:12 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
38static inline char * 50static char *
39strdup_ornull (const char *s) 51strdup_ornull (const char *s)
40{ 52{
41 return s ? strdup (s) : 0; 53 return s ? strdup (s) : 0;
42} 54}
43 55
44static inline void 56static void
45sv_to_dbt (DBT *dbt, SV *sv) 57sv_to_dbt (DBT *dbt, SV *sv)
46{ 58{
47 STRLEN len; 59 STRLEN len;
48 char *data = SvPVbyte (sv, len); 60 char *data = SvPVbyte (sv, len);
49 61
51 memcpy (dbt->data, data, len); 63 memcpy (dbt->data, data, len);
52 dbt->size = len; 64 dbt->size = len;
53 dbt->flags = DB_DBT_REALLOC; 65 dbt->flags = DB_DBT_REALLOC;
54} 66}
55 67
56static inline void 68static void
57dbt_to_sv (SV *sv, DBT *dbt) 69dbt_to_sv (SV *sv, DBT *dbt)
58{ 70{
59 if (sv) 71 if (sv)
60 { 72 {
61 SvREADONLY_off (sv); 73 SvREADONLY_off (sv);
157} 169}
158 170
159static volatile unsigned int nreqs, nready, npending; 171static volatile unsigned int nreqs, nready, npending;
160static volatile unsigned int max_idle = 4; 172static volatile unsigned int max_idle = 4;
161static volatile unsigned int max_outstanding = 0xffffffff; 173static volatile unsigned int max_outstanding = 0xffffffff;
162static int respipe [2]; 174static int respipe [2], respipe_osf [2];
163 175
164static mutex_t reslock = X_MUTEX_INIT; 176static mutex_t reslock = X_MUTEX_INIT;
165static mutex_t reqlock = X_MUTEX_INIT; 177static mutex_t reqlock = X_MUTEX_INIT;
166static cond_t reqwait = X_COND_INIT; 178static cond_t reqwait = X_COND_INIT;
167 179
338 free (req->buf1); 350 free (req->buf1);
339 free (req->buf2); 351 free (req->buf2);
340 Safefree (req); 352 Safefree (req);
341} 353}
342 354
343static void *aio_proc (void *arg); 355#ifdef USE_SOCKETS_AS_HANDLES
356# define TO_SOCKET(x) (win32_get_osfhandle (x))
357#else
358# define TO_SOCKET(x) (x)
359#endif
360
361static void
362create_pipe (int fd[2])
363{
364#ifdef _WIN32
365 int arg = 1;
366 if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, fd)
367 || ioctlsocket (TO_SOCKET (fd [0]), FIONBIO, &arg)
368 || ioctlsocket (TO_SOCKET (fd [1]), FIONBIO, &arg))
369#else
370 if (pipe (fd)
371 || fcntl (fd [0], F_SETFL, O_NONBLOCK)
372 || fcntl (fd [1], F_SETFL, O_NONBLOCK))
373#endif
374 croak ("unable to initialize result pipe");
375
376 respipe_osf [0] = TO_SOCKET (respipe [0]);
377 respipe_osf [1] = TO_SOCKET (respipe [1]);
378}
379
380X_THREAD_PROC (bdb_proc);
344 381
345static void start_thread (void) 382static void start_thread (void)
346{ 383{
347 worker *wrk = calloc (1, sizeof (worker)); 384 worker *wrk = calloc (1, sizeof (worker));
348 385
349 if (!wrk) 386 if (!wrk)
350 croak ("unable to allocate worker thread data"); 387 croak ("unable to allocate worker thread data");
351 388
352 X_LOCK (wrklock); 389 X_LOCK (wrklock);
353 if (thread_create (&wrk->tid, aio_proc, (void *)wrk)) 390 if (thread_create (&wrk->tid, bdb_proc, (void *)wrk))
354 { 391 {
355 wrk->prev = &wrk_first; 392 wrk->prev = &wrk_first;
356 wrk->next = wrk_first.next; 393 wrk->next = wrk_first.next;
357 wrk_first.next->prev = wrk; 394 wrk_first.next->prev = wrk;
358 wrk_first.next = wrk; 395 wrk_first.next = wrk;
381 SV *wait_callback = 0; 418 SV *wait_callback = 0;
382 419
383 // synthesize callback if none given 420 // synthesize callback if none given
384 if (!SvOK (req->callback)) 421 if (!SvOK (req->callback))
385 { 422 {
423 int count;
424
386 dSP; 425 dSP;
387 PUSHMARK (SP); 426 PUSHMARK (SP);
388 PUTBACK; 427 PUTBACK;
389 int count = call_sv (prepare_cb, G_ARRAY); 428 count = call_sv (prepare_cb, G_ARRAY);
390 SPAGAIN; 429 SPAGAIN;
391 430
392 if (count != 2) 431 if (count != 2)
393 croak ("prepare callback must return exactly two values\n"); 432 croak ("prepare callback must return exactly two values\n");
394 433
472 if (size) 511 if (size)
473 return; 512 return;
474 513
475 maybe_start_thread (); 514 maybe_start_thread ();
476 515
477 FD_ZERO(&rfd); 516 FD_ZERO (&rfd);
478 FD_SET(respipe [0], &rfd); 517 FD_SET (respipe [0], &rfd);
479 518
480 select (respipe [0] + 1, &rfd, 0, 0, 0); 519 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
481 } 520 }
482} 521}
483 522
484static int poll_cb () 523static int poll_cb ()
485{ 524{
508 547
509 if (!res_queue.size) 548 if (!res_queue.size)
510 { 549 {
511 /* read any signals sent by the worker threads */ 550 /* read any signals sent by the worker threads */
512 char buf [4]; 551 char buf [4];
513 while (read (respipe [0], buf, 4) == 4) 552 while (respipe_read (respipe [0], buf, 4) == 4)
514 ; 553 ;
515 } 554 }
516 } 555 }
517 556
518 X_UNLOCK (reslock); 557 X_UNLOCK (reslock);
555 return count; 594 return count;
556} 595}
557 596
558/*****************************************************************************/ 597/*****************************************************************************/
559 598
560static void *aio_proc (void *thr_arg) 599X_THREAD_PROC (bdb_proc)
561{ 600{
562 aio_req req; 601 aio_req req;
563 struct timespec ts; 602 struct timespec ts;
564 worker *self = (worker *)thr_arg; 603 worker *self = (worker *)thr_arg;
565 604
566 /* try to distribute timeouts somewhat evenly */ 605 /* try to distribute timeouts somewhat evenly */
567 ts.tv_nsec = (((unsigned long)self + (unsigned long)ts.tv_sec) & 1023UL) 606 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
568 * (1000000000UL / 1024UL);
569 607
570 for (;;) 608 for (;;)
571 { 609 {
572 ts.tv_sec = time (0) + IDLE_TIMEOUT; 610 ts.tv_sec = time (0) + IDLE_TIMEOUT;
573 611
733 771
734 ++npending; 772 ++npending;
735 773
736 if (!reqq_push (&res_queue, req)) 774 if (!reqq_push (&res_queue, req))
737 /* write a dummy byte to the pipe so fh becomes ready */ 775 /* write a dummy byte to the pipe so fh becomes ready */
738 write (respipe [1], &respipe, 1); 776 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
739 777
740 self->req = 0; 778 self->req = 0;
741 worker_clear (self); 779 worker_clear (self);
742 780
743 X_UNLOCK (reslock); 781 X_UNLOCK (reslock);
792 idle = 0; 830 idle = 0;
793 nreqs = 0; 831 nreqs = 0;
794 nready = 0; 832 nready = 0;
795 npending = 0; 833 npending = 0;
796 834
797 close (respipe [0]); 835 respipe_close (respipe [0]);
798 close (respipe [1]); 836 respipe_close (respipe [1]);
799 837
800 if (!create_pipe (respipe)) 838 create_pipe (respipe);
801 croak ("unable to initialize result pipe");
802 839
803 atfork_parent (); 840 atfork_parent ();
804} 841}
805 842
806#define dREQ(reqtype) \ 843#define dREQ(reqtype) \
885 const_iv (DSYNC_DB) 922 const_iv (DSYNC_DB)
886 const_iv (DSYNC_LOG) 923 const_iv (DSYNC_LOG)
887 const_iv (LOG_AUTOREMOVE) 924 const_iv (LOG_AUTOREMOVE)
888 const_iv (LOG_INMEMORY) 925 const_iv (LOG_INMEMORY)
889 const_iv (NOLOCKING) 926 const_iv (NOLOCKING)
890 const_iv (MULTIVERSION)
891 const_iv (NOMMAP) 927 const_iv (NOMMAP)
892 const_iv (NOPANIC) 928 const_iv (NOPANIC)
893 const_iv (OVERWRITE) 929 const_iv (OVERWRITE)
894 const_iv (PANIC_ENVIRONMENT) 930 const_iv (PANIC_ENVIRONMENT)
895 const_iv (REGION_INIT) 931 const_iv (REGION_INIT)
896 const_iv (TIME_NOTGRANTED) 932 const_iv (TIME_NOTGRANTED)
897 const_iv (TXN_NOSYNC) 933 const_iv (TXN_NOSYNC)
898 const_iv (TXN_SNAPSHOT)
899 const_iv (TXN_WRITE_NOSYNC) 934 const_iv (TXN_WRITE_NOSYNC)
900 const_iv (WRITECURSOR) 935 const_iv (WRITECURSOR)
901 const_iv (YIELDCPU) 936 const_iv (YIELDCPU)
902 const_iv (ENCRYPT_AES) 937 const_iv (ENCRYPT_AES)
903 const_iv (XA_CREATE) 938 const_iv (XA_CREATE)
946 const_iv (APPEND) 981 const_iv (APPEND)
947 const_iv (NODUPDATA) 982 const_iv (NODUPDATA)
948 const_iv (NOOVERWRITE) 983 const_iv (NOOVERWRITE)
949 984
950 const_iv (TXN_NOWAIT) 985 const_iv (TXN_NOWAIT)
951 const_iv (TXN_SNAPSHOT)
952 const_iv (TXN_SYNC) 986 const_iv (TXN_SYNC)
953 987
954 const_iv (SET_LOCK_TIMEOUT) 988 const_iv (SET_LOCK_TIMEOUT)
955 const_iv (SET_TXN_TIMEOUT) 989 const_iv (SET_TXN_TIMEOUT)
956 990
984 const_iv (LOCK_YOUNGEST) 1018 const_iv (LOCK_YOUNGEST)
985 1019
986 const_iv (SEQ_DEC) 1020 const_iv (SEQ_DEC)
987 const_iv (SEQ_INC) 1021 const_iv (SEQ_INC)
988 const_iv (SEQ_WRAP) 1022 const_iv (SEQ_WRAP)
1023#if DB_VERSION_MINOR >= 5
1024 const_iv (MULTIVERSION)
1025 const_iv (TXN_SNAPSHOT)
1026#endif
989 }; 1027 };
990 1028
991 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 1029 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
992 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 1030 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
993 1031
994 if (!create_pipe (respipe)) 1032 create_pipe (respipe);
995 croak ("unable to initialize result pipe");
996 1033
997 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child); 1034 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child);
998#ifdef _WIN32 1035#ifdef _WIN32
999 X_MUTEX_CHECK (wrklock); 1036 X_MUTEX_CHECK (wrklock);
1000 X_MUTEX_CHECK (reslock); 1037 X_MUTEX_CHECK (reslock);
1157 1194
1158void 1195void
1159db_env_open (DB_ENV *env, octetstring db_home, U32 open_flags, int mode, SV *callback = &PL_sv_undef) 1196db_env_open (DB_ENV *env, octetstring db_home, U32 open_flags, int mode, SV *callback = &PL_sv_undef)
1160 CODE: 1197 CODE:
1161{ 1198{
1199 dREQ (REQ_ENV_OPEN);
1200
1162 env->set_thread_count (env, get_nthreads ()); 1201 env->set_thread_count (env, get_nthreads ());
1163 1202
1164 dREQ (REQ_ENV_OPEN);
1165 req->env = env; 1203 req->env = env;
1166 req->uint1 = open_flags | DB_THREAD; 1204 req->uint1 = open_flags | DB_THREAD;
1167 req->int1 = mode; 1205 req->int1 = mode;
1168 req->buf1 = strdup_ornull (db_home); 1206 req->buf1 = strdup_ornull (db_home);
1169 REQ_SEND; 1207 REQ_SEND;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines