… | |
… | |
671 | returning a new coderef. Unblocking means that calling the new coderef |
671 | returning a new coderef. Unblocking means that calling the new coderef |
672 | will return immediately without blocking, returning nothing, while the |
672 | will return immediately without blocking, returning nothing, while the |
673 | original code ref will be called (with parameters) from within another |
673 | original code ref will be called (with parameters) from within another |
674 | coro. |
674 | coro. |
675 | |
675 | |
676 | The reason this function exists is that many event libraries (such as the |
676 | The reason this function exists is that many event libraries (such as |
677 | venerable L<Event|Event> module) are not thread-safe (a weaker form |
677 | the venerable L<Event|Event> module) are not thread-safe (a weaker form |
678 | of reentrancy). This means you must not block within event callbacks, |
678 | of reentrancy). This means you must not block within event callbacks, |
679 | otherwise you might suffer from crashes or worse. The only event library |
679 | otherwise you might suffer from crashes or worse. The only event library |
680 | currently known that is safe to use without C<unblock_sub> is L<EV>. |
680 | currently known that is safe to use without C<unblock_sub> is L<EV> (but |
|
|
681 | you might still run into deadlocks if all event loops are blocked). |
681 | |
682 | |
682 | Coro will try to catch you when you block in the event loop |
683 | Coro will try to catch you when you block in the event loop |
683 | ("FATAL:$Coro::IDLE blocked itself"), but this is just best effort and |
684 | ("FATAL:$Coro::IDLE blocked itself"), but this is just best effort and |
684 | only works when you do not run your own event loop. |
685 | only works when you do not run your own event loop. |
685 | |
686 | |
… | |
… | |
756 | |
757 | |
757 | =back |
758 | =back |
758 | |
759 | |
759 | =cut |
760 | =cut |
760 | |
761 | |
|
|
762 | for my $module (qw(Channel RWLock Semaphore SemaphoreSet Signal Specific)) { |
|
|
763 | my $old = defined &{"Coro::$module\::new"} && \&{"Coro::$module\::new"}; |
|
|
764 | |
|
|
765 | *{"Coro::$module\::new"} = sub { |
|
|
766 | require "Coro/$module.pm"; |
|
|
767 | |
|
|
768 | # some modules have their new predefined in State.xs, some don't |
|
|
769 | *{"Coro::$module\::new"} = $old |
|
|
770 | if $old; |
|
|
771 | |
|
|
772 | goto &{"Coro::$module\::new"}; |
|
|
773 | }; |
|
|
774 | } |
|
|
775 | |
761 | 1; |
776 | 1; |
762 | |
777 | |
763 | =head1 HOW TO WAIT FOR A CALLBACK |
778 | =head1 HOW TO WAIT FOR A CALLBACK |
764 | |
779 | |
765 | It is very common for a coro to wait for some callback to be |
780 | It is very common for a coro to wait for some callback to be |