… | |
… | |
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; |
… | |
… | |
44 | |
49 | |
45 | use Carp; |
50 | use Carp; |
46 | no warnings; |
51 | no warnings; |
47 | |
52 | |
48 | use Coro; |
53 | use Coro; |
|
|
54 | use Coro::Timer; |
49 | use Event qw(loop unloop); # we are re-exporting this, cooool! |
55 | use Event qw(loop unloop); # we are re-exporting this, cooool! |
50 | |
56 | |
51 | use XSLoader; |
57 | use XSLoader; |
52 | |
58 | |
53 | use base Exporter::; |
59 | use base Exporter::; |
54 | |
60 | |
55 | our @EXPORT = qw(loop unloop sweep reschedule); |
61 | our @EXPORT = qw(loop unloop sweep); |
56 | |
62 | |
57 | BEGIN { |
63 | BEGIN { |
58 | our $VERSION = 1.6; |
64 | our $VERSION = 1.9; |
59 | |
65 | |
60 | local $^W = 0; # avoid redefine warning for Coro::ready; |
66 | local $^W = 0; # avoid redefine warning for Coro::ready; |
61 | XSLoader::load __PACKAGE__, $VERSION; |
67 | XSLoader::load __PACKAGE__, $VERSION; |
62 | } |
68 | } |
63 | |
69 | |
… | |
… | |
117 | } |
123 | } |
118 | |
124 | |
119 | # double calls to avoid stack-cloning ;() |
125 | # double calls to avoid stack-cloning ;() |
120 | # is about 10% slower, though. |
126 | # is about 10% slower, though. |
121 | sub next($) { |
127 | sub next($) { |
122 | &Coro::schedule if &_next; $_[0]; |
128 | &Coro::schedule while &_next; |
|
|
129 | |
|
|
130 | $_[0] |
123 | } |
131 | } |
124 | |
132 | |
125 | sub Coro::Event::w { $_[0] } |
133 | sub Coro::Event::w { $_[0] } |
126 | sub Coro::Event::prio { $_[0]{Coro::Event}[3] } |
134 | sub Coro::Event::prio { $_[0]{Coro::Event}[3] } |
127 | sub Coro::Event::hits { $_[0]{Coro::Event}[4] } |
135 | sub Coro::Event::hits { $_[0]{Coro::Event}[4] } |
… | |
… | |
148 | |
156 | |
149 | This is the version of C<loop> you should use instead of C<Event::loop> |
157 | 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 |
158 | when using this module - it will ensure correct scheduling in the presence |
151 | of events. |
159 | of events. |
152 | |
160 | |
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]) |
161 | =item unloop([$result]) |
171 | |
162 | |
172 | Same as Event::unloop (provided here for your convinience only). |
163 | Same as Event::unloop (provided here for your convinience only). |
173 | |
164 | |
174 | =cut |
165 | =cut |
175 | |
166 | |
176 | $Coro::idle = new Coro sub { |
167 | $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 | |
168 | |
194 | 1; |
169 | 1; |
|
|
170 | |
|
|
171 | =back |
195 | |
172 | |
196 | =head1 AUTHOR |
173 | =head1 AUTHOR |
197 | |
174 | |
198 | Marc Lehmann <schmorp@schmorp.de> |
175 | Marc Lehmann <schmorp@schmorp.de> |
199 | http://home.schmorp.de/ |
176 | http://home.schmorp.de/ |