… | |
… | |
368 | |
368 | |
369 | This watcher might use C<%SIG> (depending on the event loop used), |
369 | This watcher might use C<%SIG> (depending on the event loop used), |
370 | so programs overwriting those signals directly will likely not work |
370 | so programs overwriting those signals directly will likely not work |
371 | correctly. |
371 | correctly. |
372 | |
372 | |
|
|
373 | Example: exit on SIGINT |
|
|
374 | |
|
|
375 | my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); |
|
|
376 | |
|
|
377 | =head3 Signal Races, Delays and Workarounds |
|
|
378 | |
373 | Also note that many event loops (e.g. Glib, Tk, Qt, IO::Async) do not |
379 | Many event loops (e.g. Glib, Tk, Qt, IO::Async) do not support attaching |
374 | support attaching callbacks to signals, which is a pity, as you cannot do |
380 | callbacks to signals in a generic way, which is a pity, as you cannot do |
375 | race-free signal handling in perl. AnyEvent will try to do it's best, but |
381 | race-free signal handling in perl. AnyEvent will try to do it's best, but |
376 | in some cases, signals will be delayed. The maximum time a signal might |
382 | in some cases, signals will be delayed. The maximum time a signal might |
377 | be delayed is specified in C<$AnyEvent::MAX_SIGNAL_LATENCY> (default: 10 |
383 | be delayed is specified in C<$AnyEvent::MAX_SIGNAL_LATENCY> (default: 10 |
378 | seconds). This variable can be changed only before the first signal |
384 | seconds). This variable can be changed only before the first signal |
379 | watcher is created, and should be left alone otherwise. Higher values |
385 | watcher is created, and should be left alone otherwise. Higher values |
380 | will cause fewer spurious wake-ups, which is better for power and CPU |
386 | will cause fewer spurious wake-ups, which is better for power and CPU |
381 | saving. All these problems can be avoided by installing the optional |
387 | saving. All these problems can be avoided by installing the optional |
382 | L<Async::Interrupt> module. |
388 | L<Async::Interrupt> module. This will not work with inherently broken |
383 | |
389 | event loops such as L<Event> or L<Event::Lib> (and not with L<POE> |
384 | Example: exit on SIGINT |
390 | currently, as POE does it's own workaround with one-second latency). With |
385 | |
391 | those, you just have to suffer the delays. |
386 | my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); |
|
|
387 | |
392 | |
388 | =head2 CHILD PROCESS WATCHERS |
393 | =head2 CHILD PROCESS WATCHERS |
389 | |
394 | |
390 | You can also watch on a child process exit and catch its exit status. |
395 | You can also watch on a child process exit and catch its exit status. |
391 | |
396 | |
… | |
… | |
1041 | |
1046 | |
1042 | =cut |
1047 | =cut |
1043 | |
1048 | |
1044 | package AnyEvent; |
1049 | package AnyEvent; |
1045 | |
1050 | |
|
|
1051 | # basically a tuned-down version of common::sense |
|
|
1052 | sub common_sense { |
1046 | no warnings; |
1053 | # no warnings |
|
|
1054 | ${^WARNING_BITS} ^= ${^WARNING_BITS}; |
1047 | use strict qw(vars subs); |
1055 | # use strict vars subs |
|
|
1056 | $^H |= 0x00000600; |
|
|
1057 | } |
|
|
1058 | |
|
|
1059 | BEGIN { AnyEvent::common_sense } |
1048 | |
1060 | |
1049 | use Carp (); |
1061 | use Carp (); |
1050 | |
1062 | |
1051 | our $VERSION = 4.83; |
1063 | our $VERSION = 4.85; |
1052 | our $MODEL; |
1064 | our $MODEL; |
1053 | |
1065 | |
1054 | our $AUTOLOAD; |
1066 | our $AUTOLOAD; |
1055 | our @ISA; |
1067 | our @ISA; |
1056 | |
1068 | |
… | |
… | |
1130 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
1142 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
1131 | } |
1143 | } |
1132 | |
1144 | |
1133 | sub detect() { |
1145 | sub detect() { |
1134 | unless ($MODEL) { |
1146 | unless ($MODEL) { |
1135 | no strict 'refs'; |
|
|
1136 | local $SIG{__DIE__}; |
1147 | local $SIG{__DIE__}; |
1137 | |
1148 | |
1138 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
1149 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
1139 | my $model = "AnyEvent::Impl::$1"; |
1150 | my $model = "AnyEvent::Impl::$1"; |
1140 | if (eval "require $model") { |
1151 | if (eval "require $model") { |
… | |
… | |
1264 | $_->() for values %{ $SIG_CB{$_} || {} }; |
1275 | $_->() for values %{ $SIG_CB{$_} || {} }; |
1265 | } |
1276 | } |
1266 | } |
1277 | } |
1267 | } |
1278 | } |
1268 | |
1279 | |
|
|
1280 | # install a dumym wakeupw atcher to reduce signal catching latency |
|
|
1281 | sub _sig_add() { |
|
|
1282 | unless ($SIG_COUNT++) { |
|
|
1283 | # try to align timer on a full-second boundary, if possible |
|
|
1284 | my $NOW = AnyEvent->now; |
|
|
1285 | |
|
|
1286 | $SIG_TW = AnyEvent->timer ( |
|
|
1287 | after => $MAX_SIGNAL_LATENCY - ($NOW - int $NOW), |
|
|
1288 | interval => $MAX_SIGNAL_LATENCY, |
|
|
1289 | cb => sub { }, # just for the PERL_ASYNC_CHECK |
|
|
1290 | ); |
|
|
1291 | } |
|
|
1292 | } |
|
|
1293 | |
|
|
1294 | sub _sig_del { |
|
|
1295 | undef $SIG_TW |
|
|
1296 | unless --$SIG_COUNT; |
|
|
1297 | } |
|
|
1298 | |
1269 | sub _signal { |
1299 | sub _signal { |
1270 | my (undef, %arg) = @_; |
1300 | my (undef, %arg) = @_; |
1271 | |
1301 | |
1272 | my $signal = uc $arg{signal} |
1302 | my $signal = uc $arg{signal} |
1273 | or Carp::croak "required option 'signal' is missing"; |
1303 | or Carp::croak "required option 'signal' is missing"; |
… | |
… | |
1297 | undef $SIG_EV{$signal}; |
1327 | undef $SIG_EV{$signal}; |
1298 | }; |
1328 | }; |
1299 | |
1329 | |
1300 | # can't do signal processing without introducing races in pure perl, |
1330 | # can't do signal processing without introducing races in pure perl, |
1301 | # so limit the signal latency. |
1331 | # so limit the signal latency. |
1302 | ++$SIG_COUNT; |
1332 | _sig_add; |
1303 | $SIG_TW ||= AnyEvent->timer ( |
|
|
1304 | after => $MAX_SIGNAL_LATENCY, |
|
|
1305 | interval => $MAX_SIGNAL_LATENCY, |
|
|
1306 | cb => sub { }, # just for the PERL_ASYNC_CHECK |
|
|
1307 | ); |
|
|
1308 | } |
1333 | } |
1309 | |
1334 | |
1310 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
1335 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
1311 | } |
1336 | } |
1312 | |
1337 | |
… | |
… | |
1351 | } |
1376 | } |
1352 | |
1377 | |
1353 | sub AnyEvent::Base::signal::DESTROY { |
1378 | sub AnyEvent::Base::signal::DESTROY { |
1354 | my ($signal, $cb) = @{$_[0]}; |
1379 | my ($signal, $cb) = @{$_[0]}; |
1355 | |
1380 | |
1356 | undef $SIG_TW |
1381 | _sig_del; |
1357 | unless --$SIG_COUNT; |
|
|
1358 | |
1382 | |
1359 | delete $SIG_CB{$signal}{$cb}; |
1383 | delete $SIG_CB{$signal}{$cb}; |
1360 | |
1384 | |
|
|
1385 | $HAVE_ASYNC_INTERRUPT |
|
|
1386 | ? delete $SIG_ASY{$signal} |
1361 | # delete doesn't work with older perls - they then |
1387 | : # delete doesn't work with older perls - they then |
1362 | # print weird messages, or just unconditionally exit |
1388 | # print weird messages, or just unconditionally exit |
1363 | # instead of getting the default action. |
1389 | # instead of getting the default action. |
1364 | undef $SIG{$signal} |
1390 | undef $SIG{$signal} |
1365 | unless keys %{ $SIG_CB{$signal} }; |
1391 | unless keys %{ $SIG_CB{$signal} }; |
1366 | } |
1392 | } |
1367 | |
1393 | |
1368 | # default implementation for ->child |
1394 | # default implementation for ->child |
1369 | |
1395 | |
… | |
… | |
1386 | defined (my $pid = $arg{pid} + 0) |
1412 | defined (my $pid = $arg{pid} + 0) |
1387 | or Carp::croak "required option 'pid' is missing"; |
1413 | or Carp::croak "required option 'pid' is missing"; |
1388 | |
1414 | |
1389 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
1415 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
1390 | |
1416 | |
|
|
1417 | # WNOHANG is almost cetrainly 1 everywhere |
|
|
1418 | $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/ |
|
|
1419 | ? 1 |
1391 | $WNOHANG ||= eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
1420 | : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
1392 | |
1421 | |
1393 | unless ($CHLD_W) { |
1422 | unless ($CHLD_W) { |
1394 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
1423 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
1395 | # child could be a zombie already, so make at least one round |
1424 | # child could be a zombie already, so make at least one round |
1396 | &_sigchld; |
1425 | &_sigchld; |
… | |
… | |
1448 | |
1477 | |
1449 | our @ISA = AnyEvent::CondVar::Base::; |
1478 | our @ISA = AnyEvent::CondVar::Base::; |
1450 | |
1479 | |
1451 | package AnyEvent::CondVar::Base; |
1480 | package AnyEvent::CondVar::Base; |
1452 | |
1481 | |
1453 | use overload |
1482 | #use overload |
1454 | '&{}' => sub { my $self = shift; sub { $self->send (@_) } }, |
1483 | # '&{}' => sub { my $self = shift; sub { $self->send (@_) } }, |
1455 | fallback => 1; |
1484 | # fallback => 1; |
|
|
1485 | |
|
|
1486 | # save 300+ kilobytes by dirtily hardcoding overloading |
|
|
1487 | ${"AnyEvent::CondVar::Base::OVERLOAD"}{dummy}++; # Register with magic by touching. |
|
|
1488 | *{'AnyEvent::CondVar::Base::()'} = sub { }; # "Make it findable via fetchmethod." |
|
|
1489 | *{'AnyEvent::CondVar::Base::(&{}'} = sub { my $self = shift; sub { $self->send (@_) } }; # &{} |
|
|
1490 | ${'AnyEvent::CondVar::Base::()'} = 1; # fallback |
1456 | |
1491 | |
1457 | our $WAITING; |
1492 | our $WAITING; |
1458 | |
1493 | |
1459 | sub _send { |
1494 | sub _send { |
1460 | # nop |
1495 | # nop |
… | |
… | |
1551 | C<PERL_ANYEVENT_MODEL>. |
1586 | C<PERL_ANYEVENT_MODEL>. |
1552 | |
1587 | |
1553 | When set to C<2> or higher, cause AnyEvent to report to STDERR which event |
1588 | When set to C<2> or higher, cause AnyEvent to report to STDERR which event |
1554 | model it chooses. |
1589 | model it chooses. |
1555 | |
1590 | |
|
|
1591 | When set to C<8> or higher, then AnyEvent will report extra information on |
|
|
1592 | which optional modules it loads and how it implements certain features. |
|
|
1593 | |
1556 | =item C<PERL_ANYEVENT_STRICT> |
1594 | =item C<PERL_ANYEVENT_STRICT> |
1557 | |
1595 | |
1558 | AnyEvent does not do much argument checking by default, as thorough |
1596 | AnyEvent does not do much argument checking by default, as thorough |
1559 | argument checking is very costly. Setting this variable to a true value |
1597 | argument checking is very costly. Setting this variable to a true value |
1560 | will cause AnyEvent to load C<AnyEvent::Strict> and then to thoroughly |
1598 | will cause AnyEvent to load C<AnyEvent::Strict> and then to thoroughly |
1561 | check the arguments passed to most method calls. If it finds any problems, |
1599 | check the arguments passed to most method calls. If it finds any problems, |
1562 | it will croak. |
1600 | it will croak. |
1563 | |
1601 | |
1564 | In other words, enables "strict" mode. |
1602 | In other words, enables "strict" mode. |
1565 | |
1603 | |
1566 | Unlike C<use strict>, it is definitely recommended to keep it off in |
1604 | Unlike C<use strict> (or it's modern cousin, C<< use L<common::sense> |
1567 | production. Keeping C<PERL_ANYEVENT_STRICT=1> in your environment while |
1605 | >>, it is definitely recommended to keep it off in production. Keeping |
1568 | developing programs can be very useful, however. |
1606 | C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs |
|
|
1607 | can be very useful, however. |
1569 | |
1608 | |
1570 | =item C<PERL_ANYEVENT_MODEL> |
1609 | =item C<PERL_ANYEVENT_MODEL> |
1571 | |
1610 | |
1572 | This can be used to specify the event model to be used by AnyEvent, before |
1611 | This can be used to specify the event model to be used by AnyEvent, before |
1573 | auto detection and -probing kicks in. It must be a string consisting |
1612 | auto detection and -probing kicks in. It must be a string consisting |
… | |
… | |
1635 | |
1674 | |
1636 | When neither C<ca_file> nor C<ca_path> was specified during |
1675 | When neither C<ca_file> nor C<ca_path> was specified during |
1637 | L<AnyEvent::TLS> context creation, and either of these environment |
1676 | L<AnyEvent::TLS> context creation, and either of these environment |
1638 | variables exist, they will be used to specify CA certificate locations |
1677 | variables exist, they will be used to specify CA certificate locations |
1639 | instead of a system-dependent default. |
1678 | instead of a system-dependent default. |
|
|
1679 | |
|
|
1680 | =item C<PERL_ANYEVENT_AVOID_GUARD> and C<PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT> |
|
|
1681 | |
|
|
1682 | When these are set to C<1>, then the respective modules are not |
|
|
1683 | loaded. Mostly good for testing AnyEvent itself. |
1640 | |
1684 | |
1641 | =back |
1685 | =back |
1642 | |
1686 | |
1643 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
1687 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
1644 | |
1688 | |
… | |
… | |
2231 | |
2275 | |
2232 | This slightly arcane module is used to implement fast signal handling: To |
2276 | This slightly arcane module is used to implement fast signal handling: To |
2233 | my knowledge, there is no way to do completely race-free and quick |
2277 | my knowledge, there is no way to do completely race-free and quick |
2234 | signal handling in pure perl. To ensure that signals still get |
2278 | signal handling in pure perl. To ensure that signals still get |
2235 | delivered, AnyEvent will start an interval timer to wake up perl (and |
2279 | delivered, AnyEvent will start an interval timer to wake up perl (and |
2236 | catch the signals) with soemd elay (default is 10 seconds, look for |
2280 | catch the signals) with some delay (default is 10 seconds, look for |
2237 | C<$AnyEvent::MAX_SIGNAL_LATENCY>). |
2281 | C<$AnyEvent::MAX_SIGNAL_LATENCY>). |
2238 | |
2282 | |
2239 | If this module is available, then it will be used to implement signal |
2283 | If this module is available, then it will be used to implement signal |
2240 | catching, which means that signals will not be delayed, and the event loop |
2284 | catching, which means that signals will not be delayed, and the event loop |
2241 | will not be interrupted regularly, which is more efficient (And good for |
2285 | will not be interrupted regularly, which is more efficient (And good for |
2242 | battery life on laptops). |
2286 | battery life on laptops). |
2243 | |
2287 | |
2244 | This affects not just the pure-perl event loop, but also other event loops |
2288 | This affects not just the pure-perl event loop, but also other event loops |
2245 | that have no signal handling on their own (e.g. Glib, Tk, Qt). |
2289 | that have no signal handling on their own (e.g. Glib, Tk, Qt). |
|
|
2290 | |
|
|
2291 | Some event loops (POE, Event, Event::Lib) offer signal watchers natively, |
|
|
2292 | and either employ their own workarounds (POE) or use AnyEvent's workaround |
|
|
2293 | (using C<$AnyEvent::MAX_SIGNAL_LATENCY>). Installing L<Async::Interrupt> |
|
|
2294 | does nothing for those backends. |
2246 | |
2295 | |
2247 | =item L<EV> |
2296 | =item L<EV> |
2248 | |
2297 | |
2249 | This module isn't really "optional", as it is simply one of the backend |
2298 | This module isn't really "optional", as it is simply one of the backend |
2250 | event loops that AnyEvent can use. However, it is simply the best event |
2299 | event loops that AnyEvent can use. However, it is simply the best event |
… | |
… | |
2264 | |
2313 | |
2265 | =item L<JSON> and L<JSON::XS> |
2314 | =item L<JSON> and L<JSON::XS> |
2266 | |
2315 | |
2267 | This module is required when you want to read or write JSON data via |
2316 | This module is required when you want to read or write JSON data via |
2268 | L<AnyEvent::Handle>. It is also written in pure-perl, but can take |
2317 | L<AnyEvent::Handle>. It is also written in pure-perl, but can take |
2269 | advantage of the ulta-high-speed L<JSON::XS> module when it is installed. |
2318 | advantage of the ultra-high-speed L<JSON::XS> module when it is installed. |
2270 | |
2319 | |
2271 | In fact, L<AnyEvent::Handle> will use L<JSON::XS> by default if it is |
2320 | In fact, L<AnyEvent::Handle> will use L<JSON::XS> by default if it is |
2272 | installed. |
2321 | installed. |
2273 | |
2322 | |
2274 | =item L<Net::SSLeay> |
2323 | =item L<Net::SSLeay> |