--- Coro/Coro.pm 2004/05/13 16:12:14 1.60 +++ Coro/Coro.pm 2006/11/26 02:54:55 1.88 @@ -32,21 +32,24 @@ package Coro; -BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } +use strict; +no warnings "uninitialized"; use Coro::State; -use vars qw($idle $main $current); +use base qw(Coro::State Exporter); -use base Exporter; +our $idle; # idle handler +our $main; # main coroutine +our $current; # current coroutine -$VERSION = 0.96; +our $VERSION = '3.0'; -@EXPORT = qw(async cede schedule terminate current); -%EXPORT_TAGS = ( +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; @@ -54,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); @@ -91,7 +97,12 @@ =item $current (or as function: current) -The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). +The current coroutine (the last coroutine switched to). The initial value +is C<$main> (of course). + +This variable is B I. It is provided for performance +reasons. If performance is not essentiel you are encouraged to use the +C function instead. =cut @@ -106,22 +117,25 @@ =item $idle -The coroutine to switch to when no other coroutine is running. The default -implementation prints "FATAL: deadlock detected" and exits. +A callback that is called whenever the scheduler finds no ready coroutines +to run. The default implementation prints "FATAL: deadlock detected" and +exits. + +This hook is overwritten by modules such as C and +C to wait on an external event that hopefully wakes up some +coroutine. =cut -# should be done using priorities :( -$idle = new Coro sub { +$idle = sub { print STDERR "FATAL: deadlock detected\n"; - exit(51); + exit (51); }; # this coroutine is necessary because a coroutine # cannot destroy itself. my @destroy; -my $manager; -$manager = new Coro sub { +my $manager; $manager = new Coro sub { while () { # by overwriting the state object with the manager we destroy it # while still being able to schedule this coroutine (in case it has @@ -133,11 +147,11 @@ $coro->{status} ||= []; $_->ready for @{delete $coro->{join} || []}; - # the next line destroys the _coro_state, but keeps the + # 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}; + $coro->_clone_state_from ($manager); } &schedule; } @@ -159,6 +173,9 @@ (usually unused). When the sub returns the new process is automatically terminated. +When the coroutine dies, the program will exit, just as in the main +program. + # create a new coroutine that just prints its arguments async { print "@_\n"; @@ -168,9 +185,8 @@ sub async(&@) { my $pid = new Coro @_; - $manager->ready; # this ensures that the stack is cloned from the manager $pid->ready; - $pid; + $pid } =item schedule @@ -218,15 +234,14 @@ =cut -sub _newcoro { +sub _new_coro { terminate &{+shift}; } sub new { my $class = shift; - bless { - _coro_state => (new Coro::State $_[0] && \&_newcoro, @_), - }, $class; + + $class->SUPER::new (\&_new_coro, @_) } =item $process->ready @@ -237,7 +252,7 @@ =item $process->cancel (arg...) -Temrinates the given process and makes it return the given arguments as +Terminates the given process and makes it return the given arguments as status (default: the empty list). =cut @@ -267,7 +282,7 @@ wantarray ? @{$self->{status}} : $self->{status}[0]; } -=item $oldprio = $process->prio($newprio) +=item $oldprio = $process->prio ($newprio) Sets (or gets, if the argument is missing) the priority of the process. Higher priority processes get run before lower priority @@ -289,26 +304,12 @@ running) will only take effect after the next schedule (of that process). This is a bug that will be fixed in some future version. -=cut - -sub prio { - my $old = $_[0]{prio}; - $_[0]{prio} = $_[1] if @_ > 1; - $old; -} - -=item $newprio = $process->nice($change) +=item $newprio = $process->nice ($change) Similar to C, but subtract the given value from the priority (i.e. higher values mean lower priority, just as in unix). -=cut - -sub nice { - $_[0]{prio} -= $_[1]; -} - -=item $olddesc = $process->desc($newdesc) +=item $olddesc = $process->desc ($newdesc) Sets (or gets in case the argument is missing) the description for this process. This is just a free-form string you can associate with a process. @@ -339,14 +340,18 @@ =head1 SEE ALSO -L, L, L, L, -L, L, L, L, -L, Handle>, 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