EV-Loop-Async
view release on metacpan or search on metacpan
EV::Loop::Async - run an EV event loop asynchronously
=head1 SYNOPSIS
use EV::Loop::Async;
my $loop = EV::Loop::Async::default;
my $timer;
my $flag;
# create a watcher, but make sure the loop is locked
{
$loop->scope_lock; # lock the loop structures
$timer = $loop->timer (5, 1, sub { $flag = 1 });
$loop->notify; # tell loop to take note of the timer
}
1 while $flag; # $flag will be set asynchronously
# implement a critical section, uninterrupted by any callbacks
{
Sometimes you need lower latency for specific events, but it's too heavy
to continuously poll for events. And perl already does this for you
anyways, so this module only uses this existing mechanism.
=item When do I have to lock?
When in doubt, lock. Do not start or stop a watcher, do not create a
watcher (unless with the C<_ns> methods) and do not DESTROY an active
watcher without locking either.
Any other event loop modifications need to be done while locked as
well. So when in doubt, lock (best using C<scope_lock>).
=item Why explicit locking?
Because I was too lazy to wrap everything and there are probably only a
few people on this world using this module.
=back
=head1 FUNCTIONS, METHODS AND VARIABLES OF THIS MODULE
=item $EV::Loop::Async::INTERRUPT
The default loop's L<Async::Interrupt> object, for easy access.
Example: create a section of code where no callback invocations will
interrupt:
{
$EV::Loop::Async::INTERRUPT->scope_block;
# no default loop callbacks will be executed here.
# the loop will not be locked, however.
}
Example: embed the default EV::Async::Loop loop into the default L<EV>
loop (note that it could be any other event loop as well).
my $async_w = EV::io
$EV::Loop::Async::LOOP->interrupt->pipe_fileno,
EV::READ,
sub { };
=item 1. creates a new C<EV::Loop> (similar C<new EV::Loop>).
=item 2. creates a new L<Async::Interrupt> object and attaches itself to it.
=item 3. creates a new background thread.
=item 4. runs C<< $loop->run >> in that thread.
=back
The resulting loop will be running and unlocked when it is returned.
Example: create a new loop, block it's interrupt object and embed
it into the foreground L<AnyEvent> event loop. This basically runs the
C<EV::Loop::Async> loop in a synchronous way inside another loop.
my $loop = new EV::Loop::Async 0;
my $async = $loop->interrupt;
$async->block;
NAME
EV::Loop::Async - run an EV event loop asynchronously
SYNOPSIS
use EV::Loop::Async;
my $loop = EV::Loop::Async::default;
my $timer;
my $flag;
# create a watcher, but make sure the loop is locked
{
$loop->scope_lock; # lock the loop structures
$timer = $loop->timer (5, 1, sub { $flag = 1 });
$loop->notify; # tell loop to take note of the timer
}
1 while $flag; # $flag will be set asynchronously
# implement a critical section, uninterrupted by any callbacks
{
Why on earth...???
Sometimes you need lower latency for specific events, but it's too
heavy to continuously poll for events. And perl already does this
for you anyways, so this module only uses this existing mechanism.
When do I have to lock?
When in doubt, lock. Do not start or stop a watcher, do not create a
watcher (unless with the "_ns" methods) and do not DESTROY an active
watcher without locking either.
Any other event loop modifications need to be done while locked as
well. So when in doubt, lock (best using "scope_lock").
Why explicit locking?
Because I was too lazy to wrap everything and there are probably
only a few people on this world using this module.
FUNCTIONS, METHODS AND VARIABLES OF THIS MODULE
$loop = EV::Loop::Async::default
Return the default loop, usable by all programs. The default loop
will be created on the first call to "default" by calling , and
$EV::Loop::Async::INTERRUPT
The default loop's Async::Interrupt object, for easy access.
Example: create a section of code where no callback invocations will
interrupt:
{
$EV::Loop::Async::INTERRUPT->scope_block;
# no default loop callbacks will be executed here.
# the loop will not be locked, however.
}
Example: embed the default EV::Async::Loop loop into the default EV
loop (note that it could be any other event loop as well).
my $async_w = EV::io
$EV::Loop::Async::LOOP->interrupt->pipe_fileno,
EV::READ,
sub { };
$loop = new EV::Loop::Async $flags, [Async-Interrupt-Arguments...]
This constructor:
1. creates a new "EV::Loop" (similar "new EV::Loop").
2. creates a new Async::Interrupt object and attaches itself to it.
3. creates a new background thread.
4. runs "$loop->run" in that thread.
The resulting loop will be running and unlocked when it is returned.
Example: create a new loop, block it's interrupt object and embed it
into the foreground AnyEvent event loop. This basically runs the
"EV::Loop::Async" loop in a synchronous way inside another loop.
my $loop = new EV::Loop::Async 0;
my $async = $loop->interrupt;
$async->block;
( run in 0.405 second using v1.01-cache-2.11-cpan-49f99fa48dc )