=head1 NAME BDB - Asynchronous Berkeley DB access =head1 SYNOPSIS use BDB; =head1 DESCRIPTION =head2 EXAMPLE =head1 REQUEST ANATOMY AND LIFETIME Every request method creates a request. which is a C data structure not directly visible to Perl. During their existance, bdb requests travel through the following states, in order: =over 4 =item ready Immediately after a request is created it is put into the ready state, waiting for a thread to execute it. =item execute A thread has accepted the request for processing and is currently executing it (e.g. blocking in read). =item pending The request has been executed and is waiting for result processing. While request submission and execution is fully asynchronous, result processing is not and relies on the perl interpreter calling C (or another function with the same effect). =item result The request results are processed synchronously by C. The C function will process all outstanding aio requests by calling their callbacks, freeing memory associated with them and managing any groups they are contained in. =item done Request has reached the end of its lifetime and holds no resources anymore (except possibly for the Perl object, but its connection to the actual aio request is severed and calling its methods will either do nothing or result in a runtime error). =back =cut package BDB; no warnings; use strict 'vars'; use base 'Exporter'; BEGIN { our $VERSION = '0.1'; our @BDB_REQ = qw(); our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush min_parallel max_parallel max_idle nreqs nready npending nthreads max_poll_time max_poll_reqs); require XSLoader; XSLoader::load ("BDB", $VERSION); } =head2 SUPPORT FUNCTIONS =head3 EVENT PROCESSING AND EVENT LOOP INTEGRATION =over 4 =item $fileno = BDB::poll_fileno Return the I. This filehandle must be polled for reading by some mechanism outside this module (e.g. Event or select, see below or the SYNOPSIS). If the pipe becomes readable you have to call C to check the results. See C for an example. =item BDB::poll_cb Process some outstanding events on the result pipe. You have to call this regularly. Returns the number of events processed. Returns immediately when no events are outstanding. The amount of events processed depends on the settings of C and C. If not all requests were processed for whatever reason, the filehandle will still be ready when C returns. Example: Install an Event watcher that automatically calls BDB::poll_cb with high priority: Event->io (fd => BDB::poll_fileno, poll => 'r', async => 1, cb => \&BDB::poll_cb); =item BDB::max_poll_reqs $nreqs =item BDB::max_poll_time $seconds These set the maximum number of requests (default C<0>, meaning infinity) that are being processed by C in one call, respectively the maximum amount of time (default C<0>, meaning infinity) spent in C to process requests (more correctly the mininum amount of time C is allowed to use). Setting C to a non-zero value creates an overhead of one syscall per request processed, which is not normally a problem unless your callbacks are really really fast or your OS is really really slow (I am not mentioning Solaris here). Using C incurs no overhead. Setting these is useful if you want to ensure some level of interactiveness when perl is not fast enough to process all requests in time. For interactive programs, values such as C<0.01> to C<0.1> should be fine. Example: Install an Event watcher that automatically calls BDB::poll_cb with low priority, to ensure that other parts of the program get the CPU sometimes even under high AIO load. # try not to spend much more than 0.1s in poll_cb BDB::max_poll_time 0.1; # use a low priority so other tasks have priority Event->io (fd => BDB::poll_fileno, poll => 'r', nice => 1, cb => &BDB::poll_cb); =item BDB::poll_wait If there are any outstanding requests and none of them in the result phase, wait till the result filehandle becomes ready for reading (simply does a C