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.358 by root, Sat Aug 13 02:35:32 2011 UTC vs.
Revision 1.364 by root, Mon Aug 15 12:56:53 2011 UTC

934Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model 934Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model
935if necessary. You should only call this function right before you would 935if necessary. You should only call this function right before you would
936have created an AnyEvent watcher anyway, that is, as late as possible at 936have created an AnyEvent watcher anyway, that is, as late as possible at
937runtime, and not e.g. during initialisation of your module. 937runtime, and not e.g. during initialisation of your module.
938 938
939The effect of calling this function is as if a watcher had been created
940(specifically, actions that happen "when the first watcher is created"
941happen when calling detetc as well).
942
939If you need to do some initialisation before AnyEvent watchers are 943If you need to do some initialisation before AnyEvent watchers are
940created, use C<post_detect>. 944created, use C<post_detect>.
941 945
942=item $guard = AnyEvent::post_detect { BLOCK } 946=item $guard = AnyEvent::post_detect { BLOCK }
943 947
1202 1206
1203BEGIN { AnyEvent::common_sense } 1207BEGIN { AnyEvent::common_sense }
1204 1208
1205use Carp (); 1209use Carp ();
1206 1210
1207our $VERSION = '5.34'; 1211our $VERSION = '6.01';
1208our $MODEL; 1212our $MODEL;
1209 1213
1210our @ISA; 1214our @ISA;
1211 1215
1212our @REGISTRY; 1216our @REGISTRY;
1288 [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], # a bitch to autodetect 1292 [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], # a bitch to autodetect
1289 [Cocoa::EventLoop:: => AnyEvent::Impl::Cocoa::], 1293 [Cocoa::EventLoop:: => AnyEvent::Impl::Cocoa::],
1290 [FLTK:: => AnyEvent::Impl::FLTK2::], 1294 [FLTK:: => AnyEvent::Impl::FLTK2::],
1291); 1295);
1292 1296
1297our @isa_hook;
1298
1299sub _isa_set {
1300 my @pkg = ("AnyEvent", (map $_->[0], grep defined, @isa_hook), $MODEL);
1301
1302 @{"$pkg[$_-1]::ISA"} = $pkg[$_]
1303 for 1 .. $#pkg;
1304
1305 grep $_ && $_->[1], @isa_hook
1306 and AE::_reset ();
1307}
1308
1309# used for hooking AnyEvent::Strict and AnyEvent::Debug::Wrap into the class hierarchy
1310sub _isa_hook($$;$) {
1311 my ($i, $pkg, $reset_ae) = @_;
1312
1313 $isa_hook[$i] = $pkg ? [$pkg, $reset_ae] : undef;
1314
1315 _isa_set;
1316}
1317
1293# all autoloaded methods reserve the complete glob, not just the method slot. 1318# all autoloaded methods reserve the complete glob, not just the method slot.
1294# due to bugs in perls method cache implementation. 1319# due to bugs in perls method cache implementation.
1295our @methods = qw(io timer time now now_update signal child idle condvar); 1320our @methods = qw(io timer time now now_update signal child idle condvar);
1296 1321
1297sub detect() { 1322sub detect() {
1323 return $MODEL if $MODEL; # some programs keep references to detect
1324
1298 local $!; # for good measure 1325 local $!; # for good measure
1299 local $SIG{__DIE__}; # we use eval 1326 local $SIG{__DIE__}; # we use eval
1300 1327
1301 # free some memory 1328 # free some memory
1302 *detect = sub () { $MODEL }; 1329 *detect = sub () { $MODEL };
1356 # free memory only needed for probing 1383 # free memory only needed for probing
1357 undef @models; 1384 undef @models;
1358 undef @REGISTRY; 1385 undef @REGISTRY;
1359 1386
1360 push @{"$MODEL\::ISA"}, "AnyEvent::Base"; 1387 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
1361 unshift @ISA, $MODEL;
1362 1388
1363 # now nuke some methods that are overridden by the backend. 1389 # now nuke some methods that are overridden by the backend.
1364 # SUPER usage is not allowed in these. 1390 # SUPER usage is not allowed in these.
1365 for (qw(time signal child idle)) { 1391 for (qw(time signal child idle)) {
1366 undef &{"AnyEvent::Base::$_"} 1392 undef &{"AnyEvent::Base::$_"}
1367 if defined &{"$MODEL\::$_"}; 1393 if defined &{"$MODEL\::$_"};
1368 } 1394 }
1369 1395
1396 _isa_set;
1397
1370 if ($ENV{PERL_ANYEVENT_STRICT}) { 1398 if ($ENV{PERL_ANYEVENT_STRICT}) {
1371 require AnyEvent::Strict; 1399 require AnyEvent::Strict;
1372 } 1400 }
1373 1401
1374 if ($ENV{PERL_ANYEVENT_DEBUG_WRAP}) { 1402 if ($ENV{PERL_ANYEVENT_DEBUG_WRAP}) {
1378 1406
1379 if (exists $ENV{PERL_ANYEVENT_DEBUG_SHELL}) { 1407 if (exists $ENV{PERL_ANYEVENT_DEBUG_SHELL}) {
1380 require AnyEvent::Socket; 1408 require AnyEvent::Socket;
1381 require AnyEvent::Debug; 1409 require AnyEvent::Debug;
1382 1410
1411 my $shell = $ENV{PERL_ANYEVENT_DEBUG_SHELL};
1412 $shell =~ s/\$\$/$$/g;
1413
1383 my ($host, $service) = AnyEvent::Socket::parse_hostport ($ENV{PERL_ANYEVENT_DEBUG_SHELL}); 1414 my ($host, $service) = AnyEvent::Socket::parse_hostport ($shell);
1384 $AnyEvent::Debug::SHELL = AnyEvent::Debug::shell ($host, $service); 1415 $AnyEvent::Debug::SHELL = AnyEvent::Debug::shell ($host, $service);
1385 } 1416 }
1386 1417
1387 (shift @post_detect)->() while @post_detect; 1418 (shift @post_detect)->() while @post_detect;
1388 undef @post_detect; 1419 undef @post_detect;
1492sub time { 1523sub time {
1493 eval q{ # poor man's autoloading {} 1524 eval q{ # poor man's autoloading {}
1494 # probe for availability of Time::HiRes 1525 # probe for availability of Time::HiRes
1495 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { 1526 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") {
1496 warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; 1527 warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8;
1528 *time = sub { Time::HiRes::time () };
1497 *AE::time = \&Time::HiRes::time; 1529 *AE::time = \& Time::HiRes::time ;
1498 # if (eval "use POSIX (); (POSIX::times())... 1530 # if (eval "use POSIX (); (POSIX::times())...
1499 } else { 1531 } else {
1500 warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; 1532 warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE;
1533 *time = sub { CORE::time };
1501 *AE::time = sub (){ time }; # epic fail 1534 *AE::time = sub (){ CORE::time };
1502 } 1535 }
1503 1536
1504 *time = sub { AE::time }; # different prototypes 1537 *now = \&time;
1505 }; 1538 };
1506 die if $@; 1539 die if $@;
1507 1540
1508 &time 1541 &time
1509} 1542}
1510 1543
1511*now = \&time; 1544*now = \&time;
1512
1513sub now_update { } 1545sub now_update { }
1514 1546
1515sub _poll { 1547sub _poll {
1516 Carp::croak "$AnyEvent::MODEL does not support blocking waits. Caught"; 1548 Carp::croak "$AnyEvent::MODEL does not support blocking waits. Caught";
1517} 1549}
1955C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs 1987C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs
1956can be very useful, however. 1988can be very useful, however.
1957 1989
1958=item C<PERL_ANYEVENT_DEBUG_SHELL> 1990=item C<PERL_ANYEVENT_DEBUG_SHELL>
1959 1991
1960If this env variable is set, then its contents will be 1992If this env variable is set, then its contents will be interpreted by
1961interpreted by C<AnyEvent::Socket::parse_hostport> and an 1993C<AnyEvent::Socket::parse_hostport> (after replacing every occurance of
1962C<AnyEvent::Debug::shell> is bound on that port. The shell object is saved 1994C<$$> by the process pid) and an C<AnyEvent::Debug::shell> is bound on
1963in C<$AnyEvent::Debug::SHELL>. 1995that port. The shell object is saved in C<$AnyEvent::Debug::SHELL>.
1996
1997This takes place when the first watcher is created.
1964 1998
1965For example, to bind a debug shell on a unix domain socket in 1999For example, to bind a debug shell on a unix domain socket in
1966F</tmp/debug.sock>, you could use this: 2000F<< /tmp/debug<pid>.sock >>, you could use this:
1967 2001
1968 PERL_ANYEVENT_DEBUG_SHELL=unix/:/tmp/debug.sock perlprog 2002 PERL_ANYEVENT_DEBUG_SHELL=/tmp/debug\$\$.sock perlprog
2003
2004Note that creating sockets in F</tmp> is very unsafe on multiuser
2005systems.
1969 2006
1970=item C<PERL_ANYEVENT_DEBUG_WRAP> 2007=item C<PERL_ANYEVENT_DEBUG_WRAP>
1971 2008
1972Can be set to C<0>, C<1> or C<2> and enables wrapping of all watchers for 2009Can be set to C<0>, C<1> or C<2> and enables wrapping of all watchers for
1973debugging purposes. See C<AnyEvent::Debug::wrap> for details. 2010debugging purposes. See C<AnyEvent::Debug::wrap> for details.
2385(even when used without AnyEvent), but most event loops have acceptable 2422(even when used without AnyEvent), but most event loops have acceptable
2386performance with or without AnyEvent. 2423performance with or without AnyEvent.
2387 2424
2388=item * The overhead AnyEvent adds is usually much smaller than the overhead of 2425=item * The overhead AnyEvent adds is usually much smaller than the overhead of
2389the actual event loop, only with extremely fast event loops such as EV 2426the actual event loop, only with extremely fast event loops such as EV
2390adds AnyEvent significant overhead. 2427does AnyEvent add significant overhead.
2391 2428
2392=item * You should avoid POE like the plague if you want performance or 2429=item * You should avoid POE like the plague if you want performance or
2393reasonable memory usage. 2430reasonable memory usage.
2394 2431
2395=back 2432=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines