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

Comparing cvsroot/Coro/Coro.pm (file contents):
Revision 1.238 by root, Mon Nov 24 04:56:38 2008 UTC vs.
Revision 1.245 by root, Sun Dec 14 19:52:58 2008 UTC

74 74
75our $idle; # idle handler 75our $idle; # idle handler
76our $main; # main coroutine 76our $main; # main coroutine
77our $current; # current coroutine 77our $current; # current coroutine
78 78
79our $VERSION = "5.0"; 79our $VERSION = 5.13;
80 80
81our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub); 81our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub);
82our %EXPORT_TAGS = ( 82our %EXPORT_TAGS = (
83 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], 83 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)],
84); 84);
175 175
176=over 4 176=over 4
177 177
178=item async { ... } [@args...] 178=item async { ... } [@args...]
179 179
180Create a new coroutine and return it's coroutine object (usually 180Create a new coroutine and return its coroutine object (usually
181unused). The coroutine will be put into the ready queue, so 181unused). The coroutine will be put into the ready queue, so
182it will start running automatically on the next scheduler run. 182it will start running automatically on the next scheduler run.
183 183
184The first argument is a codeblock/closure that should be executed in the 184The first argument is a codeblock/closure that should be executed in the
185coroutine. When it returns argument returns the coroutine is automatically 185coroutine. When it returns argument returns the coroutine is automatically
315Terminates the current coroutine with the given status values (see L<cancel>). 315Terminates the current coroutine with the given status values (see L<cancel>).
316 316
317=item killall 317=item killall
318 318
319Kills/terminates/cancels all coroutines except the currently running 319Kills/terminates/cancels all coroutines except the currently running
320one. This is useful after a fork, either in the child or the parent, as 320one. This can be useful after a fork, either in the child or the parent,
321usually only one of them should inherit the running coroutines. 321as usually only one of them should inherit the running coroutines.
322
323Note that in the implementation, destructors run as normal, making this
324function not so useful after a fork. Future versions of this function
325might try to free resources without running any code.
322 326
323Note that while this will try to free some of the main programs resources, 327Note 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 328you 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. 329program calls this function, there will be some one-time resource leak.
326 330
352See C<async> and C<Coro::State::new> for additional info about the 356See C<async> and C<Coro::State::new> for additional info about the
353coroutine environment. 357coroutine environment.
354 358
355=cut 359=cut
356 360
357sub _terminate { 361sub _coro_run {
358 terminate &{+shift}; 362 terminate &{+shift};
359} 363}
360 364
361=item $success = $coroutine->ready 365=item $success = $coroutine->ready
362 366
537would cause a deadlock unless there is an idle handler that wakes up some 541would cause a deadlock unless there is an idle handler that wakes up some
538coroutines. 542coroutines.
539 543
540=item my $guard = Coro::guard { ... } 544=item my $guard = Coro::guard { ... }
541 545
542This creates and returns a guard object. Nothing happens until the object 546This function still exists, but is deprecated. Please use the
543gets destroyed, in which case the codeblock given as argument will be 547C<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 548
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 549=cut
560 550
561sub guard(&) { 551BEGIN { *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 552
574=item unblock_sub { ... } 553=item unblock_sub { ... }
575 554
576This utility function takes a BLOCK or code reference and "unblocks" it, 555This utility function takes a BLOCK or code reference and "unblocks" it,
577returning a new coderef. Unblocking means that calling the new coderef 556returning a new coderef. Unblocking means that calling the new coderef
681 660
682Coro offers two functions specifically designed to make this easy, 661Coro offers two functions specifically designed to make this easy,
683C<Coro::rouse_cb> and C<Coro::rouse_wait>. 662C<Coro::rouse_cb> and C<Coro::rouse_wait>.
684 663
685The first function, C<rouse_cb>, generates and returns a callback that, 664The first function, C<rouse_cb>, generates and returns a callback that,
686when invoked, will save it's arguments and notify the coroutine that 665when invoked, will save its arguments and notify the coroutine that
687created the callback. 666created the callback.
688 667
689The second function, C<rouse_wait>, waits for the callback to be called 668The second function, C<rouse_wait>, waits for the callback to be called
690(by calling C<schedule> to go to sleep) and returns the arguments 669(by calling C<schedule> to go to sleep) and returns the arguments
691originally passed to the callback. 670originally passed to the callback.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines