=head1 NAME EV::Loop::Async - run an EV event loop asynchronously =head1 SYNOPSIS use EV::Loop::Async; =head1 DESCRIPTION TODO. =over 4 =cut package EV::Loop::Async; use common::sense; use EV (); use Async::Interrupt (); use base 'EV::Loop'; BEGIN { our $VERSION = '0.02'; require XSLoader; XSLoader::load ("EV::Loop::Async", $VERSION); } =item $loop = EV::Loop::Async::default Return the default loop, usable by all programs. The default loop will be created on the first call to C by calling X, and should be used by all programs unless they have special requirements. =cut our ($LOOP, $ASYNC); sub default() { $LOOP || do { $LOOP = new EV::Loop::Async; # $ASYNC = $LOOP->async; $LOOP } } =item $loop = new EV::Loop $flags, [Async-Interrupt-Arguments...] This constructor: =over 4 =item 1. creates a new C (similar C). =item 2. creates a new L object and attaches itself to it. =item 3. locks the loop (see below). =item 4. creates a new background thread. =item 5. runs C<< $loop->run >> in that thread. =back =cut sub new { my ($class, $flags, @asy) = @_; my $self = bless $class->SUPER::new ($flags), $class; my ($c_func, $c_arg) = _c_func $self; my $asy = new Async::Interrupt @asy, c_cb => [$c_func, $c_arg]; $self->_attach ($asy, $asy->signal_func); $self } =item $loop->nudge Wake up the asynchronous loop. This is useful after registering a new watcher, to ensure that the background event loop integrates the new watcher(s) (which only happens when it iterates, which you can force by calling this method). Without calling this method, the event loop I takes notice of new watchers, bit when this happens is not wlel-defined (can be instantaneous, or take a few hours). No locking is required. Example: lock the loop, create a timer, nudge the loop so it takes notice of the new timer, then evily busy-wait till the timer fires. my $timer; my $flag; { $loop->scope_lock; $timer = $loop->timer (1, 0, sub { $flag = 1 }); $loop->nudge; } 1 until $flag; =head1 SEE ALSO L, L. =head1 AUTHOR Marc Lehmann http://home.schmorp.de/ =cut 1