… | |
… | |
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> "; |
17 | my $ev = $w->next; my $data = <STDIN>; |
17 | my $ev = $w->next; my $data = <STDIN>; |
18 | } |
18 | } |
19 | } |
19 | } |
20 | |
20 | |
21 | &loop; |
21 | loop; |
|
|
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 }; |
22 | |
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 |
… | |
… | |
30 | (I<flavour>) (see L<Event>). The only difference between these and the |
38 | (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 |
39 | watcher constructors from Event is that you do not specify a callback |
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->main. |
43 | Coro::Event::loop. |
|
|
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 |
36 | |
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 | no warnings qw(uninitialized); |
82 | no warnings; |
44 | |
83 | |
45 | use Carp; |
84 | use Carp; |
|
|
85 | no warnings; |
46 | |
86 | |
47 | use Coro; |
87 | use Coro; |
48 | use Event qw(unloop); # we are re-exporting this, cooool! |
88 | use Event qw(loop unloop); # we are re-exporting this, cooool! |
49 | |
89 | |
|
|
90 | use XSLoader; |
|
|
91 | |
50 | use base 'Exporter'; |
92 | use base Exporter::; |
51 | |
93 | |
52 | @EXPORT = qw(loop unloop sweep reschedule); |
94 | our @EXPORT = qw(loop unloop sweep); |
53 | |
95 | |
54 | BEGIN { |
96 | BEGIN { |
55 | $VERSION = 0.45; |
97 | our $VERSION = 4.74; |
56 | |
98 | |
57 | local $^W = 0; # avoid redefine warning for Coro::ready |
99 | local $^W = 0; # avoid redefine warning for Coro::ready; |
58 | require XSLoader; |
|
|
59 | XSLoader::load Coro::Event, $VERSION; |
100 | XSLoader::load __PACKAGE__, $VERSION; |
60 | } |
101 | } |
61 | |
102 | |
62 | =item $w = Coro::Event->flavour(args...) |
103 | =item $w = Coro::Event->flavour (args...) |
63 | |
104 | |
64 | Create and return a watcher of the given type. |
105 | Create and return a watcher of the given type. |
65 | |
106 | |
66 | Examples: |
107 | Examples: |
67 | |
108 | |
… | |
… | |
70 | |
111 | |
71 | =cut |
112 | =cut |
72 | |
113 | |
73 | =item $w->next |
114 | =item $w->next |
74 | |
115 | |
75 | Return the next event of the event queue of the watcher. |
116 | Wait for and return the next event of the event queue of the watcher. The |
|
|
117 | returned event objects support two methods only: C<hits> and C<got>, both |
|
|
118 | of which return integers: the number this watcher was hit for this event, |
|
|
119 | and the mask of poll events received. |
76 | |
120 | |
77 | =cut |
121 | =cut |
78 | |
122 | |
79 | =item do_flavour(args...) |
123 | =item do_flavour args... |
80 | |
124 | |
81 | Create a watcher of the given type and immediately call it's next |
125 | Create a watcher of the given type and immediately call it's next method, |
|
|
126 | returning the event. |
|
|
127 | |
82 | method. This is less efficient then calling the constructor once and the |
128 | This is less efficient then calling the constructor once and the next |
83 | next method often, but it does save typing sometimes. |
129 | method often, but it does save typing sometimes. |
84 | |
130 | |
85 | =cut |
131 | =cut |
86 | |
132 | |
87 | for my $flavour (qw(idle var timer io signal)) { |
133 | for my $flavour (qw(idle var timer io signal)) { |
88 | push @EXPORT, "do_$flavour"; |
134 | push @EXPORT, "do_$flavour"; |
… | |
… | |
92 | @{"${class}::ISA"} = (Coro::Event::, "Event::$flavour"); |
138 | @{"${class}::ISA"} = (Coro::Event::, "Event::$flavour"); |
93 | my $coronew = sub { |
139 | my $coronew = sub { |
94 | # how does one do method-call-by-name? |
140 | # how does one do method-call-by-name? |
95 | # my $w = $class->SUPER::$flavour(@_); |
141 | # my $w = $class->SUPER::$flavour(@_); |
96 | |
142 | |
97 | $_[0] eq Coro::Event:: |
143 | shift eq Coro::Event:: |
98 | or croak "event constructor \"Coro::Event->$flavour\" must be called as a static method"; |
144 | or croak "event constructor \"Coro::Event->$flavour\" must be called as a static method"; |
99 | |
145 | |
100 | my $q = []; # [$coro, $event] |
|
|
101 | my $w = $new->( |
146 | my $w = $new->($class, |
102 | desc => $flavour, |
147 | desc => $flavour, |
103 | @_, |
148 | @_, |
104 | parked => 1, |
149 | parked => 1, |
105 | ); |
150 | ); |
|
|
151 | |
106 | _install_std_cb($w, $type); |
152 | _install_std_cb $w, $type; |
107 | bless $w, $class; # reblessing due to broken Event |
153 | |
|
|
154 | # reblessing due to Event being broken |
|
|
155 | bless $w, $class |
108 | }; |
156 | }; |
109 | *{ $flavour } = $coronew; |
157 | *{ $flavour } = $coronew; |
110 | *{"do_$flavour"} = sub { |
158 | *{"do_$flavour"} = sub { |
111 | unshift @_, Coro::Event::; |
159 | unshift @_, Coro::Event::; |
112 | my $e = (&$coronew)->next; |
160 | @_ = &$coronew; |
113 | $e->cancel; # $e = $e->w |
161 | &Coro::schedule while &_next; |
114 | $e; |
162 | $_[0]->cancel; |
|
|
163 | &_event |
115 | }; |
164 | }; |
116 | } |
165 | } |
117 | |
166 | |
118 | # double calls to avoid stack-cloning ;() |
167 | # do schedule in perl to avoid forcing a stack allocation. |
119 | # is about 10% slower, though. |
168 | # this is about 10% slower, though. |
120 | sub next($) { |
169 | sub next($) { |
121 | &Coro::schedule if &_next; $_[0]; |
170 | &Coro::schedule while &_next; |
|
|
171 | &_event |
122 | } |
172 | } |
123 | |
173 | |
|
|
174 | sub Coro::Event::Event::hits { $_[0][3] } |
124 | sub Coro::Event::w { $_[0] } |
175 | sub Coro::Event::Event::got { $_[0][4] } |
125 | sub Coro::Event::prio { $_[0]{Coro::Event}[3] } |
|
|
126 | sub Coro::Event::hits { $_[0]{Coro::Event}[4] } |
|
|
127 | sub Coro::Event::got { $_[0]{Coro::Event}[5] } |
|
|
128 | |
176 | |
129 | =item sweep |
177 | =item sweep |
130 | |
178 | |
131 | Similar to Event::one_event and Event::sweep: The idle task is called once |
179 | Similar to Event::one_event and Event::sweep: The idle task is called once |
132 | (this has the effect of jumping back into the Event loop once to serve new |
180 | (this has the effect of jumping back into the Event loop once to serve new |
… | |
… | |
138 | into the Event dispatcher. |
186 | into the Event dispatcher. |
139 | |
187 | |
140 | =cut |
188 | =cut |
141 | |
189 | |
142 | sub sweep { |
190 | sub sweep { |
143 | one_event(0); # for now |
191 | Event::one_event 0; # for now |
144 | } |
192 | } |
145 | |
193 | |
146 | =item $result = loop([$timeout]) |
194 | =item $result = loop([$timeout]) |
147 | |
195 | |
148 | This is the version of C<loop> you should use instead of C<Event::loop> |
196 | This is the version of C<loop> you should use instead of C<Event::loop> |
149 | when using this module - it will ensure correct scheduling in the presence |
197 | when using this module - it will ensure correct scheduling in the presence |
150 | of events. |
198 | of events. |
151 | |
199 | |
152 | =begin comment |
|
|
153 | |
|
|
154 | Unlike loop's counterpart it is not an error when no watchers are active - |
|
|
155 | loop silently returns in this case, as if unloop(undef) were called. |
|
|
156 | |
|
|
157 | =end comment |
|
|
158 | |
|
|
159 | =cut |
|
|
160 | |
|
|
161 | sub loop(;$) { |
|
|
162 | local $Coro::idle = $Coro::current; |
|
|
163 | Coro::schedule; # become idle task, which is implicitly ready |
|
|
164 | &Event::loop; |
|
|
165 | } |
|
|
166 | |
|
|
167 | =item unloop([$result]) |
200 | =item unloop([$result]) |
168 | |
201 | |
169 | Same as Event::unloop (provided here for your convinience only). |
202 | Same as Event::unloop (provided here for your convinience only). |
170 | |
203 | |
171 | =cut |
204 | =cut |
172 | |
205 | |
173 | $Coro::idle = new Coro sub { |
206 | # very inefficient |
|
|
207 | our $event_idle = new Coro sub { |
174 | while () { |
208 | while () { |
175 | Event::one_event; # inefficient |
209 | &Event::one_event; |
176 | Coro::schedule; |
210 | &Coro::schedule; |
177 | } |
211 | } |
178 | }; |
212 | }; |
|
|
213 | $event_idle->{desc} = "[Event idle process]"; |
|
|
214 | |
|
|
215 | $Coro::idle = sub { $event_idle->ready }; |
179 | |
216 | |
180 | 1; |
217 | 1; |
181 | |
218 | |
|
|
219 | =back |
|
|
220 | |
182 | =head1 AUTHOR |
221 | =head1 AUTHOR |
183 | |
222 | |
184 | Marc Lehmann <pcg@goof.com> |
223 | Marc Lehmann <schmorp@schmorp.de> |
185 | http://www.goof.com/pcg/marc/ |
224 | http://home.schmorp.de/ |
186 | |
225 | |
187 | =cut |
226 | =cut |
188 | |
227 | |