… | |
… | |
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.0'; |
55 | our $VERSION = '3.1'; |
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 | ); |
52 | our @EXPORT_OK = @{$EXPORT_TAGS{prio}}; |
61 | our @EXPORT_OK = (@{$EXPORT_TAGS{prio}}, qw(nready)); |
53 | |
62 | |
54 | { |
63 | { |
55 | my @async; |
64 | my @async; |
56 | my $init; |
65 | my $init; |
57 | |
66 | |
… | |
… | |
128 | handlers), then it must be prepared to be called recursively. |
137 | handlers), then it must be prepared to be called recursively. |
129 | |
138 | |
130 | =cut |
139 | =cut |
131 | |
140 | |
132 | $idle = sub { |
141 | $idle = sub { |
133 | print STDERR "FATAL: deadlock detected\n"; |
142 | require Carp; |
134 | exit (51); |
143 | Carp::croak ("FATAL: deadlock detected"); |
135 | }; |
144 | }; |
136 | |
145 | |
137 | # this coroutine is necessary because a coroutine |
146 | # this coroutine is necessary because a coroutine |
138 | # cannot destroy itself. |
147 | # cannot destroy itself. |
139 | my @destroy; |
148 | my @destroy; |
… | |
… | |
254 | |
263 | |
255 | Calling C<exit> in a coroutine will not work correctly, so do not do that. |
264 | Calling C<exit> in a coroutine will not work correctly, so do not do that. |
256 | |
265 | |
257 | =cut |
266 | =cut |
258 | |
267 | |
259 | sub _new_coro { |
268 | sub _run_coro { |
260 | terminate &{+shift}; |
269 | terminate &{+shift}; |
261 | } |
270 | } |
262 | |
271 | |
263 | sub new { |
272 | sub new { |
264 | my $class = shift; |
273 | my $class = shift; |
265 | |
274 | |
266 | $class->SUPER::new (\&_new_coro, @_) |
275 | $class->SUPER::new (\&_run_coro, @_) |
267 | } |
276 | } |
268 | |
277 | |
269 | =item $success = $coroutine->ready |
278 | =item $success = $coroutine->ready |
270 | |
279 | |
271 | Put the given coroutine into the ready queue (according to it's priority) |
280 | Put the given coroutine into the ready queue (according to it's priority) |
… | |
… | |
348 | $old; |
357 | $old; |
349 | } |
358 | } |
350 | |
359 | |
351 | =back |
360 | =back |
352 | |
361 | |
353 | =head2 UTILITY FUNCTIONS |
362 | =head2 GLOBAL FUNCTIONS |
354 | |
363 | |
355 | =over 4 |
364 | =over 4 |
|
|
365 | |
|
|
366 | =item Coro::nready |
|
|
367 | |
|
|
368 | Returns the number of coroutines that are currently in the ready state, |
|
|
369 | i.e. that can be swicthed to. The value C<0> means that the only runnable |
|
|
370 | coroutine is the currently running one, so C<cede> would have no effect, |
|
|
371 | and C<schedule> would cause a deadlock unless there is an idle handler |
|
|
372 | that wakes up some coroutines. |
356 | |
373 | |
357 | =item unblock_sub { ... } |
374 | =item unblock_sub { ... } |
358 | |
375 | |
359 | This utility function takes a BLOCK or code reference and "unblocks" it, |
376 | This utility function takes a BLOCK or code reference and "unblocks" it, |
360 | returning the new coderef. This means that the new coderef will return |
377 | returning the new coderef. This means that the new coderef will return |