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.359 by root, Sat Aug 13 16:11:33 2011 UTC vs.
Revision 1.365 by root, Tue Aug 16 14:47:26 2011 UTC

1049It should use C<postpone>: 1049It should use C<postpone>:
1050 1050
1051 AnyEvent::postpone { $cb->(undef) }, return # signal error to callback, later 1051 AnyEvent::postpone { $cb->(undef) }, return # signal error to callback, later
1052 if $some_error_condition; 1052 if $some_error_condition;
1053 1053
1054=item AnyEvent::log $level, $msg[, @args]
1055
1056Log the given C<$msg> at the given C<$level>.
1057
1058Loads AnyEvent::Log on first use and calls C<AnyEvent::Log::log> -
1059consequently, look at the L<AnyEvent::Log> documentation for details.
1060
1054=back 1061=back
1055 1062
1056=head1 WHAT TO DO IN A MODULE 1063=head1 WHAT TO DO IN A MODULE
1057 1064
1058As a module author, you should C<use AnyEvent> and call AnyEvent methods 1065As a module author, you should C<use AnyEvent> and call AnyEvent methods
1206 1213
1207BEGIN { AnyEvent::common_sense } 1214BEGIN { AnyEvent::common_sense }
1208 1215
1209use Carp (); 1216use Carp ();
1210 1217
1211our $VERSION = '5.34'; 1218our $VERSION = '6.01';
1212our $MODEL; 1219our $MODEL;
1213 1220
1214our @ISA; 1221our @ISA;
1215 1222
1216our @REGISTRY; 1223our @REGISTRY;
1224 1231
1225 delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} 1232 delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV}
1226 if ${^TAINT}; 1233 if ${^TAINT};
1227 1234
1228 $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; 1235 $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1;
1229
1230} 1236}
1231 1237
1232our $MAX_SIGNAL_LATENCY = 10; 1238our $MAX_SIGNAL_LATENCY = 10;
1233 1239
1234our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred 1240our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred
1270 push @POSTPONE, shift; 1276 push @POSTPONE, shift;
1271 1277
1272 $POSTPONE_W ||= AE::timer (0, 0, \&_postpone_exec); 1278 $POSTPONE_W ||= AE::timer (0, 0, \&_postpone_exec);
1273 1279
1274 () 1280 ()
1281}
1282
1283sub log($$;@) {
1284 require AnyEvent::Log;
1285 # AnyEvent::Log overwrites this function
1286 goto &log;
1275} 1287}
1276 1288
1277our @models = ( 1289our @models = (
1278 [EV:: => AnyEvent::Impl::EV:: , 1], 1290 [EV:: => AnyEvent::Impl::EV:: , 1],
1279 [AnyEvent::Loop:: => AnyEvent::Impl::Perl:: , 1], 1291 [AnyEvent::Loop:: => AnyEvent::Impl::Perl:: , 1],
1292 [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], # a bitch to autodetect 1304 [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], # a bitch to autodetect
1293 [Cocoa::EventLoop:: => AnyEvent::Impl::Cocoa::], 1305 [Cocoa::EventLoop:: => AnyEvent::Impl::Cocoa::],
1294 [FLTK:: => AnyEvent::Impl::FLTK2::], 1306 [FLTK:: => AnyEvent::Impl::FLTK2::],
1295); 1307);
1296 1308
1309our @isa_hook;
1310
1311sub _isa_set {
1312 my @pkg = ("AnyEvent", (map $_->[0], grep defined, @isa_hook), $MODEL);
1313
1314 @{"$pkg[$_-1]::ISA"} = $pkg[$_]
1315 for 1 .. $#pkg;
1316
1317 grep $_ && $_->[1], @isa_hook
1318 and AE::_reset ();
1319}
1320
1321# used for hooking AnyEvent::Strict and AnyEvent::Debug::Wrap into the class hierarchy
1322sub _isa_hook($$;$) {
1323 my ($i, $pkg, $reset_ae) = @_;
1324
1325 $isa_hook[$i] = $pkg ? [$pkg, $reset_ae] : undef;
1326
1327 _isa_set;
1328}
1329
1297# all autoloaded methods reserve the complete glob, not just the method slot. 1330# all autoloaded methods reserve the complete glob, not just the method slot.
1298# due to bugs in perls method cache implementation. 1331# due to bugs in perls method cache implementation.
1299our @methods = qw(io timer time now now_update signal child idle condvar); 1332our @methods = qw(io timer time now now_update signal child idle condvar);
1300 1333
1301sub detect() { 1334sub detect() {
1335 return $MODEL if $MODEL; # some programs keep references to detect
1336
1302 local $!; # for good measure 1337 local $!; # for good measure
1303 local $SIG{__DIE__}; # we use eval 1338 local $SIG{__DIE__}; # we use eval
1304 1339
1305 # free some memory 1340 # free some memory
1306 *detect = sub () { $MODEL }; 1341 *detect = sub () { $MODEL };
1315 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z0-9:]+)$/) { 1350 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z0-9:]+)$/) {
1316 my $model = $1; 1351 my $model = $1;
1317 $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//; 1352 $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//;
1318 if (eval "require $model") { 1353 if (eval "require $model") {
1319 $MODEL = $model; 1354 $MODEL = $model;
1320 warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2; 1355 AnyEvent::log 7 => "loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it."
1356 if $VERBOSE >= 7;
1321 } else { 1357 } else {
1322 warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE; 1358 AnyEvent::log warn => "unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@";
1323 } 1359 }
1324 } 1360 }
1325 1361
1326 # check for already loaded models 1362 # check for already loaded models
1327 unless ($MODEL) { 1363 unless ($MODEL) {
1328 for (@REGISTRY, @models) { 1364 for (@REGISTRY, @models) {
1329 my ($package, $model) = @$_; 1365 my ($package, $model) = @$_;
1330 if (${"$package\::VERSION"} > 0) { 1366 if (${"$package\::VERSION"} > 0) {
1331 if (eval "require $model") { 1367 if (eval "require $model") {
1332 $MODEL = $model; 1368 $MODEL = $model;
1333 warn "AnyEvent: autodetected model '$model', using it.\n" if $VERBOSE >= 2; 1369 AnyEvent::log 7 => "autodetected model '$model', using it."
1370 if $VERBOSE >= 7;
1334 last; 1371 last;
1335 } 1372 }
1336 } 1373 }
1337 } 1374 }
1338 1375
1345 and eval "require $package" 1382 and eval "require $package"
1346 and ${"$package\::VERSION"} > 0 1383 and ${"$package\::VERSION"} > 0
1347 and eval "require $model" 1384 and eval "require $model"
1348 ) { 1385 ) {
1349 $MODEL = $model; 1386 $MODEL = $model;
1350 warn "AnyEvent: autoloaded model '$model', using it.\n" if $VERBOSE >= 2; 1387 AnyEvent::log 7 => "autoloaded model '$model', using it."
1388 if $VERBOSE >= 7;
1351 last; 1389 last;
1352 } 1390 }
1353 } 1391 }
1354 1392
1355 $MODEL 1393 $MODEL
1356 or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?\n"; 1394 or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?";
1357 } 1395 }
1358 } 1396 }
1359 1397
1360 # free memory only needed for probing 1398 # free memory only needed for probing
1361 undef @models; 1399 undef @models;
1362 undef @REGISTRY; 1400 undef @REGISTRY;
1363 1401
1364 push @{"$MODEL\::ISA"}, "AnyEvent::Base"; 1402 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
1365 unshift @ISA, $MODEL;
1366 1403
1367 # now nuke some methods that are overridden by the backend. 1404 # now nuke some methods that are overridden by the backend.
1368 # SUPER usage is not allowed in these. 1405 # SUPER usage is not allowed in these.
1369 for (qw(time signal child idle)) { 1406 for (qw(time signal child idle)) {
1370 undef &{"AnyEvent::Base::$_"} 1407 undef &{"AnyEvent::Base::$_"}
1371 if defined &{"$MODEL\::$_"}; 1408 if defined &{"$MODEL\::$_"};
1372 } 1409 }
1410
1411 _isa_set;
1373 1412
1374 if ($ENV{PERL_ANYEVENT_STRICT}) { 1413 if ($ENV{PERL_ANYEVENT_STRICT}) {
1375 require AnyEvent::Strict; 1414 require AnyEvent::Strict;
1376 } 1415 }
1377 1416
1484 sub time() { 1523 sub time() {
1485 AnyEvent->time 1524 AnyEvent->time
1486 } 1525 }
1487 1526
1488 *postpone = \&AnyEvent::postpone; 1527 *postpone = \&AnyEvent::postpone;
1528 *log = \&AnyEvent::log;
1489 }; 1529 };
1490 die if $@; 1530 die if $@;
1491} 1531}
1492 1532
1493BEGIN { _reset } 1533BEGIN { _reset }
1498 1538
1499sub time { 1539sub time {
1500 eval q{ # poor man's autoloading {} 1540 eval q{ # poor man's autoloading {}
1501 # probe for availability of Time::HiRes 1541 # probe for availability of Time::HiRes
1502 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { 1542 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") {
1503 warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; 1543 AnyEvent::log 8 => "AnyEvent: using Time::HiRes for sub-second timing accuracy."
1544 if $AnyEvent::VERBOSE >= 8;
1545 *time = sub { Time::HiRes::time () };
1504 *AE::time = \&Time::HiRes::time; 1546 *AE::time = \& Time::HiRes::time ;
1505 # if (eval "use POSIX (); (POSIX::times())... 1547 # if (eval "use POSIX (); (POSIX::times())...
1506 } else { 1548 } else {
1507 warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; 1549 AnyEvent::log critical => "using built-in time(), WARNING, no sub-second resolution!";
1550 *time = sub { CORE::time };
1508 *AE::time = sub (){ time }; # epic fail 1551 *AE::time = sub (){ CORE::time };
1509 } 1552 }
1510 1553
1511 *time = sub { AE::time }; # different prototypes 1554 *now = \&time;
1512 }; 1555 };
1513 die if $@; 1556 die if $@;
1514 1557
1515 &time 1558 &time
1516} 1559}
1517 1560
1518*now = \&time; 1561*now = \&time;
1519
1520sub now_update { } 1562sub now_update { }
1521 1563
1522sub _poll { 1564sub _poll {
1523 Carp::croak "$AnyEvent::MODEL does not support blocking waits. Caught"; 1565 Carp::croak "$AnyEvent::MODEL does not support blocking waits. Caught";
1524} 1566}
1610 1652
1611sub signal { 1653sub signal {
1612 eval q{ # poor man's autoloading {} 1654 eval q{ # poor man's autoloading {}
1613 # probe for availability of Async::Interrupt 1655 # probe for availability of Async::Interrupt
1614 if (_have_async_interrupt) { 1656 if (_have_async_interrupt) {
1615 warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8; 1657 AnyEvent::log 8 => "using Async::Interrupt for race-free signal handling."
1658 if $AnyEvent::VERBOSE >= 8;
1616 1659
1617 $SIGPIPE_R = new Async::Interrupt::EventPipe; 1660 $SIGPIPE_R = new Async::Interrupt::EventPipe;
1618 $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; 1661 $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec;
1619 1662
1620 } else { 1663 } else {
1621 warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; 1664 AnyEvent::log 8 => "using emulated perl signal handling with latency timer."
1665 if $AnyEvent::VERBOSE >= 8;
1622 1666
1623 if (AnyEvent::WIN32) { 1667 if (AnyEvent::WIN32) {
1624 require AnyEvent::Util; 1668 require AnyEvent::Util;
1625 1669
1626 ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); 1670 ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe ();
1935 1979
1936By default, AnyEvent will be completely silent except in fatal 1980By default, AnyEvent will be completely silent except in fatal
1937conditions. You can set this environment variable to make AnyEvent more 1981conditions. You can set this environment variable to make AnyEvent more
1938talkative. 1982talkative.
1939 1983
1940When set to C<1> or higher, causes AnyEvent to warn about unexpected 1984When set to C<5> or higher, causes AnyEvent to warn about unexpected
1941conditions, such as not being able to load the event model specified by 1985conditions, such as not being able to load the event model specified by
1942C<PERL_ANYEVENT_MODEL>. 1986C<PERL_ANYEVENT_MODEL>.
1943 1987
1944When set to C<2> or higher, cause AnyEvent to report to STDERR which event 1988When set to C<7> or higher, cause AnyEvent to report to STDERR which event
1945model it chooses. 1989model it chooses.
1946 1990
1947When set to C<8> or higher, then AnyEvent will report extra information on 1991When set to C<8> or higher, then AnyEvent will report extra information on
1948which optional modules it loads and how it implements certain features. 1992which optional modules it loads and how it implements certain features.
1949 1993
1972This takes place when the first watcher is created. 2016This takes place when the first watcher is created.
1973 2017
1974For example, to bind a debug shell on a unix domain socket in 2018For example, to bind a debug shell on a unix domain socket in
1975F<< /tmp/debug<pid>.sock >>, you could use this: 2019F<< /tmp/debug<pid>.sock >>, you could use this:
1976 2020
1977 PERL_ANYEVENT_DEBUG_SHELL=unix/:/tmp/debug\$\$.sock perlprog 2021 PERL_ANYEVENT_DEBUG_SHELL=/tmp/debug\$\$.sock perlprog
1978 2022
1979Note that creating sockets in F</tmp> is very unsafe on multiuser 2023Note that creating sockets in F</tmp> is very unsafe on multiuser
1980systems. 2024systems.
1981 2025
1982=item C<PERL_ANYEVENT_DEBUG_WRAP> 2026=item C<PERL_ANYEVENT_DEBUG_WRAP>
2397(even when used without AnyEvent), but most event loops have acceptable 2441(even when used without AnyEvent), but most event loops have acceptable
2398performance with or without AnyEvent. 2442performance with or without AnyEvent.
2399 2443
2400=item * The overhead AnyEvent adds is usually much smaller than the overhead of 2444=item * The overhead AnyEvent adds is usually much smaller than the overhead of
2401the actual event loop, only with extremely fast event loops such as EV 2445the actual event loop, only with extremely fast event loops such as EV
2402adds AnyEvent significant overhead. 2446does AnyEvent add significant overhead.
2403 2447
2404=item * You should avoid POE like the plague if you want performance or 2448=item * You should avoid POE like the plague if you want performance or
2405reasonable memory usage. 2449reasonable memory usage.
2406 2450
2407=back 2451=back
2782 2826
2783Tutorial/Introduction: L<AnyEvent::Intro>. 2827Tutorial/Introduction: L<AnyEvent::Intro>.
2784 2828
2785FAQ: L<AnyEvent::FAQ>. 2829FAQ: L<AnyEvent::FAQ>.
2786 2830
2787Utility functions: L<AnyEvent::Util>. 2831Utility functions: L<AnyEvent::Util> (misc. grab-bag), L<AnyEvent::Log>
2832(simply logging).
2788 2833
2834Development/Debugging: L<AnyEvent::Strict> (stricter checking),
2835L<AnyEvent::Debug> (interactive shell, watcher tracing).
2836
2789Event modules: L<AnyEvent::Loop>, L<EV>, L<EV::Glib>, L<Glib::EV>, 2837Supported event modules: L<AnyEvent::Loop>, L<EV>, L<EV::Glib>,
2790L<Event>, L<Glib::Event>, L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. 2838L<Glib::EV>, L<Event>, L<Glib::Event>, L<Glib>, L<Tk>, L<Event::Lib>,
2839L<Qt>, L<POE>, L<FLTK>.
2791 2840
2792Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, 2841Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>,
2793L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, 2842L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>,
2794L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, 2843L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>,
2795L<AnyEvent::Impl::POE>, L<AnyEvent::Impl::IOAsync>, L<Anyevent::Impl::Irssi>. 2844L<AnyEvent::Impl::POE>, L<AnyEvent::Impl::IOAsync>, L<Anyevent::Impl::Irssi>,
2845L<AnyEvent::Impl::FLTK>.
2796 2846
2797Non-blocking file handles, sockets, TCP clients and 2847Non-blocking handles, pipes, stream sockets, TCP clients and
2798servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>, L<AnyEvent::TLS>. 2848servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>, L<AnyEvent::TLS>.
2799 2849
2800Asynchronous DNS: L<AnyEvent::DNS>. 2850Asynchronous DNS: L<AnyEvent::DNS>.
2801 2851
2802Thread support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>. 2852Thread support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines