… | |
… | |
806 | =over 4 |
806 | =over 4 |
807 | |
807 | |
808 | =item Backends that are autoprobed when no other event loop can be found. |
808 | =item Backends that are autoprobed when no other event loop can be found. |
809 | |
809 | |
810 | EV is the preferred backend when no other event loop seems to be in |
810 | EV is the preferred backend when no other event loop seems to be in |
811 | use. If EV is not installed, then AnyEvent will try Event, and, failing |
811 | use. If EV is not installed, then AnyEvent will fall back to its own |
812 | that, will fall back to its own pure-perl implementation, which is |
812 | pure-perl implementation, which is available everywhere as it comes with |
813 | available everywhere as it comes with AnyEvent itself. |
813 | AnyEvent itself. |
814 | |
814 | |
815 | AnyEvent::Impl::EV based on EV (interface to libev, best choice). |
815 | AnyEvent::Impl::EV based on EV (interface to libev, best choice). |
816 | AnyEvent::Impl::Event based on Event, very stable, few glitches. |
|
|
817 | AnyEvent::Impl::Perl pure-perl implementation, fast and portable. |
816 | AnyEvent::Impl::Perl pure-perl implementation, fast and portable. |
818 | |
817 | |
819 | =item Backends that are transparently being picked up when they are used. |
818 | =item Backends that are transparently being picked up when they are used. |
820 | |
819 | |
821 | These will be used when they are currently loaded when the first watcher |
820 | These will be used when they are currently loaded when the first watcher |
822 | is created, in which case it is assumed that the application is using |
821 | is created, in which case it is assumed that the application is using |
823 | them. This means that AnyEvent will automatically pick the right backend |
822 | them. This means that AnyEvent will automatically pick the right backend |
824 | when the main program loads an event module before anything starts to |
823 | when the main program loads an event module before anything starts to |
825 | create watchers. Nothing special needs to be done by the main program. |
824 | create watchers. Nothing special needs to be done by the main program. |
826 | |
825 | |
|
|
826 | AnyEvent::Impl::Event based on Event, very stable, few glitches. |
827 | AnyEvent::Impl::Glib based on Glib, slow but very stable. |
827 | AnyEvent::Impl::Glib based on Glib, slow but very stable. |
828 | AnyEvent::Impl::Tk based on Tk, very broken. |
828 | AnyEvent::Impl::Tk based on Tk, very broken. |
829 | AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. |
829 | AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. |
830 | AnyEvent::Impl::POE based on POE, very slow, some limitations. |
830 | AnyEvent::Impl::POE based on POE, very slow, some limitations. |
831 | AnyEvent::Impl::Irssi used when running within irssi. |
831 | AnyEvent::Impl::Irssi used when running within irssi. |
… | |
… | |
1115 | |
1115 | |
1116 | BEGIN { AnyEvent::common_sense } |
1116 | BEGIN { AnyEvent::common_sense } |
1117 | |
1117 | |
1118 | use Carp (); |
1118 | use Carp (); |
1119 | |
1119 | |
1120 | our $VERSION = 4.9; |
1120 | our $VERSION = 4.92; |
1121 | our $MODEL; |
1121 | our $MODEL; |
1122 | |
1122 | |
1123 | our $AUTOLOAD; |
1123 | our $AUTOLOAD; |
1124 | our @ISA; |
1124 | our @ISA; |
1125 | |
1125 | |
… | |
… | |
1151 | $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
1151 | $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
1152 | } |
1152 | } |
1153 | |
1153 | |
1154 | my @models = ( |
1154 | my @models = ( |
1155 | [EV:: => AnyEvent::Impl::EV:: , 1], |
1155 | [EV:: => AnyEvent::Impl::EV:: , 1], |
1156 | [Event:: => AnyEvent::Impl::Event::, 1], |
|
|
1157 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl:: , 1], |
1156 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl:: , 1], |
1158 | # everything below here will not (normally) be autoprobed |
1157 | # everything below here will not (normally) be autoprobed |
1159 | # as the pureperl backend should work everywhere |
1158 | # as the pureperl backend should work everywhere |
1160 | # and is usually faster |
1159 | # and is usually faster |
|
|
1160 | [Event:: => AnyEvent::Impl::Event::, 1], |
1161 | [Glib:: => AnyEvent::Impl::Glib:: , 1], # becomes extremely slow with many watchers |
1161 | [Glib:: => AnyEvent::Impl::Glib:: , 1], # becomes extremely slow with many watchers |
1162 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
1162 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
1163 | [Irssi:: => AnyEvent::Impl::Irssi::], # Irssi has a bogus "Event" package |
1163 | [Irssi:: => AnyEvent::Impl::Irssi::], # Irssi has a bogus "Event" package |
1164 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
1164 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
1165 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
1165 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
… | |
… | |
1287 | # we assume CLOEXEC is already set by perl in all important cases |
1287 | # we assume CLOEXEC is already set by perl in all important cases |
1288 | |
1288 | |
1289 | ($fh2, $rw) |
1289 | ($fh2, $rw) |
1290 | } |
1290 | } |
1291 | |
1291 | |
|
|
1292 | ############################################################################# |
|
|
1293 | # "new" API, currently only emulation of it |
|
|
1294 | ############################################################################# |
|
|
1295 | |
|
|
1296 | package AE; |
|
|
1297 | |
|
|
1298 | our $VERSION = $AnyEvent::VERSION; |
|
|
1299 | |
|
|
1300 | sub io($$$) { |
|
|
1301 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
|
|
1302 | } |
|
|
1303 | |
|
|
1304 | sub timer($$$) { |
|
|
1305 | AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]); |
|
|
1306 | } |
|
|
1307 | |
|
|
1308 | sub signal($$) { |
|
|
1309 | AnyEvent->signal (signal => $_[0], cb => $_[1]); |
|
|
1310 | } |
|
|
1311 | |
|
|
1312 | sub child($$) { |
|
|
1313 | AnyEvent->child (pid => $_[0], cb => $_[1]); |
|
|
1314 | } |
|
|
1315 | |
|
|
1316 | sub idle($) { |
|
|
1317 | AnyEvent->idle (cb => $_[0]); |
|
|
1318 | } |
|
|
1319 | |
|
|
1320 | sub cv(;&) { |
|
|
1321 | AnyEvent->condvar (@_ ? (cb => $_[0]) : ()) |
|
|
1322 | } |
|
|
1323 | |
|
|
1324 | sub now() { |
|
|
1325 | AnyEvent->now |
|
|
1326 | } |
|
|
1327 | |
|
|
1328 | sub now_update() { |
|
|
1329 | AnyEvent->now_update |
|
|
1330 | } |
|
|
1331 | |
|
|
1332 | sub time() { |
|
|
1333 | AnyEvent->time |
|
|
1334 | } |
|
|
1335 | |
1292 | package AnyEvent::Base; |
1336 | package AnyEvent::Base; |
1293 | |
1337 | |
1294 | # default implementations for many methods |
1338 | # default implementations for many methods |
1295 | |
1339 | |
1296 | sub _time { |
1340 | sub _time { |
… | |
… | |
1348 | |
1392 | |
1349 | # install a dummy wakeup watcher to reduce signal catching latency |
1393 | # install a dummy wakeup watcher to reduce signal catching latency |
1350 | sub _sig_add() { |
1394 | sub _sig_add() { |
1351 | unless ($SIG_COUNT++) { |
1395 | unless ($SIG_COUNT++) { |
1352 | # try to align timer on a full-second boundary, if possible |
1396 | # try to align timer on a full-second boundary, if possible |
1353 | my $NOW = AnyEvent->now; |
1397 | my $NOW = AE::now; |
1354 | |
1398 | |
1355 | $SIG_TW = AnyEvent->timer ( |
1399 | $SIG_TW = AE::timer |
1356 | after => $MAX_SIGNAL_LATENCY - ($NOW - int $NOW), |
1400 | $MAX_SIGNAL_LATENCY - ($NOW - int $NOW), |
1357 | interval => $MAX_SIGNAL_LATENCY, |
1401 | $MAX_SIGNAL_LATENCY, |
1358 | cb => sub { }, # just for the PERL_ASYNC_CHECK |
1402 | sub { } # just for the PERL_ASYNC_CHECK |
1359 | ); |
1403 | ; |
1360 | } |
1404 | } |
1361 | } |
1405 | } |
1362 | |
1406 | |
1363 | sub _sig_del { |
1407 | sub _sig_del { |
1364 | undef $SIG_TW |
1408 | undef $SIG_TW |
… | |
… | |
1401 | # probe for availability of Async::Interrupt |
1445 | # probe for availability of Async::Interrupt |
1402 | if (_have_async_interrupt) { |
1446 | if (_have_async_interrupt) { |
1403 | warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8; |
1447 | warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8; |
1404 | |
1448 | |
1405 | $SIGPIPE_R = new Async::Interrupt::EventPipe; |
1449 | $SIGPIPE_R = new Async::Interrupt::EventPipe; |
1406 | $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R->fileno, poll => "r", cb => \&_signal_exec); |
1450 | $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; |
1407 | |
1451 | |
1408 | } else { |
1452 | } else { |
1409 | warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; |
1453 | warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; |
1410 | |
1454 | |
1411 | require Fcntl; |
1455 | require Fcntl; |
… | |
… | |
1427 | } |
1471 | } |
1428 | |
1472 | |
1429 | $SIGPIPE_R |
1473 | $SIGPIPE_R |
1430 | or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; |
1474 | or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; |
1431 | |
1475 | |
1432 | $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R, poll => "r", cb => \&_signal_exec); |
1476 | $SIG_IO = AE::io $SIGPIPE_R, 0, \&_signal_exec; |
1433 | } |
1477 | } |
1434 | |
1478 | |
1435 | *signal = sub { |
1479 | *signal = sub { |
1436 | my (undef, %arg) = @_; |
1480 | my (undef, %arg) = @_; |
1437 | |
1481 | |
… | |
… | |
1526 | $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/ |
1570 | $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/ |
1527 | ? 1 |
1571 | ? 1 |
1528 | : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
1572 | : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
1529 | |
1573 | |
1530 | unless ($CHLD_W) { |
1574 | unless ($CHLD_W) { |
1531 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
1575 | $CHLD_W = AE::signal CHLD => \&_sigchld; |
1532 | # child could be a zombie already, so make at least one round |
1576 | # child could be a zombie already, so make at least one round |
1533 | &_sigchld; |
1577 | &_sigchld; |
1534 | } |
1578 | } |
1535 | |
1579 | |
1536 | bless [$pid, $arg{cb}], "AnyEvent::Base::child" |
1580 | bless [$pid, $arg{cb}], "AnyEvent::Base::child" |
… | |
… | |
1562 | # never use more then 50% of the time for the idle watcher, |
1606 | # never use more then 50% of the time for the idle watcher, |
1563 | # within some limits |
1607 | # within some limits |
1564 | $w = 0.0001 if $w < 0.0001; |
1608 | $w = 0.0001 if $w < 0.0001; |
1565 | $w = 5 if $w > 5; |
1609 | $w = 5 if $w > 5; |
1566 | |
1610 | |
1567 | $w = AnyEvent->timer (after => $w, cb => $rcb); |
1611 | $w = AE::timer $w, 0, $rcb; |
1568 | } else { |
1612 | } else { |
1569 | # clean up... |
1613 | # clean up... |
1570 | undef $w; |
1614 | undef $w; |
1571 | undef $rcb; |
1615 | undef $rcb; |
1572 | } |
1616 | } |
1573 | }; |
1617 | }; |
1574 | |
1618 | |
1575 | $w = AnyEvent->timer (after => 0.05, cb => $rcb); |
1619 | $w = AE::timer 0.05, 0, $rcb; |
1576 | |
1620 | |
1577 | bless \\$cb, "AnyEvent::Base::idle" |
1621 | bless \\$cb, "AnyEvent::Base::idle" |
1578 | } |
1622 | } |
1579 | |
1623 | |
1580 | sub AnyEvent::Base::idle::DESTROY { |
1624 | sub AnyEvent::Base::idle::DESTROY { |
… | |
… | |
1657 | } |
1701 | } |
1658 | |
1702 | |
1659 | # undocumented/compatibility with pre-3.4 |
1703 | # undocumented/compatibility with pre-3.4 |
1660 | *broadcast = \&send; |
1704 | *broadcast = \&send; |
1661 | *wait = \&_wait; |
1705 | *wait = \&_wait; |
1662 | |
|
|
1663 | ############################################################################# |
|
|
1664 | # "new" API, currently only emulation of it |
|
|
1665 | ############################################################################# |
|
|
1666 | |
|
|
1667 | package AE; |
|
|
1668 | |
|
|
1669 | sub io($$$) { |
|
|
1670 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
|
|
1671 | } |
|
|
1672 | |
|
|
1673 | sub timer($$$) { |
|
|
1674 | AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]); |
|
|
1675 | } |
|
|
1676 | |
|
|
1677 | sub signal($$) { |
|
|
1678 | AnyEvent->signal (signal => $_[0], cb => $_[1]); |
|
|
1679 | } |
|
|
1680 | |
|
|
1681 | sub child($$) { |
|
|
1682 | AnyEvent->child (pid => $_[0], cb => $_[1]); |
|
|
1683 | } |
|
|
1684 | |
|
|
1685 | sub idle($) { |
|
|
1686 | AnyEvent->idle (cb => $_[0]); |
|
|
1687 | } |
|
|
1688 | |
|
|
1689 | sub cv() { |
|
|
1690 | AnyEvent->condvar |
|
|
1691 | } |
|
|
1692 | |
|
|
1693 | sub now() { |
|
|
1694 | AnyEvent->now |
|
|
1695 | } |
|
|
1696 | |
|
|
1697 | sub now_update() { |
|
|
1698 | AnyEvent->now_update |
|
|
1699 | } |
|
|
1700 | |
|
|
1701 | sub time() { |
|
|
1702 | AnyEvent->time |
|
|
1703 | } |
|
|
1704 | |
1706 | |
1705 | =head1 ERROR AND EXCEPTION HANDLING |
1707 | =head1 ERROR AND EXCEPTION HANDLING |
1706 | |
1708 | |
1707 | In general, AnyEvent does not do any error handling - it relies on the |
1709 | In general, AnyEvent does not do any error handling - it relies on the |
1708 | caller to do that if required. The L<AnyEvent::Strict> module (see also |
1710 | caller to do that if required. The L<AnyEvent::Strict> module (see also |