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