1 |
=head1 NAME |
2 |
|
3 |
Coro::EV - do events the coro-way |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use Coro; |
8 |
use Coro::EV; |
9 |
|
10 |
=head1 DESCRIPTION |
11 |
|
12 |
This module does two things: First, it offers some utility functions that |
13 |
might be useful for coroutines, and secondly, it integrates Coro into the |
14 |
EV main loop: |
15 |
|
16 |
Before the process blocks (in EV::loop) to wait for events, this module |
17 |
will schedule and run all ready (= runnable) coroutines of the same or |
18 |
higher priority. After that, it will cede once to a coroutine of lower |
19 |
priority, then continue in the event loop. |
20 |
|
21 |
That means that coroutines with the same or higher pripority as the |
22 |
coroutine running the main loop will inhibit event processing, while |
23 |
coroutines of lower priority will get the CPU, but cannot completeley |
24 |
inhibit event processing. |
25 |
|
26 |
=head1 FUNCTIONS |
27 |
|
28 |
=over 4 |
29 |
|
30 |
=cut |
31 |
|
32 |
package Coro::EV; |
33 |
|
34 |
no warnings; |
35 |
|
36 |
use Carp; |
37 |
no warnings; |
38 |
|
39 |
use Coro; |
40 |
use Coro::Timer; |
41 |
|
42 |
use EV (); |
43 |
use XSLoader; |
44 |
|
45 |
BEGIN { |
46 |
our $VERSION = '2.1'; |
47 |
|
48 |
local $^W = 0; # avoid redefine warning for Coro::ready; |
49 |
XSLoader::load __PACKAGE__, $VERSION; |
50 |
} |
51 |
|
52 |
# relatively inefficient |
53 |
our $ev_idle = new Coro sub { |
54 |
while () { |
55 |
EV::loop EV::LOOP_ONESHOT; |
56 |
&Coro::schedule; |
57 |
} |
58 |
}; |
59 |
$ev->{desc} = "[EV idle process]"; |
60 |
|
61 |
$Coro::idle = sub { $ev_idle->ready }; |
62 |
|
63 |
=item $revents = Coro::EV::timed_io_once $fd, $events, $timeout |
64 |
|
65 |
Blocks the coroutine until either the given event set has occured on the |
66 |
fd, or the timeout has been reached (if timeout is zero, there is no |
67 |
timeout). Returns the received flags. |
68 |
|
69 |
=cut |
70 |
|
71 |
sub timed_io_once($$;$) { |
72 |
&_timed_io_once; |
73 |
do { &Coro::schedule } while !$#_; |
74 |
pop |
75 |
} |
76 |
|
77 |
=item Coro::EV::timer_once $after |
78 |
|
79 |
Blocks the coroutine for at least C<$after> seconds. |
80 |
|
81 |
=cut |
82 |
|
83 |
sub timer_once($) { |
84 |
&_timer_once; |
85 |
do { &Coro::schedule } while !$#_; |
86 |
pop |
87 |
} |
88 |
|
89 |
1; |
90 |
|
91 |
=back |
92 |
|
93 |
=head1 AUTHOR |
94 |
|
95 |
Marc Lehmann <schmorp@schmorp.de> |
96 |
http://home.schmorp.de/ |
97 |
|
98 |
=cut |
99 |
|