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.282 by root, Sun Dec 26 16:23:51 2010 UTC vs.
Revision 1.285 by root, Thu Feb 17 01:32:18 2011 UTC

81 81
82our $idle; # idle handler 82our $idle; # idle handler
83our $main; # main coro 83our $main; # main coro
84our $current; # current coro 84our $current; # current coro
85 85
86our $VERSION = 5.25; 86our $VERSION = 5.26;
87 87
88our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub rouse_cb rouse_wait); 88our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub rouse_cb rouse_wait);
89our %EXPORT_TAGS = ( 89our %EXPORT_TAGS = (
90 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], 90 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)],
91); 91);
131 131
132The default implementation dies with "FATAL: deadlock detected.", followed 132The default implementation dies with "FATAL: deadlock detected.", followed
133by a thread listing, because the program has no other way to continue. 133by a thread listing, because the program has no other way to continue.
134 134
135This hook is overwritten by modules such as C<Coro::EV> and 135This hook is overwritten by modules such as C<Coro::EV> and
136C<Coro::AnyEvent> to wait on an external event that hopefully wake up a 136C<Coro::AnyEvent> to wait on an external event that hopefully wakes up a
137coro so the scheduler can run it. 137coro so the scheduler can run it.
138 138
139See L<Coro::EV> or L<Coro::AnyEvent> for examples of using this technique. 139See L<Coro::EV> or L<Coro::AnyEvent> for examples of using this technique.
140 140
141=cut 141=cut
567 wantarray ? @{$self->{_status}} : $self->{_status}[0]; 567 wantarray ? @{$self->{_status}} : $self->{_status}[0];
568} 568}
569 569
570=item $coro->on_destroy (\&cb) 570=item $coro->on_destroy (\&cb)
571 571
572Registers a callback that is called when this coro gets destroyed, 572Registers a callback that is called when this coro thread gets destroyed,
573but before it is joined. The callback gets passed the terminate arguments, 573but before it is joined. The callback gets passed the terminate arguments,
574if any, and I<must not> die, under any circumstances. 574if any, and I<must not> die, under any circumstances.
575 575
576There can be any number of C<on_destroy> callbacks per coro.
577
576=cut 578=cut
577 579
578sub on_destroy { 580sub on_destroy {
579 my ($self, $cb) = @_; 581 my ($self, $cb) = @_;
580 582
582} 584}
583 585
584=item $oldprio = $coro->prio ($newprio) 586=item $oldprio = $coro->prio ($newprio)
585 587
586Sets (or gets, if the argument is missing) the priority of the 588Sets (or gets, if the argument is missing) the priority of the
587coro. Higher priority coro get run before lower priority 589coro thread. Higher priority coro get run before lower priority
588coro. Priorities are small signed integers (currently -4 .. +3), 590coros. Priorities are small signed integers (currently -4 .. +3),
589that you can refer to using PRIO_xxx constants (use the import tag :prio 591that you can refer to using PRIO_xxx constants (use the import tag :prio
590to get then): 592to get then):
591 593
592 PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN 594 PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN
593 3 > 1 > 0 > -1 > -3 > -4 595 3 > 1 > 0 > -1 > -3 > -4
594 596
595 # set priority to HIGH 597 # set priority to HIGH
596 current->prio (PRIO_HIGH); 598 current->prio (PRIO_HIGH);
597 599
598The idle coro ($Coro::idle) always has a lower priority than any 600The idle coro thread ($Coro::idle) always has a lower priority than any
599existing coro. 601existing coro.
600 602
601Changing the priority of the current coro will take effect immediately, 603Changing the priority of the current coro will take effect immediately,
602but changing the priority of coro in the ready queue (but not 604but changing the priority of a coro in the ready queue (but not running)
603running) will only take effect after the next schedule (of that 605will only take effect after the next schedule (of that coro). This is a
604coro). This is a bug that will be fixed in some future version. 606bug that will be fixed in some future version.
605 607
606=item $newprio = $coro->nice ($change) 608=item $newprio = $coro->nice ($change)
607 609
608Similar to C<prio>, but subtract the given value from the priority (i.e. 610Similar to C<prio>, but subtract the given value from the priority (i.e.
609higher values mean lower priority, just as in unix). 611higher values mean lower priority, just as in UNIX's nice command).
610 612
611=item $olddesc = $coro->desc ($newdesc) 613=item $olddesc = $coro->desc ($newdesc)
612 614
613Sets (or gets in case the argument is missing) the description for this 615Sets (or gets in case the argument is missing) the description for this
614coro. This is just a free-form string you can associate with a 616coro thread. This is just a free-form string you can associate with a
615coro. 617coro.
616 618
617This method simply sets the C<< $coro->{desc} >> member to the given 619This method simply sets the C<< $coro->{desc} >> member to the given
618string. You can modify this member directly if you wish, and in fact, this 620string. You can modify this member directly if you wish, and in fact, this
619is often preferred to indicate major processing states that cna then be 621is often preferred to indicate major processing states that cna then be
757 759
758=back 760=back
759 761
760=cut 762=cut
761 763
764for my $module (qw(Channel RWLock Semaphore SemaphoreSet Signal Specific)) {
765 my $old = defined &{"Coro::$module\::new"} && \&{"Coro::$module\::new"};
766
767 *{"Coro::$module\::new"} = sub {
768 require "Coro/$module.pm";
769
770 # some modules have their new predefined in State.xs, some don't
771 *{"Coro::$module\::new"} = $old
772 if $old;
773
774 goto &{"Coro::$module\::new"};
775 };
776}
777
7621; 7781;
763 779
764=head1 HOW TO WAIT FOR A CALLBACK 780=head1 HOW TO WAIT FOR A CALLBACK
765 781
766It is very common for a coro to wait for some callback to be 782It is very common for a coro to wait for some callback to be

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines