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.239 by root, Mon Nov 24 07:55:28 2008 UTC vs.
Revision 1.247 by root, Mon Dec 15 02:07:11 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.1; 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);
175 177
176=over 4 178=over 4
177 179
178=item async { ... } [@args...] 180=item async { ... } [@args...]
179 181
180Create a new coroutine and return it's coroutine object (usually 182Create a new coroutine and return its coroutine object (usually
181unused). The coroutine will be put into the ready queue, so 183unused). The coroutine will be put into the ready queue, so
182it will start running automatically on the next scheduler run. 184it will start running automatically on the next scheduler run.
183 185
184The first argument is a codeblock/closure that should be executed in the 186The first argument is a codeblock/closure that should be executed in the
185coroutine. When it returns argument returns the coroutine is automatically 187coroutine. When it returns argument returns the coroutine is automatically
312 314
313=item terminate [arg...] 315=item terminate [arg...]
314 316
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
319=item Coro::on_enter BLOCK, Coro::on_leave BLOCK
320
321These function install enter and leave winders in the current scope. The
322enter block will be executed when on_enter is called and whenever the
323current coroutine is re-entered by the scheduler, while the leave block is
324executed whenever the current coroutine is blocked by the scheduler, and
325also when the containing scope is exited (by whatever means, be it exit,
326die, last etc.).
327
328I<Neither invoking the scheduler, nor exceptions, are allowed within those
329BLOCKs>. That means: do not even think about calling C<die> without an
330eval, and do not even think of entering the scheduler in any way.
331
332Since both BLOCKs are tied to the current scope, they will automatically
333be removed when the current scope exits.
334
335These functions implement the same concept as C<dynamic-wind> in scheme
336does, and are useful when you want to localise some resource to a specific
337coroutine.
338
339They slow down coroutine switching considerably for coroutines that use
340them (But coroutine switching is still reasonably fast if the handlers are
341fast).
342
343These functions are best understood by an example: The following function
344will change the current timezone to "Antarctica/South_Pole", which
345requires a call to C<tzset>, but by using C<on_enter> and C<on_leave>,
346which remember/change the current timezone and restore the previous
347value, respectively, the timezone is only changes for the coroutine that
348installed those handlers.
349
350 use POSIX qw(tzset);
351
352 async {
353 my $old_tz; # store outside TZ value here
354
355 Coro::on_enter {
356 $old_tz = $ENV{TZ}; # remember the old value
357
358 $ENV{TZ} = "Antarctica/South_Pole";
359 tzset; # enable new value
360 };
361
362 Coro::on_leave {
363 $ENV{TZ} = $old_tz;
364 tzset; # restore old value
365 };
366
367 # at this place, the timezone is Antarctica/South_Pole,
368 # without disturbing the TZ of any other coroutine.
369 };
370
371This can be used to localise about any resource (locale, uid, current
372working directory etc.) to a block, despite the existance of other
373coroutines.
374
317=item killall 375=item killall
318 376
319Kills/terminates/cancels all coroutines except the currently running 377Kills/terminates/cancels all coroutines except the currently running one.
320one. This is useful after a fork, either in the child or the parent, as
321usually only one of them should inherit the running coroutines.
322 378
323Note that while this will try to free some of the main programs resources, 379Note that while this will try to free some of the main interpreter
380resources if the calling coroutine isn't the main coroutine, but one
324you cannot free all of them, so if a coroutine that is not the main 381cannot free all of them, so if a coroutine that is not the main coroutine
325program calls this function, there will be some one-time resource leak. 382calls this function, there will be some one-time resource leak.
326 383
327=cut 384=cut
328 385
329sub killall { 386sub killall {
330 for (Coro::State::list) { 387 for (Coro::State::list) {
352See C<async> and C<Coro::State::new> for additional info about the 409See C<async> and C<Coro::State::new> for additional info about the
353coroutine environment. 410coroutine environment.
354 411
355=cut 412=cut
356 413
357sub _terminate { 414sub _coro_run {
358 terminate &{+shift}; 415 terminate &{+shift};
359} 416}
360 417
361=item $success = $coroutine->ready 418=item $success = $coroutine->ready
362 419
537would cause a deadlock unless there is an idle handler that wakes up some 594would cause a deadlock unless there is an idle handler that wakes up some
538coroutines. 595coroutines.
539 596
540=item my $guard = Coro::guard { ... } 597=item my $guard = Coro::guard { ... }
541 598
542This creates and returns a guard object. Nothing happens until the object 599This function still exists, but is deprecated. Please use the
543gets destroyed, in which case the codeblock given as argument will be 600C<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 601
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 602=cut
560 603
561sub guard(&) { 604BEGIN { *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 605
574=item unblock_sub { ... } 606=item unblock_sub { ... }
575 607
576This utility function takes a BLOCK or code reference and "unblocks" it, 608This utility function takes a BLOCK or code reference and "unblocks" it,
577returning a new coderef. Unblocking means that calling the new coderef 609returning a new coderef. Unblocking means that calling the new coderef
681 713
682Coro offers two functions specifically designed to make this easy, 714Coro offers two functions specifically designed to make this easy,
683C<Coro::rouse_cb> and C<Coro::rouse_wait>. 715C<Coro::rouse_cb> and C<Coro::rouse_wait>.
684 716
685The first function, C<rouse_cb>, generates and returns a callback that, 717The first function, C<rouse_cb>, generates and returns a callback that,
686when invoked, will save it's arguments and notify the coroutine that 718when invoked, will save its arguments and notify the coroutine that
687created the callback. 719created the callback.
688 720
689The second function, C<rouse_wait>, waits for the callback to be called 721The 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 722(by calling C<schedule> to go to sleep) and returns the arguments
691originally passed to the callback. 723originally passed to the callback.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines