| 1 |
root |
1.1 |
=head1 NAME |
| 2 |
|
|
|
| 3 |
root |
1.31 |
Coro::EV - do events the coro-way, with EV |
| 4 |
root |
1.1 |
|
| 5 |
|
|
=head1 SYNOPSIS |
| 6 |
|
|
|
| 7 |
|
|
use Coro; |
| 8 |
|
|
use Coro::EV; |
| 9 |
|
|
|
| 10 |
root |
1.5 |
EV::loop; |
| 11 |
|
|
|
| 12 |
root |
1.4 |
=head1 DESCRIPTION |
| 13 |
root |
1.1 |
|
| 14 |
root |
1.4 |
This module does two things: First, it offers some utility functions that |
| 15 |
root |
1.38 |
might be useful for threads, and secondly, it integrates Coro into the |
| 16 |
root |
1.4 |
EV main loop: |
| 17 |
|
|
|
| 18 |
|
|
Before the process blocks (in EV::loop) to wait for events, this module |
| 19 |
root |
1.38 |
will schedule and run all ready (= runnable) threads of the same or |
| 20 |
|
|
higher priority. After that, it will cede once to a threads of lower |
| 21 |
root |
1.4 |
priority, then continue in the event loop. |
| 22 |
|
|
|
| 23 |
root |
1.38 |
That means that threads with the same or higher pripority as the threads |
| 24 |
|
|
running the main loop will inhibit event processing, while threads of |
| 25 |
|
|
lower priority will get the CPU, but cannot completeley inhibit event |
| 26 |
|
|
processing. Note that for that to work you actually have to run the EV |
| 27 |
|
|
event loop in some thread. |
| 28 |
root |
1.1 |
|
| 29 |
root |
1.39 |
=head1 RUNNING WITH OR WITHOUT A MAINLOOP |
| 30 |
root |
1.5 |
|
| 31 |
|
|
In general, you should always run EV::loop, either in your main program, |
| 32 |
root |
1.38 |
or in a separate coroutine. If you don't do that and all coroutines |
| 33 |
|
|
start waiting for some events, this module will run the event loop once, |
| 34 |
|
|
but this is very inefficient and will also not make it possible to run |
| 35 |
|
|
background threads. |
| 36 |
|
|
|
| 37 |
|
|
To run the EV event loop in a separate thread, you can simply do this: |
| 38 |
|
|
|
| 39 |
|
|
async { EV::loop }; |
| 40 |
root |
1.5 |
|
| 41 |
root |
1.1 |
=head1 FUNCTIONS |
| 42 |
|
|
|
| 43 |
|
|
=over 4 |
| 44 |
|
|
|
| 45 |
|
|
=cut |
| 46 |
|
|
|
| 47 |
|
|
package Coro::EV; |
| 48 |
|
|
|
| 49 |
|
|
no warnings; |
| 50 |
root |
1.8 |
use strict; |
| 51 |
root |
1.1 |
|
| 52 |
|
|
use Carp; |
| 53 |
|
|
no warnings; |
| 54 |
|
|
|
| 55 |
|
|
use Coro; |
| 56 |
|
|
|
| 57 |
|
|
use EV (); |
| 58 |
|
|
use XSLoader; |
| 59 |
|
|
|
| 60 |
|
|
BEGIN { |
| 61 |
root |
1.45 |
our $VERSION = 5.162; |
| 62 |
root |
1.1 |
|
| 63 |
|
|
local $^W = 0; # avoid redefine warning for Coro::ready; |
| 64 |
|
|
XSLoader::load __PACKAGE__, $VERSION; |
| 65 |
|
|
} |
| 66 |
|
|
|
| 67 |
root |
1.8 |
our $IDLE = new Coro sub { |
| 68 |
|
|
while () { |
| 69 |
|
|
&_loop_oneshot; |
| 70 |
|
|
&Coro::schedule; |
| 71 |
|
|
} |
| 72 |
|
|
}; |
| 73 |
|
|
$IDLE->{desc} = "[EV idle process]"; |
| 74 |
root |
1.1 |
|
| 75 |
root |
1.32 |
$Coro::idle = $IDLE; |
| 76 |
root |
1.1 |
|
| 77 |
root |
1.39 |
=item $revents = Coro::EV::timed_io_once $fileno_or_fh, $events[, $timeout] |
| 78 |
root |
1.3 |
|
| 79 |
|
|
Blocks the coroutine until either the given event set has occured on the |
| 80 |
root |
1.39 |
fd, or the timeout has been reached (if timeout is missing or C<undef> |
| 81 |
|
|
then there will be no timeout). Returns the received flags. |
| 82 |
root |
1.3 |
|
| 83 |
|
|
=item Coro::EV::timer_once $after |
| 84 |
|
|
|
| 85 |
|
|
Blocks the coroutine for at least C<$after> seconds. |
| 86 |
|
|
|
| 87 |
|
|
=cut |
| 88 |
|
|
|
| 89 |
root |
1.1 |
1; |
| 90 |
|
|
|
| 91 |
|
|
=back |
| 92 |
|
|
|
| 93 |
|
|
=head1 AUTHOR |
| 94 |
|
|
|
| 95 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
| 96 |
|
|
http://home.schmorp.de/ |
| 97 |
|
|
|
| 98 |
|
|
=cut |
| 99 |
|
|
|