… | |
… | |
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 | |
58 | # this way of handling attributes simply is NOT scalable ;() |
58 | # this way of handling attributes simply is NOT scalable ;() |
59 | sub import { |
59 | sub import { |
60 | no strict 'refs'; |
60 | no strict 'refs'; |
61 | |
61 | |
62 | Coro->export_to_level(1, @_); |
62 | Coro->export_to_level (1, @_); |
63 | |
63 | |
64 | my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; |
64 | my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; |
65 | *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"} = sub { |
65 | *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"} = sub { |
66 | my ($package, $ref) = (shift, shift); |
66 | my ($package, $ref) = (shift, shift); |
67 | my @attrs; |
67 | my @attrs; |
… | |
… | |
105 | C<Coro::current> function instead. |
105 | C<Coro::current> function instead. |
106 | |
106 | |
107 | =cut |
107 | =cut |
108 | |
108 | |
109 | # maybe some other module used Coro::Specific before... |
109 | # maybe some other module used Coro::Specific before... |
110 | if ($current) { |
|
|
111 | $main->{specific} = $current->{specific}; |
110 | $main->{specific} = $current->{specific} |
112 | } |
111 | if $current; |
113 | |
112 | |
114 | $current = $main; |
113 | _set_current $main; |
115 | |
114 | |
116 | sub current() { $current } |
115 | sub current() { $current } |
117 | |
116 | |
118 | =item $idle |
117 | =item $idle |
119 | |
118 | |
… | |
… | |
129 | handlers), then it must be prepared to be called recursively. |
128 | handlers), then it must be prepared to be called recursively. |
130 | |
129 | |
131 | =cut |
130 | =cut |
132 | |
131 | |
133 | $idle = sub { |
132 | $idle = sub { |
134 | print STDERR "FATAL: deadlock detected\n"; |
133 | require Carp; |
135 | exit (51); |
134 | Carp::croak ("FATAL: deadlock detected"); |
136 | }; |
135 | }; |
137 | |
136 | |
138 | # this coroutine is necessary because a coroutine |
137 | # this coroutine is necessary because a coroutine |
139 | # cannot destroy itself. |
138 | # cannot destroy itself. |
140 | my @destroy; |
139 | my @destroy; |
… | |
… | |
255 | |
254 | |
256 | Calling C<exit> in a coroutine will not work correctly, so do not do that. |
255 | Calling C<exit> in a coroutine will not work correctly, so do not do that. |
257 | |
256 | |
258 | =cut |
257 | =cut |
259 | |
258 | |
260 | sub _new_coro { |
259 | sub _run_coro { |
261 | terminate &{+shift}; |
260 | terminate &{+shift}; |
262 | } |
261 | } |
263 | |
262 | |
264 | sub new { |
263 | sub new { |
265 | my $class = shift; |
264 | my $class = shift; |
266 | |
265 | |
267 | $class->SUPER::new (\&_new_coro, @_) |
266 | $class->SUPER::new (\&_run_coro, @_) |
268 | } |
267 | } |
269 | |
268 | |
270 | =item $success = $coroutine->ready |
269 | =item $success = $coroutine->ready |
271 | |
270 | |
272 | Put the given coroutine into the ready queue (according to it's priority) |
271 | Put the given coroutine into the ready queue (according to it's priority) |
… | |
… | |
349 | $old; |
348 | $old; |
350 | } |
349 | } |
351 | |
350 | |
352 | =back |
351 | =back |
353 | |
352 | |
354 | =head2 UTILITY FUNCTIONS |
353 | =head2 GLOBAL FUNCTIONS |
355 | |
354 | |
356 | =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. |
357 | |
364 | |
358 | =item unblock_sub { ... } |
365 | =item unblock_sub { ... } |
359 | |
366 | |
360 | 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, |
361 | 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 |