… | |
… | |
41 | |
41 | |
42 | our $idle; # idle handler |
42 | our $idle; # idle handler |
43 | our $main; # main coroutine |
43 | our $main; # main coroutine |
44 | our $current; # current coroutine |
44 | our $current; # current coroutine |
45 | |
45 | |
46 | our $VERSION = '3.0'; |
46 | our $VERSION = '3.01'; |
47 | |
47 | |
48 | our @EXPORT = qw(async cede schedule terminate current unblock_sub); |
48 | our @EXPORT = qw(async cede schedule terminate current unblock_sub); |
49 | our %EXPORT_TAGS = ( |
49 | our %EXPORT_TAGS = ( |
50 | 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)], |
51 | ); |
51 | ); |
52 | our @EXPORT_OK = @{$EXPORT_TAGS{prio}}; |
52 | our @EXPORT_OK = (@{$EXPORT_TAGS{prio}}, qw(nready)); |
53 | |
53 | |
54 | { |
54 | { |
55 | my @async; |
55 | my @async; |
56 | my $init; |
56 | my $init; |
57 | |
57 | |
… | |
… | |
128 | handlers), then it must be prepared to be called recursively. |
128 | handlers), then it must be prepared to be called recursively. |
129 | |
129 | |
130 | =cut |
130 | =cut |
131 | |
131 | |
132 | $idle = sub { |
132 | $idle = sub { |
133 | print STDERR "FATAL: deadlock detected\n"; |
133 | require Carp; |
134 | exit (51); |
134 | Carp::croak ("FATAL: deadlock detected"); |
135 | }; |
135 | }; |
136 | |
136 | |
137 | # this coroutine is necessary because a coroutine |
137 | # this coroutine is necessary because a coroutine |
138 | # cannot destroy itself. |
138 | # cannot destroy itself. |
139 | my @destroy; |
139 | my @destroy; |
… | |
… | |
348 | $old; |
348 | $old; |
349 | } |
349 | } |
350 | |
350 | |
351 | =back |
351 | =back |
352 | |
352 | |
353 | =head2 UTILITY FUNCTIONS |
353 | =head2 GLOBAL FUNCTIONS |
354 | |
354 | |
355 | =over 4 |
355 | =over 4 |
|
|
356 | |
|
|
357 | =item Coro::nready |
|
|
358 | |
|
|
359 | Returns the number of coroutines that are currently in the ready state, |
|
|
360 | i.e. that can be swicthed to. The value C<0> means that the only runnable |
|
|
361 | coroutine is the currently running one, so C<cede> would have no effect, |
|
|
362 | and C<schedule> would cause a deadlock unless there is an idle handler |
|
|
363 | that wakes up some coroutines. |
356 | |
364 | |
357 | =item unblock_sub { ... } |
365 | =item unblock_sub { ... } |
358 | |
366 | |
359 | This utility function takes a BLOCK or code reference and "unblocks" it, |
367 | 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 |
368 | returning the new coderef. This means that the new coderef will return |