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