1 |
=head1 NAME |
2 |
|
3 |
Coro::BDB - truly asynchronous bdb access |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use BDB; |
8 |
use Coro::BDB; |
9 |
|
10 |
# can now use any of the bdb requests |
11 |
|
12 |
=head1 DESCRIPTION |
13 |
|
14 |
This module implements a thin wrapper around the L<BDB|BDB> module. |
15 |
|
16 |
Each BDB request that could block and doesn't get passed a callback will |
17 |
normally block all coroutines. after loading this module, this will no |
18 |
longer be the case. |
19 |
|
20 |
It will also register an AnyEvent handler (this will be done when the |
21 |
module gets loaded and thus detects the event model at the same time, so |
22 |
you need to laod your event module before Coro::BDB). |
23 |
|
24 |
This module does not export anything (unlike Coro::AIO), as BDB already |
25 |
supports leaving out the callback. |
26 |
|
27 |
The AnyEvent watcher can be disabled by executing C<undef |
28 |
$Coro::BDB::WATCHER>. Please notify the author of when and why you think |
29 |
this was necessary. |
30 |
|
31 |
=over 4 |
32 |
|
33 |
=cut |
34 |
|
35 |
package Coro::BDB; |
36 |
|
37 |
no warnings; |
38 |
use strict; |
39 |
|
40 |
use Coro (); |
41 |
use AnyEvent; |
42 |
use BDB (); |
43 |
|
44 |
use base Exporter::; |
45 |
|
46 |
our $VERSION = '1.0'; |
47 |
our $WATCHER; |
48 |
|
49 |
if (AnyEvent::detect =~ /^AnyEvent::Impl::(?:Coro)?EV$/) { |
50 |
$WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; |
51 |
} else { |
52 |
our $FH; open $FH, "<&=" . BDB::poll_fileno; |
53 |
$WATCHER = AnyEvent->io (fh => $FH, poll => 'r', cb => \&BDB::poll_cb); |
54 |
} |
55 |
|
56 |
BDB::set_sync_prepare { |
57 |
my $status; |
58 |
my $current = $Coro::current; |
59 |
( |
60 |
sub { |
61 |
$status = $!; |
62 |
$current->ready; undef $current; |
63 |
}, |
64 |
sub { |
65 |
Coro::schedule while defined $current; |
66 |
$! = $status; |
67 |
}, |
68 |
) |
69 |
}; |
70 |
|
71 |
=back |
72 |
|
73 |
=head1 SEE ALSO |
74 |
|
75 |
L<BDB> of course. |
76 |
|
77 |
=head1 AUTHOR |
78 |
|
79 |
Marc Lehmann <schmorp@schmorp.de> |
80 |
http://home.schmorp.de/ |
81 |
|
82 |
=cut |
83 |
|
84 |
1 |