… | |
… | |
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 } |
… | |
… | |
1005 | # AnyEvent not yet initialised, so make sure to load Coro::AnyEvent |
1009 | # AnyEvent not yet initialised, so make sure to load Coro::AnyEvent |
1006 | # as soon as it is |
1010 | # as soon as it is |
1007 | push @AnyEvent::post_detect, sub { require Coro::AnyEvent }; |
1011 | push @AnyEvent::post_detect, sub { require Coro::AnyEvent }; |
1008 | } |
1012 | } |
1009 | |
1013 | |
|
|
1014 | =item AnyEvent::postpone { BLOCK } |
|
|
1015 | |
|
|
1016 | Arranges for the block to be executed as soon as possible, but not before |
|
|
1017 | the call itself returns. In practise, the block will be executed just |
|
|
1018 | before the event loop polls for new events, or shortly afterwards. |
|
|
1019 | |
|
|
1020 | This function never returns anything (to make the C<return postpone { ... |
|
|
1021 | }> idiom more useful. |
|
|
1022 | |
|
|
1023 | To understand the usefulness of this function, consider a function that |
|
|
1024 | asynchronously does something for you and returns some transaction |
|
|
1025 | object or guard to let you cancel the operation. For example, |
|
|
1026 | C<AnyEvent::Socket::tcp_connect>: |
|
|
1027 | |
|
|
1028 | # start a conenction attempt unless one is active |
|
|
1029 | $self->{connect_guard} ||= AnyEvent::Socket::tcp_connect "www.example.net", 80, sub { |
|
|
1030 | delete $self->{connect_guard}; |
|
|
1031 | ... |
|
|
1032 | }; |
|
|
1033 | |
|
|
1034 | Imagine that this function could instantly call the callback, for |
|
|
1035 | example, because it detects an obvious error such as a negative port |
|
|
1036 | number. Invoking the callback before the function returns causes problems |
|
|
1037 | however: the callback will be called and will try to delete the guard |
|
|
1038 | object. But since the function hasn't returned yet, there is nothing to |
|
|
1039 | delete. When the function eventually returns it will assign the guard |
|
|
1040 | object to C<< $self->{connect_guard} >>, where it will likely never be |
|
|
1041 | deleted, so the program thinks it is still trying to connect. |
|
|
1042 | |
|
|
1043 | This is where C<AnyEvent::postpone> should be used. Instead of calling the |
|
|
1044 | callback directly on error: |
|
|
1045 | |
|
|
1046 | $cb->(undef), return # signal error to callback, BAD! |
|
|
1047 | if $some_error_condition; |
|
|
1048 | |
|
|
1049 | It should use C<postpone>: |
|
|
1050 | |
|
|
1051 | AnyEvent::postpone { $cb->(undef) }, return # signal error to callback, later |
|
|
1052 | if $some_error_condition; |
|
|
1053 | |
1010 | =back |
1054 | =back |
1011 | |
1055 | |
1012 | =head1 WHAT TO DO IN A MODULE |
1056 | =head1 WHAT TO DO IN A MODULE |
1013 | |
1057 | |
1014 | As a module author, you should C<use AnyEvent> and call AnyEvent methods |
1058 | As a module author, you should C<use AnyEvent> and call AnyEvent methods |
… | |
… | |
1162 | |
1206 | |
1163 | BEGIN { AnyEvent::common_sense } |
1207 | BEGIN { AnyEvent::common_sense } |
1164 | |
1208 | |
1165 | use Carp (); |
1209 | use Carp (); |
1166 | |
1210 | |
1167 | our $VERSION = '5.34'; |
1211 | our $VERSION = '6.01'; |
1168 | our $MODEL; |
1212 | our $MODEL; |
1169 | |
1213 | |
1170 | our $AUTOLOAD; |
|
|
1171 | our @ISA; |
1214 | our @ISA; |
1172 | |
1215 | |
1173 | our @REGISTRY; |
1216 | our @REGISTRY; |
1174 | |
1217 | |
1175 | our $VERBOSE; |
1218 | our $VERBOSE; |
… | |
… | |
1195 | $PROTOCOL{$_} = ++$idx |
1238 | $PROTOCOL{$_} = ++$idx |
1196 | for reverse split /\s*,\s*/, |
1239 | for reverse split /\s*,\s*/, |
1197 | $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
1240 | $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
1198 | } |
1241 | } |
1199 | |
1242 | |
|
|
1243 | our @post_detect; |
|
|
1244 | |
|
|
1245 | sub post_detect(&) { |
|
|
1246 | my ($cb) = @_; |
|
|
1247 | |
|
|
1248 | push @post_detect, $cb; |
|
|
1249 | |
|
|
1250 | defined wantarray |
|
|
1251 | ? bless \$cb, "AnyEvent::Util::postdetect" |
|
|
1252 | : () |
|
|
1253 | } |
|
|
1254 | |
|
|
1255 | sub AnyEvent::Util::postdetect::DESTROY { |
|
|
1256 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
|
|
1257 | } |
|
|
1258 | |
|
|
1259 | our $POSTPONE_W; |
|
|
1260 | our @POSTPONE; |
|
|
1261 | |
|
|
1262 | sub _postpone_exec { |
|
|
1263 | undef $POSTPONE_W; |
|
|
1264 | |
|
|
1265 | &{ shift @POSTPONE } |
|
|
1266 | while @POSTPONE; |
|
|
1267 | } |
|
|
1268 | |
|
|
1269 | sub postpone(&) { |
|
|
1270 | push @POSTPONE, shift; |
|
|
1271 | |
|
|
1272 | $POSTPONE_W ||= AE::timer (0, 0, \&_postpone_exec); |
|
|
1273 | |
|
|
1274 | () |
|
|
1275 | } |
|
|
1276 | |
1200 | my @models = ( |
1277 | our @models = ( |
1201 | [EV:: => AnyEvent::Impl::EV:: , 1], |
1278 | [EV:: => AnyEvent::Impl::EV:: , 1], |
1202 | [AnyEvent::Loop:: => AnyEvent::Impl::Perl:: , 1], |
1279 | [AnyEvent::Loop:: => AnyEvent::Impl::Perl:: , 1], |
1203 | # everything below here will not (normally) be autoprobed |
1280 | # everything below here will not (normally) be autoprobed |
1204 | # as the pure perl backend should work everywhere |
1281 | # as the pure perl backend should work everywhere |
1205 | # and is usually faster |
1282 | # and is usually faster |
… | |
… | |
1210 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
1287 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
1211 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
1288 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
1212 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
1289 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
1213 | [Wx:: => AnyEvent::Impl::POE::], |
1290 | [Wx:: => AnyEvent::Impl::POE::], |
1214 | [Prima:: => AnyEvent::Impl::POE::], |
1291 | [Prima:: => AnyEvent::Impl::POE::], |
1215 | [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], |
1292 | [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], # a bitch to autodetect |
1216 | [Cocoa::EventLoop:: => AnyEvent::Impl::Cocoa::], |
1293 | [Cocoa::EventLoop:: => AnyEvent::Impl::Cocoa::], |
1217 | [FLTK:: => AnyEvent::Impl::FLTK::], |
1294 | [FLTK:: => AnyEvent::Impl::FLTK2::], |
1218 | ); |
1295 | ); |
1219 | |
1296 | |
1220 | our %method = map +($_ => 1), |
1297 | our @isa_hook; |
|
|
1298 | |
|
|
1299 | sub _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 |
|
|
1310 | sub _isa_hook($$;$) { |
|
|
1311 | my ($i, $pkg, $reset_ae) = @_; |
|
|
1312 | |
|
|
1313 | $isa_hook[$i] = $pkg ? [$pkg, $reset_ae] : undef; |
|
|
1314 | |
|
|
1315 | _isa_set; |
|
|
1316 | } |
|
|
1317 | |
|
|
1318 | # all autoloaded methods reserve the complete glob, not just the method slot. |
|
|
1319 | # due to bugs in perls method cache implementation. |
1221 | qw(io timer time now now_update signal child idle condvar DESTROY); |
1320 | our @methods = qw(io timer time now now_update signal child idle condvar); |
1222 | |
|
|
1223 | our @post_detect; |
|
|
1224 | |
|
|
1225 | sub post_detect(&) { |
|
|
1226 | my ($cb) = @_; |
|
|
1227 | |
|
|
1228 | push @post_detect, $cb; |
|
|
1229 | |
|
|
1230 | defined wantarray |
|
|
1231 | ? bless \$cb, "AnyEvent::Util::postdetect" |
|
|
1232 | : () |
|
|
1233 | } |
|
|
1234 | |
|
|
1235 | sub AnyEvent::Util::postdetect::DESTROY { |
|
|
1236 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
|
|
1237 | } |
|
|
1238 | |
1321 | |
1239 | sub detect() { |
1322 | sub detect() { |
|
|
1323 | local $!; # for good measure |
|
|
1324 | local $SIG{__DIE__}; # we use eval |
|
|
1325 | |
1240 | # free some memory |
1326 | # free some memory |
1241 | *detect = sub () { $MODEL }; |
1327 | *detect = sub () { $MODEL }; |
|
|
1328 | # undef &func doesn't correctly update the method cache. grmbl. |
|
|
1329 | # so we delete the whole glob. grmbl. |
|
|
1330 | # otoh, perl doesn't let me undef an active usb, but it lets me free |
|
|
1331 | # a glob with an active sub. hrm. i hope it works, but perl is |
|
|
1332 | # usually buggy in this department. sigh. |
|
|
1333 | delete @{"AnyEvent::"}{@methods}; |
|
|
1334 | undef @methods; |
1242 | |
1335 | |
1243 | local $!; # for good measure |
|
|
1244 | local $SIG{__DIE__}; |
|
|
1245 | |
|
|
1246 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
1336 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z0-9:]+)$/) { |
1247 | my $model = "AnyEvent::Impl::$1"; |
1337 | my $model = $1; |
|
|
1338 | $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//; |
1248 | if (eval "require $model") { |
1339 | if (eval "require $model") { |
1249 | $MODEL = $model; |
1340 | $MODEL = $model; |
1250 | warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2; |
1341 | warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2; |
1251 | } else { |
1342 | } else { |
1252 | warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE; |
1343 | warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE; |
… | |
… | |
1285 | $MODEL |
1376 | $MODEL |
1286 | or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?\n"; |
1377 | or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?\n"; |
1287 | } |
1378 | } |
1288 | } |
1379 | } |
1289 | |
1380 | |
1290 | @models = (); # free probe data |
1381 | # free memory only needed for probing |
|
|
1382 | undef @models; |
|
|
1383 | undef @REGISTRY; |
1291 | |
1384 | |
1292 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
1385 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
1293 | unshift @ISA, $MODEL; |
|
|
1294 | |
1386 | |
1295 | # now nuke some methods that are overridden by the backend. |
1387 | # now nuke some methods that are overridden by the backend. |
1296 | # SUPER is not allowed. |
1388 | # SUPER usage is not allowed in these. |
1297 | for (qw(time signal child idle)) { |
1389 | for (qw(time signal child idle)) { |
1298 | undef &{"AnyEvent::Base::$_"} |
1390 | undef &{"AnyEvent::Base::$_"} |
1299 | if defined &{"$MODEL\::$_"}; |
1391 | if defined &{"$MODEL\::$_"}; |
1300 | } |
1392 | } |
1301 | |
1393 | |
|
|
1394 | _isa_set; |
|
|
1395 | |
1302 | if ($ENV{PERL_ANYEVENT_STRICT}) { |
1396 | if ($ENV{PERL_ANYEVENT_STRICT}) { |
1303 | eval { require AnyEvent::Strict }; |
1397 | require AnyEvent::Strict; |
1304 | warn "AnyEvent: cannot load AnyEvent::Strict: $@" |
|
|
1305 | if $@ && $VERBOSE; |
|
|
1306 | } |
1398 | } |
1307 | |
1399 | |
|
|
1400 | if ($ENV{PERL_ANYEVENT_DEBUG_WRAP}) { |
|
|
1401 | require AnyEvent::Debug; |
|
|
1402 | AnyEvent::Debug::wrap ($ENV{PERL_ANYEVENT_DEBUG_WRAP}); |
|
|
1403 | } |
|
|
1404 | |
|
|
1405 | if (exists $ENV{PERL_ANYEVENT_DEBUG_SHELL}) { |
|
|
1406 | require AnyEvent::Socket; |
|
|
1407 | require AnyEvent::Debug; |
|
|
1408 | |
|
|
1409 | my $shell = $ENV{PERL_ANYEVENT_DEBUG_SHELL}; |
|
|
1410 | $shell =~ s/\$\$/$$/g; |
|
|
1411 | |
|
|
1412 | my ($host, $service) = AnyEvent::Socket::parse_hostport ($shell); |
|
|
1413 | $AnyEvent::Debug::SHELL = AnyEvent::Debug::shell ($host, $service); |
|
|
1414 | } |
|
|
1415 | |
1308 | (shift @post_detect)->() while @post_detect; |
1416 | (shift @post_detect)->() while @post_detect; |
|
|
1417 | undef @post_detect; |
1309 | |
1418 | |
1310 | *post_detect = sub(&) { |
1419 | *post_detect = sub(&) { |
1311 | shift->(); |
1420 | shift->(); |
1312 | |
1421 | |
1313 | undef |
1422 | undef |
1314 | }; |
1423 | }; |
1315 | |
1424 | |
1316 | $MODEL |
1425 | $MODEL |
1317 | } |
1426 | } |
1318 | |
1427 | |
1319 | sub AUTOLOAD { |
1428 | for my $name (@methods) { |
1320 | (my $func = $AUTOLOAD) =~ s/.*://; |
1429 | *$name = sub { |
1321 | |
|
|
1322 | $method{$func} |
|
|
1323 | or Carp::croak "$func: not a valid AnyEvent class method"; |
|
|
1324 | |
|
|
1325 | detect; |
1430 | detect; |
1326 | |
1431 | # we use goto because |
1327 | my $class = shift; |
1432 | # a) it makes the thunk more transparent |
1328 | $class->$func (@_); |
1433 | # b) it allows us to delete the thunk later |
|
|
1434 | goto &{ UNIVERSAL::can AnyEvent => "SUPER::$name" } |
|
|
1435 | }; |
1329 | } |
1436 | } |
1330 | |
1437 | |
1331 | # utility function to dup a filehandle. this is used by many backends |
1438 | # utility function to dup a filehandle. this is used by many backends |
1332 | # to support binding more than one watcher per filehandle (they usually |
1439 | # to support binding more than one watcher per filehandle (they usually |
1333 | # allow only one watcher per fd, so we dup it to get a different one). |
1440 | # allow only one watcher per fd, so we dup it to get a different one). |
… | |
… | |
1357 | |
1464 | |
1358 | package AE; |
1465 | package AE; |
1359 | |
1466 | |
1360 | our $VERSION = $AnyEvent::VERSION; |
1467 | our $VERSION = $AnyEvent::VERSION; |
1361 | |
1468 | |
|
|
1469 | sub _reset() { |
|
|
1470 | eval q{ |
1362 | # fall back to the main API by default - backends and AnyEvent::Base |
1471 | # fall back to the main API by default - backends and AnyEvent::Base |
1363 | # implementations can overwrite these. |
1472 | # implementations can overwrite these. |
1364 | |
1473 | |
1365 | sub io($$$) { |
1474 | sub io($$$) { |
1366 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
1475 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
1367 | } |
1476 | } |
1368 | |
1477 | |
1369 | sub timer($$$) { |
1478 | sub timer($$$) { |
1370 | AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]) |
1479 | AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]) |
1371 | } |
1480 | } |
1372 | |
1481 | |
1373 | sub signal($$) { |
1482 | sub signal($$) { |
1374 | AnyEvent->signal (signal => $_[0], cb => $_[1]) |
1483 | AnyEvent->signal (signal => $_[0], cb => $_[1]) |
1375 | } |
1484 | } |
1376 | |
1485 | |
1377 | sub child($$) { |
1486 | sub child($$) { |
1378 | AnyEvent->child (pid => $_[0], cb => $_[1]) |
1487 | AnyEvent->child (pid => $_[0], cb => $_[1]) |
1379 | } |
1488 | } |
1380 | |
1489 | |
1381 | sub idle($) { |
1490 | sub idle($) { |
1382 | AnyEvent->idle (cb => $_[0]) |
1491 | AnyEvent->idle (cb => $_[0]); |
1383 | } |
1492 | } |
1384 | |
1493 | |
1385 | sub cv(;&) { |
1494 | sub cv(;&) { |
1386 | AnyEvent->condvar (@_ ? (cb => $_[0]) : ()) |
1495 | AnyEvent->condvar (@_ ? (cb => $_[0]) : ()) |
1387 | } |
1496 | } |
1388 | |
1497 | |
1389 | sub now() { |
1498 | sub now() { |
1390 | AnyEvent->now |
1499 | AnyEvent->now |
1391 | } |
1500 | } |
1392 | |
1501 | |
1393 | sub now_update() { |
1502 | sub now_update() { |
1394 | AnyEvent->now_update |
1503 | AnyEvent->now_update |
1395 | } |
1504 | } |
1396 | |
1505 | |
1397 | sub time() { |
1506 | sub time() { |
1398 | AnyEvent->time |
1507 | AnyEvent->time |
|
|
1508 | } |
|
|
1509 | |
|
|
1510 | *postpone = \&AnyEvent::postpone; |
|
|
1511 | }; |
|
|
1512 | die if $@; |
1399 | } |
1513 | } |
|
|
1514 | |
|
|
1515 | BEGIN { _reset } |
1400 | |
1516 | |
1401 | package AnyEvent::Base; |
1517 | package AnyEvent::Base; |
1402 | |
1518 | |
1403 | # default implementations for many methods |
1519 | # default implementations for many methods |
1404 | |
1520 | |
1405 | sub time { |
1521 | sub time { |
1406 | eval q{ # poor man's autoloading {} |
1522 | eval q{ # poor man's autoloading {} |
1407 | # probe for availability of Time::HiRes |
1523 | # probe for availability of Time::HiRes |
1408 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1524 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1409 | warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; |
1525 | warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; |
|
|
1526 | *time = sub { Time::HiRes::time () }; |
1410 | *AE::time = \&Time::HiRes::time; |
1527 | *AE::time = \& Time::HiRes::time ; |
1411 | # if (eval "use POSIX (); (POSIX::times())... |
1528 | # if (eval "use POSIX (); (POSIX::times())... |
1412 | } else { |
1529 | } else { |
1413 | warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; |
1530 | warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; |
|
|
1531 | *time = sub { CORE::time }; |
1414 | *AE::time = sub (){ time }; # epic fail |
1532 | *AE::time = sub (){ CORE::time }; |
1415 | } |
1533 | } |
1416 | |
1534 | |
1417 | *time = sub { AE::time }; # different prototypes |
1535 | *now = \&time; |
1418 | }; |
1536 | }; |
1419 | die if $@; |
1537 | die if $@; |
1420 | |
1538 | |
1421 | &time |
1539 | &time |
1422 | } |
1540 | } |
1423 | |
1541 | |
1424 | *now = \&time; |
1542 | *now = \&time; |
1425 | |
|
|
1426 | sub now_update { } |
1543 | sub now_update { } |
1427 | |
1544 | |
1428 | sub _poll { |
1545 | sub _poll { |
1429 | Carp::croak "$AnyEvent::MODEL does not support blocking waits. Caught"; |
1546 | Carp::croak "$AnyEvent::MODEL does not support blocking waits. Caught"; |
1430 | } |
1547 | } |
1431 | |
1548 | |
1432 | # default implementation for ->condvar |
1549 | # default implementation for ->condvar |
1433 | # in fact,t he default should not be overwritten |
1550 | # in fact, the default should not be overwritten |
1434 | |
1551 | |
1435 | sub condvar { |
1552 | sub condvar { |
1436 | eval q{ # poor man's autoloading {} |
1553 | eval q{ # poor man's autoloading {} |
1437 | *condvar = sub { |
1554 | *condvar = sub { |
1438 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" |
1555 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" |
… | |
… | |
1608 | : sysread $SIGPIPE_R, (my $dummy), 9; |
1725 | : sysread $SIGPIPE_R, (my $dummy), 9; |
1609 | |
1726 | |
1610 | while (%SIG_EV) { |
1727 | while (%SIG_EV) { |
1611 | for (keys %SIG_EV) { |
1728 | for (keys %SIG_EV) { |
1612 | delete $SIG_EV{$_}; |
1729 | delete $SIG_EV{$_}; |
1613 | $_->() for values %{ $SIG_CB{$_} || {} }; |
1730 | &$_ for values %{ $SIG_CB{$_} || {} }; |
1614 | } |
1731 | } |
1615 | } |
1732 | } |
1616 | }; |
1733 | }; |
1617 | }; |
1734 | }; |
1618 | die if $@; |
1735 | die if $@; |
… | |
… | |
1685 | |
1802 | |
1686 | my ($cb, $w, $rcb) = $arg{cb}; |
1803 | my ($cb, $w, $rcb) = $arg{cb}; |
1687 | |
1804 | |
1688 | $rcb = sub { |
1805 | $rcb = sub { |
1689 | if ($cb) { |
1806 | if ($cb) { |
1690 | $w = _time; |
1807 | $w = AE::time; |
1691 | &$cb; |
1808 | &$cb; |
1692 | $w = _time - $w; |
1809 | $w = AE::time - $w; |
1693 | |
1810 | |
1694 | # never use more then 50% of the time for the idle watcher, |
1811 | # never use more then 50% of the time for the idle watcher, |
1695 | # within some limits |
1812 | # within some limits |
1696 | $w = 0.0001 if $w < 0.0001; |
1813 | $w = 0.0001 if $w < 0.0001; |
1697 | $w = 5 if $w > 5; |
1814 | $w = 5 if $w > 5; |
… | |
… | |
1866 | Unlike C<use strict> (or its modern cousin, C<< use L<common::sense> |
1983 | Unlike C<use strict> (or its modern cousin, C<< use L<common::sense> |
1867 | >>, it is definitely recommended to keep it off in production. Keeping |
1984 | >>, it is definitely recommended to keep it off in production. Keeping |
1868 | C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs |
1985 | C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs |
1869 | can be very useful, however. |
1986 | can be very useful, however. |
1870 | |
1987 | |
|
|
1988 | =item C<PERL_ANYEVENT_DEBUG_SHELL> |
|
|
1989 | |
|
|
1990 | If this env variable is set, then its contents will be interpreted by |
|
|
1991 | C<AnyEvent::Socket::parse_hostport> (after replacing every occurance of |
|
|
1992 | C<$$> by the process pid) and an C<AnyEvent::Debug::shell> is bound on |
|
|
1993 | that port. The shell object is saved in C<$AnyEvent::Debug::SHELL>. |
|
|
1994 | |
|
|
1995 | This takes place when the first watcher is created. |
|
|
1996 | |
|
|
1997 | For example, to bind a debug shell on a unix domain socket in |
|
|
1998 | F<< /tmp/debug<pid>.sock >>, you could use this: |
|
|
1999 | |
|
|
2000 | PERL_ANYEVENT_DEBUG_SHELL=unix/:/tmp/debug\$\$.sock perlprog |
|
|
2001 | |
|
|
2002 | Note that creating sockets in F</tmp> is very unsafe on multiuser |
|
|
2003 | systems. |
|
|
2004 | |
|
|
2005 | =item C<PERL_ANYEVENT_DEBUG_WRAP> |
|
|
2006 | |
|
|
2007 | Can be set to C<0>, C<1> or C<2> and enables wrapping of all watchers for |
|
|
2008 | debugging purposes. See C<AnyEvent::Debug::wrap> for details. |
|
|
2009 | |
1871 | =item C<PERL_ANYEVENT_MODEL> |
2010 | =item C<PERL_ANYEVENT_MODEL> |
1872 | |
2011 | |
1873 | This can be used to specify the event model to be used by AnyEvent, before |
2012 | This can be used to specify the event model to be used by AnyEvent, before |
1874 | auto detection and -probing kicks in. It must be a string consisting |
2013 | auto detection and -probing kicks in. |
1875 | entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended |
2014 | |
|
|
2015 | It normally is a string consisting entirely of ASCII letters (e.g. C<EV> |
|
|
2016 | or C<IOAsync>). The string C<AnyEvent::Impl::> gets prepended and the |
1876 | and the resulting module name is loaded and if the load was successful, |
2017 | resulting module name is loaded and - if the load was successful - used as |
1877 | used as event model. If it fails to load AnyEvent will proceed with |
2018 | event model backend. If it fails to load then AnyEvent will proceed with |
1878 | auto detection and -probing. |
2019 | auto detection and -probing. |
1879 | |
2020 | |
1880 | This functionality might change in future versions. |
2021 | If the string ends with C<::> instead (e.g. C<AnyEvent::Impl::EV::>) then |
|
|
2022 | nothing gets prepended and the module name is used as-is (hint: C<::> at |
|
|
2023 | the end of a string designates a module name and quotes it appropriately). |
1881 | |
2024 | |
1882 | For example, to force the pure perl model (L<AnyEvent::Loop::Perl>) you |
2025 | For example, to force the pure perl model (L<AnyEvent::Loop::Perl>) you |
1883 | could start your program like this: |
2026 | could start your program like this: |
1884 | |
2027 | |
1885 | PERL_ANYEVENT_MODEL=Perl perl ... |
2028 | PERL_ANYEVENT_MODEL=Perl perl ... |
… | |
… | |
2277 | (even when used without AnyEvent), but most event loops have acceptable |
2420 | (even when used without AnyEvent), but most event loops have acceptable |
2278 | performance with or without AnyEvent. |
2421 | performance with or without AnyEvent. |
2279 | |
2422 | |
2280 | =item * The overhead AnyEvent adds is usually much smaller than the overhead of |
2423 | =item * The overhead AnyEvent adds is usually much smaller than the overhead of |
2281 | the actual event loop, only with extremely fast event loops such as EV |
2424 | the actual event loop, only with extremely fast event loops such as EV |
2282 | adds AnyEvent significant overhead. |
2425 | does AnyEvent add significant overhead. |
2283 | |
2426 | |
2284 | =item * You should avoid POE like the plague if you want performance or |
2427 | =item * You should avoid POE like the plague if you want performance or |
2285 | reasonable memory usage. |
2428 | reasonable memory usage. |
2286 | |
2429 | |
2287 | =back |
2430 | =back |