… | |
… | |
18 | |
18 | |
19 | cede; |
19 | cede; |
20 | |
20 | |
21 | =head1 DESCRIPTION |
21 | =head1 DESCRIPTION |
22 | |
22 | |
23 | This module collection manages coroutines. Coroutines are similar to |
23 | This module collection manages coroutines. Coroutines are similar |
24 | threads but don't run in parallel. |
24 | to threads but don't run in parallel at the same time even on SMP |
|
|
25 | machines. The specific flavor of coroutine use din this module also |
|
|
26 | guarentees you that it will not switch between coroutines unless |
|
|
27 | necessary, at easily-identified points in your program, so locking and |
|
|
28 | parallel access are rarely an issue, making coroutine programming much |
|
|
29 | safer than threads programming. |
25 | |
30 | |
|
|
31 | (Perl, however, does not natively support real threads but instead does a |
|
|
32 | very slow and memory-intensive emulation of processes using threads. This |
|
|
33 | is a performance win on Windows machines, and a loss everywhere else). |
|
|
34 | |
26 | In this module, coroutines are defined as "callchain + lexical variables |
35 | In this module, coroutines are defined as "callchain + lexical variables + |
27 | + @_ + $_ + $@ + $^W + C stack), that is, a coroutine has it's own |
36 | @_ + $_ + $@ + $/ + C stack), that is, a coroutine has its own callchain, |
28 | callchain, it's own set of lexicals and it's own set of perl's most |
37 | its own set of lexicals and its own set of perls most important global |
29 | important global variables. |
38 | variables. |
30 | |
39 | |
31 | =cut |
40 | =cut |
32 | |
41 | |
33 | package Coro; |
42 | package Coro; |
34 | |
43 | |
… | |
… | |
41 | |
50 | |
42 | our $idle; # idle handler |
51 | our $idle; # idle handler |
43 | our $main; # main coroutine |
52 | our $main; # main coroutine |
44 | our $current; # current coroutine |
53 | our $current; # current coroutine |
45 | |
54 | |
46 | our $VERSION = '3.01'; |
55 | our $VERSION = '3.3'; |
47 | |
56 | |
48 | our @EXPORT = qw(async cede schedule terminate current unblock_sub); |
57 | our @EXPORT = qw(async cede schedule terminate current unblock_sub); |
49 | our %EXPORT_TAGS = ( |
58 | our %EXPORT_TAGS = ( |
50 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
59 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
51 | ); |
60 | ); |
… | |
… | |
144 | # been readied multiple times. this is harmless since the manager |
153 | # been readied multiple times. this is harmless since the manager |
145 | # can be called as many times as neccessary and will always |
154 | # can be called as many times as neccessary and will always |
146 | # remove itself from the runqueue |
155 | # remove itself from the runqueue |
147 | while (@destroy) { |
156 | while (@destroy) { |
148 | my $coro = pop @destroy; |
157 | my $coro = pop @destroy; |
|
|
158 | |
149 | $coro->{status} ||= []; |
159 | $coro->{status} ||= []; |
|
|
160 | |
150 | $_->ready for @{delete $coro->{join} || []}; |
161 | $_->ready for @{(delete $coro->{join} ) || []}; |
|
|
162 | $_->(@{$coro->{status}}) for @{(delete $coro->{destroy_cb}) || []}; |
151 | |
163 | |
152 | # the next line destroys the coro state, but keeps the |
164 | # the next line destroys the coro state, but keeps the |
153 | # coroutine itself intact (we basically make it a zombie |
165 | # coroutine itself intact (we basically make it a zombie |
154 | # coroutine that always runs the manager thread, so it's possible |
166 | # coroutine that always runs the manager thread, so it's possible |
155 | # to transfer() to this coroutine). |
167 | # to transfer() to this coroutine). |
… | |
… | |
223 | |
235 | |
224 | "Cede" to other coroutines. This function puts the current coroutine into the |
236 | "Cede" to other coroutines. This function puts the current coroutine into the |
225 | ready queue and calls C<schedule>, which has the effect of giving up the |
237 | ready queue and calls C<schedule>, which has the effect of giving up the |
226 | current "timeslice" to other coroutines of the same or higher priority. |
238 | current "timeslice" to other coroutines of the same or higher priority. |
227 | |
239 | |
|
|
240 | =item Coro::cede_notself |
|
|
241 | |
|
|
242 | Works like cede, but is not exported by default and will cede to any |
|
|
243 | coroutine, regardless of priority, once. |
|
|
244 | |
228 | =item terminate [arg...] |
245 | =item terminate [arg...] |
229 | |
246 | |
230 | Terminates the current coroutine with the given status values (see L<cancel>). |
247 | Terminates the current coroutine with the given status values (see L<cancel>). |
231 | |
248 | |
232 | =cut |
249 | =cut |
… | |
… | |
304 | unless ($self->{status}) { |
321 | unless ($self->{status}) { |
305 | push @{$self->{join}}, $current; |
322 | push @{$self->{join}}, $current; |
306 | &schedule; |
323 | &schedule; |
307 | } |
324 | } |
308 | wantarray ? @{$self->{status}} : $self->{status}[0]; |
325 | wantarray ? @{$self->{status}} : $self->{status}[0]; |
|
|
326 | } |
|
|
327 | |
|
|
328 | =item $coroutine->on_destroy (\&cb) |
|
|
329 | |
|
|
330 | Registers a callback that is called when this coroutine gets destroyed, |
|
|
331 | but before it is joined. The callback gets passed the terminate arguments, |
|
|
332 | if any. |
|
|
333 | |
|
|
334 | =cut |
|
|
335 | |
|
|
336 | sub on_destroy { |
|
|
337 | my ($self, $cb) = @_; |
|
|
338 | |
|
|
339 | push @{ $self->{destroy_cb} }, $cb; |
309 | } |
340 | } |
310 | |
341 | |
311 | =item $oldprio = $coroutine->prio ($newprio) |
342 | =item $oldprio = $coroutine->prio ($newprio) |
312 | |
343 | |
313 | Sets (or gets, if the argument is missing) the priority of the |
344 | Sets (or gets, if the argument is missing) the priority of the |