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