--- Coro/Coro.pm 2003/05/27 01:15:26 1.53 +++ Coro/Coro.pm 2006/11/01 01:21:21 1.78 @@ -32,19 +32,24 @@ package Coro; -no warnings qw(uninitialized); +use strict; +no warnings "uninitialized"; use Coro::State; -use base Exporter; +use base Exporter::; -$VERSION = 0.7; +our $idle; # idle coroutine +our $main; # main coroutine +our $current; # current coroutine -@EXPORT = qw(async cede schedule terminate current); -%EXPORT_TAGS = ( +our $VERSION = '2.1'; + +our @EXPORT = qw(async cede schedule terminate current); +our %EXPORT_TAGS = ( prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], ); -@EXPORT_OK = @{$EXPORT_TAGS{prio}}; +our @EXPORT_OK = @{$EXPORT_TAGS{prio}}; { my @async; @@ -52,7 +57,10 @@ # this way of handling attributes simply is NOT scalable ;() sub import { + no strict 'refs'; + Coro->export_to_level(1, @_); + my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"} = sub { my ($package, $ref) = (shift, shift); @@ -85,7 +93,7 @@ =cut -our $main = new Coro; +$main = new Coro; =item $current (or as function: current) @@ -98,7 +106,7 @@ $main->{specific} = $current->{specific}; } -our $current = $main; +$current = $main; sub current() { $current } @@ -110,7 +118,7 @@ =cut # should be done using priorities :( -our $idle = new Coro sub { +$idle = new Coro sub { print STDERR "FATAL: deadlock detected\n"; exit(51); }; @@ -120,7 +128,7 @@ my @destroy; my $manager; $manager = new Coro sub { - while() { + while () { # by overwriting the state object with the manager we destroy it # while still being able to schedule this coroutine (in case it has # been readied multiple times. this is harmless since the manager @@ -130,6 +138,11 @@ my $coro = pop @destroy; $coro->{status} ||= []; $_->ready for @{delete $coro->{join} || []}; + + # the next line destroys the _coro_state, but keeps the + # process itself intact (we basically make it a zombie + # process that always runs the manager thread, so it's possible + # to transfer() to this process). $coro->{_coro_state} = $manager->{_coro_state}; } &schedule; @@ -157,9 +170,6 @@ print "@_\n"; } 1,2,3,4; -The coderef you submit MUST NOT be a closure that refers to variables -in an outer scope. This does NOT work. Pass arguments into it instead. - =cut sub async(&@) { @@ -187,17 +197,12 @@ =item terminate [arg...] -Terminates the current process. - -Future versions of this function will allow result arguments. +Terminates the current process with the given status values (see L). =cut sub terminate { - $current->{status} = [@_]; - $current->cancel; - &schedule; - die; # NORETURN + $current->cancel (@_); } =back @@ -236,23 +241,26 @@ =cut -=item $process->cancel +=item $process->cancel (arg...) -Like C, but terminates the specified process instead. +Temrinates the given process and makes it return the given arguments as +status (default: the empty list). =cut sub cancel { - push @destroy, $_[0]; + my $self = shift; + $self->{status} = [@_]; + push @destroy, $self; $manager->ready; - &schedule if $current == $_[0]; + &schedule if $current == $self; } =item $process->join Wait until the coroutine terminates and return any values given to the -C function. C can be called multiple times from multiple -processes. +C or C functions. C can be called multiple times +from multiple processes. =cut @@ -337,14 +345,18 @@ =head1 SEE ALSO -L, L, L, L, -L, L, L, L, -L, L. +Support/Utility: L, L, L, L. + +Locking/IPC: L, L, L, L, L. + +Event/IO: L, L, L, L, L. + +Embedding: L =head1 AUTHOR - Marc Lehmann - http://www.goof.com/pcg/marc/ + Marc Lehmann + http://home.schmorp.de/ =cut