… | |
… | |
85 | that isn't them. What's worse, all the potential users of your |
85 | that isn't them. What's worse, all the potential users of your |
86 | module are I<also> forced to use the same event loop you use. |
86 | module are I<also> forced to use the same event loop you use. |
87 | |
87 | |
88 | AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works |
88 | AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works |
89 | fine. AnyEvent + Tk works fine etc. etc. but none of these work together |
89 | fine. AnyEvent + Tk works fine etc. etc. but none of these work together |
90 | with the rest: POE + IO::Async? No go. Tk + Event? No go. Again: if |
90 | with the rest: POE + EV? No go. Tk + Event? No go. Again: if your module |
91 | your module uses one of those, every user of your module has to use it, |
91 | uses one of those, every user of your module has to use it, too. But if |
92 | too. But if your module uses AnyEvent, it works transparently with all |
92 | your module uses AnyEvent, it works transparently with all event models it |
93 | event models it supports (including stuff like IO::Async, as long as those |
93 | supports (including stuff like IO::Async, as long as those use one of the |
94 | use one of the supported event loops. It is easy to add new event loops |
94 | supported event loops. It is easy to add new event loops to AnyEvent, too, |
95 | to AnyEvent, too, so it is future-proof). |
95 | so it is future-proof). |
96 | |
96 | |
97 | In addition to being free of having to use I<the one and only true event |
97 | In addition to being free of having to use I<the one and only true event |
98 | model>, AnyEvent also is free of bloat and policy: with POE or similar |
98 | model>, AnyEvent also is free of bloat and policy: with POE or similar |
99 | modules, you get an enormous amount of code and strict rules you have to |
99 | modules, you get an enormous amount of code and strict rules you have to |
100 | follow. AnyEvent, on the other hand, is lean and to the point, by only |
100 | follow. AnyEvent, on the other hand, is lean and to the point, by only |
… | |
… | |
876 | AnyEvent::Impl::Glib based on Glib, slow but very stable. |
876 | AnyEvent::Impl::Glib based on Glib, slow but very stable. |
877 | AnyEvent::Impl::Tk based on Tk, very broken. |
877 | AnyEvent::Impl::Tk based on Tk, very broken. |
878 | AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. |
878 | AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. |
879 | AnyEvent::Impl::POE based on POE, very slow, some limitations. |
879 | AnyEvent::Impl::POE based on POE, very slow, some limitations. |
880 | AnyEvent::Impl::Irssi used when running within irssi. |
880 | AnyEvent::Impl::Irssi used when running within irssi. |
|
|
881 | AnyEvent::Impl::IOAsync based on IO::Async. |
|
|
882 | AnyEvent::Impl::Cocoa based on Cocoa::EventLoop. |
|
|
883 | AnyEvent::Impl::FLTK based on FLTK. |
881 | |
884 | |
882 | =item Backends with special needs. |
885 | =item Backends with special needs. |
883 | |
886 | |
884 | Qt requires the Qt::Application to be instantiated first, but will |
887 | Qt requires the Qt::Application to be instantiated first, but will |
885 | otherwise be picked up automatically. As long as the main program |
888 | otherwise be picked up automatically. As long as the main program |
886 | instantiates the application before any AnyEvent watchers are created, |
889 | instantiates the application before any AnyEvent watchers are created, |
887 | everything should just work. |
890 | everything should just work. |
888 | |
891 | |
889 | AnyEvent::Impl::Qt based on Qt. |
892 | AnyEvent::Impl::Qt based on Qt. |
890 | |
|
|
891 | Support for IO::Async can only be partial, as it is too broken and |
|
|
892 | architecturally limited to even support the AnyEvent API. It also |
|
|
893 | is the only event loop that needs the loop to be set explicitly, so |
|
|
894 | it can only be used by a main program knowing about AnyEvent. See |
|
|
895 | L<AnyEvent::Impl::IOAsync> for the gory details. |
|
|
896 | |
|
|
897 | AnyEvent::Impl::IOAsync based on IO::Async, cannot be autoprobed. |
|
|
898 | |
893 | |
899 | =item Event loops that are indirectly supported via other backends. |
894 | =item Event loops that are indirectly supported via other backends. |
900 | |
895 | |
901 | Some event loops can be supported via other modules: |
896 | Some event loops can be supported via other modules: |
902 | |
897 | |
… | |
… | |
1157 | |
1152 | |
1158 | package AnyEvent; |
1153 | package AnyEvent; |
1159 | |
1154 | |
1160 | # basically a tuned-down version of common::sense |
1155 | # basically a tuned-down version of common::sense |
1161 | sub common_sense { |
1156 | sub common_sense { |
1162 | # from common:.sense 3.3 |
1157 | # from common:.sense 3.4 |
1163 | ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\x3c\x3f\x33\x00\x0f\xf3\x0f\xc0\xf0\xfc\x33\x00"; |
1158 | ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\x3c\x3f\x33\x00\x0f\xf0\x0f\xc0\xf0\xfc\x33\x00"; |
1164 | # use strict vars subs - NO UTF-8, as Util.pm doesn't like this atm. (uts46data.pl) |
1159 | # use strict vars subs - NO UTF-8, as Util.pm doesn't like this atm. (uts46data.pl) |
1165 | $^H |= 0x00000600; |
1160 | $^H |= 0x00000600; |
1166 | } |
1161 | } |
1167 | |
1162 | |
1168 | BEGIN { AnyEvent::common_sense } |
1163 | BEGIN { AnyEvent::common_sense } |
1169 | |
1164 | |
1170 | use Carp (); |
1165 | use Carp (); |
1171 | |
1166 | |
1172 | our $VERSION = '5.271'; |
1167 | our $VERSION = '5.34'; |
1173 | our $MODEL; |
1168 | our $MODEL; |
1174 | |
1169 | |
1175 | our $AUTOLOAD; |
1170 | our $AUTOLOAD; |
1176 | our @ISA; |
1171 | our @ISA; |
1177 | |
1172 | |
… | |
… | |
1215 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
1210 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
1216 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
1211 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
1217 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
1212 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
1218 | [Wx:: => AnyEvent::Impl::POE::], |
1213 | [Wx:: => AnyEvent::Impl::POE::], |
1219 | [Prima:: => AnyEvent::Impl::POE::], |
1214 | [Prima:: => AnyEvent::Impl::POE::], |
1220 | # IO::Async is just too broken - we would need workarounds for its |
|
|
1221 | # byzantine signal and broken child handling, among others. |
|
|
1222 | # IO::Async is rather hard to detect, as it doesn't have any |
|
|
1223 | # obvious default class. |
|
|
1224 | [IO::Async:: => AnyEvent::Impl::IOAsync::], # requires special main program |
|
|
1225 | [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], # requires special main program |
1215 | [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], |
1226 | [IO::Async::Notifier:: => AnyEvent::Impl::IOAsync::], # requires special main program |
1216 | [Cocoa::EventLoop:: => AnyEvent::Impl::Cocoa::], |
1227 | [AnyEvent::Impl::IOAsync:: => AnyEvent::Impl::IOAsync::], # requires special main program |
1217 | [FLTK:: => AnyEvent::Impl::FLTK::], |
1228 | ); |
1218 | ); |
1229 | |
1219 | |
1230 | our %method = map +($_ => 1), |
1220 | our %method = map +($_ => 1), |
1231 | qw(io timer time now now_update signal child idle condvar one_event DESTROY); |
1221 | qw(io timer time now now_update signal child idle condvar DESTROY); |
1232 | |
1222 | |
1233 | our @post_detect; |
1223 | our @post_detect; |
1234 | |
1224 | |
1235 | sub post_detect(&) { |
1225 | sub post_detect(&) { |
1236 | my ($cb) = @_; |
1226 | my ($cb) = @_; |
… | |
… | |
1291 | last; |
1281 | last; |
1292 | } |
1282 | } |
1293 | } |
1283 | } |
1294 | |
1284 | |
1295 | $MODEL |
1285 | $MODEL |
1296 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib.\n"; |
1286 | or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?\n"; |
1297 | } |
1287 | } |
1298 | } |
1288 | } |
1299 | |
1289 | |
1300 | @models = (); # free probe data |
1290 | @models = (); # free probe data |
1301 | |
1291 | |
1302 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
1292 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
1303 | unshift @ISA, $MODEL; |
1293 | unshift @ISA, $MODEL; |
1304 | |
1294 | |
1305 | # now nuke some methods that are overriden by the backend. |
1295 | # now nuke some methods that are overridden by the backend. |
1306 | # SUPER is not allowed. |
1296 | # SUPER is not allowed. |
1307 | for (qw(time signal child idle)) { |
1297 | for (qw(time signal child idle)) { |
1308 | undef &{"AnyEvent::Base::$_"} |
1298 | undef &{"AnyEvent::Base::$_"} |
1309 | if defined &{"$MODEL\::$_"}; |
1299 | if defined &{"$MODEL\::$_"}; |
1310 | } |
1300 | } |
1311 | |
1301 | |
1312 | require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT}; |
1302 | if ($ENV{PERL_ANYEVENT_STRICT}) { |
|
|
1303 | eval { require AnyEvent::Strict }; |
|
|
1304 | warn "AnyEvent: cannot load AnyEvent::Strict: $@" |
|
|
1305 | if $@ && $VERBOSE; |
|
|
1306 | } |
1313 | |
1307 | |
1314 | (shift @post_detect)->() while @post_detect; |
1308 | (shift @post_detect)->() while @post_detect; |
1315 | |
1309 | |
1316 | *post_detect = sub(&) { |
1310 | *post_detect = sub(&) { |
1317 | shift->(); |
1311 | shift->(); |
… | |
… | |
1624 | # default implementation for ->child |
1618 | # default implementation for ->child |
1625 | |
1619 | |
1626 | our %PID_CB; |
1620 | our %PID_CB; |
1627 | our $CHLD_W; |
1621 | our $CHLD_W; |
1628 | our $CHLD_DELAY_W; |
1622 | our $CHLD_DELAY_W; |
1629 | our $WNOHANG; |
|
|
1630 | |
1623 | |
1631 | # used by many Impl's |
1624 | # used by many Impl's |
1632 | sub _emit_childstatus($$) { |
1625 | sub _emit_childstatus($$) { |
1633 | my (undef, $rpid, $rstatus) = @_; |
1626 | my (undef, $rpid, $rstatus) = @_; |
1634 | |
1627 | |
… | |
… | |
1641 | eval q{ # poor man's autoloading {} |
1634 | eval q{ # poor man's autoloading {} |
1642 | *_sigchld = sub { |
1635 | *_sigchld = sub { |
1643 | my $pid; |
1636 | my $pid; |
1644 | |
1637 | |
1645 | AnyEvent->_emit_childstatus ($pid, $?) |
1638 | AnyEvent->_emit_childstatus ($pid, $?) |
1646 | while ($pid = waitpid -1, $WNOHANG) > 0; |
1639 | while ($pid = waitpid -1, WNOHANG) > 0; |
1647 | }; |
1640 | }; |
1648 | |
1641 | |
1649 | *child = sub { |
1642 | *child = sub { |
1650 | my (undef, %arg) = @_; |
1643 | my (undef, %arg) = @_; |
1651 | |
1644 | |
1652 | defined (my $pid = $arg{pid} + 0) |
1645 | defined (my $pid = $arg{pid} + 0) |
1653 | or Carp::croak "required option 'pid' is missing"; |
1646 | or Carp::croak "required option 'pid' is missing"; |
1654 | |
1647 | |
1655 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
1648 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
1656 | |
|
|
1657 | # WNOHANG is almost cetrainly 1 everywhere |
|
|
1658 | $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/ |
|
|
1659 | ? 1 |
|
|
1660 | : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
|
|
1661 | |
1649 | |
1662 | unless ($CHLD_W) { |
1650 | unless ($CHLD_W) { |
1663 | $CHLD_W = AE::signal CHLD => \&_sigchld; |
1651 | $CHLD_W = AE::signal CHLD => \&_sigchld; |
1664 | # child could be a zombie already, so make at least one round |
1652 | # child could be a zombie already, so make at least one round |
1665 | &_sigchld; |
1653 | &_sigchld; |
… | |
… | |
1751 | |
1739 | |
1752 | sub _send { |
1740 | sub _send { |
1753 | # nop |
1741 | # nop |
1754 | } |
1742 | } |
1755 | |
1743 | |
|
|
1744 | sub _wait { |
|
|
1745 | Carp::croak "$AnyEvent::MODEL does not support blocking waits. Caught"; |
|
|
1746 | } |
|
|
1747 | |
1756 | sub send { |
1748 | sub send { |
1757 | my $cv = shift; |
1749 | my $cv = shift; |
1758 | $cv->{_ae_sent} = [@_]; |
1750 | $cv->{_ae_sent} = [@_]; |
1759 | (delete $cv->{_ae_cb})->($cv) if $cv->{_ae_cb}; |
1751 | (delete $cv->{_ae_cb})->($cv) if $cv->{_ae_cb}; |
1760 | $cv->_send; |
1752 | $cv->_send; |
… | |
… | |
1767 | |
1759 | |
1768 | sub ready { |
1760 | sub ready { |
1769 | $_[0]{_ae_sent} |
1761 | $_[0]{_ae_sent} |
1770 | } |
1762 | } |
1771 | |
1763 | |
1772 | sub _wait { |
|
|
1773 | $WAITING |
|
|
1774 | and !$_[0]{_ae_sent} |
|
|
1775 | and Carp::croak "AnyEvent::CondVar: recursive blocking wait detected"; |
|
|
1776 | |
|
|
1777 | local $WAITING = 1; |
|
|
1778 | AnyEvent->one_event while !$_[0]{_ae_sent}; |
|
|
1779 | } |
|
|
1780 | |
|
|
1781 | sub recv { |
1764 | sub recv { |
|
|
1765 | unless ($_[0]{_ae_sent}) { |
|
|
1766 | $WAITING |
|
|
1767 | and Carp::croak "AnyEvent::CondVar: recursive blocking wait detected"; |
|
|
1768 | |
|
|
1769 | local $WAITING = 1; |
1782 | $_[0]->_wait; |
1770 | $_[0]->_wait; |
|
|
1771 | } |
1783 | |
1772 | |
1784 | Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak}; |
1773 | $_[0]{_ae_croak} |
1785 | wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0] |
1774 | and Carp::croak $_[0]{_ae_croak}; |
|
|
1775 | |
|
|
1776 | wantarray |
|
|
1777 | ? @{ $_[0]{_ae_sent} } |
|
|
1778 | : $_[0]{_ae_sent}[0] |
1786 | } |
1779 | } |
1787 | |
1780 | |
1788 | sub cb { |
1781 | sub cb { |
1789 | my $cv = shift; |
1782 | my $cv = shift; |
1790 | |
1783 | |
… | |
… | |
1806 | &{ $_[0]{_ae_end_cb} || sub { $_[0]->send } }; |
1799 | &{ $_[0]{_ae_end_cb} || sub { $_[0]->send } }; |
1807 | } |
1800 | } |
1808 | |
1801 | |
1809 | # undocumented/compatibility with pre-3.4 |
1802 | # undocumented/compatibility with pre-3.4 |
1810 | *broadcast = \&send; |
1803 | *broadcast = \&send; |
1811 | *wait = \&_wait; |
1804 | *wait = \&recv; |
1812 | |
1805 | |
1813 | =head1 ERROR AND EXCEPTION HANDLING |
1806 | =head1 ERROR AND EXCEPTION HANDLING |
1814 | |
1807 | |
1815 | In general, AnyEvent does not do any error handling - it relies on the |
1808 | In general, AnyEvent does not do any error handling - it relies on the |
1816 | caller to do that if required. The L<AnyEvent::Strict> module (see also |
1809 | caller to do that if required. The L<AnyEvent::Strict> module (see also |