… | |
… | |
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 | |
22 | |
23 | =head1 DESCRIPTION |
23 | =head1 DESCRIPTION |
24 | |
24 | |
25 | This module enables you to create programs using the powerful Event model |
25 | This module enables you to create programs using the powerful Event model |
26 | (and module), while retaining the linear style known from simple or |
26 | (and module), while retaining the linear style known from simple or |
… | |
… | |
30 | (I<flavour>) (see L<Event>). The only difference between these and the |
30 | (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 |
31 | watcher constructors from Event is that you do not specify a callback |
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->main. |
35 | Coro::Event::loop. |
|
|
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 | |
|
|
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 SEMANTICS |
|
|
53 | |
|
|
54 | Whenever Event blocks (e.g. in a call to C<one_event>, C<loop> etc.), |
|
|
55 | this module cede's to all other coroutines with the same or higher |
|
|
56 | priority. When any coroutines of lower priority are ready, it will not |
|
|
57 | block but run one of them and then check for events. |
|
|
58 | |
|
|
59 | The effect is that coroutines with the same or higher priority than |
|
|
60 | the blocking coroutine will keep Event from checking for events, while |
|
|
61 | coroutines with lower priority are being run, but Event checks for new |
|
|
62 | events after every cede. |
|
|
63 | |
|
|
64 | =head1 FUNCTIONS |
36 | |
65 | |
37 | =over 4 |
66 | =over 4 |
38 | |
67 | |
39 | =cut |
68 | =cut |
40 | |
69 | |
41 | package Coro::Event; |
70 | package Coro::Event; |
42 | |
71 | |
43 | no warnings qw(uninitialized); |
72 | no warnings; |
44 | |
73 | |
45 | use Carp; |
74 | use Carp; |
|
|
75 | no warnings; |
46 | |
76 | |
47 | use Coro; |
77 | use Coro; |
|
|
78 | use Coro::Timer; |
48 | use Event qw(unloop); # we are re-exporting this, cooool! |
79 | use Event qw(loop unloop); # we are re-exporting this, cooool! |
49 | |
80 | |
|
|
81 | use XSLoader; |
|
|
82 | |
50 | use base 'Exporter'; |
83 | use base Exporter::; |
51 | |
84 | |
52 | @EXPORT = qw(loop unloop sweep reschedule); |
85 | our @EXPORT = qw(loop unloop sweep); |
53 | |
86 | |
54 | BEGIN { |
87 | BEGIN { |
55 | $VERSION = 0.45; |
88 | our $VERSION = '2.0'; |
56 | |
89 | |
57 | local $^W = 0; # avoid redefine warning for Coro::ready |
90 | local $^W = 0; # avoid redefine warning for Coro::ready; |
58 | require XSLoader; |
|
|
59 | XSLoader::load Coro::Event, $VERSION; |
91 | XSLoader::load __PACKAGE__, $VERSION; |
60 | } |
92 | } |
61 | |
93 | |
62 | =item $w = Coro::Event->flavour(args...) |
94 | =item $w = Coro::Event->flavour (args...) |
63 | |
95 | |
64 | Create and return a watcher of the given type. |
96 | Create and return a watcher of the given type. |
65 | |
97 | |
66 | Examples: |
98 | Examples: |
67 | |
99 | |
… | |
… | |
74 | |
106 | |
75 | Return the next event of the event queue of the watcher. |
107 | Return the next event of the event queue of the watcher. |
76 | |
108 | |
77 | =cut |
109 | =cut |
78 | |
110 | |
79 | =item do_flavour(args...) |
111 | =item do_flavour args... |
80 | |
112 | |
81 | Create a watcher of the given type and immediately call it's next |
113 | Create a watcher of the given type and immediately call it's next |
82 | method. This is less efficient then calling the constructor once and the |
114 | method. This is less efficient then calling the constructor once and the |
83 | next method often, but it does save typing sometimes. |
115 | next method often, but it does save typing sometimes. |
84 | |
116 | |
… | |
… | |
92 | @{"${class}::ISA"} = (Coro::Event::, "Event::$flavour"); |
124 | @{"${class}::ISA"} = (Coro::Event::, "Event::$flavour"); |
93 | my $coronew = sub { |
125 | my $coronew = sub { |
94 | # how does one do method-call-by-name? |
126 | # how does one do method-call-by-name? |
95 | # my $w = $class->SUPER::$flavour(@_); |
127 | # my $w = $class->SUPER::$flavour(@_); |
96 | |
128 | |
97 | $_[0] eq Coro::Event:: |
129 | shift eq Coro::Event:: |
98 | or croak "event constructor \"Coro::Event->$flavour\" must be called as a static method"; |
130 | or croak "event constructor \"Coro::Event->$flavour\" must be called as a static method"; |
99 | |
131 | |
100 | my $q = []; # [$coro, $event] |
|
|
101 | my $w = $new->( |
132 | my $w = $new->($class, |
102 | desc => $flavour, |
133 | desc => $flavour, |
103 | @_, |
134 | @_, |
104 | parked => 1, |
135 | parked => 1, |
105 | ); |
136 | ); |
|
|
137 | |
106 | _install_std_cb($w, $type); |
138 | _install_std_cb $w, $type; |
107 | bless $w, $class; # reblessing due to broken Event |
139 | |
|
|
140 | # reblessing due to Event being broken |
|
|
141 | bless $w, $class |
108 | }; |
142 | }; |
109 | *{ $flavour } = $coronew; |
143 | *{ $flavour } = $coronew; |
110 | *{"do_$flavour"} = sub { |
144 | *{"do_$flavour"} = sub { |
111 | unshift @_, Coro::Event::; |
145 | unshift @_, Coro::Event::; |
112 | my $e = (&$coronew)->next; |
146 | @_ = &$coronew; |
113 | $e->cancel; # $e = $e->w |
147 | &Coro::schedule while &_next; |
114 | $e; |
148 | $_[0]->cancel; |
|
|
149 | &_event |
115 | }; |
150 | }; |
116 | } |
151 | } |
117 | |
152 | |
118 | # double calls to avoid stack-cloning ;() |
153 | # do schedule in perl to avoid forcing a stack allocation. |
119 | # is about 10% slower, though. |
154 | # this is about 10% slower, though. |
120 | sub next($) { |
155 | sub next($) { |
121 | &Coro::schedule if &_next; $_[0]; |
156 | &Coro::schedule while &_next; |
|
|
157 | |
|
|
158 | &_event |
122 | } |
159 | } |
123 | |
160 | |
124 | sub Coro::Event::w { $_[0] } |
161 | sub Coro::Event::w { $_[0] } |
125 | sub Coro::Event::prio { $_[0]{Coro::Event}[3] } |
162 | sub Coro::Event::prio { $_[0]{Coro::Event}[3] } |
126 | sub Coro::Event::hits { $_[0]{Coro::Event}[4] } |
163 | sub Coro::Event::hits { $_[0]{Coro::Event}[4] } |
… | |
… | |
138 | into the Event dispatcher. |
175 | into the Event dispatcher. |
139 | |
176 | |
140 | =cut |
177 | =cut |
141 | |
178 | |
142 | sub sweep { |
179 | sub sweep { |
143 | Event::one_event(0); # for now |
180 | Event::one_event 0; # for now |
144 | } |
181 | } |
145 | |
182 | |
146 | =item $result = loop([$timeout]) |
183 | =item $result = loop([$timeout]) |
147 | |
184 | |
148 | This is the version of C<loop> you should use instead of C<Event::loop> |
185 | 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 |
186 | when using this module - it will ensure correct scheduling in the presence |
150 | of events. |
187 | of events. |
151 | |
188 | |
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]) |
189 | =item unloop([$result]) |
168 | |
190 | |
169 | Same as Event::unloop (provided here for your convinience only). |
191 | Same as Event::unloop (provided here for your convinience only). |
170 | |
192 | |
171 | =cut |
193 | =cut |
172 | |
194 | |
173 | $Coro::idle = new Coro sub { |
195 | $Coro::idle = \&Event::one_event; # inefficient |
174 | while () { |
|
|
175 | Event::one_event; # inefficient |
|
|
176 | Coro::schedule; |
|
|
177 | } |
|
|
178 | }; |
|
|
179 | |
196 | |
180 | 1; |
197 | 1; |
181 | |
198 | |
|
|
199 | =back |
|
|
200 | |
182 | =head1 AUTHOR |
201 | =head1 AUTHOR |
183 | |
202 | |
184 | Marc Lehmann <pcg@goof.com> |
203 | Marc Lehmann <schmorp@schmorp.de> |
185 | http://www.goof.com/pcg/marc/ |
204 | http://home.schmorp.de/ |
186 | |
205 | |
187 | =cut |
206 | =cut |
188 | |
207 | |