… | |
… | |
81 | |
81 | |
82 | our $idle; # idle handler |
82 | our $idle; # idle handler |
83 | our $main; # main coro |
83 | our $main; # main coro |
84 | our $current; # current coro |
84 | our $current; # current coro |
85 | |
85 | |
86 | our $VERSION = 5.2; |
86 | our $VERSION = 5.26; |
87 | |
87 | |
88 | our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub rouse_cb rouse_wait); |
88 | our @EXPORT = qw(async async_pool cede schedule terminate current unblock_sub rouse_cb rouse_wait); |
89 | our %EXPORT_TAGS = ( |
89 | our %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 | ); |
… | |
… | |
138 | |
138 | |
139 | See L<Coro::EV> or L<Coro::AnyEvent> for examples of using this technique. |
139 | See L<Coro::EV> or L<Coro::AnyEvent> for examples of using this technique. |
140 | |
140 | |
141 | =cut |
141 | =cut |
142 | |
142 | |
|
|
143 | # ||= because other modules could have provided their own by now |
143 | $idle = new Coro sub { |
144 | $idle ||= new Coro sub { |
144 | require Coro::Debug; |
145 | require Coro::Debug; |
145 | die "FATAL: deadlock detected.\n" |
146 | die "FATAL: deadlock detected.\n" |
146 | . Coro::Debug::ps_listing (); |
147 | . Coro::Debug::ps_listing (); |
147 | }; |
148 | }; |
148 | |
149 | |
… | |
… | |
566 | wantarray ? @{$self->{_status}} : $self->{_status}[0]; |
567 | wantarray ? @{$self->{_status}} : $self->{_status}[0]; |
567 | } |
568 | } |
568 | |
569 | |
569 | =item $coro->on_destroy (\&cb) |
570 | =item $coro->on_destroy (\&cb) |
570 | |
571 | |
571 | Registers a callback that is called when this coro gets destroyed, |
572 | Registers a callback that is called when this coro thread gets destroyed, |
572 | but before it is joined. The callback gets passed the terminate arguments, |
573 | but before it is joined. The callback gets passed the terminate arguments, |
573 | if any, and I<must not> die, under any circumstances. |
574 | if any, and I<must not> die, under any circumstances. |
574 | |
575 | |
|
|
576 | There can be any number of C<on_destroy> callbacks per coro. |
|
|
577 | |
575 | =cut |
578 | =cut |
576 | |
579 | |
577 | sub on_destroy { |
580 | sub on_destroy { |
578 | my ($self, $cb) = @_; |
581 | my ($self, $cb) = @_; |
579 | |
582 | |
… | |
… | |
581 | } |
584 | } |
582 | |
585 | |
583 | =item $oldprio = $coro->prio ($newprio) |
586 | =item $oldprio = $coro->prio ($newprio) |
584 | |
587 | |
585 | Sets (or gets, if the argument is missing) the priority of the |
588 | Sets (or gets, if the argument is missing) the priority of the |
586 | coro. Higher priority coro get run before lower priority |
589 | coro thread. Higher priority coro get run before lower priority |
587 | coro. Priorities are small signed integers (currently -4 .. +3), |
590 | coros. Priorities are small signed integers (currently -4 .. +3), |
588 | that you can refer to using PRIO_xxx constants (use the import tag :prio |
591 | that you can refer to using PRIO_xxx constants (use the import tag :prio |
589 | to get then): |
592 | to get then): |
590 | |
593 | |
591 | 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 |
592 | 3 > 1 > 0 > -1 > -3 > -4 |
595 | 3 > 1 > 0 > -1 > -3 > -4 |
593 | |
596 | |
594 | # set priority to HIGH |
597 | # set priority to HIGH |
595 | current->prio (PRIO_HIGH); |
598 | current->prio (PRIO_HIGH); |
596 | |
599 | |
597 | The idle coro ($Coro::idle) always has a lower priority than any |
600 | The idle coro thread ($Coro::idle) always has a lower priority than any |
598 | existing coro. |
601 | existing coro. |
599 | |
602 | |
600 | Changing the priority of the current coro will take effect immediately, |
603 | Changing the priority of the current coro will take effect immediately, |
601 | but changing the priority of coro in the ready queue (but not |
604 | but changing the priority of a coro in the ready queue (but not running) |
602 | running) will only take effect after the next schedule (of that |
605 | will only take effect after the next schedule (of that coro). This is a |
603 | coro). This is a bug that will be fixed in some future version. |
606 | bug that will be fixed in some future version. |
604 | |
607 | |
605 | =item $newprio = $coro->nice ($change) |
608 | =item $newprio = $coro->nice ($change) |
606 | |
609 | |
607 | Similar to C<prio>, but subtract the given value from the priority (i.e. |
610 | Similar to C<prio>, but subtract the given value from the priority (i.e. |
608 | higher values mean lower priority, just as in unix). |
611 | higher values mean lower priority, just as in UNIX's nice command). |
609 | |
612 | |
610 | =item $olddesc = $coro->desc ($newdesc) |
613 | =item $olddesc = $coro->desc ($newdesc) |
611 | |
614 | |
612 | Sets (or gets in case the argument is missing) the description for this |
615 | Sets (or gets in case the argument is missing) the description for this |
613 | coro. This is just a free-form string you can associate with a |
616 | coro thread. This is just a free-form string you can associate with a |
614 | coro. |
617 | coro. |
615 | |
618 | |
616 | This method simply sets the C<< $coro->{desc} >> member to the given |
619 | This method simply sets the C<< $coro->{desc} >> member to the given |
617 | string. You can modify this member directly if you wish. |
620 | string. You can modify this member directly if you wish, and in fact, this |
|
|
621 | is often preferred to indicate major processing states that cna then be |
|
|
622 | seen for example in a L<Coro::Debug> session: |
|
|
623 | |
|
|
624 | sub my_long_function { |
|
|
625 | local $Coro::current->{desc} = "now in my_long_function"; |
|
|
626 | ... |
|
|
627 | $Coro::current->{desc} = "my_long_function: phase 1"; |
|
|
628 | ... |
|
|
629 | $Coro::current->{desc} = "my_long_function: phase 2"; |
|
|
630 | ... |
|
|
631 | } |
618 | |
632 | |
619 | =cut |
633 | =cut |
620 | |
634 | |
621 | sub desc { |
635 | sub desc { |
622 | my $old = $_[0]{desc}; |
636 | my $old = $_[0]{desc}; |
… | |
… | |
659 | returning a new coderef. Unblocking means that calling the new coderef |
673 | returning a new coderef. Unblocking means that calling the new coderef |
660 | will return immediately without blocking, returning nothing, while the |
674 | will return immediately without blocking, returning nothing, while the |
661 | original code ref will be called (with parameters) from within another |
675 | original code ref will be called (with parameters) from within another |
662 | coro. |
676 | coro. |
663 | |
677 | |
664 | The reason this function exists is that many event libraries (such as the |
678 | The reason this function exists is that many event libraries (such as |
665 | venerable L<Event|Event> module) are not thread-safe (a weaker form |
679 | the venerable L<Event|Event> module) are not thread-safe (a weaker form |
666 | of reentrancy). This means you must not block within event callbacks, |
680 | of reentrancy). This means you must not block within event callbacks, |
667 | otherwise you might suffer from crashes or worse. The only event library |
681 | otherwise you might suffer from crashes or worse. The only event library |
668 | currently known that is safe to use without C<unblock_sub> is L<EV>. |
682 | currently known that is safe to use without C<unblock_sub> is L<EV> (but |
|
|
683 | you might still run into deadlocks if all event loops are blocked). |
|
|
684 | |
|
|
685 | Coro will try to catch you when you block in the event loop |
|
|
686 | ("FATAL:$Coro::IDLE blocked itself"), but this is just best effort and |
|
|
687 | only works when you do not run your own event loop. |
669 | |
688 | |
670 | This function allows your callbacks to block by executing them in another |
689 | This function allows your callbacks to block by executing them in another |
671 | coro where it is safe to block. One example where blocking is handy |
690 | coro where it is safe to block. One example where blocking is handy |
672 | is when you use the L<Coro::AIO|Coro::AIO> functions to save results to |
691 | is when you use the L<Coro::AIO|Coro::AIO> functions to save results to |
673 | disk, for example. |
692 | disk, for example. |
… | |
… | |
740 | |
759 | |
741 | =back |
760 | =back |
742 | |
761 | |
743 | =cut |
762 | =cut |
744 | |
763 | |
|
|
764 | for 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 | |
745 | 1; |
778 | 1; |
746 | |
779 | |
747 | =head1 HOW TO WAIT FOR A CALLBACK |
780 | =head1 HOW TO WAIT FOR A CALLBACK |
748 | |
781 | |
749 | It is very common for a coro to wait for some callback to be |
782 | It is very common for a coro to wait for some callback to be |
… | |
… | |
830 | the windows process emulation enabled under unix roughly halves perl |
863 | the windows process emulation enabled under unix roughly halves perl |
831 | performance, even when not used. |
864 | performance, even when not used. |
832 | |
865 | |
833 | =item coro switching is not signal safe |
866 | =item coro switching is not signal safe |
834 | |
867 | |
835 | You must not switch to another coro from within a signal handler |
868 | You must not switch to another coro from within a signal handler (only |
836 | (only relevant with %SIG - most event libraries provide safe signals). |
869 | relevant with %SIG - most event libraries provide safe signals), I<unless> |
|
|
870 | you are sure you are not interrupting a Coro function. |
837 | |
871 | |
838 | That means you I<MUST NOT> call any function that might "block" the |
872 | That means you I<MUST NOT> call any function that might "block" the |
839 | current coro - C<cede>, C<schedule> C<< Coro::Semaphore->down >> or |
873 | current coro - C<cede>, C<schedule> C<< Coro::Semaphore->down >> or |
840 | anything that calls those. Everything else, including calling C<ready>, |
874 | anything that calls those. Everything else, including calling C<ready>, |
841 | works. |
875 | works. |
… | |
… | |
851 | ithreads (for example, that memory or files would be shared), showing his |
885 | ithreads (for example, that memory or files would be shared), showing his |
852 | lack of understanding of this area - if it is hard to understand for Chip, |
886 | lack of understanding of this area - if it is hard to understand for Chip, |
853 | it is probably not obvious to everybody). |
887 | it is probably not obvious to everybody). |
854 | |
888 | |
855 | What follows is an ultra-condensed version of my talk about threads in |
889 | What follows is an ultra-condensed version of my talk about threads in |
856 | scripting languages given onthe perl workshop 2009: |
890 | scripting languages given on the perl workshop 2009: |
857 | |
891 | |
858 | The so-called "ithreads" were originally implemented for two reasons: |
892 | The so-called "ithreads" were originally implemented for two reasons: |
859 | first, to (badly) emulate unix processes on native win32 perls, and |
893 | first, to (badly) emulate unix processes on native win32 perls, and |
860 | secondly, to replace the older, real thread model ("5.005-threads"). |
894 | secondly, to replace the older, real thread model ("5.005-threads"). |
861 | |
895 | |