| 1 |
root |
1.1 |
=head1 NAME |
| 2 |
|
|
|
| 3 |
|
|
EV::Loop::Async - run an EV event loop asynchronously |
| 4 |
|
|
|
| 5 |
|
|
=head1 SYNOPSIS |
| 6 |
|
|
|
| 7 |
|
|
use EV::Loop::Async; |
| 8 |
|
|
|
| 9 |
|
|
=head1 DESCRIPTION |
| 10 |
|
|
|
| 11 |
|
|
TODO. |
| 12 |
|
|
|
| 13 |
|
|
=over 4 |
| 14 |
|
|
|
| 15 |
|
|
=cut |
| 16 |
|
|
|
| 17 |
|
|
package EV::Loop::Async; |
| 18 |
|
|
|
| 19 |
|
|
use common::sense; |
| 20 |
|
|
|
| 21 |
|
|
use EV (); |
| 22 |
|
|
use Async::Interrupt (); |
| 23 |
|
|
|
| 24 |
|
|
use base 'EV::Loop'; |
| 25 |
|
|
|
| 26 |
|
|
BEGIN { |
| 27 |
|
|
our $VERSION = '0.02'; |
| 28 |
|
|
|
| 29 |
|
|
require XSLoader; |
| 30 |
|
|
XSLoader::load ("EV::Loop::Async", $VERSION); |
| 31 |
|
|
} |
| 32 |
|
|
|
| 33 |
|
|
=item $loop = EV::Loop::Async::default |
| 34 |
|
|
|
| 35 |
|
|
Return the default loop, usable by all programs. The default loop will be |
| 36 |
|
|
created on the first call to C<default> by calling X<new EV::Loop>, and |
| 37 |
|
|
should be used by all programs unless they have special requirements. |
| 38 |
|
|
|
| 39 |
|
|
=cut |
| 40 |
|
|
|
| 41 |
|
|
our ($LOOP, $ASYNC); |
| 42 |
|
|
|
| 43 |
|
|
sub default() { |
| 44 |
|
|
$LOOP || do { |
| 45 |
|
|
$LOOP = new EV::Loop::Async; |
| 46 |
|
|
# $ASYNC = $LOOP->async; |
| 47 |
|
|
|
| 48 |
|
|
$LOOP |
| 49 |
|
|
} |
| 50 |
|
|
} |
| 51 |
|
|
|
| 52 |
|
|
|
| 53 |
|
|
=item $loop = new EV::Loop $flags, [Async-Interrupt-Arguments...] |
| 54 |
|
|
|
| 55 |
|
|
This constructor: |
| 56 |
|
|
|
| 57 |
|
|
=over 4 |
| 58 |
|
|
|
| 59 |
|
|
=item 1. creates a new C<EV::Loop> (similar C<new EV::Loop>). |
| 60 |
|
|
|
| 61 |
|
|
=item 2. creates a new L<Async::Interrupt> object and attaches itself to it. |
| 62 |
|
|
|
| 63 |
|
|
=item 3. locks the loop (see below). |
| 64 |
|
|
|
| 65 |
|
|
=item 4. creates a new background thread. |
| 66 |
|
|
|
| 67 |
|
|
=item 5. runs C<< $loop->run >> in that thread. |
| 68 |
|
|
|
| 69 |
|
|
=back |
| 70 |
|
|
|
| 71 |
|
|
=cut |
| 72 |
|
|
|
| 73 |
|
|
sub new { |
| 74 |
|
|
my ($class, $flags, @asy) = @_; |
| 75 |
|
|
|
| 76 |
|
|
my $self = bless $class->SUPER::new ($flags), $class; |
| 77 |
|
|
my ($c_func, $c_arg) = _c_func $self; |
| 78 |
|
|
my $asy = new Async::Interrupt @asy, c_cb => [$c_func, $c_arg]; |
| 79 |
|
|
$self->_attach ($asy, $asy->signal_func); |
| 80 |
|
|
|
| 81 |
|
|
$self |
| 82 |
|
|
} |
| 83 |
|
|
|
| 84 |
|
|
=item $loop->nudge |
| 85 |
|
|
|
| 86 |
|
|
Wake up the asynchronous loop. This is useful after registering a new |
| 87 |
|
|
watcher, to ensure that the background event loop integrates the new |
| 88 |
|
|
watcher(s) (which only happens when it iterates, which you can force by |
| 89 |
|
|
calling this method). |
| 90 |
|
|
|
| 91 |
|
|
Without calling this method, the event loop I<eventually> takes notice |
| 92 |
|
|
of new watchers, bit when this happens is not wlel-defined (can be |
| 93 |
|
|
instantaneous, or take a few hours). |
| 94 |
|
|
|
| 95 |
|
|
No locking is required. |
| 96 |
|
|
|
| 97 |
|
|
Example: lock the loop, create a timer, nudge the loop so it takes notice |
| 98 |
|
|
of the new timer, then evily busy-wait till the timer fires. |
| 99 |
|
|
|
| 100 |
|
|
my $timer; |
| 101 |
|
|
my $flag; |
| 102 |
|
|
|
| 103 |
|
|
{ |
| 104 |
|
|
$loop->scope_lock; |
| 105 |
|
|
$timer = $loop->timer (1, 0, sub { $flag = 1 }); |
| 106 |
|
|
$loop->nudge; |
| 107 |
|
|
} |
| 108 |
|
|
|
| 109 |
|
|
1 until $flag; |
| 110 |
|
|
|
| 111 |
|
|
=head1 SEE ALSO |
| 112 |
|
|
|
| 113 |
|
|
L<EV>, L<Async::Interrupt>. |
| 114 |
|
|
|
| 115 |
|
|
=head1 AUTHOR |
| 116 |
|
|
|
| 117 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
| 118 |
|
|
http://home.schmorp.de/ |
| 119 |
|
|
|
| 120 |
|
|
=cut |
| 121 |
|
|
|
| 122 |
|
|
1 |
| 123 |
|
|
|