1 |
#include <sys/user.h> |
2 |
#include <sys/ptrace.h> |
3 |
|
4 |
#define X_STACKSIZE 1024 * 128 + sizeof (long) * 64 * 1024 / 4 |
5 |
|
6 |
#include "xthread.h" |
7 |
|
8 |
#include <errno.h> |
9 |
|
10 |
#include "EXTERN.h" |
11 |
#include "perl.h" |
12 |
#include "XSUB.h" |
13 |
|
14 |
// perl stupidly defines these as macros, breaking |
15 |
// lots and lots of code. |
16 |
#undef open |
17 |
#undef close |
18 |
#undef abort |
19 |
#undef malloc |
20 |
#undef free |
21 |
#undef send |
22 |
|
23 |
#include <stddef.h> |
24 |
#include <stdlib.h> |
25 |
#include <errno.h> |
26 |
#include <sys/types.h> |
27 |
#include <limits.h> |
28 |
#include <fcntl.h> |
29 |
|
30 |
#ifndef _WIN32 |
31 |
# include <sys/time.h> |
32 |
# include <unistd.h> |
33 |
#endif |
34 |
|
35 |
#include <db.h> |
36 |
|
37 |
#if DB_VERSION_MAJOR != 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 3) |
38 |
# error you need Berkeley DB 4.3 or a newer 4.x version installed |
39 |
#endif |
40 |
|
41 |
/* number of seconds after which idle threads exit */ |
42 |
#define IDLE_TIMEOUT 10 |
43 |
|
44 |
typedef DB_ENV DB_ENV_ornull; |
45 |
typedef DB_TXN DB_TXN_ornull; |
46 |
typedef DBC DBC_ornull; |
47 |
typedef DB DB_ornull; |
48 |
|
49 |
typedef DB_ENV DB_ENV_ornuked; |
50 |
typedef DB_TXN DB_TXN_ornuked; |
51 |
typedef DBC DBC_ornuked; |
52 |
typedef DB DB_ornuked; |
53 |
|
54 |
#if DB_VERSION_MINOR >= 3 |
55 |
typedef DB_SEQUENCE DB_SEQUENCE_ornull; |
56 |
typedef DB_SEQUENCE DB_SEQUENCE_ornuked; |
57 |
#endif |
58 |
|
59 |
typedef SV SV8; /* byte-sv, used for argument-checking */ |
60 |
typedef char *bdb_filename; |
61 |
|
62 |
static SV *prepare_cb; |
63 |
|
64 |
#if DB_VERSION_MINOR >= 6 |
65 |
# define c_close close |
66 |
# define c_count count |
67 |
# define c_del del |
68 |
# define c_dup dup |
69 |
# define c_get get |
70 |
# define c_pget pget |
71 |
# define c_put put |
72 |
#endif |
73 |
|
74 |
static char * |
75 |
get_bdb_filename (SV *sv) |
76 |
{ |
77 |
if (!SvOK (sv)) |
78 |
return 0; |
79 |
|
80 |
#if _WIN32 |
81 |
/* win32 madness + win32 perl absolutely brokenness make for horrible hacks */ |
82 |
{ |
83 |
STRLEN len; |
84 |
char *src = SvPVbyte (sv, len); |
85 |
SV *t1 = sv_newmortal (); |
86 |
SV *t2 = sv_newmortal (); |
87 |
|
88 |
sv_upgrade (t1, SVt_PV); SvPOK_only (t1); SvGROW (t1, len * 16 + 1); |
89 |
sv_upgrade (t2, SVt_PV); SvPOK_only (t2); SvGROW (t2, len * 16 + 1); |
90 |
|
91 |
len = MultiByteToWideChar (CP_ACP, 0, src, len, (WCHAR *)SvPVX (t1), SvLEN (t1) / sizeof (WCHAR)); |
92 |
len = WideCharToMultiByte (CP_UTF8, 0, (WCHAR *)SvPVX (t1), len, SvPVX (t2), SvLEN (t2), 0, 0); |
93 |
SvPOK_only (t2); |
94 |
SvPVX (t2)[len] = 0; |
95 |
SvCUR_set (t2, len); |
96 |
|
97 |
return SvPVX (t2); |
98 |
} |
99 |
#else |
100 |
return SvPVbyte_nolen (sv); |
101 |
#endif |
102 |
} |
103 |
|
104 |
static void |
105 |
debug_errcall (const DB_ENV *dbenv, const char *errpfx, const char *msg) |
106 |
{ |
107 |
printf ("err[%s]\n", msg); |
108 |
} |
109 |
|
110 |
static void |
111 |
debug_msgcall (const DB_ENV *dbenv, const char *msg) |
112 |
{ |
113 |
printf ("msg[%s]\n", msg); |
114 |
} |
115 |
|
116 |
static char * |
117 |
strdup_ornull (const char *s) |
118 |
{ |
119 |
return s ? strdup (s) : 0; |
120 |
} |
121 |
|
122 |
static void |
123 |
sv_to_dbt (DBT *dbt, SV *sv) |
124 |
{ |
125 |
STRLEN len; |
126 |
char *data = SvPVbyte (sv, len); |
127 |
|
128 |
dbt->data = malloc (len); |
129 |
memcpy (dbt->data, data, len); |
130 |
dbt->size = len; |
131 |
dbt->flags = DB_DBT_REALLOC; |
132 |
} |
133 |
|
134 |
static void |
135 |
dbt_to_sv (SV *sv, DBT *dbt) |
136 |
{ |
137 |
if (sv) |
138 |
{ |
139 |
SvREADONLY_off (sv); |
140 |
sv_setsv_mg (sv, dbt->data ? newSVpvn (dbt->data, dbt->size) : &PL_sv_undef); |
141 |
SvREFCNT_dec (sv); |
142 |
} |
143 |
|
144 |
//assert (dbt->flags & DBT_MALLOC || !dbt->data); |
145 |
free (dbt->data); |
146 |
} |
147 |
|
148 |
enum { |
149 |
REQ_QUIT, |
150 |
REQ_ENV_OPEN, REQ_ENV_CLOSE, REQ_ENV_TXN_CHECKPOINT, REQ_ENV_LOCK_DETECT, |
151 |
REQ_ENV_MEMP_SYNC, REQ_ENV_MEMP_TRICKLE, REQ_ENV_DBREMOVE, REQ_ENV_DBRENAME, |
152 |
REQ_DB_OPEN, REQ_DB_CLOSE, REQ_DB_COMPACT, REQ_DB_SYNC, REQ_DB_UPGRADE, |
153 |
REQ_DB_PUT, REQ_DB_EXISTS, REQ_DB_GET, REQ_DB_PGET, REQ_DB_DEL, REQ_DB_KEY_RANGE, |
154 |
REQ_TXN_COMMIT, REQ_TXN_ABORT, REQ_TXN_FINISH, |
155 |
REQ_C_CLOSE, REQ_C_COUNT, REQ_C_PUT, REQ_C_GET, REQ_C_PGET, REQ_C_DEL, |
156 |
REQ_SEQ_OPEN, REQ_SEQ_CLOSE, REQ_SEQ_GET, REQ_SEQ_REMOVE, |
157 |
}; |
158 |
|
159 |
typedef struct bdb_cb |
160 |
{ |
161 |
struct bdb_cb *volatile next; |
162 |
SV *callback; |
163 |
int type, pri, result; |
164 |
|
165 |
DB_ENV *env; |
166 |
DB *db; |
167 |
DB_TXN *txn; |
168 |
DBC *dbc; |
169 |
|
170 |
UV uv1; |
171 |
int int1, int2; |
172 |
U32 uint1, uint2; |
173 |
char *buf1, *buf2, *buf3; |
174 |
SV *sv1, *sv2, *sv3; |
175 |
|
176 |
DBT dbt1, dbt2, dbt3; |
177 |
DB_KEY_RANGE key_range; |
178 |
#if DB_VERSION_MINOR >= 3 |
179 |
DB_SEQUENCE *seq; |
180 |
db_seq_t seq_t; |
181 |
#endif |
182 |
|
183 |
SV *rsv1, *rsv2; // keep some request objects alive |
184 |
} bdb_cb; |
185 |
|
186 |
typedef bdb_cb *bdb_req; |
187 |
|
188 |
enum { |
189 |
PRI_MIN = -4, |
190 |
PRI_MAX = 4, |
191 |
|
192 |
DEFAULT_PRI = 0, |
193 |
PRI_BIAS = -PRI_MIN, |
194 |
NUM_PRI = PRI_MAX + PRI_BIAS + 1, |
195 |
}; |
196 |
|
197 |
#define AIO_TICKS ((1000000 + 1023) >> 10) |
198 |
|
199 |
static SV *on_next_submit; |
200 |
|
201 |
static unsigned int max_poll_time = 0; |
202 |
static unsigned int max_poll_reqs = 0; |
203 |
|
204 |
/* calculcate time difference in ~1/AIO_TICKS of a second */ |
205 |
static int tvdiff (struct timeval *tv1, struct timeval *tv2) |
206 |
{ |
207 |
return (tv2->tv_sec - tv1->tv_sec ) * AIO_TICKS |
208 |
+ ((tv2->tv_usec - tv1->tv_usec) >> 10); |
209 |
} |
210 |
|
211 |
static int next_pri = DEFAULT_PRI + PRI_BIAS; |
212 |
|
213 |
static unsigned int started, idle, wanted; |
214 |
|
215 |
/* worker threads management */ |
216 |
static mutex_t wrklock = X_MUTEX_INIT; |
217 |
|
218 |
typedef struct worker { |
219 |
/* locked by wrklock */ |
220 |
struct worker *prev, *next; |
221 |
|
222 |
thread_t tid; |
223 |
|
224 |
/* locked by reslock, reqlock or wrklock */ |
225 |
bdb_req req; /* currently processed request */ |
226 |
void *dbuf; |
227 |
DIR *dirp; |
228 |
} worker; |
229 |
|
230 |
static worker wrk_first = { &wrk_first, &wrk_first, 0 }; |
231 |
|
232 |
static void worker_clear (worker *wrk) |
233 |
{ |
234 |
} |
235 |
|
236 |
static void worker_free (worker *wrk) |
237 |
{ |
238 |
wrk->next->prev = wrk->prev; |
239 |
wrk->prev->next = wrk->next; |
240 |
|
241 |
free (wrk); |
242 |
} |
243 |
|
244 |
static volatile unsigned int nreqs, nready, npending; |
245 |
static volatile unsigned int max_idle = 4; |
246 |
static volatile unsigned int max_outstanding = 0xffffffff; |
247 |
static int respipe_osf [2], respipe [2] = { -1, -1 }; |
248 |
|
249 |
static mutex_t reslock = X_MUTEX_INIT; |
250 |
static mutex_t reqlock = X_MUTEX_INIT; |
251 |
static cond_t reqwait = X_COND_INIT; |
252 |
|
253 |
#if WORDACCESS_UNSAFE |
254 |
|
255 |
static unsigned int get_nready (void) |
256 |
{ |
257 |
unsigned int retval; |
258 |
|
259 |
X_LOCK (reqlock); |
260 |
retval = nready; |
261 |
X_UNLOCK (reqlock); |
262 |
|
263 |
return retval; |
264 |
} |
265 |
|
266 |
static unsigned int get_npending (void) |
267 |
{ |
268 |
unsigned int retval; |
269 |
|
270 |
X_LOCK (reslock); |
271 |
retval = npending; |
272 |
X_UNLOCK (reslock); |
273 |
|
274 |
return retval; |
275 |
} |
276 |
|
277 |
static unsigned int get_nthreads (void) |
278 |
{ |
279 |
unsigned int retval; |
280 |
|
281 |
X_LOCK (wrklock); |
282 |
retval = started; |
283 |
X_UNLOCK (wrklock); |
284 |
|
285 |
return retval; |
286 |
} |
287 |
|
288 |
#else |
289 |
|
290 |
# define get_nready() nready |
291 |
# define get_npending() npending |
292 |
# define get_nthreads() started |
293 |
|
294 |
#endif |
295 |
|
296 |
/* |
297 |
* a somewhat faster data structure might be nice, but |
298 |
* with 8 priorities this actually needs <20 insns |
299 |
* per shift, the most expensive operation. |
300 |
*/ |
301 |
typedef struct { |
302 |
bdb_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */ |
303 |
int size; |
304 |
} reqq; |
305 |
|
306 |
static reqq req_queue; |
307 |
static reqq res_queue; |
308 |
|
309 |
int reqq_push (reqq *q, bdb_req req) |
310 |
{ |
311 |
int pri = req->pri; |
312 |
req->next = 0; |
313 |
|
314 |
if (q->qe[pri]) |
315 |
{ |
316 |
q->qe[pri]->next = req; |
317 |
q->qe[pri] = req; |
318 |
} |
319 |
else |
320 |
q->qe[pri] = q->qs[pri] = req; |
321 |
|
322 |
return q->size++; |
323 |
} |
324 |
|
325 |
bdb_req reqq_shift (reqq *q) |
326 |
{ |
327 |
int pri; |
328 |
|
329 |
if (!q->size) |
330 |
return 0; |
331 |
|
332 |
--q->size; |
333 |
|
334 |
for (pri = NUM_PRI; pri--; ) |
335 |
{ |
336 |
bdb_req req = q->qs[pri]; |
337 |
|
338 |
if (req) |
339 |
{ |
340 |
if (!(q->qs[pri] = req->next)) |
341 |
q->qe[pri] = 0; |
342 |
|
343 |
return req; |
344 |
} |
345 |
} |
346 |
|
347 |
abort (); |
348 |
} |
349 |
|
350 |
static int poll_cb (void); |
351 |
static void req_free (bdb_req req); |
352 |
static void req_cancel (bdb_req req); |
353 |
|
354 |
static int req_invoke (bdb_req req) |
355 |
{ |
356 |
dSP; |
357 |
|
358 |
if (req->callback) |
359 |
{ |
360 |
ENTER; |
361 |
SAVETMPS; |
362 |
PUSHMARK (SP); |
363 |
|
364 |
switch (req->type) |
365 |
{ |
366 |
case REQ_DB_CLOSE: |
367 |
SvREFCNT_dec (req->sv1); |
368 |
break; |
369 |
|
370 |
case REQ_DB_GET: |
371 |
case REQ_DB_PGET: |
372 |
dbt_to_sv (req->sv3, &req->dbt3); |
373 |
break; |
374 |
|
375 |
case REQ_C_GET: |
376 |
case REQ_C_PGET: |
377 |
dbt_to_sv (req->sv1, &req->dbt1); |
378 |
dbt_to_sv (req->sv2, &req->dbt2); |
379 |
dbt_to_sv (req->sv3, &req->dbt3); |
380 |
break; |
381 |
|
382 |
case REQ_DB_PUT: |
383 |
case REQ_C_PUT: |
384 |
dbt_to_sv (0, &req->dbt1); |
385 |
dbt_to_sv (0, &req->dbt2); |
386 |
break; |
387 |
|
388 |
case REQ_DB_KEY_RANGE: |
389 |
{ |
390 |
AV *av = newAV (); |
391 |
|
392 |
av_push (av, newSVnv (req->key_range.less)); |
393 |
av_push (av, newSVnv (req->key_range.equal)); |
394 |
av_push (av, newSVnv (req->key_range.greater)); |
395 |
|
396 |
SvREADONLY_off (req->sv1); |
397 |
sv_setsv_mg (req->sv1, newRV_noinc ((SV *)av)); |
398 |
SvREFCNT_dec (req->sv1); |
399 |
} |
400 |
break; |
401 |
|
402 |
#if DB_VERSION_MINOR >= 3 |
403 |
case REQ_SEQ_GET: |
404 |
SvREADONLY_off (req->sv1); |
405 |
|
406 |
if (sizeof (IV) > 4) |
407 |
sv_setiv_mg (req->sv1, (IV)req->seq_t); |
408 |
else |
409 |
sv_setnv_mg (req->sv1, (NV)req->seq_t); |
410 |
|
411 |
SvREFCNT_dec (req->sv1); |
412 |
break; |
413 |
#endif |
414 |
} |
415 |
|
416 |
errno = req->result; |
417 |
|
418 |
PUTBACK; |
419 |
call_sv (req->callback, G_VOID | G_EVAL); |
420 |
SPAGAIN; |
421 |
|
422 |
FREETMPS; |
423 |
LEAVE; |
424 |
} |
425 |
|
426 |
return !SvTRUE (ERRSV); |
427 |
} |
428 |
|
429 |
static void req_free (bdb_req req) |
430 |
{ |
431 |
SvREFCNT_dec (req->callback); |
432 |
|
433 |
SvREFCNT_dec (req->rsv1); |
434 |
SvREFCNT_dec (req->rsv2); |
435 |
|
436 |
free (req->buf1); |
437 |
free (req->buf2); |
438 |
free (req->buf3); |
439 |
|
440 |
Safefree (req); |
441 |
} |
442 |
|
443 |
#ifdef USE_SOCKETS_AS_HANDLES |
444 |
# define TO_SOCKET(x) (win32_get_osfhandle (x)) |
445 |
#else |
446 |
# define TO_SOCKET(x) (x) |
447 |
#endif |
448 |
|
449 |
static void |
450 |
create_respipe (void) |
451 |
{ |
452 |
#ifdef _WIN32 |
453 |
int arg; /* argg */ |
454 |
#endif |
455 |
int old_readfd = respipe [0]; |
456 |
|
457 |
if (respipe [1] >= 0) |
458 |
respipe_close (TO_SOCKET (respipe [1])); |
459 |
|
460 |
#ifdef _WIN32 |
461 |
if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe)) |
462 |
#else |
463 |
if (pipe (respipe)) |
464 |
#endif |
465 |
croak ("unable to initialize result pipe"); |
466 |
|
467 |
if (old_readfd >= 0) |
468 |
{ |
469 |
if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0) |
470 |
croak ("unable to initialize result pipe(2)"); |
471 |
|
472 |
respipe_close (respipe [0]); |
473 |
respipe [0] = old_readfd; |
474 |
} |
475 |
|
476 |
#ifdef _WIN32 |
477 |
arg = 1; |
478 |
if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg) |
479 |
|| ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg)) |
480 |
#else |
481 |
if (fcntl (respipe [0], F_SETFL, O_NONBLOCK) |
482 |
|| fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
483 |
#endif |
484 |
croak ("unable to initialize result pipe(3)"); |
485 |
|
486 |
respipe_osf [0] = TO_SOCKET (respipe [0]); |
487 |
respipe_osf [1] = TO_SOCKET (respipe [1]); |
488 |
} |
489 |
|
490 |
X_THREAD_PROC (bdb_proc); |
491 |
|
492 |
static void start_thread (void) |
493 |
{ |
494 |
worker *wrk = calloc (1, sizeof (worker)); |
495 |
|
496 |
if (!wrk) |
497 |
croak ("unable to allocate worker thread data"); |
498 |
|
499 |
X_LOCK (wrklock); |
500 |
if (thread_create (&wrk->tid, bdb_proc, (void *)wrk)) |
501 |
{ |
502 |
wrk->prev = &wrk_first; |
503 |
wrk->next = wrk_first.next; |
504 |
wrk_first.next->prev = wrk; |
505 |
wrk_first.next = wrk; |
506 |
++started; |
507 |
} |
508 |
else |
509 |
free (wrk); |
510 |
|
511 |
X_UNLOCK (wrklock); |
512 |
} |
513 |
|
514 |
static void maybe_start_thread (void) |
515 |
{ |
516 |
if (get_nthreads () >= wanted) |
517 |
return; |
518 |
|
519 |
/* todo: maybe use idle here, but might be less exact */ |
520 |
if (0 <= (int)get_nthreads () + (int)get_npending () - (int)nreqs) |
521 |
return; |
522 |
|
523 |
start_thread (); |
524 |
} |
525 |
|
526 |
static void req_send (bdb_req req) |
527 |
{ |
528 |
SV *wait_callback = 0; |
529 |
|
530 |
if (on_next_submit) |
531 |
{ |
532 |
dSP; |
533 |
SV *cb = sv_2mortal (on_next_submit); |
534 |
|
535 |
on_next_submit = 0; |
536 |
|
537 |
PUSHMARK (SP); |
538 |
PUTBACK; |
539 |
call_sv (cb, G_DISCARD | G_EVAL); |
540 |
SPAGAIN; |
541 |
} |
542 |
|
543 |
// synthesize callback if none given |
544 |
if (!req->callback) |
545 |
{ |
546 |
int count; |
547 |
|
548 |
dSP; |
549 |
PUSHMARK (SP); |
550 |
PUTBACK; |
551 |
count = call_sv (prepare_cb, G_ARRAY); |
552 |
SPAGAIN; |
553 |
|
554 |
if (count != 2) |
555 |
croak ("prepare callback must return exactly two values\n"); |
556 |
|
557 |
wait_callback = POPs; |
558 |
req->callback = SvREFCNT_inc (POPs); |
559 |
} |
560 |
|
561 |
++nreqs; |
562 |
|
563 |
X_LOCK (reqlock); |
564 |
++nready; |
565 |
reqq_push (&req_queue, req); |
566 |
X_COND_SIGNAL (reqwait); |
567 |
X_UNLOCK (reqlock); |
568 |
|
569 |
maybe_start_thread (); |
570 |
|
571 |
if (wait_callback) |
572 |
{ |
573 |
dSP; |
574 |
PUSHMARK (SP); |
575 |
PUTBACK; |
576 |
call_sv (wait_callback, G_DISCARD); |
577 |
} |
578 |
} |
579 |
|
580 |
static void end_thread (void) |
581 |
{ |
582 |
bdb_req req; |
583 |
|
584 |
Newz (0, req, 1, bdb_cb); |
585 |
|
586 |
req->type = REQ_QUIT; |
587 |
req->pri = PRI_MAX + PRI_BIAS; |
588 |
|
589 |
X_LOCK (reqlock); |
590 |
reqq_push (&req_queue, req); |
591 |
X_COND_SIGNAL (reqwait); |
592 |
X_UNLOCK (reqlock); |
593 |
|
594 |
X_LOCK (wrklock); |
595 |
--started; |
596 |
X_UNLOCK (wrklock); |
597 |
} |
598 |
|
599 |
static void set_max_idle (int nthreads) |
600 |
{ |
601 |
if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
602 |
max_idle = nthreads <= 0 ? 1 : nthreads; |
603 |
if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
604 |
} |
605 |
|
606 |
static void min_parallel (int nthreads) |
607 |
{ |
608 |
if (wanted < nthreads) |
609 |
wanted = nthreads; |
610 |
} |
611 |
|
612 |
static void max_parallel (int nthreads) |
613 |
{ |
614 |
if (wanted > nthreads) |
615 |
wanted = nthreads; |
616 |
|
617 |
while (started > wanted) |
618 |
end_thread (); |
619 |
} |
620 |
|
621 |
static void poll_wait (void) |
622 |
{ |
623 |
fd_set rfd; |
624 |
|
625 |
while (nreqs) |
626 |
{ |
627 |
int size; |
628 |
if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
629 |
size = res_queue.size; |
630 |
if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
631 |
|
632 |
if (size) |
633 |
return; |
634 |
|
635 |
maybe_start_thread (); |
636 |
|
637 |
FD_ZERO (&rfd); |
638 |
FD_SET (respipe [0], &rfd); |
639 |
|
640 |
PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); |
641 |
} |
642 |
} |
643 |
|
644 |
static int poll_cb (void) |
645 |
{ |
646 |
dSP; |
647 |
int count = 0; |
648 |
int maxreqs = max_poll_reqs; |
649 |
int do_croak = 0; |
650 |
struct timeval tv_start, tv_now; |
651 |
bdb_req req; |
652 |
|
653 |
if (max_poll_time) |
654 |
gettimeofday (&tv_start, 0); |
655 |
|
656 |
for (;;) |
657 |
{ |
658 |
for (;;) |
659 |
{ |
660 |
maybe_start_thread (); |
661 |
|
662 |
X_LOCK (reslock); |
663 |
req = reqq_shift (&res_queue); |
664 |
|
665 |
if (req) |
666 |
{ |
667 |
--npending; |
668 |
|
669 |
if (!res_queue.size) |
670 |
{ |
671 |
/* read any signals sent by the worker threads */ |
672 |
char buf [4]; |
673 |
while (respipe_read (respipe [0], buf, 4) == 4) |
674 |
; |
675 |
} |
676 |
} |
677 |
|
678 |
X_UNLOCK (reslock); |
679 |
|
680 |
if (!req) |
681 |
break; |
682 |
|
683 |
--nreqs; |
684 |
|
685 |
if (!req_invoke (req)) |
686 |
{ |
687 |
req_free (req); |
688 |
croak (0); |
689 |
} |
690 |
|
691 |
count++; |
692 |
|
693 |
req_free (req); |
694 |
|
695 |
if (maxreqs && !--maxreqs) |
696 |
break; |
697 |
|
698 |
if (max_poll_time) |
699 |
{ |
700 |
gettimeofday (&tv_now, 0); |
701 |
|
702 |
if (tvdiff (&tv_start, &tv_now) >= max_poll_time) |
703 |
break; |
704 |
} |
705 |
} |
706 |
|
707 |
if (nreqs <= max_outstanding) |
708 |
break; |
709 |
|
710 |
poll_wait (); |
711 |
|
712 |
++maxreqs; |
713 |
} |
714 |
|
715 |
return count; |
716 |
} |
717 |
|
718 |
/*****************************************************************************/ |
719 |
|
720 |
X_THREAD_PROC (bdb_proc) |
721 |
{ |
722 |
bdb_req req; |
723 |
struct timespec ts; |
724 |
worker *self = (worker *)thr_arg; |
725 |
|
726 |
/* try to distribute timeouts somewhat evenly */ |
727 |
ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); |
728 |
|
729 |
for (;;) |
730 |
{ |
731 |
ts.tv_sec = time (0) + IDLE_TIMEOUT; |
732 |
|
733 |
X_LOCK (reqlock); |
734 |
|
735 |
for (;;) |
736 |
{ |
737 |
self->req = req = reqq_shift (&req_queue); |
738 |
|
739 |
if (req) |
740 |
break; |
741 |
|
742 |
++idle; |
743 |
|
744 |
if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) |
745 |
== ETIMEDOUT) |
746 |
{ |
747 |
if (idle > max_idle) |
748 |
{ |
749 |
--idle; |
750 |
X_UNLOCK (reqlock); |
751 |
X_LOCK (wrklock); |
752 |
--started; |
753 |
X_UNLOCK (wrklock); |
754 |
goto quit; |
755 |
} |
756 |
|
757 |
/* we are allowed to idle, so do so without any timeout */ |
758 |
X_COND_WAIT (reqwait, reqlock); |
759 |
ts.tv_sec = time (0) + IDLE_TIMEOUT; |
760 |
} |
761 |
|
762 |
--idle; |
763 |
} |
764 |
|
765 |
--nready; |
766 |
|
767 |
X_UNLOCK (reqlock); |
768 |
|
769 |
switch (req->type) |
770 |
{ |
771 |
case REQ_QUIT: |
772 |
req->result = ENOSYS; |
773 |
goto quit; |
774 |
|
775 |
case REQ_ENV_OPEN: |
776 |
req->result = req->env->open (req->env, req->buf1, req->uint1, req->int1); |
777 |
break; |
778 |
|
779 |
case REQ_ENV_CLOSE: |
780 |
req->result = req->env->close (req->env, req->uint1); |
781 |
break; |
782 |
|
783 |
case REQ_ENV_TXN_CHECKPOINT: |
784 |
req->result = req->env->txn_checkpoint (req->env, req->uint1, req->int1, req->uint2); |
785 |
break; |
786 |
|
787 |
case REQ_ENV_LOCK_DETECT: |
788 |
req->result = req->env->lock_detect (req->env, req->uint1, req->uint2, &req->int1); |
789 |
break; |
790 |
|
791 |
case REQ_ENV_MEMP_SYNC: |
792 |
req->result = req->env->memp_sync (req->env, 0); |
793 |
break; |
794 |
|
795 |
case REQ_ENV_MEMP_TRICKLE: |
796 |
req->result = req->env->memp_trickle (req->env, req->int1, &req->int2); |
797 |
break; |
798 |
|
799 |
case REQ_ENV_DBREMOVE: |
800 |
req->result = req->env->dbremove (req->env, req->txn, req->buf1, req->buf2, req->uint1); |
801 |
break; |
802 |
|
803 |
case REQ_ENV_DBRENAME: |
804 |
req->result = req->env->dbrename (req->env, req->txn, req->buf1, req->buf2, req->buf3, req->uint1); |
805 |
break; |
806 |
|
807 |
case REQ_DB_OPEN: |
808 |
req->result = req->db->open (req->db, req->txn, req->buf1, req->buf2, req->int1, req->uint1, req->int2); |
809 |
break; |
810 |
|
811 |
case REQ_DB_CLOSE: |
812 |
req->result = req->db->close (req->db, req->uint1); |
813 |
break; |
814 |
|
815 |
#if DB_VERSION_MINOR >= 4 |
816 |
case REQ_DB_COMPACT: |
817 |
req->result = req->db->compact (req->db, req->txn, &req->dbt1, &req->dbt2, 0, req->uint1, 0); |
818 |
break; |
819 |
#endif |
820 |
|
821 |
case REQ_DB_SYNC: |
822 |
req->result = req->db->sync (req->db, req->uint1); |
823 |
break; |
824 |
|
825 |
case REQ_DB_UPGRADE: |
826 |
req->result = req->db->upgrade (req->db, req->buf1, req->uint1); |
827 |
break; |
828 |
|
829 |
case REQ_DB_PUT: |
830 |
req->result = req->db->put (req->db, req->txn, &req->dbt1, &req->dbt2, req->uint1); |
831 |
break; |
832 |
|
833 |
#if DB_VERSION_MINOR >= 6 |
834 |
case REQ_DB_EXISTS: |
835 |
req->result = req->db->exists (req->db, req->txn, &req->dbt1, req->uint1); |
836 |
break; |
837 |
#endif |
838 |
case REQ_DB_GET: |
839 |
req->result = req->db->get (req->db, req->txn, &req->dbt1, &req->dbt3, req->uint1); |
840 |
break; |
841 |
|
842 |
case REQ_DB_PGET: |
843 |
req->result = req->db->pget (req->db, req->txn, &req->dbt1, &req->dbt2, &req->dbt3, req->uint1); |
844 |
break; |
845 |
|
846 |
case REQ_DB_DEL: |
847 |
req->result = req->db->del (req->db, req->txn, &req->dbt1, req->uint1); |
848 |
break; |
849 |
|
850 |
case REQ_DB_KEY_RANGE: |
851 |
req->result = req->db->key_range (req->db, req->txn, &req->dbt1, &req->key_range, req->uint1); |
852 |
break; |
853 |
|
854 |
case REQ_TXN_COMMIT: |
855 |
req->result = req->txn->commit (req->txn, req->uint1); |
856 |
break; |
857 |
|
858 |
case REQ_TXN_ABORT: |
859 |
req->result = req->txn->abort (req->txn); |
860 |
break; |
861 |
|
862 |
case REQ_TXN_FINISH: |
863 |
if (req->txn->flags & TXN_DEADLOCK) |
864 |
{ |
865 |
req->result = req->txn->abort (req->txn); |
866 |
if (!req->result) |
867 |
req->result = DB_LOCK_DEADLOCK; |
868 |
} |
869 |
else |
870 |
req->result = req->txn->commit (req->txn, req->uint1); |
871 |
break; |
872 |
|
873 |
case REQ_C_CLOSE: |
874 |
req->result = req->dbc->c_close (req->dbc); |
875 |
break; |
876 |
|
877 |
case REQ_C_COUNT: |
878 |
{ |
879 |
db_recno_t recno; |
880 |
req->result = req->dbc->c_count (req->dbc, &recno, req->uint1); |
881 |
req->uv1 = recno; |
882 |
} |
883 |
break; |
884 |
|
885 |
case REQ_C_PUT: |
886 |
req->result = req->dbc->c_put (req->dbc, &req->dbt1, &req->dbt2, req->uint1); |
887 |
break; |
888 |
|
889 |
case REQ_C_GET: |
890 |
req->result = req->dbc->c_get (req->dbc, &req->dbt1, &req->dbt3, req->uint1); |
891 |
break; |
892 |
|
893 |
case REQ_C_PGET: |
894 |
req->result = req->dbc->c_pget (req->dbc, &req->dbt1, &req->dbt2, &req->dbt3, req->uint1); |
895 |
break; |
896 |
|
897 |
case REQ_C_DEL: |
898 |
req->result = req->dbc->c_del (req->dbc, req->uint1); |
899 |
break; |
900 |
|
901 |
#if DB_VERSION_MINOR >= 3 |
902 |
case REQ_SEQ_OPEN: |
903 |
req->result = req->seq->open (req->seq, req->txn, &req->dbt1, req->uint1); |
904 |
break; |
905 |
|
906 |
case REQ_SEQ_CLOSE: |
907 |
req->result = req->seq->close (req->seq, req->uint1); |
908 |
break; |
909 |
|
910 |
case REQ_SEQ_GET: |
911 |
req->result = req->seq->get (req->seq, req->txn, req->int1, &req->seq_t, req->uint1); |
912 |
break; |
913 |
|
914 |
case REQ_SEQ_REMOVE: |
915 |
req->result = req->seq->remove (req->seq, req->txn, req->uint1); |
916 |
break; |
917 |
#endif |
918 |
|
919 |
default: |
920 |
req->result = ENOSYS; |
921 |
break; |
922 |
} |
923 |
|
924 |
if (req->txn && (req->result > 0 || req->result == DB_LOCK_NOTGRANTED)) |
925 |
req->txn->flags |= TXN_DEADLOCK; |
926 |
|
927 |
X_LOCK (reslock); |
928 |
|
929 |
++npending; |
930 |
|
931 |
if (!reqq_push (&res_queue, req)) |
932 |
/* write a dummy byte to the pipe so fh becomes ready */ |
933 |
respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1); |
934 |
|
935 |
self->req = 0; |
936 |
worker_clear (self); |
937 |
|
938 |
X_UNLOCK (reslock); |
939 |
} |
940 |
|
941 |
quit: |
942 |
X_LOCK (wrklock); |
943 |
worker_free (self); |
944 |
X_UNLOCK (wrklock); |
945 |
|
946 |
return 0; |
947 |
} |
948 |
|
949 |
/*****************************************************************************/ |
950 |
|
951 |
static void atfork_prepare (void) |
952 |
{ |
953 |
X_LOCK (wrklock); |
954 |
X_LOCK (reqlock); |
955 |
X_LOCK (reslock); |
956 |
} |
957 |
|
958 |
static void atfork_parent (void) |
959 |
{ |
960 |
X_UNLOCK (reslock); |
961 |
X_UNLOCK (reqlock); |
962 |
X_UNLOCK (wrklock); |
963 |
} |
964 |
|
965 |
static void atfork_child (void) |
966 |
{ |
967 |
bdb_req prv; |
968 |
|
969 |
while (prv = reqq_shift (&req_queue)) |
970 |
req_free (prv); |
971 |
|
972 |
while (prv = reqq_shift (&res_queue)) |
973 |
req_free (prv); |
974 |
|
975 |
while (wrk_first.next != &wrk_first) |
976 |
{ |
977 |
worker *wrk = wrk_first.next; |
978 |
|
979 |
if (wrk->req) |
980 |
req_free (wrk->req); |
981 |
|
982 |
worker_clear (wrk); |
983 |
worker_free (wrk); |
984 |
} |
985 |
|
986 |
started = 0; |
987 |
idle = 0; |
988 |
nreqs = 0; |
989 |
nready = 0; |
990 |
npending = 0; |
991 |
|
992 |
create_respipe (); |
993 |
|
994 |
atfork_parent (); |
995 |
} |
996 |
|
997 |
#define dREQ(reqtype,rsvcnt) \ |
998 |
bdb_req req; \ |
999 |
int req_pri = next_pri; \ |
1000 |
next_pri = DEFAULT_PRI + PRI_BIAS; \ |
1001 |
\ |
1002 |
if (callback && SvOK (callback)) \ |
1003 |
croak ("callback has illegal type or extra arguments"); \ |
1004 |
\ |
1005 |
Newz (0, req, 1, bdb_cb); \ |
1006 |
if (!req) \ |
1007 |
croak ("out of memory during bdb_req allocation"); \ |
1008 |
\ |
1009 |
req->callback = SvREFCNT_inc (cb); \ |
1010 |
req->type = (reqtype); \ |
1011 |
req->pri = req_pri; \ |
1012 |
if (rsvcnt >= 1) req->rsv1 = SvREFCNT_inc (ST (0)); \ |
1013 |
if (rsvcnt >= 2) req->rsv2 = SvREFCNT_inc (ST (1)); \ |
1014 |
(void)0; |
1015 |
|
1016 |
#define REQ_SEND \ |
1017 |
req_send (req) |
1018 |
|
1019 |
#define SvPTR(var, arg, type, class, nullok) \ |
1020 |
if (!SvOK (arg)) \ |
1021 |
{ \ |
1022 |
if (nullok != 1) \ |
1023 |
croak (# var " must be a " # class " object, not undef"); \ |
1024 |
\ |
1025 |
(var) = 0; \ |
1026 |
} \ |
1027 |
else if (sv_derived_from ((arg), # class)) \ |
1028 |
{ \ |
1029 |
IV tmp = SvIV ((SV*) SvRV (arg)); \ |
1030 |
(var) = INT2PTR (type, tmp); \ |
1031 |
if (!var && nullok != 2) \ |
1032 |
croak (# var " is not a valid " # class " object anymore"); \ |
1033 |
} \ |
1034 |
else \ |
1035 |
croak (# var " is not of type " # class); |
1036 |
|
1037 |
static void |
1038 |
ptr_nuke (SV *sv) |
1039 |
{ |
1040 |
assert (SvROK (sv)); |
1041 |
sv_setiv (SvRV (sv), 0); |
1042 |
} |
1043 |
|
1044 |
static int |
1045 |
errno_get (pTHX_ SV *sv, MAGIC *mg) |
1046 |
{ |
1047 |
if (*mg->mg_ptr == '!') // should always be the case |
1048 |
if (-30999 <= errno && errno <= -30800) |
1049 |
{ |
1050 |
sv_setnv (sv, (NV)errno); |
1051 |
sv_setpv (sv, db_strerror (errno)); |
1052 |
SvNOK_on (sv); /* what a wonderful hack! */ |
1053 |
// ^^^ copied from perl sources |
1054 |
return 0; |
1055 |
} |
1056 |
|
1057 |
return PL_vtbl_sv.svt_get (aTHX_ sv, mg); |
1058 |
} |
1059 |
|
1060 |
static MGVTBL vtbl_errno; |
1061 |
|
1062 |
// this wonderful hack :( patches perl's $! variable to support our errno values |
1063 |
static void |
1064 |
patch_errno (void) |
1065 |
{ |
1066 |
SV *sv; |
1067 |
MAGIC *mg; |
1068 |
|
1069 |
if (!(sv = get_sv ("!", 1))) |
1070 |
return; |
1071 |
|
1072 |
if (!(mg = mg_find (sv, PERL_MAGIC_sv))) |
1073 |
return; |
1074 |
|
1075 |
if (mg->mg_virtual != &PL_vtbl_sv) |
1076 |
return; |
1077 |
|
1078 |
vtbl_errno = PL_vtbl_sv; |
1079 |
vtbl_errno.svt_get = errno_get; |
1080 |
mg->mg_virtual = &vtbl_errno; |
1081 |
} |
1082 |
|
1083 |
#if __GNUC__ >= 4 |
1084 |
# define noinline __attribute__ ((noinline)) |
1085 |
#else |
1086 |
# define noinline |
1087 |
#endif |
1088 |
|
1089 |
static noinline SV * |
1090 |
pop_callback (I32 *ritems, SV *sv) |
1091 |
{ |
1092 |
if (SvROK (sv)) |
1093 |
{ |
1094 |
HV *st; |
1095 |
GV *gvp; |
1096 |
CV *cv; |
1097 |
const char *name; |
1098 |
|
1099 |
/* forgive me */ |
1100 |
if (SvTYPE (SvRV (sv)) == SVt_PVMG |
1101 |
&& (st = SvSTASH (SvRV (sv))) |
1102 |
&& (name = HvNAME_get (st)) |
1103 |
&& (name [0] == 'B' && name [1] == 'D' && name [2] == 'B' && name [3] == ':')) |
1104 |
return 0; |
1105 |
|
1106 |
if ((cv = sv_2cv (sv, &st, &gvp, 0))) |
1107 |
{ |
1108 |
--*ritems; |
1109 |
return (SV *)cv; |
1110 |
} |
1111 |
} |
1112 |
|
1113 |
return 0; |
1114 |
} |
1115 |
|
1116 |
/* stupid windoes defined CALLBACK as well */ |
1117 |
#undef CALLBACK |
1118 |
#define CALLBACK SV *cb = pop_callback (&items, ST (items - 1)); |
1119 |
|
1120 |
MODULE = BDB PACKAGE = BDB |
1121 |
|
1122 |
PROTOTYPES: ENABLE |
1123 |
|
1124 |
BOOT: |
1125 |
{ |
1126 |
HV *stash = gv_stashpv ("BDB", 1); |
1127 |
|
1128 |
static const struct { |
1129 |
const char *name; |
1130 |
IV iv; |
1131 |
} *civ, const_iv[] = { |
1132 |
#define const_iv(name) { # name, (IV)DB_ ## name }, |
1133 |
const_iv (RPCCLIENT) |
1134 |
const_iv (INIT_CDB) |
1135 |
const_iv (INIT_LOCK) |
1136 |
const_iv (INIT_LOG) |
1137 |
const_iv (INIT_MPOOL) |
1138 |
const_iv (INIT_REP) |
1139 |
const_iv (INIT_TXN) |
1140 |
const_iv (RECOVER) |
1141 |
const_iv (INIT_TXN) |
1142 |
const_iv (RECOVER_FATAL) |
1143 |
const_iv (CREATE) |
1144 |
const_iv (RDONLY) |
1145 |
const_iv (USE_ENVIRON) |
1146 |
const_iv (USE_ENVIRON_ROOT) |
1147 |
const_iv (LOCKDOWN) |
1148 |
const_iv (PRIVATE) |
1149 |
const_iv (SYSTEM_MEM) |
1150 |
const_iv (AUTO_COMMIT) |
1151 |
const_iv (CDB_ALLDB) |
1152 |
const_iv (DIRECT_DB) |
1153 |
const_iv (NOLOCKING) |
1154 |
const_iv (NOMMAP) |
1155 |
const_iv (NOPANIC) |
1156 |
const_iv (OVERWRITE) |
1157 |
const_iv (PANIC_ENVIRONMENT) |
1158 |
const_iv (REGION_INIT) |
1159 |
const_iv (TIME_NOTGRANTED) |
1160 |
const_iv (TXN_NOSYNC) |
1161 |
const_iv (TXN_NOT_DURABLE) |
1162 |
const_iv (TXN_WRITE_NOSYNC) |
1163 |
const_iv (WRITECURSOR) |
1164 |
const_iv (YIELDCPU) |
1165 |
const_iv (ENCRYPT_AES) |
1166 |
const_iv (XA_CREATE) |
1167 |
const_iv (BTREE) |
1168 |
const_iv (HASH) |
1169 |
const_iv (QUEUE) |
1170 |
const_iv (RECNO) |
1171 |
const_iv (UNKNOWN) |
1172 |
const_iv (EXCL) |
1173 |
const_iv (TRUNCATE) |
1174 |
const_iv (NOSYNC) |
1175 |
const_iv (CHKSUM) |
1176 |
const_iv (ENCRYPT) |
1177 |
const_iv (DUP) |
1178 |
const_iv (DUPSORT) |
1179 |
const_iv (RECNUM) |
1180 |
const_iv (RENUMBER) |
1181 |
const_iv (REVSPLITOFF) |
1182 |
const_iv (CONSUME) |
1183 |
const_iv (CONSUME_WAIT) |
1184 |
const_iv (GET_BOTH) |
1185 |
const_iv (GET_BOTH_RANGE) |
1186 |
//const_iv (SET_RECNO) |
1187 |
//const_iv (MULTIPLE) |
1188 |
const_iv (SNAPSHOT) |
1189 |
const_iv (JOIN_ITEM) |
1190 |
const_iv (JOIN_NOSORT) |
1191 |
const_iv (RMW) |
1192 |
|
1193 |
const_iv (NOTFOUND) |
1194 |
const_iv (KEYEMPTY) |
1195 |
const_iv (LOCK_DEADLOCK) |
1196 |
const_iv (LOCK_NOTGRANTED) |
1197 |
const_iv (RUNRECOVERY) |
1198 |
const_iv (OLD_VERSION) |
1199 |
const_iv (REP_HANDLE_DEAD) |
1200 |
const_iv (SECONDARY_BAD) |
1201 |
|
1202 |
const_iv (APPEND) |
1203 |
const_iv (NODUPDATA) |
1204 |
const_iv (NOOVERWRITE) |
1205 |
|
1206 |
const_iv (TXN_NOWAIT) |
1207 |
const_iv (TXN_SYNC) |
1208 |
|
1209 |
const_iv (SET_LOCK_TIMEOUT) |
1210 |
const_iv (SET_TXN_TIMEOUT) |
1211 |
|
1212 |
const_iv (FIRST) |
1213 |
const_iv (NEXT) |
1214 |
const_iv (NEXT_DUP) |
1215 |
const_iv (NEXT_NODUP) |
1216 |
const_iv (PREV) |
1217 |
const_iv (PREV_NODUP) |
1218 |
const_iv (SET) |
1219 |
const_iv (SET_RANGE) |
1220 |
const_iv (LAST) |
1221 |
const_iv (BEFORE) |
1222 |
const_iv (AFTER) |
1223 |
const_iv (CURRENT) |
1224 |
const_iv (KEYFIRST) |
1225 |
const_iv (KEYLAST) |
1226 |
const_iv (NODUPDATA) |
1227 |
|
1228 |
const_iv (FORCE) |
1229 |
|
1230 |
const_iv (LOCK_DEFAULT) |
1231 |
const_iv (LOCK_EXPIRE) |
1232 |
const_iv (LOCK_MAXLOCKS) |
1233 |
const_iv (LOCK_MINLOCKS) |
1234 |
const_iv (LOCK_MINWRITE) |
1235 |
const_iv (LOCK_OLDEST) |
1236 |
const_iv (LOCK_RANDOM) |
1237 |
const_iv (LOCK_YOUNGEST) |
1238 |
|
1239 |
const_iv (DONOTINDEX) |
1240 |
const_iv (KEYEMPTY ) |
1241 |
const_iv (KEYEXIST ) |
1242 |
const_iv (LOCK_DEADLOCK) |
1243 |
const_iv (LOCK_NOTGRANTED) |
1244 |
const_iv (NOSERVER) |
1245 |
const_iv (NOSERVER_HOME) |
1246 |
const_iv (NOSERVER_ID) |
1247 |
const_iv (NOTFOUND) |
1248 |
const_iv (PAGE_NOTFOUND) |
1249 |
const_iv (REP_DUPMASTER) |
1250 |
const_iv (REP_HANDLE_DEAD) |
1251 |
const_iv (REP_HOLDELECTION) |
1252 |
const_iv (REP_ISPERM) |
1253 |
const_iv (REP_NEWMASTER) |
1254 |
const_iv (REP_NEWSITE) |
1255 |
const_iv (REP_NOTPERM) |
1256 |
const_iv (REP_UNAVAIL) |
1257 |
const_iv (RUNRECOVERY) |
1258 |
const_iv (SECONDARY_BAD) |
1259 |
const_iv (VERIFY_BAD) |
1260 |
|
1261 |
const_iv (VERB_DEADLOCK) |
1262 |
const_iv (VERB_RECOVERY) |
1263 |
const_iv (VERB_REPLICATION) |
1264 |
const_iv (VERB_WAITSFOR) |
1265 |
|
1266 |
const_iv (VERSION_MAJOR) |
1267 |
const_iv (VERSION_MINOR) |
1268 |
const_iv (VERSION_PATCH) |
1269 |
#if DB_VERSION_MINOR >= 3 |
1270 |
const_iv (INORDER) |
1271 |
const_iv (LOCK_MAXWRITE) |
1272 |
const_iv (SEQ_DEC) |
1273 |
const_iv (SEQ_INC) |
1274 |
const_iv (SEQ_WRAP) |
1275 |
const_iv (BUFFER_SMALL) |
1276 |
const_iv (LOG_BUFFER_FULL) |
1277 |
const_iv (VERSION_MISMATCH) |
1278 |
#endif |
1279 |
#if DB_VERSION_MINOR >= 4 |
1280 |
const_iv (REGISTER) |
1281 |
const_iv (DSYNC_DB) |
1282 |
const_iv (READ_COMMITTED) |
1283 |
const_iv (READ_UNCOMMITTED) |
1284 |
const_iv (REP_IGNORE) |
1285 |
const_iv (REP_LOCKOUT) |
1286 |
const_iv (REP_JOIN_FAILURE) |
1287 |
const_iv (FREE_SPACE) |
1288 |
const_iv (FREELIST_ONLY) |
1289 |
const_iv (VERB_REGISTER) |
1290 |
#endif |
1291 |
#if DB_VERSION_MINOR >= 5 |
1292 |
const_iv (MULTIVERSION) |
1293 |
const_iv (TXN_SNAPSHOT) |
1294 |
#endif |
1295 |
#if DB_VERSION_MINOR >= 6 |
1296 |
const_iv (PREV_DUP) |
1297 |
const_iv (PRIORITY_UNCHANGED) |
1298 |
const_iv (PRIORITY_VERY_LOW) |
1299 |
const_iv (PRIORITY_LOW) |
1300 |
const_iv (PRIORITY_DEFAULT) |
1301 |
const_iv (PRIORITY_HIGH) |
1302 |
const_iv (PRIORITY_VERY_HIGH) |
1303 |
#endif |
1304 |
#if DB_VERSION_MINOR >= 7 |
1305 |
const_iv (LOG_DIRECT) |
1306 |
const_iv (LOG_DSYNC) |
1307 |
const_iv (LOG_AUTO_REMOVE) |
1308 |
const_iv (LOG_IN_MEMORY) |
1309 |
const_iv (LOG_ZERO) |
1310 |
#else |
1311 |
const_iv (DIRECT_LOG) |
1312 |
const_iv (LOG_AUTOREMOVE) |
1313 |
# if DB_VERSION_MINOR >= 3 |
1314 |
const_iv (DSYNC_LOG) |
1315 |
const_iv (LOG_INMEMORY) |
1316 |
# endif |
1317 |
#endif |
1318 |
}; |
1319 |
|
1320 |
for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
1321 |
newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
1322 |
|
1323 |
{ |
1324 |
/* we currently only allow version, minor-version and patchlevel to go up to 255 */ |
1325 |
char vstring[3] = { DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH }; |
1326 |
|
1327 |
newCONSTSUB (stash, "VERSION_v", newSVpvn (vstring, 3)); |
1328 |
} |
1329 |
|
1330 |
newCONSTSUB (stash, "VERSION_STRING", newSVpv (DB_VERSION_STRING, 0)); |
1331 |
|
1332 |
create_respipe (); |
1333 |
|
1334 |
X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child); |
1335 |
patch_errno (); |
1336 |
} |
1337 |
|
1338 |
void |
1339 |
max_poll_reqs (int nreqs) |
1340 |
PROTOTYPE: $ |
1341 |
CODE: |
1342 |
max_poll_reqs = nreqs; |
1343 |
|
1344 |
void |
1345 |
max_poll_time (double nseconds) |
1346 |
PROTOTYPE: $ |
1347 |
CODE: |
1348 |
max_poll_time = nseconds * AIO_TICKS; |
1349 |
|
1350 |
void |
1351 |
min_parallel (int nthreads) |
1352 |
PROTOTYPE: $ |
1353 |
|
1354 |
void |
1355 |
max_parallel (int nthreads) |
1356 |
PROTOTYPE: $ |
1357 |
|
1358 |
void |
1359 |
max_idle (int nthreads) |
1360 |
PROTOTYPE: $ |
1361 |
CODE: |
1362 |
set_max_idle (nthreads); |
1363 |
|
1364 |
int |
1365 |
max_outstanding (int maxreqs) |
1366 |
PROTOTYPE: $ |
1367 |
CODE: |
1368 |
RETVAL = max_outstanding; |
1369 |
max_outstanding = maxreqs; |
1370 |
OUTPUT: |
1371 |
RETVAL |
1372 |
|
1373 |
int |
1374 |
dbreq_pri (int pri = 0) |
1375 |
PROTOTYPE: ;$ |
1376 |
CODE: |
1377 |
RETVAL = next_pri - PRI_BIAS; |
1378 |
if (items > 0) |
1379 |
{ |
1380 |
if (pri < PRI_MIN) pri = PRI_MIN; |
1381 |
if (pri > PRI_MAX) pri = PRI_MAX; |
1382 |
next_pri = pri + PRI_BIAS; |
1383 |
} |
1384 |
OUTPUT: |
1385 |
RETVAL |
1386 |
|
1387 |
void |
1388 |
dbreq_nice (int nice = 0) |
1389 |
CODE: |
1390 |
nice = next_pri - nice; |
1391 |
if (nice < PRI_MIN) nice = PRI_MIN; |
1392 |
if (nice > PRI_MAX) nice = PRI_MAX; |
1393 |
next_pri = nice + PRI_BIAS; |
1394 |
|
1395 |
void |
1396 |
flush () |
1397 |
PROTOTYPE: |
1398 |
CODE: |
1399 |
while (nreqs) |
1400 |
{ |
1401 |
poll_wait (); |
1402 |
poll_cb (); |
1403 |
} |
1404 |
|
1405 |
int |
1406 |
poll () |
1407 |
PROTOTYPE: |
1408 |
CODE: |
1409 |
poll_wait (); |
1410 |
RETVAL = poll_cb (); |
1411 |
OUTPUT: |
1412 |
RETVAL |
1413 |
|
1414 |
int |
1415 |
poll_fileno () |
1416 |
PROTOTYPE: |
1417 |
CODE: |
1418 |
RETVAL = respipe [0]; |
1419 |
OUTPUT: |
1420 |
RETVAL |
1421 |
|
1422 |
int |
1423 |
poll_cb (...) |
1424 |
PROTOTYPE: |
1425 |
CODE: |
1426 |
RETVAL = poll_cb (); |
1427 |
OUTPUT: |
1428 |
RETVAL |
1429 |
|
1430 |
void |
1431 |
poll_wait () |
1432 |
PROTOTYPE: |
1433 |
CODE: |
1434 |
poll_wait (); |
1435 |
|
1436 |
int |
1437 |
nreqs () |
1438 |
PROTOTYPE: |
1439 |
CODE: |
1440 |
RETVAL = nreqs; |
1441 |
OUTPUT: |
1442 |
RETVAL |
1443 |
|
1444 |
int |
1445 |
nready () |
1446 |
PROTOTYPE: |
1447 |
CODE: |
1448 |
RETVAL = get_nready (); |
1449 |
OUTPUT: |
1450 |
RETVAL |
1451 |
|
1452 |
int |
1453 |
npending () |
1454 |
PROTOTYPE: |
1455 |
CODE: |
1456 |
RETVAL = get_npending (); |
1457 |
OUTPUT: |
1458 |
RETVAL |
1459 |
|
1460 |
int |
1461 |
nthreads () |
1462 |
PROTOTYPE: |
1463 |
CODE: |
1464 |
if (WORDACCESS_UNSAFE) X_LOCK (wrklock); |
1465 |
RETVAL = started; |
1466 |
if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock); |
1467 |
OUTPUT: |
1468 |
RETVAL |
1469 |
|
1470 |
void |
1471 |
set_sync_prepare (SV *cb) |
1472 |
PROTOTYPE: & |
1473 |
CODE: |
1474 |
SvREFCNT_dec (prepare_cb); |
1475 |
prepare_cb = newSVsv (cb); |
1476 |
|
1477 |
char * |
1478 |
strerror (int errorno = errno) |
1479 |
PROTOTYPE: ;$ |
1480 |
CODE: |
1481 |
RETVAL = db_strerror (errorno); |
1482 |
OUTPUT: |
1483 |
RETVAL |
1484 |
|
1485 |
void _on_next_submit (SV *cb) |
1486 |
CODE: |
1487 |
SvREFCNT_dec (on_next_submit); |
1488 |
on_next_submit = SvOK (cb) ? newSVsv (cb) : 0; |
1489 |
|
1490 |
DB_ENV * |
1491 |
db_env_create (U32 env_flags = 0) |
1492 |
CODE: |
1493 |
{ |
1494 |
errno = db_env_create (&RETVAL, env_flags); |
1495 |
if (errno) |
1496 |
croak ("db_env_create: %s", db_strerror (errno)); |
1497 |
|
1498 |
if (0) |
1499 |
{ |
1500 |
RETVAL->set_errcall (RETVAL, debug_errcall); |
1501 |
RETVAL->set_msgcall (RETVAL, debug_msgcall); |
1502 |
} |
1503 |
} |
1504 |
OUTPUT: |
1505 |
RETVAL |
1506 |
|
1507 |
void |
1508 |
db_env_open (DB_ENV *env, bdb_filename db_home, U32 open_flags, int mode, SV *callback = 0) |
1509 |
PREINIT: |
1510 |
CALLBACK |
1511 |
CODE: |
1512 |
{ |
1513 |
dREQ (REQ_ENV_OPEN, 1); |
1514 |
req->env = env; |
1515 |
req->uint1 = open_flags | DB_THREAD; |
1516 |
req->int1 = mode; |
1517 |
req->buf1 = strdup_ornull (db_home); |
1518 |
REQ_SEND; |
1519 |
} |
1520 |
|
1521 |
void |
1522 |
db_env_close (DB_ENV *env, U32 flags = 0, SV *callback = 0) |
1523 |
PREINIT: |
1524 |
CALLBACK |
1525 |
CODE: |
1526 |
{ |
1527 |
dREQ (REQ_ENV_CLOSE, 1); |
1528 |
req->env = env; |
1529 |
req->uint1 = flags; |
1530 |
REQ_SEND; |
1531 |
ptr_nuke (ST (0)); |
1532 |
} |
1533 |
|
1534 |
void |
1535 |
db_env_txn_checkpoint (DB_ENV *env, U32 kbyte = 0, U32 min = 0, U32 flags = 0, SV *callback = 0) |
1536 |
PREINIT: |
1537 |
CALLBACK |
1538 |
CODE: |
1539 |
{ |
1540 |
dREQ (REQ_ENV_TXN_CHECKPOINT, 1); |
1541 |
req->env = env; |
1542 |
req->uint1 = kbyte; |
1543 |
req->int1 = min; |
1544 |
req->uint2 = flags; |
1545 |
REQ_SEND; |
1546 |
} |
1547 |
|
1548 |
void |
1549 |
db_env_lock_detect (DB_ENV *env, U32 flags = 0, U32 atype = DB_LOCK_DEFAULT, SV *dummy = 0, SV *callback = 0) |
1550 |
PREINIT: |
1551 |
CALLBACK |
1552 |
CODE: |
1553 |
{ |
1554 |
dREQ (REQ_ENV_LOCK_DETECT, 1); |
1555 |
req->env = env; |
1556 |
req->uint1 = flags; |
1557 |
req->uint2 = atype; |
1558 |
/* req->int2 = 0; dummy */ |
1559 |
REQ_SEND; |
1560 |
} |
1561 |
|
1562 |
void |
1563 |
db_env_memp_sync (DB_ENV *env, SV *dummy = 0, SV *callback = 0) |
1564 |
PREINIT: |
1565 |
CALLBACK |
1566 |
CODE: |
1567 |
{ |
1568 |
dREQ (REQ_ENV_MEMP_SYNC, 1); |
1569 |
req->env = env; |
1570 |
REQ_SEND; |
1571 |
} |
1572 |
|
1573 |
void |
1574 |
db_env_memp_trickle (DB_ENV *env, int percent, SV *dummy = 0, SV *callback = 0) |
1575 |
PREINIT: |
1576 |
CALLBACK |
1577 |
CODE: |
1578 |
{ |
1579 |
dREQ (REQ_ENV_MEMP_TRICKLE, 1); |
1580 |
req->env = env; |
1581 |
req->int1 = percent; |
1582 |
REQ_SEND; |
1583 |
} |
1584 |
|
1585 |
void |
1586 |
db_env_dbremove (DB_ENV *env, DB_TXN_ornull *txnid, bdb_filename file, bdb_filename database, U32 flags = 0, SV *callback = 0) |
1587 |
PREINIT: |
1588 |
CALLBACK |
1589 |
CODE: |
1590 |
{ |
1591 |
dREQ (REQ_ENV_DBREMOVE, 2); |
1592 |
req->env = env; |
1593 |
req->buf1 = strdup_ornull (file); |
1594 |
req->buf2 = strdup_ornull (database); |
1595 |
req->uint1 = flags; |
1596 |
REQ_SEND; |
1597 |
} |
1598 |
|
1599 |
void |
1600 |
db_env_dbrename (DB_ENV *env, DB_TXN_ornull *txnid, bdb_filename file, bdb_filename database, bdb_filename newname, U32 flags = 0, SV *callback = 0) |
1601 |
PREINIT: |
1602 |
CALLBACK |
1603 |
CODE: |
1604 |
{ |
1605 |
dREQ (REQ_ENV_DBRENAME, 2); |
1606 |
req->env = env; |
1607 |
req->buf1 = strdup_ornull (file); |
1608 |
req->buf2 = strdup_ornull (database); |
1609 |
req->buf3 = strdup_ornull (newname); |
1610 |
req->uint1 = flags; |
1611 |
REQ_SEND; |
1612 |
} |
1613 |
|
1614 |
DB * |
1615 |
db_create (DB_ENV *env = 0, U32 flags = 0) |
1616 |
CODE: |
1617 |
{ |
1618 |
errno = db_create (&RETVAL, env, flags); |
1619 |
if (errno) |
1620 |
croak ("db_create: %s", db_strerror (errno)); |
1621 |
|
1622 |
if (RETVAL) |
1623 |
RETVAL->app_private = (void *)newSVsv (ST (0)); |
1624 |
} |
1625 |
OUTPUT: |
1626 |
RETVAL |
1627 |
|
1628 |
void |
1629 |
db_open (DB *db, DB_TXN_ornull *txnid, bdb_filename file, bdb_filename database, int type, U32 flags, int mode, SV *callback = 0) |
1630 |
PREINIT: |
1631 |
CALLBACK |
1632 |
CODE: |
1633 |
{ |
1634 |
dREQ (REQ_DB_OPEN, 2); |
1635 |
req->db = db; |
1636 |
req->txn = txnid; |
1637 |
req->buf1 = strdup_ornull (file); |
1638 |
req->buf2 = strdup_ornull (database); |
1639 |
req->int1 = type; |
1640 |
req->uint1 = flags | DB_THREAD; |
1641 |
req->int2 = mode; |
1642 |
REQ_SEND; |
1643 |
} |
1644 |
|
1645 |
void |
1646 |
db_close (DB *db, U32 flags = 0, SV *callback = 0) |
1647 |
PREINIT: |
1648 |
CALLBACK |
1649 |
CODE: |
1650 |
{ |
1651 |
dREQ (REQ_DB_CLOSE, 1); |
1652 |
req->db = db; |
1653 |
req->uint1 = flags; |
1654 |
req->sv1 = (SV *)db->app_private; |
1655 |
REQ_SEND; |
1656 |
ptr_nuke (ST (0)); |
1657 |
} |
1658 |
|
1659 |
#if DB_VERSION_MINOR >= 4 |
1660 |
|
1661 |
void |
1662 |
db_compact (DB *db, DB_TXN_ornull *txn = 0, SV *start = 0, SV *stop = 0, SV *unused1 = 0, U32 flags = DB_FREE_SPACE, SV *unused2 = 0, SV *callback = 0) |
1663 |
PREINIT: |
1664 |
CALLBACK |
1665 |
CODE: |
1666 |
{ |
1667 |
dREQ (REQ_DB_COMPACT, 2); |
1668 |
req->db = db; |
1669 |
req->txn = txn; |
1670 |
sv_to_dbt (&req->dbt1, start); |
1671 |
sv_to_dbt (&req->dbt2, stop); |
1672 |
req->uint1 = flags; |
1673 |
REQ_SEND; |
1674 |
} |
1675 |
|
1676 |
#endif |
1677 |
|
1678 |
void |
1679 |
db_sync (DB *db, U32 flags = 0, SV *callback = 0) |
1680 |
PREINIT: |
1681 |
CALLBACK |
1682 |
CODE: |
1683 |
{ |
1684 |
dREQ (REQ_DB_SYNC, 1); |
1685 |
req->db = db; |
1686 |
req->uint1 = flags; |
1687 |
REQ_SEND; |
1688 |
} |
1689 |
|
1690 |
void |
1691 |
db_upgrade (DB *db, bdb_filename file, U32 flags = 0, SV *callback = 0) |
1692 |
PREINIT: |
1693 |
CALLBACK |
1694 |
CODE: |
1695 |
{ |
1696 |
dREQ (REQ_DB_SYNC, 1); |
1697 |
req->db = db; |
1698 |
req->buf1 = strdup (file); |
1699 |
req->uint1 = flags; |
1700 |
REQ_SEND; |
1701 |
} |
1702 |
|
1703 |
void |
1704 |
db_key_range (DB *db, DB_TXN_ornull *txn, SV *key, SV *key_range, U32 flags = 0, SV *callback = 0) |
1705 |
PREINIT: |
1706 |
CALLBACK |
1707 |
CODE: |
1708 |
{ |
1709 |
dREQ (REQ_DB_KEY_RANGE, 2); |
1710 |
req->db = db; |
1711 |
req->txn = txn; |
1712 |
sv_to_dbt (&req->dbt1, key); |
1713 |
req->uint1 = flags; |
1714 |
req->sv1 = SvREFCNT_inc (key_range); SvREADONLY_on (key_range); |
1715 |
REQ_SEND; |
1716 |
} |
1717 |
|
1718 |
void |
1719 |
db_put (DB *db, DB_TXN_ornull *txn, SV *key, SV *data, U32 flags = 0, SV *callback = 0) |
1720 |
PREINIT: |
1721 |
CALLBACK |
1722 |
CODE: |
1723 |
{ |
1724 |
dREQ (REQ_DB_PUT, 2); |
1725 |
req->db = db; |
1726 |
req->txn = txn; |
1727 |
sv_to_dbt (&req->dbt1, key); |
1728 |
sv_to_dbt (&req->dbt2, data); |
1729 |
req->uint1 = flags; |
1730 |
REQ_SEND; |
1731 |
} |
1732 |
|
1733 |
#if DB_VERSION_MINOR >= 6 |
1734 |
|
1735 |
void |
1736 |
db_exists (DB *db, DB_TXN_ornull *txn, SV *key, U32 flags = 0, SV *callback = 0) |
1737 |
PREINIT: |
1738 |
CALLBACK |
1739 |
CODE: |
1740 |
{ |
1741 |
dREQ (REQ_DB_EXISTS, 2); |
1742 |
req->db = db; |
1743 |
req->txn = txn; |
1744 |
req->uint1 = flags; |
1745 |
sv_to_dbt (&req->dbt1, key); |
1746 |
REQ_SEND; |
1747 |
} |
1748 |
|
1749 |
#endif |
1750 |
|
1751 |
void |
1752 |
db_get (DB *db, DB_TXN_ornull *txn, SV *key, SV *data, U32 flags = 0, SV *callback = 0) |
1753 |
PREINIT: |
1754 |
CALLBACK |
1755 |
CODE: |
1756 |
if (SvREADONLY (data)) |
1757 |
croak ("can't modify read-only data scalar in db_get"); |
1758 |
{ |
1759 |
dREQ (REQ_DB_GET, 2); |
1760 |
req->db = db; |
1761 |
req->txn = txn; |
1762 |
req->uint1 = flags; |
1763 |
sv_to_dbt (&req->dbt1, key); |
1764 |
req->dbt3.flags = DB_DBT_MALLOC; |
1765 |
req->sv3 = SvREFCNT_inc (data); SvREADONLY_on (data); |
1766 |
REQ_SEND; |
1767 |
} |
1768 |
|
1769 |
void |
1770 |
db_pget (DB *db, DB_TXN_ornull *txn, SV *key, SV *pkey, SV *data, U32 flags = 0, SV *callback = 0) |
1771 |
PREINIT: |
1772 |
CALLBACK |
1773 |
CODE: |
1774 |
if (SvREADONLY (data)) |
1775 |
croak ("can't modify read-only data scalar in db_pget"); |
1776 |
{ |
1777 |
dREQ (REQ_DB_PGET, 2); |
1778 |
req->db = db; |
1779 |
req->txn = txn; |
1780 |
req->uint1 = flags; |
1781 |
sv_to_dbt (&req->dbt1, key); |
1782 |
sv_to_dbt (&req->dbt2, pkey); |
1783 |
req->dbt3.flags = DB_DBT_MALLOC; |
1784 |
req->sv3 = SvREFCNT_inc (data); SvREADONLY_on (data); |
1785 |
REQ_SEND; |
1786 |
} |
1787 |
|
1788 |
void |
1789 |
db_del (DB *db, DB_TXN_ornull *txn, SV *key, U32 flags = 0, SV *callback = 0) |
1790 |
PREINIT: |
1791 |
CALLBACK |
1792 |
CODE: |
1793 |
{ |
1794 |
dREQ (REQ_DB_DEL, 2); |
1795 |
req->db = db; |
1796 |
req->txn = txn; |
1797 |
req->uint1 = flags; |
1798 |
sv_to_dbt (&req->dbt1, key); |
1799 |
REQ_SEND; |
1800 |
} |
1801 |
|
1802 |
void |
1803 |
db_txn_commit (DB_TXN *txn, U32 flags = 0, SV *callback = 0) |
1804 |
PREINIT: |
1805 |
CALLBACK |
1806 |
CODE: |
1807 |
{ |
1808 |
dREQ (REQ_TXN_COMMIT, 1); |
1809 |
req->txn = txn; |
1810 |
req->uint1 = flags; |
1811 |
REQ_SEND; |
1812 |
ptr_nuke (ST (0)); |
1813 |
} |
1814 |
|
1815 |
void |
1816 |
db_txn_abort (DB_TXN *txn, SV *callback = 0) |
1817 |
PREINIT: |
1818 |
CALLBACK |
1819 |
CODE: |
1820 |
{ |
1821 |
dREQ (REQ_TXN_ABORT, 1); |
1822 |
req->txn = txn; |
1823 |
REQ_SEND; |
1824 |
ptr_nuke (ST (0)); |
1825 |
} |
1826 |
|
1827 |
void |
1828 |
db_txn_finish (DB_TXN *txn, U32 flags = 0, SV *callback = 0) |
1829 |
PREINIT: |
1830 |
CALLBACK |
1831 |
CODE: |
1832 |
{ |
1833 |
dREQ (REQ_TXN_FINISH, 1); |
1834 |
req->txn = txn; |
1835 |
req->uint1 = flags; |
1836 |
REQ_SEND; |
1837 |
ptr_nuke (ST (0)); |
1838 |
} |
1839 |
|
1840 |
void |
1841 |
db_c_close (DBC *dbc, SV *callback = 0) |
1842 |
PREINIT: |
1843 |
CALLBACK |
1844 |
CODE: |
1845 |
{ |
1846 |
dREQ (REQ_C_CLOSE, 1); |
1847 |
req->dbc = dbc; |
1848 |
REQ_SEND; |
1849 |
ptr_nuke (ST (0)); |
1850 |
} |
1851 |
|
1852 |
void |
1853 |
db_c_count (DBC *dbc, SV *count, U32 flags = 0, SV *callback = 0) |
1854 |
PREINIT: |
1855 |
CALLBACK |
1856 |
CODE: |
1857 |
{ |
1858 |
dREQ (REQ_C_COUNT, 1); |
1859 |
req->dbc = dbc; |
1860 |
req->sv1 = SvREFCNT_inc (count); |
1861 |
REQ_SEND; |
1862 |
} |
1863 |
|
1864 |
void |
1865 |
db_c_put (DBC *dbc, SV *key, SV *data, U32 flags = 0, SV *callback = 0) |
1866 |
PREINIT: |
1867 |
CALLBACK |
1868 |
CODE: |
1869 |
{ |
1870 |
dREQ (REQ_C_PUT, 1); |
1871 |
req->dbc = dbc; |
1872 |
sv_to_dbt (&req->dbt1, key); |
1873 |
sv_to_dbt (&req->dbt2, data); |
1874 |
req->uint1 = flags; |
1875 |
REQ_SEND; |
1876 |
} |
1877 |
|
1878 |
void |
1879 |
db_c_get (DBC *dbc, SV *key, SV *data, U32 flags = 0, SV *callback = 0) |
1880 |
PREINIT: |
1881 |
CALLBACK |
1882 |
CODE: |
1883 |
{ |
1884 |
dREQ (REQ_C_GET, 1); |
1885 |
req->dbc = dbc; |
1886 |
req->uint1 = flags; |
1887 |
if ((flags & DB_SET) == DB_SET |
1888 |
|| (flags & DB_SET_RANGE) == DB_SET_RANGE) |
1889 |
sv_to_dbt (&req->dbt1, key); |
1890 |
else |
1891 |
req->dbt1.flags = DB_DBT_MALLOC; |
1892 |
|
1893 |
req->sv1 = SvREFCNT_inc (key); SvREADONLY_on (key); |
1894 |
|
1895 |
if ((flags & DB_GET_BOTH) == DB_GET_BOTH |
1896 |
|| (flags & DB_GET_BOTH_RANGE) == DB_GET_BOTH_RANGE) |
1897 |
sv_to_dbt (&req->dbt3, data); |
1898 |
else |
1899 |
req->dbt3.flags = DB_DBT_MALLOC; |
1900 |
|
1901 |
req->sv3 = SvREFCNT_inc (data); SvREADONLY_on (data); |
1902 |
REQ_SEND; |
1903 |
} |
1904 |
|
1905 |
void |
1906 |
db_c_pget (DBC *dbc, SV *key, SV *pkey, SV *data, U32 flags = 0, SV *callback = 0) |
1907 |
PREINIT: |
1908 |
CALLBACK |
1909 |
CODE: |
1910 |
{ |
1911 |
dREQ (REQ_C_PGET, 1); |
1912 |
req->dbc = dbc; |
1913 |
req->uint1 = flags; |
1914 |
if ((flags & DB_SET) == DB_SET |
1915 |
|| (flags & DB_SET_RANGE) == DB_SET_RANGE) |
1916 |
sv_to_dbt (&req->dbt1, key); |
1917 |
else |
1918 |
req->dbt1.flags = DB_DBT_MALLOC; |
1919 |
|
1920 |
req->sv1 = SvREFCNT_inc (key); SvREADONLY_on (key); |
1921 |
|
1922 |
req->dbt2.flags = DB_DBT_MALLOC; |
1923 |
req->sv2 = SvREFCNT_inc (pkey); SvREADONLY_on (pkey); |
1924 |
|
1925 |
if ((flags & DB_GET_BOTH) == DB_GET_BOTH |
1926 |
|| (flags & DB_GET_BOTH_RANGE) == DB_GET_BOTH_RANGE) |
1927 |
sv_to_dbt (&req->dbt3, data); |
1928 |
else |
1929 |
req->dbt3.flags = DB_DBT_MALLOC; |
1930 |
|
1931 |
req->sv3 = SvREFCNT_inc (data); SvREADONLY_on (data); |
1932 |
REQ_SEND; |
1933 |
} |
1934 |
|
1935 |
void |
1936 |
db_c_del (DBC *dbc, U32 flags = 0, SV *callback = 0) |
1937 |
PREINIT: |
1938 |
CALLBACK |
1939 |
CODE: |
1940 |
{ |
1941 |
dREQ (REQ_C_DEL, 1); |
1942 |
req->dbc = dbc; |
1943 |
req->uint1 = flags; |
1944 |
REQ_SEND; |
1945 |
} |
1946 |
|
1947 |
|
1948 |
#if DB_VERSION_MINOR >= 3 |
1949 |
|
1950 |
void |
1951 |
db_sequence_open (DB_SEQUENCE *seq, DB_TXN_ornull *txnid, SV *key, U32 flags = 0, SV *callback = 0) |
1952 |
PREINIT: |
1953 |
CALLBACK |
1954 |
CODE: |
1955 |
{ |
1956 |
dREQ (REQ_SEQ_OPEN, 2); |
1957 |
req->seq = seq; |
1958 |
req->txn = txnid; |
1959 |
req->uint1 = flags | DB_THREAD; |
1960 |
sv_to_dbt (&req->dbt1, key); |
1961 |
REQ_SEND; |
1962 |
} |
1963 |
|
1964 |
void |
1965 |
db_sequence_close (DB_SEQUENCE *seq, U32 flags = 0, SV *callback = 0) |
1966 |
PREINIT: |
1967 |
CALLBACK |
1968 |
CODE: |
1969 |
{ |
1970 |
dREQ (REQ_SEQ_CLOSE, 1); |
1971 |
req->seq = seq; |
1972 |
req->uint1 = flags; |
1973 |
REQ_SEND; |
1974 |
ptr_nuke (ST (0)); |
1975 |
} |
1976 |
|
1977 |
void |
1978 |
db_sequence_get (DB_SEQUENCE *seq, DB_TXN_ornull *txnid, int delta, SV *seq_value, U32 flags = DB_TXN_NOSYNC, SV *callback = 0) |
1979 |
PREINIT: |
1980 |
CALLBACK |
1981 |
CODE: |
1982 |
{ |
1983 |
dREQ (REQ_SEQ_GET, 2); |
1984 |
req->seq = seq; |
1985 |
req->txn = txnid; |
1986 |
req->int1 = delta; |
1987 |
req->uint1 = flags; |
1988 |
req->sv1 = SvREFCNT_inc (seq_value); SvREADONLY_on (seq_value); |
1989 |
REQ_SEND; |
1990 |
} |
1991 |
|
1992 |
void |
1993 |
db_sequence_remove (DB_SEQUENCE *seq, DB_TXN_ornull *txnid = 0, U32 flags = 0, SV *callback = 0) |
1994 |
PREINIT: |
1995 |
CALLBACK |
1996 |
CODE: |
1997 |
{ |
1998 |
dREQ (REQ_SEQ_REMOVE, 2); |
1999 |
req->seq = seq; |
2000 |
req->txn = txnid; |
2001 |
req->uint1 = flags; |
2002 |
REQ_SEND; |
2003 |
} |
2004 |
|
2005 |
#endif |
2006 |
|
2007 |
|
2008 |
MODULE = BDB PACKAGE = BDB::Env |
2009 |
|
2010 |
void |
2011 |
DESTROY (DB_ENV_ornuked *env) |
2012 |
CODE: |
2013 |
if (env) |
2014 |
env->close (env, 0); |
2015 |
|
2016 |
int set_data_dir (DB_ENV *env, const char *dir) |
2017 |
CODE: |
2018 |
RETVAL = env->set_data_dir (env, dir); |
2019 |
OUTPUT: |
2020 |
RETVAL |
2021 |
|
2022 |
int set_tmp_dir (DB_ENV *env, const char *dir) |
2023 |
CODE: |
2024 |
RETVAL = env->set_tmp_dir (env, dir); |
2025 |
OUTPUT: |
2026 |
RETVAL |
2027 |
|
2028 |
int set_lg_dir (DB_ENV *env, const char *dir) |
2029 |
CODE: |
2030 |
RETVAL = env->set_lg_dir (env, dir); |
2031 |
OUTPUT: |
2032 |
RETVAL |
2033 |
|
2034 |
int set_shm_key (DB_ENV *env, long shm_key) |
2035 |
CODE: |
2036 |
RETVAL = env->set_shm_key (env, shm_key); |
2037 |
OUTPUT: |
2038 |
RETVAL |
2039 |
|
2040 |
int set_cachesize (DB_ENV *env, U32 gbytes, U32 bytes, int ncache = 0) |
2041 |
CODE: |
2042 |
RETVAL = env->set_cachesize (env, gbytes, bytes, ncache); |
2043 |
OUTPUT: |
2044 |
RETVAL |
2045 |
|
2046 |
int set_flags (DB_ENV *env, U32 flags, int onoff = 1) |
2047 |
CODE: |
2048 |
RETVAL = env->set_flags (env, flags, onoff); |
2049 |
OUTPUT: |
2050 |
RETVAL |
2051 |
|
2052 |
#if DB_VERSION_MINOR >= 7 |
2053 |
|
2054 |
int set_intermediate_dir_mode (DB_ENV *env, const char *mode) |
2055 |
CODE: |
2056 |
RETVAL = env->set_intermediate_dir_mode (env, mode); |
2057 |
OUTPUT: |
2058 |
RETVAL |
2059 |
|
2060 |
int log_set_config (DB_ENV *env, U32 flags, int onoff = 1) |
2061 |
CODE: |
2062 |
RETVAL = env->log_set_config (env, flags, onoff); |
2063 |
OUTPUT: |
2064 |
RETVAL |
2065 |
|
2066 |
#endif |
2067 |
|
2068 |
|
2069 |
void set_errfile (DB_ENV *env, FILE *errfile = 0) |
2070 |
CODE: |
2071 |
env->set_errfile (env, errfile); |
2072 |
|
2073 |
void set_msgfile (DB_ENV *env, FILE *msgfile = 0) |
2074 |
CODE: |
2075 |
env->set_msgfile (env, msgfile); |
2076 |
|
2077 |
int set_verbose (DB_ENV *env, U32 which = -1, int onoff = 1) |
2078 |
CODE: |
2079 |
RETVAL = env->set_verbose (env, which, onoff); |
2080 |
OUTPUT: |
2081 |
RETVAL |
2082 |
|
2083 |
int set_encrypt (DB_ENV *env, const char *password, U32 flags = 0) |
2084 |
CODE: |
2085 |
RETVAL = env->set_encrypt (env, password, flags); |
2086 |
OUTPUT: |
2087 |
RETVAL |
2088 |
|
2089 |
int set_timeout (DB_ENV *env, NV timeout, U32 flags = DB_SET_TXN_TIMEOUT) |
2090 |
CODE: |
2091 |
RETVAL = env->set_timeout (env, timeout * 1000000, flags); |
2092 |
OUTPUT: |
2093 |
RETVAL |
2094 |
|
2095 |
int set_mp_max_openfd (DB_ENV *env, int maxopenfd); |
2096 |
CODE: |
2097 |
RETVAL = env->set_mp_max_openfd (env, maxopenfd); |
2098 |
OUTPUT: |
2099 |
RETVAL |
2100 |
|
2101 |
int set_mp_max_write (DB_ENV *env, int maxwrite, int maxwrite_sleep); |
2102 |
CODE: |
2103 |
RETVAL = env->set_mp_max_write (env, maxwrite, maxwrite_sleep); |
2104 |
OUTPUT: |
2105 |
RETVAL |
2106 |
|
2107 |
int set_mp_mmapsize (DB_ENV *env, int mmapsize_mb) |
2108 |
CODE: |
2109 |
RETVAL = env->set_mp_mmapsize (env, ((size_t)mmapsize_mb) << 20); |
2110 |
OUTPUT: |
2111 |
RETVAL |
2112 |
|
2113 |
int set_lk_detect (DB_ENV *env, U32 detect = DB_LOCK_DEFAULT) |
2114 |
CODE: |
2115 |
RETVAL = env->set_lk_detect (env, detect); |
2116 |
OUTPUT: |
2117 |
RETVAL |
2118 |
|
2119 |
int set_lk_max_lockers (DB_ENV *env, U32 max) |
2120 |
CODE: |
2121 |
RETVAL = env->set_lk_max_lockers (env, max); |
2122 |
OUTPUT: |
2123 |
RETVAL |
2124 |
|
2125 |
int set_lk_max_locks (DB_ENV *env, U32 max) |
2126 |
CODE: |
2127 |
RETVAL = env->set_lk_max_locks (env, max); |
2128 |
OUTPUT: |
2129 |
RETVAL |
2130 |
|
2131 |
int set_lk_max_objects (DB_ENV *env, U32 max) |
2132 |
CODE: |
2133 |
RETVAL = env->set_lk_max_objects (env, max); |
2134 |
OUTPUT: |
2135 |
RETVAL |
2136 |
|
2137 |
int set_lg_bsize (DB_ENV *env, U32 max) |
2138 |
CODE: |
2139 |
RETVAL = env->set_lg_bsize (env, max); |
2140 |
OUTPUT: |
2141 |
RETVAL |
2142 |
|
2143 |
int set_lg_max (DB_ENV *env, U32 max) |
2144 |
CODE: |
2145 |
RETVAL = env->set_lg_max (env, max); |
2146 |
OUTPUT: |
2147 |
RETVAL |
2148 |
|
2149 |
#if DB_VERSION_MINOR >= 4 |
2150 |
|
2151 |
int mutex_set_max (DB_ENV *env, U32 max) |
2152 |
CODE: |
2153 |
RETVAL = env->mutex_set_max (env, max); |
2154 |
OUTPUT: |
2155 |
RETVAL |
2156 |
|
2157 |
int mutex_set_increment (DB_ENV *env, U32 increment) |
2158 |
CODE: |
2159 |
RETVAL = env->mutex_set_increment (env, increment); |
2160 |
OUTPUT: |
2161 |
RETVAL |
2162 |
|
2163 |
int mutex_set_tas_spins (DB_ENV *env, U32 tas_spins) |
2164 |
CODE: |
2165 |
RETVAL = env->mutex_set_tas_spins (env, tas_spins); |
2166 |
OUTPUT: |
2167 |
RETVAL |
2168 |
|
2169 |
int mutex_set_align (DB_ENV *env, U32 align) |
2170 |
CODE: |
2171 |
RETVAL = env->mutex_set_align (env, align); |
2172 |
OUTPUT: |
2173 |
RETVAL |
2174 |
|
2175 |
#endif |
2176 |
|
2177 |
DB_TXN * |
2178 |
txn_begin (DB_ENV *env, DB_TXN_ornull *parent = 0, U32 flags = 0) |
2179 |
CODE: |
2180 |
errno = env->txn_begin (env, parent, &RETVAL, flags); |
2181 |
if (errno) |
2182 |
croak ("DB_ENV->txn_begin: %s", db_strerror (errno)); |
2183 |
OUTPUT: |
2184 |
RETVAL |
2185 |
|
2186 |
#if DB_VERSION_MINOR >= 5 |
2187 |
|
2188 |
DB_TXN * |
2189 |
cdsgroup_begin (DB_ENV *env) |
2190 |
CODE: |
2191 |
errno = env->cdsgroup_begin (env, &RETVAL); |
2192 |
if (errno) |
2193 |
croak ("DB_ENV->cdsgroup_begin: %s", db_strerror (errno)); |
2194 |
OUTPUT: |
2195 |
RETVAL |
2196 |
|
2197 |
#endif |
2198 |
|
2199 |
MODULE = BDB PACKAGE = BDB::Db |
2200 |
|
2201 |
void |
2202 |
DESTROY (DB_ornuked *db) |
2203 |
CODE: |
2204 |
if (db) |
2205 |
{ |
2206 |
SV *env = (SV *)db->app_private; |
2207 |
db->close (db, 0); |
2208 |
SvREFCNT_dec (env); |
2209 |
} |
2210 |
|
2211 |
int set_cachesize (DB *db, U32 gbytes, U32 bytes, int ncache = 0) |
2212 |
CODE: |
2213 |
RETVAL = db->set_cachesize (db, gbytes, bytes, ncache); |
2214 |
OUTPUT: |
2215 |
RETVAL |
2216 |
|
2217 |
int set_flags (DB *db, U32 flags) |
2218 |
CODE: |
2219 |
RETVAL = db->set_flags (db, flags); |
2220 |
OUTPUT: |
2221 |
RETVAL |
2222 |
|
2223 |
int set_encrypt (DB *db, const char *password, U32 flags) |
2224 |
CODE: |
2225 |
RETVAL = db->set_encrypt (db, password, flags); |
2226 |
OUTPUT: |
2227 |
RETVAL |
2228 |
|
2229 |
int set_lorder (DB *db, int lorder) |
2230 |
CODE: |
2231 |
RETVAL = db->set_lorder (db, lorder); |
2232 |
OUTPUT: |
2233 |
RETVAL |
2234 |
|
2235 |
int set_bt_minkey (DB *db, U32 minkey) |
2236 |
CODE: |
2237 |
RETVAL = db->set_bt_minkey (db, minkey); |
2238 |
OUTPUT: |
2239 |
RETVAL |
2240 |
|
2241 |
int set_re_delim (DB *db, int delim) |
2242 |
CODE: |
2243 |
RETVAL = db->set_re_delim (db, delim); |
2244 |
OUTPUT: |
2245 |
RETVAL |
2246 |
|
2247 |
int set_re_pad (DB *db, int re_pad) |
2248 |
CODE: |
2249 |
RETVAL = db->set_re_pad (db, re_pad); |
2250 |
OUTPUT: |
2251 |
RETVAL |
2252 |
|
2253 |
int set_re_source (DB *db, char *source) |
2254 |
CODE: |
2255 |
RETVAL = db->set_re_source (db, source); |
2256 |
OUTPUT: |
2257 |
RETVAL |
2258 |
|
2259 |
int set_re_len (DB *db, U32 re_len) |
2260 |
CODE: |
2261 |
RETVAL = db->set_re_len (db, re_len); |
2262 |
OUTPUT: |
2263 |
RETVAL |
2264 |
|
2265 |
int set_h_ffactor (DB *db, U32 h_ffactor) |
2266 |
CODE: |
2267 |
RETVAL = db->set_h_ffactor (db, h_ffactor); |
2268 |
OUTPUT: |
2269 |
RETVAL |
2270 |
|
2271 |
int set_h_nelem (DB *db, U32 h_nelem) |
2272 |
CODE: |
2273 |
RETVAL = db->set_h_nelem (db, h_nelem); |
2274 |
OUTPUT: |
2275 |
RETVAL |
2276 |
|
2277 |
int set_q_extentsize (DB *db, U32 extentsize) |
2278 |
CODE: |
2279 |
RETVAL = db->set_q_extentsize (db, extentsize); |
2280 |
OUTPUT: |
2281 |
RETVAL |
2282 |
|
2283 |
DBC * |
2284 |
cursor (DB *db, DB_TXN_ornull *txn = 0, U32 flags = 0) |
2285 |
CODE: |
2286 |
errno = db->cursor (db, txn, &RETVAL, flags); |
2287 |
if (errno) |
2288 |
croak ("DB->cursor: %s", db_strerror (errno)); |
2289 |
OUTPUT: |
2290 |
RETVAL |
2291 |
|
2292 |
#if DB_VERSION_MINOR >= 3 |
2293 |
|
2294 |
DB_SEQUENCE * |
2295 |
sequence (DB *db, U32 flags = 0) |
2296 |
CODE: |
2297 |
{ |
2298 |
errno = db_sequence_create (&RETVAL, db, flags); |
2299 |
if (errno) |
2300 |
croak ("db_sequence_create: %s", db_strerror (errno)); |
2301 |
} |
2302 |
OUTPUT: |
2303 |
RETVAL |
2304 |
|
2305 |
#endif |
2306 |
|
2307 |
|
2308 |
MODULE = BDB PACKAGE = BDB::Txn |
2309 |
|
2310 |
void |
2311 |
DESTROY (DB_TXN_ornuked *txn) |
2312 |
CODE: |
2313 |
if (txn) |
2314 |
txn->abort (txn); |
2315 |
|
2316 |
int set_timeout (DB_TXN *txn, NV timeout, U32 flags = DB_SET_TXN_TIMEOUT) |
2317 |
CODE: |
2318 |
RETVAL = txn->set_timeout (txn, timeout * 1000000, flags); |
2319 |
OUTPUT: |
2320 |
RETVAL |
2321 |
|
2322 |
int failed (DB_TXN *txn) |
2323 |
CODE: |
2324 |
RETVAL = !!(txn->flags & TXN_DEADLOCK); |
2325 |
OUTPUT: |
2326 |
RETVAL |
2327 |
|
2328 |
|
2329 |
MODULE = BDB PACKAGE = BDB::Cursor |
2330 |
|
2331 |
void |
2332 |
DESTROY (DBC_ornuked *dbc) |
2333 |
CODE: |
2334 |
if (dbc) |
2335 |
dbc->c_close (dbc); |
2336 |
|
2337 |
#if DB_VERSION_MINOR >= 6 |
2338 |
|
2339 |
int set_priority (DBC *dbc, int priority) |
2340 |
CODE: |
2341 |
dbc->set_priority (dbc, priority); |
2342 |
|
2343 |
#endif |
2344 |
|
2345 |
#if DB_VERSION_MINOR >= 3 |
2346 |
|
2347 |
MODULE = BDB PACKAGE = BDB::Sequence |
2348 |
|
2349 |
void |
2350 |
DESTROY (DB_SEQUENCE_ornuked *seq) |
2351 |
CODE: |
2352 |
if (seq) |
2353 |
seq->close (seq, 0); |
2354 |
|
2355 |
int initial_value (DB_SEQUENCE *seq, db_seq_t value) |
2356 |
CODE: |
2357 |
RETVAL = seq->initial_value (seq, value); |
2358 |
OUTPUT: |
2359 |
RETVAL |
2360 |
|
2361 |
int set_cachesize (DB_SEQUENCE *seq, U32 size) |
2362 |
CODE: |
2363 |
RETVAL = seq->set_cachesize (seq, size); |
2364 |
OUTPUT: |
2365 |
RETVAL |
2366 |
|
2367 |
int set_flags (DB_SEQUENCE *seq, U32 flags) |
2368 |
CODE: |
2369 |
RETVAL = seq->set_flags (seq, flags); |
2370 |
OUTPUT: |
2371 |
RETVAL |
2372 |
|
2373 |
int set_range (DB_SEQUENCE *seq, db_seq_t min, db_seq_t max) |
2374 |
CODE: |
2375 |
RETVAL = seq->set_range (seq, min, max); |
2376 |
OUTPUT: |
2377 |
RETVAL |
2378 |
|
2379 |
#endif |
2380 |
|
2381 |
|