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.354 by root, Thu Aug 11 21:26:39 2011 UTC vs.
Revision 1.356 by root, Fri Aug 12 18:41:25 2011 UTC

878 AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. 878 AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse.
879 AnyEvent::Impl::POE based on POE, very slow, some limitations. 879 AnyEvent::Impl::POE based on POE, very slow, some limitations.
880 AnyEvent::Impl::Irssi used when running within irssi. 880 AnyEvent::Impl::Irssi used when running within irssi.
881 AnyEvent::Impl::IOAsync based on IO::Async. 881 AnyEvent::Impl::IOAsync based on IO::Async.
882 AnyEvent::Impl::Cocoa based on Cocoa::EventLoop. 882 AnyEvent::Impl::Cocoa based on Cocoa::EventLoop.
883 AnyEvent::Impl::FLTK based on FLTK. 883 AnyEvent::Impl::FLTK2 based on FLTK (fltk 2 binding).
884 884
885=item Backends with special needs. 885=item Backends with special needs.
886 886
887Qt requires the Qt::Application to be instantiated first, but will 887Qt requires the Qt::Application to be instantiated first, but will
888otherwise be picked up automatically. As long as the main program 888otherwise be picked up automatically. As long as the main program
1235 $PROTOCOL{$_} = ++$idx 1235 $PROTOCOL{$_} = ++$idx
1236 for reverse split /\s*,\s*/, 1236 for reverse split /\s*,\s*/,
1237 $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; 1237 $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6";
1238} 1238}
1239 1239
1240our @post_detect;
1241
1242sub post_detect(&) {
1243 my ($cb) = @_;
1244
1245 push @post_detect, $cb;
1246
1247 defined wantarray
1248 ? bless \$cb, "AnyEvent::Util::postdetect"
1249 : ()
1250}
1251
1252sub AnyEvent::Util::postdetect::DESTROY {
1253 @post_detect = grep $_ != ${$_[0]}, @post_detect;
1254}
1255
1256our $POSTPONE_W;
1257our @POSTPONE;
1258
1259sub _postpone_exec {
1260 undef $POSTPONE_W;
1261
1262 &{ shift @POSTPONE }
1263 while @POSTPONE;
1264}
1265
1266sub postpone(&) {
1267 push @POSTPONE, shift;
1268
1269 $POSTPONE_W ||= AE::timer (0, 0, \&_postpone_exec);
1270
1271 ()
1272}
1273
1240my @models = ( 1274our @models = (
1241 [EV:: => AnyEvent::Impl::EV:: , 1], 1275 [EV:: => AnyEvent::Impl::EV:: , 1],
1242 [AnyEvent::Loop:: => AnyEvent::Impl::Perl:: , 1], 1276 [AnyEvent::Loop:: => AnyEvent::Impl::Perl:: , 1],
1243 # everything below here will not (normally) be autoprobed 1277 # everything below here will not (normally) be autoprobed
1244 # as the pure perl backend should work everywhere 1278 # as the pure perl backend should work everywhere
1245 # and is usually faster 1279 # and is usually faster
1250 [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles 1284 [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles
1251 [Qt:: => AnyEvent::Impl::Qt::], # requires special main program 1285 [Qt:: => AnyEvent::Impl::Qt::], # requires special main program
1252 [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza 1286 [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza
1253 [Wx:: => AnyEvent::Impl::POE::], 1287 [Wx:: => AnyEvent::Impl::POE::],
1254 [Prima:: => AnyEvent::Impl::POE::], 1288 [Prima:: => AnyEvent::Impl::POE::],
1255 [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], 1289 [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], # a bitch to autodetect
1256 [Cocoa::EventLoop:: => AnyEvent::Impl::Cocoa::], 1290 [Cocoa::EventLoop:: => AnyEvent::Impl::Cocoa::],
1257 [FLTK:: => AnyEvent::Impl::FLTK::], 1291 [FLTK:: => AnyEvent::Impl::FLTK2::],
1258); 1292);
1259
1260our %method = map +($_ => 1),
1261 qw(io timer time now now_update signal child idle condvar DESTROY);
1262
1263our @post_detect;
1264
1265sub post_detect(&) {
1266 my ($cb) = @_;
1267
1268 push @post_detect, $cb;
1269
1270 defined wantarray
1271 ? bless \$cb, "AnyEvent::Util::postdetect"
1272 : ()
1273}
1274
1275sub AnyEvent::Util::postdetect::DESTROY {
1276 @post_detect = grep $_ != ${$_[0]}, @post_detect;
1277}
1278 1293
1279sub detect() { 1294sub detect() {
1280 # free some memory 1295 # free some memory
1281 *detect = sub () { $MODEL }; 1296 *detect = sub () { $MODEL };
1282 1297
1283 local $!; # for good measure 1298 local $!; # for good measure
1284 local $SIG{__DIE__}; 1299 local $SIG{__DIE__};
1285 1300
1286 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { 1301 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z0-9:]+)$/) {
1287 my $model = "AnyEvent::Impl::$1"; 1302 my $model = $1;
1303 $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//;
1288 if (eval "require $model") { 1304 if (eval "require $model") {
1289 $MODEL = $model; 1305 $MODEL = $model;
1290 warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2; 1306 warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2;
1291 } else { 1307 } else {
1292 warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE; 1308 warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE;
1325 $MODEL 1341 $MODEL
1326 or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?\n"; 1342 or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?\n";
1327 } 1343 }
1328 } 1344 }
1329 1345
1330 @models = (); # free probe data 1346 # free memory only needed for probing
1347 undef @models;
1348 undef @REGISTRY;
1331 1349
1332 push @{"$MODEL\::ISA"}, "AnyEvent::Base"; 1350 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
1333 unshift @ISA, $MODEL; 1351 unshift @ISA, $MODEL;
1334 1352
1335 # now nuke some methods that are overridden by the backend. 1353 # now nuke some methods that are overridden by the backend.
1336 # SUPER is not allowed. 1354 # SUPER usage is not allowed in these.
1337 for (qw(time signal child idle)) { 1355 for (qw(time signal child idle)) {
1338 undef &{"AnyEvent::Base::$_"} 1356 undef &{"AnyEvent::Base::$_"}
1339 if defined &{"$MODEL\::$_"}; 1357 if defined &{"$MODEL\::$_"};
1340 } 1358 }
1341 1359
1344 warn "AnyEvent: cannot load AnyEvent::Strict: $@" 1362 warn "AnyEvent: cannot load AnyEvent::Strict: $@"
1345 if $@ && $VERBOSE; 1363 if $@ && $VERBOSE;
1346 } 1364 }
1347 1365
1348 (shift @post_detect)->() while @post_detect; 1366 (shift @post_detect)->() while @post_detect;
1367 undef @post_detect;
1349 1368
1350 *post_detect = sub(&) { 1369 *post_detect = sub(&) {
1351 shift->(); 1370 shift->();
1352 1371
1353 undef 1372 undef
1354 }; 1373 };
1355 1374
1375 # recover a few more bytes
1376 postpone {
1377 undef &AUTOLOAD;
1378 };
1379
1356 $MODEL 1380 $MODEL
1357} 1381}
1382
1383our %method = map +($_ => 1),
1384 qw(io timer time now now_update signal child idle condvar DESTROY);
1358 1385
1359sub AUTOLOAD { 1386sub AUTOLOAD {
1360 (my $func = $AUTOLOAD) =~ s/.*://; 1387 (my $func = $AUTOLOAD) =~ s/.*://;
1361 1388
1362 $method{$func} 1389 $method{$func}
1363 or Carp::croak "$func: not a valid AnyEvent class method"; 1390 or Carp::croak "$func: not a valid AnyEvent class method";
1364 1391
1392 # free some memory
1393 undef %method;
1394
1365 detect; 1395 detect;
1366 1396
1367 my $class = shift; 1397 my $class = shift;
1368 $class->$func (@_); 1398 $class->$func (@_);
1369}
1370
1371our $POSTPONE_W;
1372our @POSTPONE;
1373
1374sub _postpone_exec {
1375 undef $POSTPONE_W;
1376 (pop @POSTPONE)->()
1377 while @POSTPONE;
1378}
1379
1380sub postpone(&) {
1381 push @POSTPONE, shift;
1382
1383 $POSTPONE_W ||= AE::timer (0, 0, \&_postpone_exec);
1384
1385 ()
1386} 1399}
1387 1400
1388# utility function to dup a filehandle. this is used by many backends 1401# utility function to dup a filehandle. this is used by many backends
1389# to support binding more than one watcher per filehandle (they usually 1402# to support binding more than one watcher per filehandle (they usually
1390# allow only one watcher per fd, so we dup it to get a different one). 1403# allow only one watcher per fd, so we dup it to get a different one).
1414 1427
1415package AE; 1428package AE;
1416 1429
1417our $VERSION = $AnyEvent::VERSION; 1430our $VERSION = $AnyEvent::VERSION;
1418 1431
1432
1433sub _reset() {
1434 eval q{
1419# fall back to the main API by default - backends and AnyEvent::Base 1435 # fall back to the main API by default - backends and AnyEvent::Base
1420# implementations can overwrite these. 1436 # implementations can overwrite these.
1421 1437
1422sub io($$$) { 1438 sub io($$$) {
1423 AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) 1439 AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2])
1424} 1440 }
1425 1441
1426sub timer($$$) { 1442 sub timer($$$) {
1427 AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]) 1443 AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2])
1428} 1444 }
1429 1445
1430sub signal($$) { 1446 sub signal($$) {
1431 AnyEvent->signal (signal => $_[0], cb => $_[1]) 1447 AnyEvent->signal (signal => $_[0], cb => $_[1])
1432} 1448 }
1433 1449
1434sub child($$) { 1450 sub child($$) {
1435 AnyEvent->child (pid => $_[0], cb => $_[1]) 1451 AnyEvent->child (pid => $_[0], cb => $_[1])
1436} 1452 }
1437 1453
1438sub idle($) { 1454 sub idle($) {
1439 AnyEvent->idle (cb => $_[0]) 1455 AnyEvent->idle (cb => $_[0])
1440} 1456 }
1441 1457
1442sub cv(;&) { 1458 sub cv(;&) {
1443 AnyEvent->condvar (@_ ? (cb => $_[0]) : ()) 1459 AnyEvent->condvar (@_ ? (cb => $_[0]) : ())
1444} 1460 }
1445 1461
1446sub now() { 1462 sub now() {
1447 AnyEvent->now 1463 AnyEvent->now
1448} 1464 }
1449 1465
1450sub now_update() { 1466 sub now_update() {
1451 AnyEvent->now_update 1467 AnyEvent->now_update
1452} 1468 }
1453 1469
1454sub time() { 1470 sub time() {
1455 AnyEvent->time 1471 AnyEvent->time
1456} 1472 }
1457 1473
1458*postpone = \&AnyEvent::postpone; 1474 *postpone = \&AnyEvent::postpone;
1475 };
1476 die if $@;
1477}
1478
1479BEGIN { _reset }
1459 1480
1460package AnyEvent::Base; 1481package AnyEvent::Base;
1461 1482
1462# default implementations for many methods 1483# default implementations for many methods
1463 1484
1667 : sysread $SIGPIPE_R, (my $dummy), 9; 1688 : sysread $SIGPIPE_R, (my $dummy), 9;
1668 1689
1669 while (%SIG_EV) { 1690 while (%SIG_EV) {
1670 for (keys %SIG_EV) { 1691 for (keys %SIG_EV) {
1671 delete $SIG_EV{$_}; 1692 delete $SIG_EV{$_};
1672 $_->() for values %{ $SIG_CB{$_} || {} }; 1693 &$_ for values %{ $SIG_CB{$_} || {} };
1673 } 1694 }
1674 } 1695 }
1675 }; 1696 };
1676 }; 1697 };
1677 die if $@; 1698 die if $@;
1744 1765
1745 my ($cb, $w, $rcb) = $arg{cb}; 1766 my ($cb, $w, $rcb) = $arg{cb};
1746 1767
1747 $rcb = sub { 1768 $rcb = sub {
1748 if ($cb) { 1769 if ($cb) {
1749 $w = _time; 1770 $w = AE::time;
1750 &$cb; 1771 &$cb;
1751 $w = _time - $w; 1772 $w = AE::time - $w;
1752 1773
1753 # never use more then 50% of the time for the idle watcher, 1774 # never use more then 50% of the time for the idle watcher,
1754 # within some limits 1775 # within some limits
1755 $w = 0.0001 if $w < 0.0001; 1776 $w = 0.0001 if $w < 0.0001;
1756 $w = 5 if $w > 5; 1777 $w = 5 if $w > 5;
1928can be very useful, however. 1949can be very useful, however.
1929 1950
1930=item C<PERL_ANYEVENT_MODEL> 1951=item C<PERL_ANYEVENT_MODEL>
1931 1952
1932This can be used to specify the event model to be used by AnyEvent, before 1953This can be used to specify the event model to be used by AnyEvent, before
1933auto detection and -probing kicks in. It must be a string consisting 1954auto detection and -probing kicks in.
1934entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended 1955
1956It normally is a string consisting entirely of ASCII letters (e.g. C<EV>
1957or C<IOAsync>). The string C<AnyEvent::Impl::> gets prepended and the
1935and the resulting module name is loaded and if the load was successful, 1958resulting module name is loaded and - if the load was successful - used as
1936used as event model. If it fails to load AnyEvent will proceed with 1959event model backend. If it fails to load then AnyEvent will proceed with
1937auto detection and -probing. 1960auto detection and -probing.
1938 1961
1939This functionality might change in future versions. 1962If the string ends with C<::> instead (e.g. C<AnyEvent::Impl::EV::>) then
1963nothing gets prepended and the module name is used as-is (hint: C<::> at
1964the end of a string designates a module name and quotes it appropriately).
1940 1965
1941For example, to force the pure perl model (L<AnyEvent::Loop::Perl>) you 1966For example, to force the pure perl model (L<AnyEvent::Loop::Perl>) you
1942could start your program like this: 1967could start your program like this:
1943 1968
1944 PERL_ANYEVENT_MODEL=Perl perl ... 1969 PERL_ANYEVENT_MODEL=Perl perl ...

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines