… | |
… | |
18 | } |
18 | } |
19 | } |
19 | } |
20 | |
20 | |
21 | loop; |
21 | loop; |
22 | |
22 | |
|
|
23 | # wait for input on stdin for one second |
|
|
24 | |
|
|
25 | Coro::Event::do_io (fd => \*STDIN, timeout => 1) & Event::Watcher::R |
|
|
26 | or die "no input received"; |
|
|
27 | |
|
|
28 | # use a separate coroutine for event processing, if impossible in main: |
|
|
29 | Coro::async { Event::loop }; |
|
|
30 | |
23 | =head1 DESCRIPTION |
31 | =head1 DESCRIPTION |
24 | |
32 | |
25 | This module enables you to create programs using the powerful Event model |
33 | This module enables you to create programs using the powerful Event model |
26 | (and module), while retaining the linear style known from simple or |
34 | (and module), while retaining the linear style known from simple or |
27 | threaded programs. |
35 | threaded programs. |
… | |
… | |
32 | function - it will be managed by this module. |
40 | function - it will be managed by this module. |
33 | |
41 | |
34 | Your application should just create all necessary coroutines and then call |
42 | Your application should just create all necessary coroutines and then call |
35 | Coro::Event::loop. |
43 | Coro::Event::loop. |
36 | |
44 | |
|
|
45 | Please note that even programs or modules (such as |
|
|
46 | L<Coro::Handle|Coro::Handle>) that use "traditional" |
|
|
47 | event-based/continuation style will run more efficient with this module |
|
|
48 | then when using only Event. |
|
|
49 | |
|
|
50 | =head1 WARNING |
|
|
51 | |
|
|
52 | Please note that Event does not support coroutines or threads. That |
|
|
53 | means that you B<MUST NOT> block in an event callback. Again: In Event |
|
|
54 | callbacks, you I<must never ever> call a Coroutine function that blocks |
|
|
55 | the current coroutine. |
|
|
56 | |
|
|
57 | While this seems to work superficially, it will eventually cause memory |
|
|
58 | corruption and often results in deadlocks. |
|
|
59 | |
|
|
60 | Best practise is to always use B<Coro::unblock_sub> for your callbacks. |
|
|
61 | |
|
|
62 | =head1 SEMANTICS |
|
|
63 | |
|
|
64 | Whenever Event blocks (e.g. in a call to C<one_event>, C<loop> etc.), |
|
|
65 | this module cede's to all other coroutines with the same or higher |
|
|
66 | priority. When any coroutines of lower priority are ready, it will not |
|
|
67 | block but run one of them and then check for events. |
|
|
68 | |
|
|
69 | The effect is that coroutines with the same or higher priority than |
|
|
70 | the blocking coroutine will keep Event from checking for events, while |
|
|
71 | coroutines with lower priority are being run, but Event checks for new |
|
|
72 | events after every cede. |
|
|
73 | |
|
|
74 | =head1 FUNCTIONS |
|
|
75 | |
37 | =over 4 |
76 | =over 4 |
38 | |
77 | |
39 | =cut |
78 | =cut |
40 | |
79 | |
41 | package Coro::Event; |
80 | package Coro::Event; |
42 | |
81 | |
43 | BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } |
82 | no warnings; |
44 | |
83 | |
45 | use Carp; |
84 | use Carp; |
46 | no warnings; |
85 | no warnings; |
47 | |
86 | |
48 | use Coro; |
87 | use Coro; |
|
|
88 | use Coro::Timer; |
49 | use Event qw(loop unloop); # we are re-exporting this, cooool! |
89 | use Event qw(loop unloop); # we are re-exporting this, cooool! |
50 | |
90 | |
51 | use XSLoader; |
91 | use XSLoader; |
52 | |
92 | |
53 | use base Exporter::; |
93 | use base Exporter::; |
54 | |
94 | |
55 | our @EXPORT = qw(loop unloop sweep reschedule); |
95 | our @EXPORT = qw(loop unloop sweep); |
56 | |
96 | |
57 | BEGIN { |
97 | BEGIN { |
58 | our $VERSION = 1.5; |
98 | our $VERSION = 4.6; |
59 | |
99 | |
60 | local $^W = 0; # avoid redefine warning for Coro::ready; |
100 | local $^W = 0; # avoid redefine warning for Coro::ready; |
61 | XSLoader::load __PACKAGE__, $VERSION; |
101 | XSLoader::load __PACKAGE__, $VERSION; |
62 | } |
102 | } |
63 | |
103 | |
64 | =item $w = Coro::Event->flavour(args...) |
104 | =item $w = Coro::Event->flavour (args...) |
65 | |
105 | |
66 | Create and return a watcher of the given type. |
106 | Create and return a watcher of the given type. |
67 | |
107 | |
68 | Examples: |
108 | Examples: |
69 | |
109 | |
… | |
… | |
72 | |
112 | |
73 | =cut |
113 | =cut |
74 | |
114 | |
75 | =item $w->next |
115 | =item $w->next |
76 | |
116 | |
77 | Return the next event of the event queue of the watcher. |
117 | Wait for and return the next event of the event queue of the watcher. The |
|
|
118 | returned event objects support two methods only: C<hits> and C<got>, both |
|
|
119 | of which return integers: the number this watcher was hit for this event, |
|
|
120 | and the mask of poll events received. |
78 | |
121 | |
79 | =cut |
122 | =cut |
80 | |
123 | |
81 | =item do_flavour(args...) |
124 | =item do_flavour args... |
82 | |
125 | |
83 | Create a watcher of the given type and immediately call it's next |
126 | Create a watcher of the given type and immediately call it's next method, |
|
|
127 | returning the event. |
|
|
128 | |
84 | method. This is less efficient then calling the constructor once and the |
129 | This is less efficient then calling the constructor once and the next |
85 | next method often, but it does save typing sometimes. |
130 | method often, but it does save typing sometimes. |
86 | |
131 | |
87 | =cut |
132 | =cut |
88 | |
133 | |
89 | for my $flavour (qw(idle var timer io signal)) { |
134 | for my $flavour (qw(idle var timer io signal)) { |
90 | push @EXPORT, "do_$flavour"; |
135 | push @EXPORT, "do_$flavour"; |
… | |
… | |
98 | |
143 | |
99 | shift eq Coro::Event:: |
144 | shift eq Coro::Event:: |
100 | or croak "event constructor \"Coro::Event->$flavour\" must be called as a static method"; |
145 | or croak "event constructor \"Coro::Event->$flavour\" must be called as a static method"; |
101 | |
146 | |
102 | my $w = $new->($class, |
147 | my $w = $new->($class, |
103 | desc => $flavour, |
148 | desc => $flavour, |
104 | @_, |
149 | @_, |
105 | parked => 1, |
150 | parked => 1, |
106 | ); |
151 | ); |
|
|
152 | |
107 | _install_std_cb($w, $type); |
153 | _install_std_cb $w, $type; |
108 | bless $w, $class; # reblessing due to broken Event |
154 | |
|
|
155 | # reblessing due to Event being broken |
|
|
156 | bless $w, $class |
109 | }; |
157 | }; |
110 | *{ $flavour } = $coronew; |
158 | *{ $flavour } = $coronew; |
111 | *{"do_$flavour"} = sub { |
159 | *{"do_$flavour"} = sub { |
112 | unshift @_, Coro::Event::; |
160 | unshift @_, Coro::Event::; |
113 | my $e = (&$coronew)->next; |
161 | @_ = &$coronew; |
114 | $e->cancel; # $e === $e->w |
162 | &Coro::schedule while &_next; |
115 | $e; |
163 | $_[0]->cancel; |
|
|
164 | &_event |
116 | }; |
165 | }; |
117 | } |
166 | } |
118 | |
167 | |
119 | # double calls to avoid stack-cloning ;() |
168 | # do schedule in perl to avoid forcing a stack allocation. |
120 | # is about 10% slower, though. |
169 | # this is about 10% slower, though. |
121 | sub next($) { |
170 | sub next($) { |
122 | &Coro::schedule if &_next; $_[0]; |
171 | &Coro::schedule while &_next; |
|
|
172 | &_event |
123 | } |
173 | } |
124 | |
174 | |
|
|
175 | sub Coro::Event::Event::hits { $_[0][3] } |
125 | sub Coro::Event::w { $_[0] } |
176 | sub Coro::Event::Event::got { $_[0][4] } |
126 | sub Coro::Event::prio { $_[0]{Coro::Event}[3] } |
|
|
127 | sub Coro::Event::hits { $_[0]{Coro::Event}[4] } |
|
|
128 | sub Coro::Event::got { $_[0]{Coro::Event}[5] } |
|
|
129 | |
177 | |
130 | =item sweep |
178 | =item sweep |
131 | |
179 | |
132 | Similar to Event::one_event and Event::sweep: The idle task is called once |
180 | Similar to Event::one_event and Event::sweep: The idle task is called once |
133 | (this has the effect of jumping back into the Event loop once to serve new |
181 | (this has the effect of jumping back into the Event loop once to serve new |
… | |
… | |
139 | into the Event dispatcher. |
187 | into the Event dispatcher. |
140 | |
188 | |
141 | =cut |
189 | =cut |
142 | |
190 | |
143 | sub sweep { |
191 | sub sweep { |
144 | Event::one_event(0); # for now |
192 | Event::one_event 0; # for now |
145 | } |
193 | } |
146 | |
194 | |
147 | =item $result = loop([$timeout]) |
195 | =item $result = loop([$timeout]) |
148 | |
196 | |
149 | This is the version of C<loop> you should use instead of C<Event::loop> |
197 | 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 |
198 | when using this module - it will ensure correct scheduling in the presence |
151 | of events. |
199 | of events. |
152 | |
200 | |
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]) |
201 | =item unloop([$result]) |
171 | |
202 | |
172 | Same as Event::unloop (provided here for your convinience only). |
203 | Same as Event::unloop (provided here for your convinience only). |
173 | |
204 | |
174 | =cut |
205 | =cut |
175 | |
206 | |
176 | $Coro::idle = new Coro sub { |
207 | # very inefficient |
|
|
208 | our $event_idle = new Coro sub { |
177 | while () { |
209 | while () { |
178 | Event::one_event; # inefficient |
210 | &Event::one_event; |
179 | Coro::schedule; |
211 | &Coro::schedule; |
180 | } |
212 | } |
181 | }; |
213 | }; |
|
|
214 | $event_idle->{desc} = "[Event idle process]"; |
182 | |
215 | |
183 | # provide hooks for Coro::Timer |
216 | $Coro::idle = sub { $event_idle->ready }; |
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 | |
217 | |
194 | 1; |
218 | 1; |
|
|
219 | |
|
|
220 | =back |
195 | |
221 | |
196 | =head1 AUTHOR |
222 | =head1 AUTHOR |
197 | |
223 | |
198 | Marc Lehmann <schmorp@schmorp.de> |
224 | Marc Lehmann <schmorp@schmorp.de> |
199 | http://home.schmorp.de/ |
225 | http://home.schmorp.de/ |