| 1 |
=head1 NAME |
| 2 |
|
| 3 |
Coro::Event - do events the coro-way |
| 4 |
|
| 5 |
=head1 SYNOPSIS |
| 6 |
|
| 7 |
use Coro; |
| 8 |
use Coro::Event; |
| 9 |
|
| 10 |
sub keyboard : Coro { |
| 11 |
my $w = Coro::Event->io(fd => \*STDIN, poll => 'r'); |
| 12 |
while() { |
| 13 |
print "cmd> "; |
| 14 |
my $ev = $w->next; my $cmd = <STDIN>; |
| 15 |
unloop unless $cmd ne ""; |
| 16 |
print "data> "; |
| 17 |
my $ev = $w->next; my $data = <STDIN>; |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
loop; |
| 22 |
|
| 23 |
=head1 DESCRIPTION |
| 24 |
|
| 25 |
This module enables you to create programs using the powerful Event model |
| 26 |
(and module), while retaining the linear style known from simple or |
| 27 |
threaded programs. |
| 28 |
|
| 29 |
This module provides a method and a function for every watcher type |
| 30 |
(I<flavour>) (see L<Event>). The only difference between these and the |
| 31 |
watcher constructors from Event is that you do not specify a callback |
| 32 |
function - it will be managed by this module. |
| 33 |
|
| 34 |
Your application should just create all necessary coroutines and then call |
| 35 |
Coro::Event::loop. |
| 36 |
|
| 37 |
Please note that even programs or modules (such as |
| 38 |
L<Coro::Handle|Coro::Handle>) that use "traditional" |
| 39 |
event-based/continuation style will run more efficient with this module |
| 40 |
then when using only Event. |
| 41 |
|
| 42 |
=over 4 |
| 43 |
|
| 44 |
=cut |
| 45 |
|
| 46 |
package Coro::Event; |
| 47 |
|
| 48 |
BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } |
| 49 |
|
| 50 |
use Carp; |
| 51 |
no warnings; |
| 52 |
|
| 53 |
use Coro; |
| 54 |
use Coro::Timer; |
| 55 |
use Event qw(loop unloop); # we are re-exporting this, cooool! |
| 56 |
|
| 57 |
use XSLoader; |
| 58 |
|
| 59 |
use base Exporter::; |
| 60 |
|
| 61 |
our @EXPORT = qw(loop unloop sweep reschedule); |
| 62 |
|
| 63 |
BEGIN { |
| 64 |
our $VERSION = 1.9; |
| 65 |
|
| 66 |
local $^W = 0; # avoid redefine warning for Coro::ready; |
| 67 |
XSLoader::load __PACKAGE__, $VERSION; |
| 68 |
} |
| 69 |
|
| 70 |
=item $w = Coro::Event->flavour(args...) |
| 71 |
|
| 72 |
Create and return a watcher of the given type. |
| 73 |
|
| 74 |
Examples: |
| 75 |
|
| 76 |
my $reader = Coro::Event->io(fd => $filehandle, poll => 'r'); |
| 77 |
$reader->next; |
| 78 |
|
| 79 |
=cut |
| 80 |
|
| 81 |
=item $w->next |
| 82 |
|
| 83 |
Return the next event of the event queue of the watcher. |
| 84 |
|
| 85 |
=cut |
| 86 |
|
| 87 |
=item do_flavour(args...) |
| 88 |
|
| 89 |
Create a watcher of the given type and immediately call it's next |
| 90 |
method. This is less efficient then calling the constructor once and the |
| 91 |
next method often, but it does save typing sometimes. |
| 92 |
|
| 93 |
=cut |
| 94 |
|
| 95 |
for my $flavour (qw(idle var timer io signal)) { |
| 96 |
push @EXPORT, "do_$flavour"; |
| 97 |
my $new = \&{"Event::$flavour"}; |
| 98 |
my $class = "Coro::Event::$flavour"; |
| 99 |
my $type = $flavour eq "io" ? 1 : 0; |
| 100 |
@{"${class}::ISA"} = (Coro::Event::, "Event::$flavour"); |
| 101 |
my $coronew = sub { |
| 102 |
# how does one do method-call-by-name? |
| 103 |
# my $w = $class->SUPER::$flavour(@_); |
| 104 |
|
| 105 |
shift eq Coro::Event:: |
| 106 |
or croak "event constructor \"Coro::Event->$flavour\" must be called as a static method"; |
| 107 |
|
| 108 |
my $w = $new->($class, |
| 109 |
desc => $flavour, |
| 110 |
@_, |
| 111 |
parked => 1, |
| 112 |
); |
| 113 |
_install_std_cb($w, $type); |
| 114 |
bless $w, $class; # reblessing due to broken Event |
| 115 |
}; |
| 116 |
*{ $flavour } = $coronew; |
| 117 |
*{"do_$flavour"} = sub { |
| 118 |
unshift @_, Coro::Event::; |
| 119 |
my $e = (&$coronew)->next; |
| 120 |
$e->cancel; # $e === $e->w |
| 121 |
$e; |
| 122 |
}; |
| 123 |
} |
| 124 |
|
| 125 |
# double calls to avoid stack-cloning ;() |
| 126 |
# is about 10% slower, though. |
| 127 |
sub next($) { |
| 128 |
&Coro::schedule if &_next; $_[0]; |
| 129 |
} |
| 130 |
|
| 131 |
sub Coro::Event::w { $_[0] } |
| 132 |
sub Coro::Event::prio { $_[0]{Coro::Event}[3] } |
| 133 |
sub Coro::Event::hits { $_[0]{Coro::Event}[4] } |
| 134 |
sub Coro::Event::got { $_[0]{Coro::Event}[5] } |
| 135 |
|
| 136 |
=item sweep |
| 137 |
|
| 138 |
Similar to Event::one_event and Event::sweep: The idle task is called once |
| 139 |
(this has the effect of jumping back into the Event loop once to serve new |
| 140 |
events). |
| 141 |
|
| 142 |
The reason this function exists is that you sometimes want to serve events |
| 143 |
while doing other work. Calling C<Coro::cede> does not work because |
| 144 |
C<cede> implies that the current coroutine is runnable and does not call |
| 145 |
into the Event dispatcher. |
| 146 |
|
| 147 |
=cut |
| 148 |
|
| 149 |
sub sweep { |
| 150 |
Event::one_event(0); # for now |
| 151 |
} |
| 152 |
|
| 153 |
=item $result = loop([$timeout]) |
| 154 |
|
| 155 |
This is the version of C<loop> you should use instead of C<Event::loop> |
| 156 |
when using this module - it will ensure correct scheduling in the presence |
| 157 |
of events. |
| 158 |
|
| 159 |
=item unloop([$result]) |
| 160 |
|
| 161 |
Same as Event::unloop (provided here for your convinience only). |
| 162 |
|
| 163 |
=cut |
| 164 |
|
| 165 |
$Coro::idle = \&Event::one_event; # inefficient |
| 166 |
|
| 167 |
1; |
| 168 |
|
| 169 |
=back |
| 170 |
|
| 171 |
=head1 AUTHOR |
| 172 |
|
| 173 |
Marc Lehmann <schmorp@schmorp.de> |
| 174 |
http://home.schmorp.de/ |
| 175 |
|
| 176 |
=cut |
| 177 |
|