1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | AnyEvent - the DBI of event loop programming |
3 | AnyEvent - the DBI of event loop programming |
4 | |
4 | |
5 | EV, Event, Glib, Tk, Perl, Event::Lib, Irssi, rxvt-unicode, IO::Async, Qt |
5 | EV, Event, Glib, Tk, Perl, Event::Lib, Irssi, rxvt-unicode, IO::Async, Qt, |
6 | and POE are various supported event loops/environments. |
6 | FLTK and POE are various supported event loops/environments. |
7 | |
7 | |
8 | =head1 SYNOPSIS |
8 | =head1 SYNOPSIS |
9 | |
9 | |
10 | use AnyEvent; |
10 | use AnyEvent; |
11 | |
11 | |
… | |
… | |
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 | |
887 | Qt requires the Qt::Application to be instantiated first, but will |
887 | Qt requires the Qt::Application to be instantiated first, but will |
888 | otherwise be picked up automatically. As long as the main program |
888 | otherwise be picked up automatically. As long as the main program |
… | |
… | |
933 | |
933 | |
934 | Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model |
934 | Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model |
935 | if necessary. You should only call this function right before you would |
935 | if necessary. You should only call this function right before you would |
936 | have created an AnyEvent watcher anyway, that is, as late as possible at |
936 | have created an AnyEvent watcher anyway, that is, as late as possible at |
937 | runtime, and not e.g. during initialisation of your module. |
937 | runtime, and not e.g. during initialisation of your module. |
|
|
938 | |
|
|
939 | The effect of calling this function is as if a watcher had been created |
|
|
940 | (specifically, actions that happen "when the first watcher is created" |
|
|
941 | happen when calling detetc as well). |
938 | |
942 | |
939 | If you need to do some initialisation before AnyEvent watchers are |
943 | If you need to do some initialisation before AnyEvent watchers are |
940 | created, use C<post_detect>. |
944 | created, use C<post_detect>. |
941 | |
945 | |
942 | =item $guard = AnyEvent::post_detect { BLOCK } |
946 | =item $guard = AnyEvent::post_detect { BLOCK } |
… | |
… | |
1045 | It should use C<postpone>: |
1049 | It should use C<postpone>: |
1046 | |
1050 | |
1047 | AnyEvent::postpone { $cb->(undef) }, return # signal error to callback, later |
1051 | AnyEvent::postpone { $cb->(undef) }, return # signal error to callback, later |
1048 | if $some_error_condition; |
1052 | if $some_error_condition; |
1049 | |
1053 | |
|
|
1054 | =item AnyEvent::log $level, $msg[, @args] |
|
|
1055 | |
|
|
1056 | Log the given C<$msg> at the given C<$level>. |
|
|
1057 | |
|
|
1058 | Loads AnyEvent::Log on first use and calls C<AnyEvent::Log::log> - |
|
|
1059 | consequently, look at the L<AnyEvent::Log> documentation for details. |
|
|
1060 | |
|
|
1061 | If you want to sprinkle loads of logging calls around your code, consider |
|
|
1062 | creating a logger callback with the C<AnyEvent::Log::logger< function. |
|
|
1063 | |
1050 | =back |
1064 | =back |
1051 | |
1065 | |
1052 | =head1 WHAT TO DO IN A MODULE |
1066 | =head1 WHAT TO DO IN A MODULE |
1053 | |
1067 | |
1054 | As a module author, you should C<use AnyEvent> and call AnyEvent methods |
1068 | As a module author, you should C<use AnyEvent> and call AnyEvent methods |
… | |
… | |
1110 | |
1124 | |
1111 | |
1125 | |
1112 | =head1 OTHER MODULES |
1126 | =head1 OTHER MODULES |
1113 | |
1127 | |
1114 | The following is a non-exhaustive list of additional modules that use |
1128 | The following is a non-exhaustive list of additional modules that use |
1115 | AnyEvent as a client and can therefore be mixed easily with other AnyEvent |
1129 | AnyEvent as a client and can therefore be mixed easily with other |
1116 | modules and other event loops in the same program. Some of the modules |
1130 | AnyEvent modules and other event loops in the same program. Some of the |
1117 | come as part of AnyEvent, the others are available via CPAN. |
1131 | modules come as part of AnyEvent, the others are available via CPAN (see |
|
|
1132 | L<http://search.cpan.org/search?m=module&q=anyevent%3A%3A*1> for |
|
|
1133 | a longer non-exhaustive list), and the list is heavily biased towards |
|
|
1134 | modules of the AnyEvent author himself :) |
1118 | |
1135 | |
1119 | =over 4 |
1136 | =over 4 |
1120 | |
1137 | |
1121 | =item L<AnyEvent::Util> |
1138 | =item L<AnyEvent::Util> |
1122 | |
1139 | |
… | |
… | |
1202 | |
1219 | |
1203 | BEGIN { AnyEvent::common_sense } |
1220 | BEGIN { AnyEvent::common_sense } |
1204 | |
1221 | |
1205 | use Carp (); |
1222 | use Carp (); |
1206 | |
1223 | |
1207 | our $VERSION = '5.34'; |
1224 | our $VERSION = '6.01'; |
1208 | our $MODEL; |
1225 | our $MODEL; |
1209 | |
1226 | |
1210 | our $AUTOLOAD; |
|
|
1211 | our @ISA; |
1227 | our @ISA; |
1212 | |
1228 | |
1213 | our @REGISTRY; |
1229 | our @REGISTRY; |
1214 | |
1230 | |
1215 | our $VERBOSE; |
1231 | our $VERBOSE; |
… | |
… | |
1221 | |
1237 | |
1222 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1238 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1223 | if ${^TAINT}; |
1239 | if ${^TAINT}; |
1224 | |
1240 | |
1225 | $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
1241 | $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
1226 | |
|
|
1227 | } |
1242 | } |
1228 | |
1243 | |
1229 | our $MAX_SIGNAL_LATENCY = 10; |
1244 | our $MAX_SIGNAL_LATENCY = 10; |
1230 | |
1245 | |
1231 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
1246 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
… | |
… | |
1235 | $PROTOCOL{$_} = ++$idx |
1250 | $PROTOCOL{$_} = ++$idx |
1236 | for reverse split /\s*,\s*/, |
1251 | for reverse split /\s*,\s*/, |
1237 | $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
1252 | $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
1238 | } |
1253 | } |
1239 | |
1254 | |
|
|
1255 | our @post_detect; |
|
|
1256 | |
|
|
1257 | sub post_detect(&) { |
|
|
1258 | my ($cb) = @_; |
|
|
1259 | |
|
|
1260 | push @post_detect, $cb; |
|
|
1261 | |
|
|
1262 | defined wantarray |
|
|
1263 | ? bless \$cb, "AnyEvent::Util::postdetect" |
|
|
1264 | : () |
|
|
1265 | } |
|
|
1266 | |
|
|
1267 | sub AnyEvent::Util::postdetect::DESTROY { |
|
|
1268 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
|
|
1269 | } |
|
|
1270 | |
|
|
1271 | our $POSTPONE_W; |
|
|
1272 | our @POSTPONE; |
|
|
1273 | |
|
|
1274 | sub _postpone_exec { |
|
|
1275 | undef $POSTPONE_W; |
|
|
1276 | |
|
|
1277 | &{ shift @POSTPONE } |
|
|
1278 | while @POSTPONE; |
|
|
1279 | } |
|
|
1280 | |
|
|
1281 | sub postpone(&) { |
|
|
1282 | push @POSTPONE, shift; |
|
|
1283 | |
|
|
1284 | $POSTPONE_W ||= AE::timer (0, 0, \&_postpone_exec); |
|
|
1285 | |
|
|
1286 | () |
|
|
1287 | } |
|
|
1288 | |
|
|
1289 | sub log($$;@) { |
|
|
1290 | require AnyEvent::Log; |
|
|
1291 | # AnyEvent::Log overwrites this function |
|
|
1292 | goto &log; |
|
|
1293 | } |
|
|
1294 | |
1240 | my @models = ( |
1295 | our @models = ( |
1241 | [EV:: => AnyEvent::Impl::EV:: , 1], |
1296 | [EV:: => AnyEvent::Impl::EV:: , 1], |
1242 | [AnyEvent::Loop:: => AnyEvent::Impl::Perl:: , 1], |
1297 | [AnyEvent::Loop:: => AnyEvent::Impl::Perl:: , 1], |
1243 | # everything below here will not (normally) be autoprobed |
1298 | # everything below here will not (normally) be autoprobed |
1244 | # as the pure perl backend should work everywhere |
1299 | # as the pure perl backend should work everywhere |
1245 | # and is usually faster |
1300 | # and is usually faster |
… | |
… | |
1250 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
1305 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
1251 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
1306 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
1252 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
1307 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
1253 | [Wx:: => AnyEvent::Impl::POE::], |
1308 | [Wx:: => AnyEvent::Impl::POE::], |
1254 | [Prima:: => AnyEvent::Impl::POE::], |
1309 | [Prima:: => AnyEvent::Impl::POE::], |
1255 | [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], |
1310 | [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], # a bitch to autodetect |
1256 | [Cocoa::EventLoop:: => AnyEvent::Impl::Cocoa::], |
1311 | [Cocoa::EventLoop:: => AnyEvent::Impl::Cocoa::], |
1257 | [FLTK:: => AnyEvent::Impl::FLTK::], |
1312 | [FLTK:: => AnyEvent::Impl::FLTK2::], |
1258 | ); |
1313 | ); |
1259 | |
1314 | |
1260 | our %method = map +($_ => 1), |
1315 | our @isa_hook; |
|
|
1316 | |
|
|
1317 | sub _isa_set { |
|
|
1318 | my @pkg = ("AnyEvent", (map $_->[0], grep defined, @isa_hook), $MODEL); |
|
|
1319 | |
|
|
1320 | @{"$pkg[$_-1]::ISA"} = $pkg[$_] |
|
|
1321 | for 1 .. $#pkg; |
|
|
1322 | |
|
|
1323 | grep $_ && $_->[1], @isa_hook |
|
|
1324 | and AE::_reset (); |
|
|
1325 | } |
|
|
1326 | |
|
|
1327 | # used for hooking AnyEvent::Strict and AnyEvent::Debug::Wrap into the class hierarchy |
|
|
1328 | sub _isa_hook($$;$) { |
|
|
1329 | my ($i, $pkg, $reset_ae) = @_; |
|
|
1330 | |
|
|
1331 | $isa_hook[$i] = $pkg ? [$pkg, $reset_ae] : undef; |
|
|
1332 | |
|
|
1333 | _isa_set; |
|
|
1334 | } |
|
|
1335 | |
|
|
1336 | # all autoloaded methods reserve the complete glob, not just the method slot. |
|
|
1337 | # due to bugs in perls method cache implementation. |
1261 | qw(io timer time now now_update signal child idle condvar DESTROY); |
1338 | our @methods = qw(io timer time now now_update signal child idle condvar); |
1262 | |
|
|
1263 | our @post_detect; |
|
|
1264 | |
|
|
1265 | sub post_detect(&) { |
|
|
1266 | my ($cb) = @_; |
|
|
1267 | |
|
|
1268 | push @post_detect, $cb; |
|
|
1269 | |
|
|
1270 | defined wantarray |
|
|
1271 | ? bless \$cb, "AnyEvent::Util::postdetect" |
|
|
1272 | : () |
|
|
1273 | } |
|
|
1274 | |
|
|
1275 | sub AnyEvent::Util::postdetect::DESTROY { |
|
|
1276 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
|
|
1277 | } |
|
|
1278 | |
1339 | |
1279 | sub detect() { |
1340 | sub detect() { |
|
|
1341 | return $MODEL if $MODEL; # some programs keep references to detect |
|
|
1342 | |
|
|
1343 | local $!; # for good measure |
|
|
1344 | local $SIG{__DIE__}; # we use eval |
|
|
1345 | |
1280 | # free some memory |
1346 | # free some memory |
1281 | *detect = sub () { $MODEL }; |
1347 | *detect = sub () { $MODEL }; |
|
|
1348 | # undef &func doesn't correctly update the method cache. grmbl. |
|
|
1349 | # so we delete the whole glob. grmbl. |
|
|
1350 | # otoh, perl doesn't let me undef an active usb, but it lets me free |
|
|
1351 | # a glob with an active sub. hrm. i hope it works, but perl is |
|
|
1352 | # usually buggy in this department. sigh. |
|
|
1353 | delete @{"AnyEvent::"}{@methods}; |
|
|
1354 | undef @methods; |
1282 | |
1355 | |
1283 | local $!; # for good measure |
|
|
1284 | local $SIG{__DIE__}; |
|
|
1285 | |
|
|
1286 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
1356 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z0-9:]+)$/) { |
1287 | my $model = "AnyEvent::Impl::$1"; |
1357 | my $model = $1; |
|
|
1358 | $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//; |
1288 | if (eval "require $model") { |
1359 | if (eval "require $model") { |
1289 | $MODEL = $model; |
1360 | $MODEL = $model; |
1290 | warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2; |
1361 | AnyEvent::log 7 => "loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it." |
|
|
1362 | if $VERBOSE >= 7; |
1291 | } else { |
1363 | } else { |
1292 | warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE; |
1364 | AnyEvent::log warn => "unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@"; |
1293 | } |
1365 | } |
1294 | } |
1366 | } |
1295 | |
1367 | |
1296 | # check for already loaded models |
1368 | # check for already loaded models |
1297 | unless ($MODEL) { |
1369 | unless ($MODEL) { |
1298 | for (@REGISTRY, @models) { |
1370 | for (@REGISTRY, @models) { |
1299 | my ($package, $model) = @$_; |
1371 | my ($package, $model) = @$_; |
1300 | if (${"$package\::VERSION"} > 0) { |
1372 | if (${"$package\::VERSION"} > 0) { |
1301 | if (eval "require $model") { |
1373 | if (eval "require $model") { |
1302 | $MODEL = $model; |
1374 | $MODEL = $model; |
1303 | warn "AnyEvent: autodetected model '$model', using it.\n" if $VERBOSE >= 2; |
1375 | AnyEvent::log 7 => "autodetected model '$model', using it." |
|
|
1376 | if $VERBOSE >= 7; |
1304 | last; |
1377 | last; |
1305 | } |
1378 | } |
1306 | } |
1379 | } |
1307 | } |
1380 | } |
1308 | |
1381 | |
… | |
… | |
1315 | and eval "require $package" |
1388 | and eval "require $package" |
1316 | and ${"$package\::VERSION"} > 0 |
1389 | and ${"$package\::VERSION"} > 0 |
1317 | and eval "require $model" |
1390 | and eval "require $model" |
1318 | ) { |
1391 | ) { |
1319 | $MODEL = $model; |
1392 | $MODEL = $model; |
1320 | warn "AnyEvent: autoloaded model '$model', using it.\n" if $VERBOSE >= 2; |
1393 | AnyEvent::log 7 => "autoloaded model '$model', using it." |
|
|
1394 | if $VERBOSE >= 7; |
1321 | last; |
1395 | last; |
1322 | } |
1396 | } |
1323 | } |
1397 | } |
1324 | |
1398 | |
1325 | $MODEL |
1399 | $MODEL |
1326 | or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?\n"; |
1400 | or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?"; |
1327 | } |
1401 | } |
1328 | } |
1402 | } |
1329 | |
1403 | |
1330 | @models = (); # free probe data |
1404 | # free memory only needed for probing |
|
|
1405 | undef @models; |
|
|
1406 | undef @REGISTRY; |
1331 | |
1407 | |
1332 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
1408 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
1333 | unshift @ISA, $MODEL; |
|
|
1334 | |
1409 | |
1335 | # now nuke some methods that are overridden by the backend. |
1410 | # now nuke some methods that are overridden by the backend. |
1336 | # SUPER is not allowed. |
1411 | # SUPER usage is not allowed in these. |
1337 | for (qw(time signal child idle)) { |
1412 | for (qw(time signal child idle)) { |
1338 | undef &{"AnyEvent::Base::$_"} |
1413 | undef &{"AnyEvent::Base::$_"} |
1339 | if defined &{"$MODEL\::$_"}; |
1414 | if defined &{"$MODEL\::$_"}; |
1340 | } |
1415 | } |
1341 | |
1416 | |
|
|
1417 | _isa_set; |
|
|
1418 | |
|
|
1419 | # we're officially open! |
|
|
1420 | |
1342 | if ($ENV{PERL_ANYEVENT_STRICT}) { |
1421 | if ($ENV{PERL_ANYEVENT_STRICT}) { |
1343 | eval { require AnyEvent::Strict }; |
1422 | require AnyEvent::Strict; |
1344 | warn "AnyEvent: cannot load AnyEvent::Strict: $@" |
|
|
1345 | if $@ && $VERBOSE; |
|
|
1346 | } |
1423 | } |
1347 | |
1424 | |
|
|
1425 | if ($ENV{PERL_ANYEVENT_DEBUG_WRAP}) { |
|
|
1426 | require AnyEvent::Debug; |
|
|
1427 | AnyEvent::Debug::wrap ($ENV{PERL_ANYEVENT_DEBUG_WRAP}); |
|
|
1428 | } |
|
|
1429 | |
|
|
1430 | if (length $ENV{PERL_ANYEVENT_DEBUG_SHELL}) { |
|
|
1431 | require AnyEvent::Socket; |
|
|
1432 | require AnyEvent::Debug; |
|
|
1433 | |
|
|
1434 | my $shell = $ENV{PERL_ANYEVENT_DEBUG_SHELL}; |
|
|
1435 | $shell =~ s/\$\$/$$/g; |
|
|
1436 | |
|
|
1437 | my ($host, $service) = AnyEvent::Socket::parse_hostport ($shell); |
|
|
1438 | $AnyEvent::Debug::SHELL = AnyEvent::Debug::shell ($host, $service); |
|
|
1439 | } |
|
|
1440 | |
|
|
1441 | # now the anyevent environment is set up as the user told us to, so |
|
|
1442 | # call the actual user code - post detects |
|
|
1443 | |
1348 | (shift @post_detect)->() while @post_detect; |
1444 | (shift @post_detect)->() while @post_detect; |
|
|
1445 | undef @post_detect; |
1349 | |
1446 | |
1350 | *post_detect = sub(&) { |
1447 | *post_detect = sub(&) { |
1351 | shift->(); |
1448 | shift->(); |
1352 | |
1449 | |
1353 | undef |
1450 | undef |
1354 | }; |
1451 | }; |
1355 | |
1452 | |
1356 | $MODEL |
1453 | $MODEL |
1357 | } |
1454 | } |
1358 | |
1455 | |
1359 | sub AUTOLOAD { |
1456 | for my $name (@methods) { |
1360 | (my $func = $AUTOLOAD) =~ s/.*://; |
1457 | *$name = sub { |
1361 | |
|
|
1362 | $method{$func} |
|
|
1363 | or Carp::croak "$func: not a valid AnyEvent class method"; |
|
|
1364 | |
|
|
1365 | detect; |
1458 | detect; |
1366 | |
1459 | # we use goto because |
1367 | my $class = shift; |
1460 | # a) it makes the thunk more transparent |
1368 | $class->$func (@_); |
1461 | # b) it allows us to delete the thunk later |
1369 | } |
1462 | goto &{ UNIVERSAL::can AnyEvent => "SUPER::$name" } |
1370 | |
1463 | }; |
1371 | our $POSTPONE_W; |
|
|
1372 | our @POSTPONE; |
|
|
1373 | |
|
|
1374 | sub _postpone_exec { |
|
|
1375 | undef $POSTPONE_W; |
|
|
1376 | (pop @POSTPONE)->() |
|
|
1377 | while @POSTPONE; |
|
|
1378 | } |
|
|
1379 | |
|
|
1380 | sub postpone(&) { |
|
|
1381 | push @POSTPONE, shift; |
|
|
1382 | |
|
|
1383 | $POSTPONE_W ||= AE::timer (0, 0, \&_postpone_exec); |
|
|
1384 | |
|
|
1385 | () |
|
|
1386 | } |
1464 | } |
1387 | |
1465 | |
1388 | # utility function to dup a filehandle. this is used by many backends |
1466 | # utility function to dup a filehandle. this is used by many backends |
1389 | # to support binding more than one watcher per filehandle (they usually |
1467 | # 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). |
1468 | # allow only one watcher per fd, so we dup it to get a different one). |
… | |
… | |
1414 | |
1492 | |
1415 | package AE; |
1493 | package AE; |
1416 | |
1494 | |
1417 | our $VERSION = $AnyEvent::VERSION; |
1495 | our $VERSION = $AnyEvent::VERSION; |
1418 | |
1496 | |
|
|
1497 | sub _reset() { |
|
|
1498 | eval q{ |
1419 | # fall back to the main API by default - backends and AnyEvent::Base |
1499 | # fall back to the main API by default - backends and AnyEvent::Base |
1420 | # implementations can overwrite these. |
1500 | # implementations can overwrite these. |
1421 | |
1501 | |
1422 | sub io($$$) { |
1502 | sub io($$$) { |
1423 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
1503 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
1424 | } |
1504 | } |
1425 | |
1505 | |
1426 | sub timer($$$) { |
1506 | sub timer($$$) { |
1427 | AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]) |
1507 | AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]) |
1428 | } |
1508 | } |
1429 | |
1509 | |
1430 | sub signal($$) { |
1510 | sub signal($$) { |
1431 | AnyEvent->signal (signal => $_[0], cb => $_[1]) |
1511 | AnyEvent->signal (signal => $_[0], cb => $_[1]) |
1432 | } |
1512 | } |
1433 | |
1513 | |
1434 | sub child($$) { |
1514 | sub child($$) { |
1435 | AnyEvent->child (pid => $_[0], cb => $_[1]) |
1515 | AnyEvent->child (pid => $_[0], cb => $_[1]) |
1436 | } |
1516 | } |
1437 | |
1517 | |
1438 | sub idle($) { |
1518 | sub idle($) { |
1439 | AnyEvent->idle (cb => $_[0]) |
1519 | AnyEvent->idle (cb => $_[0]); |
1440 | } |
1520 | } |
1441 | |
1521 | |
1442 | sub cv(;&) { |
1522 | sub cv(;&) { |
1443 | AnyEvent->condvar (@_ ? (cb => $_[0]) : ()) |
1523 | AnyEvent->condvar (@_ ? (cb => $_[0]) : ()) |
1444 | } |
1524 | } |
1445 | |
1525 | |
1446 | sub now() { |
1526 | sub now() { |
1447 | AnyEvent->now |
1527 | AnyEvent->now |
1448 | } |
1528 | } |
1449 | |
1529 | |
1450 | sub now_update() { |
1530 | sub now_update() { |
1451 | AnyEvent->now_update |
1531 | AnyEvent->now_update |
1452 | } |
1532 | } |
1453 | |
1533 | |
1454 | sub time() { |
1534 | sub time() { |
1455 | AnyEvent->time |
1535 | AnyEvent->time |
1456 | } |
1536 | } |
1457 | |
1537 | |
1458 | *postpone = \&AnyEvent::postpone; |
1538 | *postpone = \&AnyEvent::postpone; |
|
|
1539 | *log = \&AnyEvent::log; |
|
|
1540 | }; |
|
|
1541 | die if $@; |
|
|
1542 | } |
|
|
1543 | |
|
|
1544 | BEGIN { _reset } |
1459 | |
1545 | |
1460 | package AnyEvent::Base; |
1546 | package AnyEvent::Base; |
1461 | |
1547 | |
1462 | # default implementations for many methods |
1548 | # default implementations for many methods |
1463 | |
1549 | |
1464 | sub time { |
1550 | sub time { |
1465 | eval q{ # poor man's autoloading {} |
1551 | eval q{ # poor man's autoloading {} |
1466 | # probe for availability of Time::HiRes |
1552 | # probe for availability of Time::HiRes |
1467 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1553 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1468 | warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; |
1554 | AnyEvent::log 8 => "AnyEvent: using Time::HiRes for sub-second timing accuracy." |
|
|
1555 | if $AnyEvent::VERBOSE >= 8; |
|
|
1556 | *time = sub { Time::HiRes::time () }; |
1469 | *AE::time = \&Time::HiRes::time; |
1557 | *AE::time = \& Time::HiRes::time ; |
1470 | # if (eval "use POSIX (); (POSIX::times())... |
1558 | # if (eval "use POSIX (); (POSIX::times())... |
1471 | } else { |
1559 | } else { |
1472 | warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; |
1560 | AnyEvent::log critical => "using built-in time(), WARNING, no sub-second resolution!"; |
|
|
1561 | *time = sub { CORE::time }; |
1473 | *AE::time = sub (){ time }; # epic fail |
1562 | *AE::time = sub (){ CORE::time }; |
1474 | } |
1563 | } |
1475 | |
1564 | |
1476 | *time = sub { AE::time }; # different prototypes |
1565 | *now = \&time; |
1477 | }; |
1566 | }; |
1478 | die if $@; |
1567 | die if $@; |
1479 | |
1568 | |
1480 | &time |
1569 | &time |
1481 | } |
1570 | } |
1482 | |
1571 | |
1483 | *now = \&time; |
1572 | *now = \&time; |
1484 | |
|
|
1485 | sub now_update { } |
1573 | sub now_update { } |
1486 | |
1574 | |
1487 | sub _poll { |
1575 | sub _poll { |
1488 | Carp::croak "$AnyEvent::MODEL does not support blocking waits. Caught"; |
1576 | Carp::croak "$AnyEvent::MODEL does not support blocking waits. Caught"; |
1489 | } |
1577 | } |
… | |
… | |
1575 | |
1663 | |
1576 | sub signal { |
1664 | sub signal { |
1577 | eval q{ # poor man's autoloading {} |
1665 | eval q{ # poor man's autoloading {} |
1578 | # probe for availability of Async::Interrupt |
1666 | # probe for availability of Async::Interrupt |
1579 | if (_have_async_interrupt) { |
1667 | if (_have_async_interrupt) { |
1580 | warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8; |
1668 | AnyEvent::log 8 => "using Async::Interrupt for race-free signal handling." |
|
|
1669 | if $AnyEvent::VERBOSE >= 8; |
1581 | |
1670 | |
1582 | $SIGPIPE_R = new Async::Interrupt::EventPipe; |
1671 | $SIGPIPE_R = new Async::Interrupt::EventPipe; |
1583 | $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; |
1672 | $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; |
1584 | |
1673 | |
1585 | } else { |
1674 | } else { |
1586 | warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; |
1675 | AnyEvent::log 8 => "using emulated perl signal handling with latency timer." |
|
|
1676 | if $AnyEvent::VERBOSE >= 8; |
1587 | |
1677 | |
1588 | if (AnyEvent::WIN32) { |
1678 | if (AnyEvent::WIN32) { |
1589 | require AnyEvent::Util; |
1679 | require AnyEvent::Util; |
1590 | |
1680 | |
1591 | ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); |
1681 | ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); |
… | |
… | |
1667 | : sysread $SIGPIPE_R, (my $dummy), 9; |
1757 | : sysread $SIGPIPE_R, (my $dummy), 9; |
1668 | |
1758 | |
1669 | while (%SIG_EV) { |
1759 | while (%SIG_EV) { |
1670 | for (keys %SIG_EV) { |
1760 | for (keys %SIG_EV) { |
1671 | delete $SIG_EV{$_}; |
1761 | delete $SIG_EV{$_}; |
1672 | $_->() for values %{ $SIG_CB{$_} || {} }; |
1762 | &$_ for values %{ $SIG_CB{$_} || {} }; |
1673 | } |
1763 | } |
1674 | } |
1764 | } |
1675 | }; |
1765 | }; |
1676 | }; |
1766 | }; |
1677 | die if $@; |
1767 | die if $@; |
… | |
… | |
1744 | |
1834 | |
1745 | my ($cb, $w, $rcb) = $arg{cb}; |
1835 | my ($cb, $w, $rcb) = $arg{cb}; |
1746 | |
1836 | |
1747 | $rcb = sub { |
1837 | $rcb = sub { |
1748 | if ($cb) { |
1838 | if ($cb) { |
1749 | $w = _time; |
1839 | $w = AE::time; |
1750 | &$cb; |
1840 | &$cb; |
1751 | $w = _time - $w; |
1841 | $w = AE::time - $w; |
1752 | |
1842 | |
1753 | # never use more then 50% of the time for the idle watcher, |
1843 | # never use more then 50% of the time for the idle watcher, |
1754 | # within some limits |
1844 | # within some limits |
1755 | $w = 0.0001 if $w < 0.0001; |
1845 | $w = 0.0001 if $w < 0.0001; |
1756 | $w = 5 if $w > 5; |
1846 | $w = 5 if $w > 5; |
… | |
… | |
1900 | |
1990 | |
1901 | By default, AnyEvent will be completely silent except in fatal |
1991 | By default, AnyEvent will be completely silent except in fatal |
1902 | conditions. You can set this environment variable to make AnyEvent more |
1992 | conditions. You can set this environment variable to make AnyEvent more |
1903 | talkative. |
1993 | talkative. |
1904 | |
1994 | |
1905 | When set to C<1> or higher, causes AnyEvent to warn about unexpected |
1995 | When set to C<5> or higher, causes AnyEvent to warn about unexpected |
1906 | conditions, such as not being able to load the event model specified by |
1996 | conditions, such as not being able to load the event model specified by |
1907 | C<PERL_ANYEVENT_MODEL>. |
1997 | C<PERL_ANYEVENT_MODEL>. |
1908 | |
1998 | |
1909 | When set to C<2> or higher, cause AnyEvent to report to STDERR which event |
1999 | When set to C<7> or higher, cause AnyEvent to report to STDERR which event |
1910 | model it chooses. |
2000 | model it chooses. |
1911 | |
2001 | |
1912 | When set to C<8> or higher, then AnyEvent will report extra information on |
2002 | When set to C<8> or higher, then AnyEvent will report extra information on |
1913 | which optional modules it loads and how it implements certain features. |
2003 | which optional modules it loads and how it implements certain features. |
1914 | |
2004 | |
… | |
… | |
1925 | Unlike C<use strict> (or its modern cousin, C<< use L<common::sense> |
2015 | Unlike C<use strict> (or its modern cousin, C<< use L<common::sense> |
1926 | >>, it is definitely recommended to keep it off in production. Keeping |
2016 | >>, it is definitely recommended to keep it off in production. Keeping |
1927 | C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs |
2017 | C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs |
1928 | can be very useful, however. |
2018 | can be very useful, however. |
1929 | |
2019 | |
|
|
2020 | =item C<PERL_ANYEVENT_DEBUG_SHELL> |
|
|
2021 | |
|
|
2022 | If this env variable is set, then its contents will be interpreted by |
|
|
2023 | C<AnyEvent::Socket::parse_hostport> (after replacing every occurance of |
|
|
2024 | C<$$> by the process pid) and an C<AnyEvent::Debug::shell> is bound on |
|
|
2025 | that port. The shell object is saved in C<$AnyEvent::Debug::SHELL>. |
|
|
2026 | |
|
|
2027 | This takes place when the first watcher is created. |
|
|
2028 | |
|
|
2029 | For example, to bind a debug shell on a unix domain socket in |
|
|
2030 | F<< /tmp/debug<pid>.sock >>, you could use this: |
|
|
2031 | |
|
|
2032 | PERL_ANYEVENT_DEBUG_SHELL=/tmp/debug\$\$.sock perlprog |
|
|
2033 | |
|
|
2034 | Note that creating sockets in F</tmp> is very unsafe on multiuser |
|
|
2035 | systems. |
|
|
2036 | |
|
|
2037 | =item C<PERL_ANYEVENT_DEBUG_WRAP> |
|
|
2038 | |
|
|
2039 | Can be set to C<0>, C<1> or C<2> and enables wrapping of all watchers for |
|
|
2040 | debugging purposes. See C<AnyEvent::Debug::wrap> for details. |
|
|
2041 | |
1930 | =item C<PERL_ANYEVENT_MODEL> |
2042 | =item C<PERL_ANYEVENT_MODEL> |
1931 | |
2043 | |
1932 | This can be used to specify the event model to be used by AnyEvent, before |
2044 | This can be used to specify the event model to be used by AnyEvent, before |
1933 | auto detection and -probing kicks in. It must be a string consisting |
2045 | auto detection and -probing kicks in. |
1934 | entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended |
2046 | |
|
|
2047 | It normally is a string consisting entirely of ASCII letters (e.g. C<EV> |
|
|
2048 | or C<IOAsync>). The string C<AnyEvent::Impl::> gets prepended and the |
1935 | and the resulting module name is loaded and if the load was successful, |
2049 | resulting module name is loaded and - if the load was successful - used as |
1936 | used as event model. If it fails to load AnyEvent will proceed with |
2050 | event model backend. If it fails to load then AnyEvent will proceed with |
1937 | auto detection and -probing. |
2051 | auto detection and -probing. |
1938 | |
2052 | |
1939 | This functionality might change in future versions. |
2053 | If the string ends with C<::> instead (e.g. C<AnyEvent::Impl::EV::>) then |
|
|
2054 | nothing gets prepended and the module name is used as-is (hint: C<::> at |
|
|
2055 | the end of a string designates a module name and quotes it appropriately). |
1940 | |
2056 | |
1941 | For example, to force the pure perl model (L<AnyEvent::Loop::Perl>) you |
2057 | For example, to force the pure perl model (L<AnyEvent::Loop::Perl>) you |
1942 | could start your program like this: |
2058 | could start your program like this: |
1943 | |
2059 | |
1944 | PERL_ANYEVENT_MODEL=Perl perl ... |
2060 | PERL_ANYEVENT_MODEL=Perl perl ... |
… | |
… | |
2336 | (even when used without AnyEvent), but most event loops have acceptable |
2452 | (even when used without AnyEvent), but most event loops have acceptable |
2337 | performance with or without AnyEvent. |
2453 | performance with or without AnyEvent. |
2338 | |
2454 | |
2339 | =item * The overhead AnyEvent adds is usually much smaller than the overhead of |
2455 | =item * The overhead AnyEvent adds is usually much smaller than the overhead of |
2340 | the actual event loop, only with extremely fast event loops such as EV |
2456 | the actual event loop, only with extremely fast event loops such as EV |
2341 | adds AnyEvent significant overhead. |
2457 | does AnyEvent add significant overhead. |
2342 | |
2458 | |
2343 | =item * You should avoid POE like the plague if you want performance or |
2459 | =item * You should avoid POE like the plague if you want performance or |
2344 | reasonable memory usage. |
2460 | reasonable memory usage. |
2345 | |
2461 | |
2346 | =back |
2462 | =back |
… | |
… | |
2721 | |
2837 | |
2722 | Tutorial/Introduction: L<AnyEvent::Intro>. |
2838 | Tutorial/Introduction: L<AnyEvent::Intro>. |
2723 | |
2839 | |
2724 | FAQ: L<AnyEvent::FAQ>. |
2840 | FAQ: L<AnyEvent::FAQ>. |
2725 | |
2841 | |
2726 | Utility functions: L<AnyEvent::Util>. |
2842 | Utility functions: L<AnyEvent::Util> (misc. grab-bag), L<AnyEvent::Log> |
|
|
2843 | (simply logging). |
2727 | |
2844 | |
|
|
2845 | Development/Debugging: L<AnyEvent::Strict> (stricter checking), |
|
|
2846 | L<AnyEvent::Debug> (interactive shell, watcher tracing). |
|
|
2847 | |
2728 | Event modules: L<AnyEvent::Loop>, L<EV>, L<EV::Glib>, L<Glib::EV>, |
2848 | Supported event modules: L<AnyEvent::Loop>, L<EV>, L<EV::Glib>, |
2729 | L<Event>, L<Glib::Event>, L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. |
2849 | L<Glib::EV>, L<Event>, L<Glib::Event>, L<Glib>, L<Tk>, L<Event::Lib>, |
|
|
2850 | L<Qt>, L<POE>, L<FLTK>. |
2730 | |
2851 | |
2731 | Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, |
2852 | Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, |
2732 | L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, |
2853 | L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, |
2733 | L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, |
2854 | L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, |
2734 | L<AnyEvent::Impl::POE>, L<AnyEvent::Impl::IOAsync>, L<Anyevent::Impl::Irssi>. |
2855 | L<AnyEvent::Impl::POE>, L<AnyEvent::Impl::IOAsync>, L<Anyevent::Impl::Irssi>, |
|
|
2856 | L<AnyEvent::Impl::FLTK>. |
2735 | |
2857 | |
2736 | Non-blocking file handles, sockets, TCP clients and |
2858 | Non-blocking handles, pipes, stream sockets, TCP clients and |
2737 | servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>, L<AnyEvent::TLS>. |
2859 | servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>, L<AnyEvent::TLS>. |
2738 | |
2860 | |
2739 | Asynchronous DNS: L<AnyEvent::DNS>. |
2861 | Asynchronous DNS: L<AnyEvent::DNS>. |
2740 | |
2862 | |
2741 | Thread support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>. |
2863 | Thread support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>. |