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.381 by root, Thu Sep 1 22:09:25 2011 UTC vs.
Revision 1.386 by root, Mon Sep 26 11:32:19 2011 UTC

1237 1237
1238use Carp (); 1238use Carp ();
1239 1239
1240our $VERSION = '6.02'; 1240our $VERSION = '6.02';
1241our $MODEL; 1241our $MODEL;
1242
1243our @ISA; 1242our @ISA;
1244
1245our @REGISTRY; 1243our @REGISTRY;
1246
1247our $VERBOSE; 1244our $VERBOSE;
1245our $MAX_SIGNAL_LATENCY = 10;
1246our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred
1248 1247
1249BEGIN { 1248BEGIN {
1250 require "AnyEvent/constants.pl"; 1249 require "AnyEvent/constants.pl";
1251 1250
1252 eval "sub TAINT (){" . (${^TAINT}*1) . "}"; 1251 eval "sub TAINT (){" . (${^TAINT}*1) . "}";
1260 @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} = () 1259 @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} = ()
1261 if ${^TAINT}; 1260 if ${^TAINT};
1262 1261
1263 # $ENV{PERL_ANYEVENT_xxx} now valid 1262 # $ENV{PERL_ANYEVENT_xxx} now valid
1264 1263
1265 $VERBOSE = length $ENV{PERL_ANYEVENT_VERBOSE} ? $ENV{PERL_ANYEVENT_VERBOSE}*1 : 3; 1264 $VERBOSE = length $ENV{PERL_ANYEVENT_VERBOSE} ? $ENV{PERL_ANYEVENT_VERBOSE}*1 : 4;
1266}
1267 1265
1268our $MAX_SIGNAL_LATENCY = 10;
1269
1270our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred
1271
1272{
1273 my $idx; 1266 my $idx;
1274 $PROTOCOL{$_} = ++$idx 1267 $PROTOCOL{$_} = ++$idx
1275 for reverse split /\s*,\s*/, 1268 for reverse split /\s*,\s*/,
1276 $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; 1269 $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6";
1277} 1270}
1310 () 1303 ()
1311} 1304}
1312 1305
1313sub log($$;@) { 1306sub log($$;@) {
1314 # only load the big bloated module when we actually are about to log something 1307 # only load the big bloated module when we actually are about to log something
1315 if ($_[0] <= $VERBOSE) { # also catches non-numeric levels(!) 1308 if ($_[0] <= ($VERBOSE || 1)) { # also catches non-numeric levels(!) and fatal
1316 require AnyEvent::Log; 1309 require AnyEvent::Log; # among other things, sets $VERBOSE to 9
1317 # AnyEvent::Log overwrites this function 1310 # AnyEvent::Log overwrites this function
1318 goto &log; 1311 goto &log;
1319 } 1312 }
1320 1313
1321 0 # not logged 1314 0 # not logged
1322} 1315}
1323 1316
1317sub logger($;$) {
1318 package AnyEvent::Log;
1319
1320 my ($level, $renabled) = @_;
1321
1322 $$renabled = $level <= $VERBOSE;
1323
1324 my $pkg = (caller)[0];
1325
1326 my $logger = [$pkg, $level, $renabled];
1327
1328 our %LOGGER;
1329 $LOGGER{$logger+0} = $logger;
1330
1331 require AnyEvent::Util;
1332 my $guard = AnyEvent::Util::guard (sub {
1333 # "clean up"
1334 delete $LOGGER{$logger+0};
1335 });
1336
1337 sub {
1338 return 0 unless $$renabled;
1339
1340 $guard if 0; # keep guard alive, but don't cause runtime overhead
1341 require AnyEvent::Log unless $AnyEvent::Log::VERSION;
1342 package AnyEvent::Log;
1343 _log ($logger->[0], $level, @_) # logger->[0] has been converted at load time
1344 }
1345}
1346
1324if (length $ENV{PERL_ANYEVENT_LOG}) { 1347if (length $ENV{PERL_ANYEVENT_LOG}) {
1325 require AnyEvent::Log; # AnyEvent::Log does the thing for us 1348 require AnyEvent::Log; # AnyEvent::Log does the thing for us
1326} 1349}
1327 1350
1328our @models = ( 1351our @models = (
1329 [EV:: => AnyEvent::Impl::EV:: , 1], 1352 [EV:: => AnyEvent::Impl::EV::],
1330 [AnyEvent::Loop:: => AnyEvent::Impl::Perl:: , 1], 1353 [AnyEvent::Loop:: => AnyEvent::Impl::Perl::],
1331 # everything below here will not (normally) be autoprobed 1354 # everything below here will not (normally) be autoprobed
1332 # as the pure perl backend should work everywhere 1355 # as the pure perl backend should work everywhere
1333 # and is usually faster 1356 # and is usually faster
1357 [Irssi:: => AnyEvent::Impl::Irssi::], # Irssi has a bogus "Event" package, so msut be near the top
1334 [Event:: => AnyEvent::Impl::Event::, 1], 1358 [Event:: => AnyEvent::Impl::Event::], # slow, stable
1335 [Glib:: => AnyEvent::Impl::Glib:: , 1], # becomes extremely slow with many watchers 1359 [Glib:: => AnyEvent::Impl::Glib::], # becomes extremely slow with many watchers
1360 # everything below here should not be autoloaded
1336 [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy 1361 [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy
1337 [Irssi:: => AnyEvent::Impl::Irssi::], # Irssi has a bogus "Event" package
1338 [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles 1362 [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles
1339 [Qt:: => AnyEvent::Impl::Qt::], # requires special main program 1363 [Qt:: => AnyEvent::Impl::Qt::], # requires special main program
1340 [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza 1364 [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza
1341 [Wx:: => AnyEvent::Impl::POE::], 1365 [Wx:: => AnyEvent::Impl::POE::],
1342 [Prima:: => AnyEvent::Impl::POE::], 1366 [Prima:: => AnyEvent::Impl::POE::],
1371our @methods = qw(io timer time now now_update signal child idle condvar); 1395our @methods = qw(io timer time now now_update signal child idle condvar);
1372 1396
1373sub detect() { 1397sub detect() {
1374 return $MODEL if $MODEL; # some programs keep references to detect 1398 return $MODEL if $MODEL; # some programs keep references to detect
1375 1399
1400 # IO::Async::Loop::AnyEvent is extremely evil, refuse to work with it
1401 # the author knows about the problems and what it does to AnyEvent as a whole
1402 # (and the ability of others to use AnyEvent), but simply wants to abuse AnyEvent
1403 # anyway.
1404 AnyEvent::log fatal => "AnyEvent: IO::Async::Loop::AnyEvent detected - this module is broken by design,\n"
1405 . "abuses internals and breaks AnyEvent, will not continue."
1406 if exists $INC{"IO/Async/Loop/AnyEvent.pm"};
1407
1376 local $!; # for good measure 1408 local $!; # for good measure
1377 local $SIG{__DIE__}; # we use eval 1409 local $SIG{__DIE__}; # we use eval
1378 1410
1379 # free some memory 1411 # free some memory
1380 *detect = sub () { $MODEL }; 1412 *detect = sub () { $MODEL };
1391 $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//; 1423 $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//;
1392 if (eval "require $model") { 1424 if (eval "require $model") {
1393 AnyEvent::log 7 => "loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it."; 1425 AnyEvent::log 7 => "loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.";
1394 $MODEL = $model; 1426 $MODEL = $model;
1395 } else { 1427 } else {
1396 AnyEvent::log 5 => "unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@"; 1428 AnyEvent::log 4 => "unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@";
1397 } 1429 }
1398 } 1430 }
1399 1431
1400 # check for already loaded models 1432 # check for already loaded models
1401 unless ($MODEL) { 1433 unless ($MODEL) {
1411 } 1443 }
1412 1444
1413 unless ($MODEL) { 1445 unless ($MODEL) {
1414 # try to autoload a model 1446 # try to autoload a model
1415 for (@REGISTRY, @models) { 1447 for (@REGISTRY, @models) {
1416 my ($package, $model, $autoload) = @$_; 1448 my ($package, $model) = @$_;
1417 if ( 1449 if (
1418 $autoload
1419 and eval "require $package" 1450 eval "require $package"
1420 and ${"$package\::VERSION"} > 0 1451 and ${"$package\::VERSION"} > 0
1421 and eval "require $model" 1452 and eval "require $model"
1422 ) { 1453 ) {
1423 AnyEvent::log 7 => "autoloaded model '$model', using it."; 1454 AnyEvent::log 7 => "autoloaded model '$model', using it.";
1424 $MODEL = $model; 1455 $MODEL = $model;
1425 last; 1456 last;
1426 } 1457 }
1427 } 1458 }
1428 1459
1429 $MODEL 1460 $MODEL
1430 or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?"; 1461 or AnyEvent::log fatal => "AnyEvent: backend autodetection failed - did you properly install AnyEvent?";
1431 } 1462 }
1432 } 1463 }
1433 1464
1434 # free memory only needed for probing 1465 # free memory only needed for probing
1435 undef @models; 1466 undef @models;
2102C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs 2133C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs
2103can be very useful, however. 2134can be very useful, however.
2104 2135
2105=item C<PERL_ANYEVENT_DEBUG_SHELL> 2136=item C<PERL_ANYEVENT_DEBUG_SHELL>
2106 2137
2107If this env variable is set, then its contents will be interpreted by 2138If this env variable is nonempty, then its contents will be interpreted by
2108C<AnyEvent::Socket::parse_hostport> (after replacing every occurance of 2139C<AnyEvent::Socket::parse_hostport> and C<AnyEvent::Debug::shell> (after
2109C<$$> by the process pid) and an C<AnyEvent::Debug::shell> is bound on 2140replacing every occurance of C<$$> by the process pid). The shell object
2110that port. The shell object is saved in C<$AnyEvent::Debug::SHELL>. 2141is saved in C<$AnyEvent::Debug::SHELL>.
2111 2142
2112This happens when the first watcher is created. 2143This happens when the first watcher is created.
2113 2144
2114For example, to bind a debug shell on a unix domain socket in 2145For example, to bind a debug shell on a unix domain socket in
2115F<< /tmp/debug<pid>.sock >>, you could use this: 2146F<< /tmp/debug<pid>.sock >>, you could use this:
2116 2147
2117 PERL_ANYEVENT_DEBUG_SHELL=/tmp/debug\$\$.sock perlprog 2148 PERL_ANYEVENT_DEBUG_SHELL=/tmp/debug\$\$.sock perlprog
2149 # connect with e.g.: socat readline /tmp/debug123.sock
2118 2150
2151Or to bind to tcp port 4545 on localhost:
2152
2153 PERL_ANYEVENT_DEBUG_SHELL=127.0.0.1:4545 perlprog
2154 # connect with e.g.: telnet localhost 4545
2155
2119Note that creating sockets in F</tmp> is very unsafe on multiuser 2156Note that creating sockets in F</tmp> or on localhost is very unsafe on
2120systems. 2157multiuser systems.
2121 2158
2122=item C<PERL_ANYEVENT_DEBUG_WRAP> 2159=item C<PERL_ANYEVENT_DEBUG_WRAP>
2123 2160
2124Can be set to C<0>, C<1> or C<2> and enables wrapping of all watchers for 2161Can be set to C<0>, C<1> or C<2> and enables wrapping of all watchers for
2125debugging purposes. See C<AnyEvent::Debug::wrap> for details. 2162debugging purposes. See C<AnyEvent::Debug::wrap> for details.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines