ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro.pm
(Generate patch)

Comparing Coro/Coro.pm (file contents):
Revision 1.242 by root, Thu Dec 4 17:30:00 2008 UTC vs.
Revision 1.246 by root, Mon Dec 15 00:30:40 2008 UTC

66package Coro; 66package Coro;
67 67
68use strict qw(vars subs); 68use strict qw(vars subs);
69no warnings "uninitialized"; 69no warnings "uninitialized";
70 70
71use Guard ();
72
71use Coro::State; 73use Coro::State;
72 74
73use base qw(Coro::State Exporter); 75use base qw(Coro::State Exporter);
74 76
75our $idle; # idle handler 77our $idle; # idle handler
76our $main; # main coroutine 78our $main; # main coroutine
77our $current; # current coroutine 79our $current; # current coroutine
78 80
79our $VERSION = 5.12; 81our $VERSION = 5.13;
80 82
81our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub); 83our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub);
82our %EXPORT_TAGS = ( 84our %EXPORT_TAGS = (
83 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], 85 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)],
84); 86);
315Terminates the current coroutine with the given status values (see L<cancel>). 317Terminates the current coroutine with the given status values (see L<cancel>).
316 318
317=item killall 319=item killall
318 320
319Kills/terminates/cancels all coroutines except the currently running 321Kills/terminates/cancels all coroutines except the currently running
320one. This is useful after a fork, either in the child or the parent, as 322one. This can be useful after a fork, either in the child or the parent,
321usually only one of them should inherit the running coroutines. 323as usually only one of them should inherit the running coroutines.
324
325Note that in the implementation, destructors run as normal, making this
326function not so useful after a fork. Future versions of this function
327might try to free resources without running any code.
322 328
323Note that while this will try to free some of the main programs resources, 329Note that while this will try to free some of the main programs resources,
324you cannot free all of them, so if a coroutine that is not the main 330you cannot free all of them, so if a coroutine that is not the main
325program calls this function, there will be some one-time resource leak. 331program calls this function, there will be some one-time resource leak.
326 332
537would cause a deadlock unless there is an idle handler that wakes up some 543would cause a deadlock unless there is an idle handler that wakes up some
538coroutines. 544coroutines.
539 545
540=item my $guard = Coro::guard { ... } 546=item my $guard = Coro::guard { ... }
541 547
542This creates and returns a guard object. Nothing happens until the object 548This function still exists, but is deprecated. Please use the
543gets destroyed, in which case the codeblock given as argument will be 549C<Guard::guard> function instead.
544executed. This is useful to free locks or other resources in case of a
545runtime error or when the coroutine gets canceled, as in both cases the
546guard block will be executed. The guard object supports only one method,
547C<< ->cancel >>, which will keep the codeblock from being executed.
548 550
549Example: set some flag and clear it again when the coroutine gets canceled
550or the function returns:
551
552 sub do_something {
553 my $guard = Coro::guard { $busy = 0 };
554 $busy = 1;
555
556 # do something that requires $busy to be true
557 }
558
559=cut 551=cut
560 552
561sub guard(&) { 553BEGIN { *guard = \&Guard::guard }
562 bless \(my $cb = $_[0]), "Coro::guard"
563}
564
565sub Coro::guard::cancel {
566 ${$_[0]} = sub { };
567}
568
569sub Coro::guard::DESTROY {
570 ${$_[0]}->();
571}
572
573 554
574=item unblock_sub { ... } 555=item unblock_sub { ... }
575 556
576This utility function takes a BLOCK or code reference and "unblocks" it, 557This utility function takes a BLOCK or code reference and "unblocks" it,
577returning a new coderef. Unblocking means that calling the new coderef 558returning a new coderef. Unblocking means that calling the new coderef

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines