… | |
… | |
37 | Please note that even programs or modules (such as |
37 | Please note that even programs or modules (such as |
38 | L<Coro::Handle|Coro::Handle>) that use "traditional" |
38 | L<Coro::Handle|Coro::Handle>) that use "traditional" |
39 | event-based/continuation style will run more efficient with this module |
39 | event-based/continuation style will run more efficient with this module |
40 | then when using only Event. |
40 | then when using only Event. |
41 | |
41 | |
|
|
42 | =head1 WARNING |
|
|
43 | |
|
|
44 | Please note that Event does not support coroutines or threads. That |
|
|
45 | means that you B<MUST NOT> block in an event callback. Again: In Event |
|
|
46 | callbacks, you I<must never ever> call a Coroutine fucntion that blocks |
|
|
47 | the current coroutine. |
|
|
48 | |
|
|
49 | While this seems to work superficially, it will eventually cause memory |
|
|
50 | corruption. |
|
|
51 | |
|
|
52 | =head1 FUNCTIONS |
|
|
53 | |
42 | =over 4 |
54 | =over 4 |
43 | |
55 | |
44 | =cut |
56 | =cut |
45 | |
57 | |
46 | package Coro::Event; |
58 | package Coro::Event; |
47 | |
59 | |
48 | BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } |
60 | no warnings; |
49 | |
61 | |
50 | use Carp; |
62 | use Carp; |
51 | no warnings; |
63 | no warnings; |
52 | |
64 | |
53 | use Coro; |
65 | use Coro; |
|
|
66 | use Coro::Timer; |
54 | use Event qw(loop unloop); # we are re-exporting this, cooool! |
67 | use Event qw(loop unloop); # we are re-exporting this, cooool! |
55 | |
68 | |
56 | use XSLoader; |
69 | use XSLoader; |
57 | |
70 | |
58 | use base Exporter::; |
71 | use base Exporter::; |
59 | |
72 | |
60 | our @EXPORT = qw(loop unloop sweep reschedule); |
73 | our @EXPORT = qw(loop unloop sweep); |
61 | |
74 | |
62 | BEGIN { |
75 | BEGIN { |
63 | our $VERSION = 1.9; |
76 | our $VERSION = 1.9; |
64 | |
77 | |
65 | local $^W = 0; # avoid redefine warning for Coro::ready; |
78 | local $^W = 0; # avoid redefine warning for Coro::ready; |
66 | XSLoader::load __PACKAGE__, $VERSION; |
79 | XSLoader::load __PACKAGE__, $VERSION; |
67 | } |
80 | } |
68 | |
81 | |
69 | =item $w = Coro::Event->flavour(args...) |
82 | =item $w = Coro::Event->flavour (args...) |
70 | |
83 | |
71 | Create and return a watcher of the given type. |
84 | Create and return a watcher of the given type. |
72 | |
85 | |
73 | Examples: |
86 | Examples: |
74 | |
87 | |
… | |
… | |
81 | |
94 | |
82 | Return the next event of the event queue of the watcher. |
95 | Return the next event of the event queue of the watcher. |
83 | |
96 | |
84 | =cut |
97 | =cut |
85 | |
98 | |
86 | =item do_flavour(args...) |
99 | =item do_flavour args... |
87 | |
100 | |
88 | Create a watcher of the given type and immediately call it's next |
101 | Create a watcher of the given type and immediately call it's next |
89 | method. This is less efficient then calling the constructor once and the |
102 | method. This is less efficient then calling the constructor once and the |
90 | next method often, but it does save typing sometimes. |
103 | next method often, but it does save typing sometimes. |
91 | |
104 | |
… | |
… | |
122 | } |
135 | } |
123 | |
136 | |
124 | # double calls to avoid stack-cloning ;() |
137 | # double calls to avoid stack-cloning ;() |
125 | # is about 10% slower, though. |
138 | # is about 10% slower, though. |
126 | sub next($) { |
139 | sub next($) { |
127 | &Coro::schedule if &_next; $_[0]; |
140 | &Coro::schedule while &_next; |
|
|
141 | |
|
|
142 | $_[0] |
128 | } |
143 | } |
129 | |
144 | |
130 | sub Coro::Event::w { $_[0] } |
145 | sub Coro::Event::w { $_[0] } |
131 | sub Coro::Event::prio { $_[0]{Coro::Event}[3] } |
146 | sub Coro::Event::prio { $_[0]{Coro::Event}[3] } |
132 | sub Coro::Event::hits { $_[0]{Coro::Event}[4] } |
147 | sub Coro::Event::hits { $_[0]{Coro::Event}[4] } |
… | |
… | |
144 | into the Event dispatcher. |
159 | into the Event dispatcher. |
145 | |
160 | |
146 | =cut |
161 | =cut |
147 | |
162 | |
148 | sub sweep { |
163 | sub sweep { |
149 | Event::one_event(0); # for now |
164 | Event::one_event 0; # for now |
150 | } |
165 | } |
151 | |
166 | |
152 | =item $result = loop([$timeout]) |
167 | =item $result = loop([$timeout]) |
153 | |
168 | |
154 | This is the version of C<loop> you should use instead of C<Event::loop> |
169 | This is the version of C<loop> you should use instead of C<Event::loop> |
155 | when using this module - it will ensure correct scheduling in the presence |
170 | when using this module - it will ensure correct scheduling in the presence |
156 | of events. |
171 | of events. |
157 | |
172 | |
158 | =begin comment |
|
|
159 | |
|
|
160 | Unlike loop's counterpart it is not an error when no watchers are active - |
|
|
161 | loop silently returns in this case, as if unloop(undef) were called. |
|
|
162 | |
|
|
163 | =end comment |
|
|
164 | |
|
|
165 | =cut |
|
|
166 | |
|
|
167 | # no longer do something special - it's done internally now |
|
|
168 | |
|
|
169 | #sub loop(;$) { |
|
|
170 | # #local $Coro::idle = $Coro::current; |
|
|
171 | # #Coro::schedule; # become idle task, which is implicitly ready |
|
|
172 | # &Event::loop; |
|
|
173 | #} |
|
|
174 | |
|
|
175 | =item unloop([$result]) |
173 | =item unloop([$result]) |
176 | |
174 | |
177 | Same as Event::unloop (provided here for your convinience only). |
175 | Same as Event::unloop (provided here for your convinience only). |
178 | |
176 | |
179 | =cut |
177 | =cut |
180 | |
178 | |
181 | $Coro::idle = new Coro sub { |
179 | $Coro::idle = \&Event::one_event; # inefficient |
182 | while () { |
|
|
183 | Event::one_event; # inefficient |
|
|
184 | Coro::schedule; |
|
|
185 | } |
|
|
186 | }; |
|
|
187 | |
|
|
188 | # provide hooks for Coro::Timer |
|
|
189 | |
|
|
190 | package Coro::Timer; |
|
|
191 | |
|
|
192 | unless ($override) { |
|
|
193 | $override = 1; |
|
|
194 | *_new_timer = sub { |
|
|
195 | Event->timer(at => $_[0], cb => $_[1]); |
|
|
196 | }; |
|
|
197 | } |
|
|
198 | |
180 | |
199 | 1; |
181 | 1; |
200 | |
182 | |
201 | =back |
183 | =back |
202 | |
184 | |