… | |
… | |
36 | |
36 | |
37 | use Coro::State; |
37 | use Coro::State; |
38 | |
38 | |
39 | use base Exporter; |
39 | use base Exporter; |
40 | |
40 | |
41 | $VERSION = 0.12; |
41 | $VERSION = 0.45; |
42 | |
42 | |
43 | @EXPORT = qw(async cede schedule terminate current); |
43 | @EXPORT = qw(async cede schedule terminate current); |
44 | @EXPORT_OK = qw($current); |
44 | @EXPORT_OK = qw($current); |
45 | |
45 | |
46 | { |
46 | { |
47 | my @async; |
47 | my @async; |
|
|
48 | my $init; |
48 | |
49 | |
49 | # this way of handling attributes simply is NOT scalable ;() |
50 | # this way of handling attributes simply is NOT scalable ;() |
50 | sub import { |
51 | sub import { |
51 | Coro->export_to_level(1, @_); |
52 | Coro->export_to_level(1, @_); |
52 | my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; |
53 | my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; |
… | |
… | |
54 | my ($package, $ref) = (shift, shift); |
55 | my ($package, $ref) = (shift, shift); |
55 | my @attrs; |
56 | my @attrs; |
56 | for (@_) { |
57 | for (@_) { |
57 | if ($_ eq "Coro") { |
58 | if ($_ eq "Coro") { |
58 | push @async, $ref; |
59 | push @async, $ref; |
|
|
60 | unless ($init++) { |
|
|
61 | eval q{ |
|
|
62 | sub INIT { |
|
|
63 | &async(pop @async) while @async; |
|
|
64 | } |
|
|
65 | }; |
|
|
66 | } |
59 | } else { |
67 | } else { |
60 | push @attrs, $_; |
68 | push @attrs, $_; |
61 | } |
69 | } |
62 | } |
70 | } |
63 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
71 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
64 | }; |
72 | }; |
65 | } |
73 | } |
66 | |
74 | |
67 | sub INIT { |
|
|
68 | &async(pop @async) while @async; |
|
|
69 | } |
|
|
70 | } |
75 | } |
71 | |
76 | |
72 | =item $main |
77 | =item $main |
73 | |
78 | |
74 | This coroutine represents the main program. |
79 | This coroutine represents the main program. |
… | |
… | |
113 | delete ((pop @destroy)->{_coro_state}) while @destroy; |
118 | delete ((pop @destroy)->{_coro_state}) while @destroy; |
114 | &schedule; |
119 | &schedule; |
115 | } |
120 | } |
116 | }; |
121 | }; |
117 | |
122 | |
118 | # we really need priorities... |
|
|
119 | my @ready; # the ready queue. hehe, rather broken ;) |
|
|
120 | |
|
|
121 | # static methods. not really. |
123 | # static methods. not really. |
122 | |
124 | |
123 | =head2 STATIC METHODS |
125 | =head2 STATIC METHODS |
124 | |
126 | |
125 | Static methods are actually functions that operate on the current process only. |
127 | Static methods are actually functions that operate on the current process only. |
… | |
… | |
155 | into the ready queue, so calling this function usually means you will |
157 | into the ready queue, so calling this function usually means you will |
156 | never be called again. |
158 | never be called again. |
157 | |
159 | |
158 | =cut |
160 | =cut |
159 | |
161 | |
160 | my $prev; |
|
|
161 | |
|
|
162 | sub schedule { |
|
|
163 | # should be done using priorities :( |
|
|
164 | ($prev, $current) = ($current, shift @ready || $idle); |
|
|
165 | Coro::State::transfer($prev, $current); |
|
|
166 | } |
|
|
167 | |
|
|
168 | =item cede |
162 | =item cede |
169 | |
163 | |
170 | "Cede" to other processes. This function puts the current process into the |
164 | "Cede" to other processes. This function puts the current process into the |
171 | ready queue and calls C<schedule>, which has the effect of giving up the |
165 | ready queue and calls C<schedule>, which has the effect of giving up the |
172 | current "timeslice" to other coroutines of the same or higher priority. |
166 | current "timeslice" to other coroutines of the same or higher priority. |
173 | |
167 | |
174 | =cut |
168 | =cut |
175 | |
169 | |
176 | sub cede { |
170 | =item terminate |
|
|
171 | |
|
|
172 | Terminates the current process. |
|
|
173 | |
|
|
174 | Future versions of this function will allow result arguments. |
|
|
175 | |
|
|
176 | =cut |
|
|
177 | |
|
|
178 | sub terminate { |
177 | $current->ready; |
179 | $current->cancel; |
178 | &schedule; |
180 | &schedule; |
179 | } |
|
|
180 | |
|
|
181 | =item terminate |
|
|
182 | |
|
|
183 | Terminates the current process. |
|
|
184 | |
|
|
185 | Future versions of this function will allow result arguments. |
|
|
186 | |
|
|
187 | =cut |
|
|
188 | |
|
|
189 | sub terminate { |
|
|
190 | push @destroy, $current; |
|
|
191 | $manager->ready; |
|
|
192 | &schedule; |
|
|
193 | # NORETURN |
181 | die; # NORETURN |
194 | } |
182 | } |
195 | |
183 | |
196 | =back |
184 | =back |
197 | |
185 | |
198 | # dynamic methods |
186 | # dynamic methods |
… | |
… | |
229 | |
217 | |
230 | Put the current process into the ready queue. |
218 | Put the current process into the ready queue. |
231 | |
219 | |
232 | =cut |
220 | =cut |
233 | |
221 | |
234 | sub ready { |
222 | =item $process->cancel |
|
|
223 | |
|
|
224 | Like C<terminate>, but terminates the specified process instead. |
|
|
225 | |
|
|
226 | =cut |
|
|
227 | |
|
|
228 | sub cancel { |
235 | push @ready, $_[0]; |
229 | push @destroy, $_[0]; |
|
|
230 | $manager->ready; |
236 | } |
231 | } |
237 | |
232 | |
238 | =back |
233 | =back |
239 | |
234 | |
240 | =cut |
235 | =cut |
… | |
… | |
256 | allow per-thread schedulers, but Coro::State does not yet allow this). |
251 | allow per-thread schedulers, but Coro::State does not yet allow this). |
257 | |
252 | |
258 | =head1 SEE ALSO |
253 | =head1 SEE ALSO |
259 | |
254 | |
260 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
255 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
261 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |
256 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>, L<Coro::RWLock>, |
|
|
257 | L<Coro::Handle>, L<Coro::Socket>. |
262 | |
258 | |
263 | =head1 AUTHOR |
259 | =head1 AUTHOR |
264 | |
260 | |
265 | Marc Lehmann <pcg@goof.com> |
261 | Marc Lehmann <pcg@goof.com> |
266 | http://www.goof.com/pcg/marc/ |
262 | http://www.goof.com/pcg/marc/ |