ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent.pm (file contents):
Revision 1.269 by root, Fri Jul 31 20:16:29 2009 UTC vs.
Revision 1.277 by root, Sun Aug 9 13:27:23 2009 UTC

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
810EV is the preferred backend when no other event loop seems to be in 810EV is the preferred backend when no other event loop seems to be in
811use. If EV is not installed, then AnyEvent will try Event, and, failing 811use. If EV is not installed, then AnyEvent will fall back to its own
812that, will fall back to its own pure-perl implementation, which is 812pure-perl implementation, which is available everywhere as it comes with
813available everywhere as it comes with AnyEvent itself. 813AnyEvent 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
821These will be used when they are currently loaded when the first watcher 820These will be used when they are currently loaded when the first watcher
822is created, in which case it is assumed that the application is using 821is created, in which case it is assumed that the application is using
823them. This means that AnyEvent will automatically pick the right backend 822them. This means that AnyEvent will automatically pick the right backend
824when the main program loads an event module before anything starts to 823when the main program loads an event module before anything starts to
825create watchers. Nothing special needs to be done by the main program. 824create 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
1116BEGIN { AnyEvent::common_sense } 1116BEGIN { AnyEvent::common_sense }
1117 1117
1118use Carp (); 1118use Carp ();
1119 1119
1120our $VERSION = 4.9; 1120our $VERSION = 4.92;
1121our $MODEL; 1121our $MODEL;
1122 1122
1123our $AUTOLOAD; 1123our $AUTOLOAD;
1124our @ISA; 1124our @ISA;
1125 1125
1151 $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; 1151 $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6";
1152} 1152}
1153 1153
1154my @models = ( 1154my @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
1168 [Prima:: => AnyEvent::Impl::POE::], 1168 [Prima:: => AnyEvent::Impl::POE::],
1169 # IO::Async is just too broken - we would need workarounds for its 1169 # IO::Async is just too broken - we would need workarounds for its
1170 # byzantine signal and broken child handling, among others. 1170 # byzantine signal and broken child handling, among others.
1171 # IO::Async is rather hard to detect, as it doesn't have any 1171 # IO::Async is rather hard to detect, as it doesn't have any
1172 # obvious default class. 1172 # obvious default class.
1173# [0, IO::Async:: => AnyEvent::Impl::IOAsync::], # requires special main program 1173 [IO::Async:: => AnyEvent::Impl::IOAsync::], # requires special main program
1174# [0, IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], # requires special main program 1174 [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], # requires special main program
1175# [0, IO::Async::Notifier:: => AnyEvent::Impl::IOAsync::], # requires special main program 1175 [IO::Async::Notifier:: => AnyEvent::Impl::IOAsync::], # requires special main program
1176 [AnyEvent::Impl::IOAsync:: => AnyEvent::Impl::IOAsync::], # requires special main program
1176); 1177);
1177 1178
1178our %method = map +($_ => 1), 1179our %method = map +($_ => 1),
1179 qw(io timer time now now_update signal child idle condvar one_event DESTROY); 1180 qw(io timer time now now_update signal child idle condvar one_event DESTROY);
1180 1181
1287 # we assume CLOEXEC is already set by perl in all important cases 1288 # we assume CLOEXEC is already set by perl in all important cases
1288 1289
1289 ($fh2, $rw) 1290 ($fh2, $rw)
1290} 1291}
1291 1292
1293#############################################################################
1294# "new" API, currently only emulation of it
1295#############################################################################
1296
1297package AE;
1298
1299our $VERSION = $AnyEvent::VERSION;
1300
1301sub io($$$) {
1302 AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2])
1303}
1304
1305sub timer($$$) {
1306 AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2])
1307}
1308
1309sub signal($$) {
1310 AnyEvent->signal (signal => $_[0], cb => $_[1])
1311}
1312
1313sub child($$) {
1314 AnyEvent->child (pid => $_[0], cb => $_[1])
1315}
1316
1317sub idle($) {
1318 AnyEvent->idle (cb => $_[0])
1319}
1320
1321sub cv(;&) {
1322 AnyEvent->condvar (@_ ? (cb => $_[0]) : ())
1323}
1324
1325sub now() {
1326 AnyEvent->now
1327}
1328
1329sub now_update() {
1330 AnyEvent->now_update
1331}
1332
1333sub time() {
1334 AnyEvent->time
1335}
1336
1292package AnyEvent::Base; 1337package AnyEvent::Base;
1293 1338
1294# default implementations for many methods 1339# default implementations for many methods
1295 1340
1296sub _time { 1341sub _time {
1348 1393
1349# install a dummy wakeup watcher to reduce signal catching latency 1394# install a dummy wakeup watcher to reduce signal catching latency
1350sub _sig_add() { 1395sub _sig_add() {
1351 unless ($SIG_COUNT++) { 1396 unless ($SIG_COUNT++) {
1352 # try to align timer on a full-second boundary, if possible 1397 # try to align timer on a full-second boundary, if possible
1353 my $NOW = AnyEvent->now; 1398 my $NOW = AE::now;
1354 1399
1355 $SIG_TW = AnyEvent->timer ( 1400 $SIG_TW = AE::timer
1356 after => $MAX_SIGNAL_LATENCY - ($NOW - int $NOW), 1401 $MAX_SIGNAL_LATENCY - ($NOW - int $NOW),
1357 interval => $MAX_SIGNAL_LATENCY, 1402 $MAX_SIGNAL_LATENCY,
1358 cb => sub { }, # just for the PERL_ASYNC_CHECK 1403 sub { } # just for the PERL_ASYNC_CHECK
1359 ); 1404 ;
1360 } 1405 }
1361} 1406}
1362 1407
1363sub _sig_del { 1408sub _sig_del {
1364 undef $SIG_TW 1409 undef $SIG_TW
1401 # probe for availability of Async::Interrupt 1446 # probe for availability of Async::Interrupt
1402 if (_have_async_interrupt) { 1447 if (_have_async_interrupt) {
1403 warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8; 1448 warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8;
1404 1449
1405 $SIGPIPE_R = new Async::Interrupt::EventPipe; 1450 $SIGPIPE_R = new Async::Interrupt::EventPipe;
1406 $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R->fileno, poll => "r", cb => \&_signal_exec); 1451 $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec;
1407 1452
1408 } else { 1453 } else {
1409 warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; 1454 warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8;
1410 1455
1411 require Fcntl; 1456 require Fcntl;
1427 } 1472 }
1428 1473
1429 $SIGPIPE_R 1474 $SIGPIPE_R
1430 or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; 1475 or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n";
1431 1476
1432 $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R, poll => "r", cb => \&_signal_exec); 1477 $SIG_IO = AE::io $SIGPIPE_R, 0, \&_signal_exec;
1433 } 1478 }
1434 1479
1435 *signal = sub { 1480 *signal = sub {
1436 my (undef, %arg) = @_; 1481 my (undef, %arg) = @_;
1437 1482
1526 $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/ 1571 $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/
1527 ? 1 1572 ? 1
1528 : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; 1573 : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1;
1529 1574
1530 unless ($CHLD_W) { 1575 unless ($CHLD_W) {
1531 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); 1576 $CHLD_W = AE::signal CHLD => \&_sigchld;
1532 # child could be a zombie already, so make at least one round 1577 # child could be a zombie already, so make at least one round
1533 &_sigchld; 1578 &_sigchld;
1534 } 1579 }
1535 1580
1536 bless [$pid, $arg{cb}], "AnyEvent::Base::child" 1581 bless [$pid, $arg{cb}], "AnyEvent::Base::child"
1562 # never use more then 50% of the time for the idle watcher, 1607 # never use more then 50% of the time for the idle watcher,
1563 # within some limits 1608 # within some limits
1564 $w = 0.0001 if $w < 0.0001; 1609 $w = 0.0001 if $w < 0.0001;
1565 $w = 5 if $w > 5; 1610 $w = 5 if $w > 5;
1566 1611
1567 $w = AnyEvent->timer (after => $w, cb => $rcb); 1612 $w = AE::timer $w, 0, $rcb;
1568 } else { 1613 } else {
1569 # clean up... 1614 # clean up...
1570 undef $w; 1615 undef $w;
1571 undef $rcb; 1616 undef $rcb;
1572 } 1617 }
1573 }; 1618 };
1574 1619
1575 $w = AnyEvent->timer (after => 0.05, cb => $rcb); 1620 $w = AE::timer 0.05, 0, $rcb;
1576 1621
1577 bless \\$cb, "AnyEvent::Base::idle" 1622 bless \\$cb, "AnyEvent::Base::idle"
1578} 1623}
1579 1624
1580sub AnyEvent::Base::idle::DESTROY { 1625sub AnyEvent::Base::idle::DESTROY {
1640 1685
1641 @_ 1686 @_
1642 and $cv->{_ae_cb} = shift 1687 and $cv->{_ae_cb} = shift
1643 and $cv->{_ae_sent} 1688 and $cv->{_ae_sent}
1644 and (delete $cv->{_ae_cb})->($cv); 1689 and (delete $cv->{_ae_cb})->($cv);
1690
1645 $cv->{_ae_cb} 1691 $cv->{_ae_cb}
1646} 1692}
1647 1693
1648sub begin { 1694sub begin {
1649 ++$_[0]{_ae_counter}; 1695 ++$_[0]{_ae_counter};
1656} 1702}
1657 1703
1658# undocumented/compatibility with pre-3.4 1704# undocumented/compatibility with pre-3.4
1659*broadcast = \&send; 1705*broadcast = \&send;
1660*wait = \&_wait; 1706*wait = \&_wait;
1661
1662#############################################################################
1663# "new" API, currently only emulation of it
1664#############################################################################
1665
1666package AE;
1667
1668sub io($$$) {
1669 AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2])
1670}
1671
1672sub timer($$$) {
1673 AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]);
1674}
1675
1676sub signal($$) {
1677 AnyEvent->signal (signal => $_[0], cb => $_[1]);
1678}
1679
1680sub child($$) {
1681 AnyEvent->child (pid => $_[0], cb => $_[1]);
1682}
1683
1684sub idle($) {
1685 AnyEvent->idle (cb => $_[0]);
1686}
1687
1688sub cv() {
1689 AnyEvent->condvar
1690}
1691
1692sub now() {
1693 AnyEvent->now
1694}
1695
1696sub now_update() {
1697 AnyEvent->now_update
1698}
1699
1700sub time() {
1701 AnyEvent->time
1702}
1703 1707
1704=head1 ERROR AND EXCEPTION HANDLING 1708=head1 ERROR AND EXCEPTION HANDLING
1705 1709
1706In general, AnyEvent does not do any error handling - it relies on the 1710In general, AnyEvent does not do any error handling - it relies on the
1707caller to do that if required. The L<AnyEvent::Strict> module (see also 1711caller to do that if required. The L<AnyEvent::Strict> module (see also

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines