… | |
… | |
30 | |
30 | |
31 | =cut |
31 | =cut |
32 | |
32 | |
33 | package Coro; |
33 | package Coro; |
34 | |
34 | |
35 | BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } |
35 | use strict; |
|
|
36 | no warnings "uninitialized"; |
36 | |
37 | |
37 | use Coro::State; |
38 | use Coro::State; |
38 | |
39 | |
39 | use vars qw($idle $main $current); |
|
|
40 | |
|
|
41 | use base Exporter; |
40 | use base Exporter::; |
42 | |
41 | |
43 | $VERSION = "0.9"; |
42 | our $idle; # idle coroutine |
|
|
43 | our $main; # main coroutine |
|
|
44 | our $current; # current coroutine |
44 | |
45 | |
|
|
46 | our $VERSION = '2.5'; |
|
|
47 | |
45 | @EXPORT = qw(async cede schedule terminate current); |
48 | our @EXPORT = qw(async cede schedule terminate current); |
46 | %EXPORT_TAGS = ( |
49 | our %EXPORT_TAGS = ( |
47 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
50 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
48 | ); |
51 | ); |
49 | @EXPORT_OK = @{$EXPORT_TAGS{prio}}; |
52 | our @EXPORT_OK = @{$EXPORT_TAGS{prio}}; |
50 | |
53 | |
51 | { |
54 | { |
52 | my @async; |
55 | my @async; |
53 | my $init; |
56 | my $init; |
54 | |
57 | |
55 | # this way of handling attributes simply is NOT scalable ;() |
58 | # this way of handling attributes simply is NOT scalable ;() |
56 | sub import { |
59 | sub import { |
|
|
60 | no strict 'refs'; |
|
|
61 | |
57 | Coro->export_to_level(1, @_); |
62 | Coro->export_to_level(1, @_); |
|
|
63 | |
58 | my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; |
64 | my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; |
59 | *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"} = sub { |
65 | *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"} = sub { |
60 | my ($package, $ref) = (shift, shift); |
66 | my ($package, $ref) = (shift, shift); |
61 | my @attrs; |
67 | my @attrs; |
62 | for (@_) { |
68 | for (@_) { |
… | |
… | |
120 | # this coroutine is necessary because a coroutine |
126 | # this coroutine is necessary because a coroutine |
121 | # cannot destroy itself. |
127 | # cannot destroy itself. |
122 | my @destroy; |
128 | my @destroy; |
123 | my $manager; |
129 | my $manager; |
124 | $manager = new Coro sub { |
130 | $manager = new Coro sub { |
125 | while() { |
131 | while () { |
126 | # by overwriting the state object with the manager we destroy it |
132 | # by overwriting the state object with the manager we destroy it |
127 | # while still being able to schedule this coroutine (in case it has |
133 | # while still being able to schedule this coroutine (in case it has |
128 | # been readied multiple times. this is harmless since the manager |
134 | # been readied multiple times. this is harmless since the manager |
129 | # can be called as many times as neccessary and will always |
135 | # can be called as many times as neccessary and will always |
130 | # remove itself from the runqueue |
136 | # remove itself from the runqueue |
131 | while (@destroy) { |
137 | while (@destroy) { |
132 | my $coro = pop @destroy; |
138 | my $coro = pop @destroy; |
133 | $coro->{status} ||= []; |
139 | $coro->{status} ||= []; |
134 | $_->ready for @{delete $coro->{join} || []}; |
140 | $_->ready for @{delete $coro->{join} || []}; |
|
|
141 | |
|
|
142 | # the next line destroys the _coro_state, but keeps the |
|
|
143 | # process itself intact (we basically make it a zombie |
|
|
144 | # process that always runs the manager thread, so it's possible |
|
|
145 | # to transfer() to this process). |
135 | $coro->{_coro_state} = $manager->{_coro_state}; |
146 | $coro->{_coro_state} = $manager->{_coro_state}; |
136 | } |
147 | } |
137 | &schedule; |
148 | &schedule; |
138 | } |
149 | } |
139 | }; |
150 | }; |
… | |
… | |
152 | |
163 | |
153 | Create a new asynchronous process and return it's process object |
164 | Create a new asynchronous process and return it's process object |
154 | (usually unused). When the sub returns the new process is automatically |
165 | (usually unused). When the sub returns the new process is automatically |
155 | terminated. |
166 | terminated. |
156 | |
167 | |
|
|
168 | When the coroutine dies, the program will exit, just as in the main |
|
|
169 | program. |
|
|
170 | |
157 | # create a new coroutine that just prints its arguments |
171 | # create a new coroutine that just prints its arguments |
158 | async { |
172 | async { |
159 | print "@_\n"; |
173 | print "@_\n"; |
160 | } 1,2,3,4; |
174 | } 1,2,3,4; |
161 | |
|
|
162 | The coderef you submit MUST NOT be a closure that refers to variables |
|
|
163 | in an outer scope. This does NOT work. Pass arguments into it instead. |
|
|
164 | |
175 | |
165 | =cut |
176 | =cut |
166 | |
177 | |
167 | sub async(&@) { |
178 | sub async(&@) { |
168 | my $pid = new Coro @_; |
179 | my $pid = new Coro @_; |
… | |
… | |
187 | |
198 | |
188 | =cut |
199 | =cut |
189 | |
200 | |
190 | =item terminate [arg...] |
201 | =item terminate [arg...] |
191 | |
202 | |
192 | Terminates the current process. |
203 | Terminates the current process with the given status values (see L<cancel>). |
193 | |
|
|
194 | Future versions of this function will allow result arguments. |
|
|
195 | |
204 | |
196 | =cut |
205 | =cut |
197 | |
206 | |
198 | sub terminate { |
207 | sub terminate { |
199 | $current->{status} = [@_]; |
|
|
200 | $current->cancel; |
208 | $current->cancel (@_); |
201 | &schedule; |
|
|
202 | die; # NORETURN |
|
|
203 | } |
209 | } |
204 | |
210 | |
205 | =back |
211 | =back |
206 | |
212 | |
207 | # dynamic methods |
213 | # dynamic methods |
… | |
… | |
226 | } |
232 | } |
227 | |
233 | |
228 | sub new { |
234 | sub new { |
229 | my $class = shift; |
235 | my $class = shift; |
230 | bless { |
236 | bless { |
231 | _coro_state => (new Coro::State $_[0] && \&_newcoro, @_), |
237 | _coro_state => (new Coro::State \&_newcoro, @_), |
232 | }, $class; |
238 | }, $class; |
233 | } |
239 | } |
234 | |
240 | |
235 | =item $process->ready |
241 | =item $process->ready |
236 | |
242 | |
237 | Put the given process into the ready queue. |
243 | Put the given process into the ready queue. |
238 | |
244 | |
239 | =cut |
245 | =cut |
240 | |
246 | |
241 | =item $process->cancel |
247 | =item $process->cancel (arg...) |
242 | |
248 | |
243 | Like C<terminate>, but terminates the specified process instead. |
249 | Terminates the given process and makes it return the given arguments as |
|
|
250 | status (default: the empty list). |
244 | |
251 | |
245 | =cut |
252 | =cut |
246 | |
253 | |
247 | sub cancel { |
254 | sub cancel { |
|
|
255 | my $self = shift; |
|
|
256 | $self->{status} = [@_]; |
248 | push @destroy, $_[0]; |
257 | push @destroy, $self; |
249 | $manager->ready; |
258 | $manager->ready; |
250 | &schedule if $current == $_[0]; |
259 | &schedule if $current == $self; |
251 | } |
260 | } |
252 | |
261 | |
253 | =item $process->join |
262 | =item $process->join |
254 | |
263 | |
255 | Wait until the coroutine terminates and return any values given to the |
264 | Wait until the coroutine terminates and return any values given to the |
256 | C<terminate> function. C<join> can be called multiple times from multiple |
265 | C<terminate> or C<cancel> functions. C<join> can be called multiple times |
257 | processes. |
266 | from multiple processes. |
258 | |
267 | |
259 | =cut |
268 | =cut |
260 | |
269 | |
261 | sub join { |
270 | sub join { |
262 | my $self = shift; |
271 | my $self = shift; |
… | |
… | |
337 | to allow per-thread schedulers, but Coro::State does not yet allow |
346 | to allow per-thread schedulers, but Coro::State does not yet allow |
338 | this). |
347 | this). |
339 | |
348 | |
340 | =head1 SEE ALSO |
349 | =head1 SEE ALSO |
341 | |
350 | |
342 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
351 | Support/Utility: L<Coro::Cont>, L<Coro::Specific>, L<Coro::State>, L<Coro::Util>. |
343 | L<Coro::Signal>, L<Coro::State>, L<Coro::Timer>, L<Coro::Event>, |
352 | |
344 | L<Coro::L<Coro::RWLock>, Handle>, L<Coro::Socket>. |
353 | Locking/IPC: L<Coro::Signal>, L<Coro::Channel>, L<Coro::Semaphore>, L<Coro::SemaphoreSet>, L<Coro::RWLock>. |
|
|
354 | |
|
|
355 | Event/IO: L<Coro::Timer>, L<Coro::Event>, L<Coro::Handle>, L<Coro::Socket>, L<Coro::Select>. |
|
|
356 | |
|
|
357 | Embedding: L<Coro:MakeMaker> |
345 | |
358 | |
346 | =head1 AUTHOR |
359 | =head1 AUTHOR |
347 | |
360 | |
348 | Marc Lehmann <pcg@goof.com> |
361 | Marc Lehmann <schmorp@schmorp.de> |
349 | http://www.goof.com/pcg/marc/ |
362 | http://home.schmorp.de/ |
350 | |
363 | |
351 | =cut |
364 | =cut |
352 | |
365 | |