… | |
… | |
6 | |
6 | |
7 | use Coro; |
7 | use Coro; |
8 | use Coro::Event; |
8 | use Coro::Event; |
9 | |
9 | |
10 | sub keyboard : Coro { |
10 | sub keyboard : Coro { |
11 | my $w = Coro::Event->io(fd => *STDIN, poll => 'r'); |
11 | my $w = Coro::Event->io(fd => \*STDIN, poll => 'r'); |
12 | while() { |
12 | while() { |
13 | print "cmd> "; |
13 | print "cmd> "; |
14 | my $ev = $w->next; my $cmd = <STDIN>; |
14 | my $ev = $w->next; my $cmd = <STDIN>; |
15 | unloop unless $cmd ne ""; |
15 | unloop unless $cmd ne ""; |
16 | print "data> "; |
16 | print "data> "; |
… | |
… | |
32 | function - it will be managed by this module. |
32 | function - it will be managed by this module. |
33 | |
33 | |
34 | Your application should just create all necessary coroutines and then call |
34 | Your application should just create all necessary coroutines and then call |
35 | Coro::Event::loop. |
35 | Coro::Event::loop. |
36 | |
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 | |
37 | =over 4 |
42 | =over 4 |
38 | |
43 | |
39 | =cut |
44 | =cut |
40 | |
45 | |
41 | package Coro::Event; |
46 | package Coro::Event; |
42 | |
47 | |
43 | BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } |
48 | BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } |
44 | |
49 | |
45 | use Carp; |
50 | use Carp; |
|
|
51 | no warnings; |
46 | |
52 | |
47 | use Coro; |
53 | use Coro; |
|
|
54 | use Coro::Timer; |
48 | use Event qw(loop unloop); # we are re-exporting this, cooool! |
55 | use Event qw(loop unloop); # we are re-exporting this, cooool! |
49 | |
56 | |
50 | use base 'Exporter'; |
57 | use XSLoader; |
51 | |
58 | |
|
|
59 | use base Exporter::; |
|
|
60 | |
52 | @EXPORT = qw(loop unloop sweep reschedule); |
61 | our @EXPORT = qw(loop unloop sweep reschedule); |
53 | |
62 | |
54 | BEGIN { |
63 | BEGIN { |
55 | $VERSION = 0.8; |
64 | our $VERSION = 1.9; |
56 | |
65 | |
57 | local $^W = 0; # avoid redefine warning for Coro::ready; |
66 | local $^W = 0; # avoid redefine warning for Coro::ready; |
58 | |
67 | XSLoader::load __PACKAGE__, $VERSION; |
59 | require DynaLoader; |
|
|
60 | push @ISA, 'DynaLoader'; |
|
|
61 | bootstrap Coro::Event $VERSION; |
|
|
62 | } |
68 | } |
63 | |
69 | |
64 | =item $w = Coro::Event->flavour(args...) |
70 | =item $w = Coro::Event->flavour(args...) |
65 | |
71 | |
66 | Create and return a watcher of the given type. |
72 | Create and return a watcher of the given type. |
… | |
… | |
109 | }; |
115 | }; |
110 | *{ $flavour } = $coronew; |
116 | *{ $flavour } = $coronew; |
111 | *{"do_$flavour"} = sub { |
117 | *{"do_$flavour"} = sub { |
112 | unshift @_, Coro::Event::; |
118 | unshift @_, Coro::Event::; |
113 | my $e = (&$coronew)->next; |
119 | my $e = (&$coronew)->next; |
114 | $e->cancel; # $e = $e->w->cancel ($e == $e->w!) |
120 | $e->cancel; # $e === $e->w |
115 | $e; |
121 | $e; |
116 | }; |
122 | }; |
117 | } |
123 | } |
118 | |
124 | |
119 | # double calls to avoid stack-cloning ;() |
125 | # double calls to avoid stack-cloning ;() |
… | |
… | |
148 | |
154 | |
149 | This is the version of C<loop> you should use instead of C<Event::loop> |
155 | This is the version of C<loop> you should use instead of C<Event::loop> |
150 | when using this module - it will ensure correct scheduling in the presence |
156 | when using this module - it will ensure correct scheduling in the presence |
151 | of events. |
157 | of events. |
152 | |
158 | |
153 | =begin comment |
|
|
154 | |
|
|
155 | Unlike loop's counterpart it is not an error when no watchers are active - |
|
|
156 | loop silently returns in this case, as if unloop(undef) were called. |
|
|
157 | |
|
|
158 | =end comment |
|
|
159 | |
|
|
160 | =cut |
|
|
161 | |
|
|
162 | # no longer do something special - it's done internally now |
|
|
163 | |
|
|
164 | #sub loop(;$) { |
|
|
165 | # #local $Coro::idle = $Coro::current; |
|
|
166 | # #Coro::schedule; # become idle task, which is implicitly ready |
|
|
167 | # &Event::loop; |
|
|
168 | #} |
|
|
169 | |
|
|
170 | =item unloop([$result]) |
159 | =item unloop([$result]) |
171 | |
160 | |
172 | Same as Event::unloop (provided here for your convinience only). |
161 | Same as Event::unloop (provided here for your convinience only). |
173 | |
162 | |
174 | =cut |
163 | =cut |
175 | |
164 | |
176 | $Coro::idle = new Coro sub { |
165 | $Coro::idle = \&Event::one_event; # inefficient |
177 | while () { |
|
|
178 | Event::one_event; # inefficient |
|
|
179 | Coro::schedule; |
|
|
180 | } |
|
|
181 | }; |
|
|
182 | |
|
|
183 | # provide hooks for Coro::Timer |
|
|
184 | |
|
|
185 | package Coro::Timer; |
|
|
186 | |
|
|
187 | unless ($override) { |
|
|
188 | $override = 1; |
|
|
189 | *_new_timer = sub { |
|
|
190 | Event->timer(at => $_[0], cb => $_[1]); |
|
|
191 | }; |
|
|
192 | } |
|
|
193 | |
166 | |
194 | 1; |
167 | 1; |
195 | |
168 | |
|
|
169 | =back |
|
|
170 | |
196 | =head1 AUTHOR |
171 | =head1 AUTHOR |
197 | |
172 | |
198 | Marc Lehmann <pcg@goof.com> |
173 | Marc Lehmann <schmorp@schmorp.de> |
199 | http://www.goof.com/pcg/marc/ |
174 | http://home.schmorp.de/ |
200 | |
175 | |
201 | =cut |
176 | =cut |
202 | |
177 | |