… | |
… | |
399 | my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); |
399 | my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); |
400 | |
400 | |
401 | =head3 Signal Races, Delays and Workarounds |
401 | =head3 Signal Races, Delays and Workarounds |
402 | |
402 | |
403 | Many event loops (e.g. Glib, Tk, Qt, IO::Async) do not support attaching |
403 | Many event loops (e.g. Glib, Tk, Qt, IO::Async) do not support attaching |
404 | callbacks to signals in a generic way, which is a pity, as you cannot do |
404 | callbacks to signals in a generic way, which is a pity, as you cannot |
405 | race-free signal handling in perl. AnyEvent will try to do it's best, but |
405 | do race-free signal handling in perl, requiring C libraries for |
|
|
406 | this. AnyEvent will try to do it's best, which means in some cases, |
406 | in some cases, signals will be delayed. The maximum time a signal might |
407 | signals will be delayed. The maximum time a signal might be delayed is |
407 | be delayed is specified in C<$AnyEvent::MAX_SIGNAL_LATENCY> (default: 10 |
408 | specified in C<$AnyEvent::MAX_SIGNAL_LATENCY> (default: 10 seconds). This |
408 | seconds). This variable can be changed only before the first signal |
409 | variable can be changed only before the first signal watcher is created, |
409 | watcher is created, and should be left alone otherwise. Higher values |
410 | and should be left alone otherwise. This variable determines how often |
|
|
411 | AnyEvent polls for signals (in case a wake-up was missed). Higher values |
410 | will cause fewer spurious wake-ups, which is better for power and CPU |
412 | will cause fewer spurious wake-ups, which is better for power and CPU |
|
|
413 | saving. |
|
|
414 | |
411 | saving. All these problems can be avoided by installing the optional |
415 | All these problems can be avoided by installing the optional |
412 | L<Async::Interrupt> module. This will not work with inherently broken |
416 | L<Async::Interrupt> module, which works with most event loops. It will not |
413 | event loops such as L<Event> or L<Event::Lib> (and not with L<POE> |
417 | work with inherently broken event loops such as L<Event> or L<Event::Lib> |
414 | currently, as POE does it's own workaround with one-second latency). With |
418 | (and not with L<POE> currently, as POE does it's own workaround with |
415 | those, you just have to suffer the delays. |
419 | one-second latency). For those, you just have to suffer the delays. |
416 | |
420 | |
417 | =head2 CHILD PROCESS WATCHERS |
421 | =head2 CHILD PROCESS WATCHERS |
418 | |
422 | |
419 | $w = AnyEvent->child (pid => <process id>, cb => <callback>); |
423 | $w = AnyEvent->child (pid => <process id>, cb => <callback>); |
420 | |
424 | |
… | |
… | |
786 | =item $cb = $cv->cb ($cb->($cv)) |
790 | =item $cb = $cv->cb ($cb->($cv)) |
787 | |
791 | |
788 | This is a mutator function that returns the callback set and optionally |
792 | This is a mutator function that returns the callback set and optionally |
789 | replaces it before doing so. |
793 | replaces it before doing so. |
790 | |
794 | |
791 | The callback will be called when the condition becomes "true", i.e. when |
795 | The callback will be called when the condition becomes (or already was) |
792 | C<send> or C<croak> are called, with the only argument being the condition |
796 | "true", i.e. when C<send> or C<croak> are called (or were called), with |
793 | variable itself. Calling C<recv> inside the callback or at any later time |
797 | the only argument being the condition variable itself. Calling C<recv> |
794 | is guaranteed not to block. |
798 | inside the callback or at any later time is guaranteed not to block. |
795 | |
799 | |
796 | =back |
800 | =back |
797 | |
801 | |
798 | =head1 SUPPORTED EVENT LOOPS/BACKENDS |
802 | =head1 SUPPORTED EVENT LOOPS/BACKENDS |
799 | |
803 | |
… | |
… | |
1111 | |
1115 | |
1112 | BEGIN { AnyEvent::common_sense } |
1116 | BEGIN { AnyEvent::common_sense } |
1113 | |
1117 | |
1114 | use Carp (); |
1118 | use Carp (); |
1115 | |
1119 | |
1116 | our $VERSION = 4.881; |
1120 | our $VERSION = 4.91; |
1117 | our $MODEL; |
1121 | our $MODEL; |
1118 | |
1122 | |
1119 | our $AUTOLOAD; |
1123 | our $AUTOLOAD; |
1120 | our @ISA; |
1124 | our @ISA; |
1121 | |
1125 | |
… | |
… | |
1283 | # we assume CLOEXEC is already set by perl in all important cases |
1287 | # we assume CLOEXEC is already set by perl in all important cases |
1284 | |
1288 | |
1285 | ($fh2, $rw) |
1289 | ($fh2, $rw) |
1286 | } |
1290 | } |
1287 | |
1291 | |
|
|
1292 | ############################################################################# |
|
|
1293 | # "new" API, currently only emulation of it |
|
|
1294 | ############################################################################# |
|
|
1295 | |
|
|
1296 | package AE; |
|
|
1297 | |
|
|
1298 | sub io($$$) { |
|
|
1299 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
|
|
1300 | } |
|
|
1301 | |
|
|
1302 | sub timer($$$) { |
|
|
1303 | AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]); |
|
|
1304 | } |
|
|
1305 | |
|
|
1306 | sub signal($$) { |
|
|
1307 | AnyEvent->signal (signal => $_[0], cb => $_[1]); |
|
|
1308 | } |
|
|
1309 | |
|
|
1310 | sub child($$) { |
|
|
1311 | AnyEvent->child (pid => $_[0], cb => $_[1]); |
|
|
1312 | } |
|
|
1313 | |
|
|
1314 | sub idle($) { |
|
|
1315 | AnyEvent->idle (cb => $_[0]); |
|
|
1316 | } |
|
|
1317 | |
|
|
1318 | sub cv(;&) { |
|
|
1319 | AnyEvent->condvar (@_ ? (cb => $_[0]) : ()) |
|
|
1320 | } |
|
|
1321 | |
|
|
1322 | sub now() { |
|
|
1323 | AnyEvent->now |
|
|
1324 | } |
|
|
1325 | |
|
|
1326 | sub now_update() { |
|
|
1327 | AnyEvent->now_update |
|
|
1328 | } |
|
|
1329 | |
|
|
1330 | sub time() { |
|
|
1331 | AnyEvent->time |
|
|
1332 | } |
|
|
1333 | |
1288 | package AnyEvent::Base; |
1334 | package AnyEvent::Base; |
1289 | |
1335 | |
1290 | # default implementations for many methods |
1336 | # default implementations for many methods |
1291 | |
1337 | |
1292 | sub _time { |
1338 | sub _time { |
… | |
… | |
1344 | |
1390 | |
1345 | # install a dummy wakeup watcher to reduce signal catching latency |
1391 | # install a dummy wakeup watcher to reduce signal catching latency |
1346 | sub _sig_add() { |
1392 | sub _sig_add() { |
1347 | unless ($SIG_COUNT++) { |
1393 | unless ($SIG_COUNT++) { |
1348 | # try to align timer on a full-second boundary, if possible |
1394 | # try to align timer on a full-second boundary, if possible |
1349 | my $NOW = AnyEvent->now; |
1395 | my $NOW = AE::now; |
1350 | |
1396 | |
1351 | $SIG_TW = AnyEvent->timer ( |
1397 | $SIG_TW = AE::timer |
1352 | after => $MAX_SIGNAL_LATENCY - ($NOW - int $NOW), |
1398 | $MAX_SIGNAL_LATENCY - ($NOW - int $NOW), |
1353 | interval => $MAX_SIGNAL_LATENCY, |
1399 | $MAX_SIGNAL_LATENCY, |
1354 | cb => sub { }, # just for the PERL_ASYNC_CHECK |
1400 | sub { } # just for the PERL_ASYNC_CHECK |
1355 | ); |
1401 | ; |
1356 | } |
1402 | } |
1357 | } |
1403 | } |
1358 | |
1404 | |
1359 | sub _sig_del { |
1405 | sub _sig_del { |
1360 | undef $SIG_TW |
1406 | undef $SIG_TW |
… | |
… | |
1397 | # probe for availability of Async::Interrupt |
1443 | # probe for availability of Async::Interrupt |
1398 | if (_have_async_interrupt) { |
1444 | if (_have_async_interrupt) { |
1399 | warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8; |
1445 | warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8; |
1400 | |
1446 | |
1401 | $SIGPIPE_R = new Async::Interrupt::EventPipe; |
1447 | $SIGPIPE_R = new Async::Interrupt::EventPipe; |
1402 | $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R->fileno, poll => "r", cb => \&_signal_exec); |
1448 | $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; |
1403 | |
1449 | |
1404 | } else { |
1450 | } else { |
1405 | warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; |
1451 | warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; |
1406 | |
1452 | |
1407 | require Fcntl; |
1453 | require Fcntl; |
… | |
… | |
1423 | } |
1469 | } |
1424 | |
1470 | |
1425 | $SIGPIPE_R |
1471 | $SIGPIPE_R |
1426 | or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; |
1472 | or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; |
1427 | |
1473 | |
1428 | $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R, poll => "r", cb => \&_signal_exec); |
1474 | $SIG_IO = AE::io $SIGPIPE_R, 0, \&_signal_exec; |
1429 | } |
1475 | } |
1430 | |
1476 | |
1431 | *signal = sub { |
1477 | *signal = sub { |
1432 | my (undef, %arg) = @_; |
1478 | my (undef, %arg) = @_; |
1433 | |
1479 | |
… | |
… | |
1522 | $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/ |
1568 | $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/ |
1523 | ? 1 |
1569 | ? 1 |
1524 | : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
1570 | : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
1525 | |
1571 | |
1526 | unless ($CHLD_W) { |
1572 | unless ($CHLD_W) { |
1527 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
1573 | $CHLD_W = AE::signal CHLD => \&_sigchld; |
1528 | # child could be a zombie already, so make at least one round |
1574 | # child could be a zombie already, so make at least one round |
1529 | &_sigchld; |
1575 | &_sigchld; |
1530 | } |
1576 | } |
1531 | |
1577 | |
1532 | bless [$pid, $arg{cb}], "AnyEvent::Base::child" |
1578 | bless [$pid, $arg{cb}], "AnyEvent::Base::child" |
… | |
… | |
1558 | # never use more then 50% of the time for the idle watcher, |
1604 | # never use more then 50% of the time for the idle watcher, |
1559 | # within some limits |
1605 | # within some limits |
1560 | $w = 0.0001 if $w < 0.0001; |
1606 | $w = 0.0001 if $w < 0.0001; |
1561 | $w = 5 if $w > 5; |
1607 | $w = 5 if $w > 5; |
1562 | |
1608 | |
1563 | $w = AnyEvent->timer (after => $w, cb => $rcb); |
1609 | $w = AE::timer $w, 0, $rcb; |
1564 | } else { |
1610 | } else { |
1565 | # clean up... |
1611 | # clean up... |
1566 | undef $w; |
1612 | undef $w; |
1567 | undef $rcb; |
1613 | undef $rcb; |
1568 | } |
1614 | } |
1569 | }; |
1615 | }; |
1570 | |
1616 | |
1571 | $w = AnyEvent->timer (after => 0.05, cb => $rcb); |
1617 | $w = AE::timer 0.05, 0, $rcb; |
1572 | |
1618 | |
1573 | bless \\$cb, "AnyEvent::Base::idle" |
1619 | bless \\$cb, "AnyEvent::Base::idle" |
1574 | } |
1620 | } |
1575 | |
1621 | |
1576 | sub AnyEvent::Base::idle::DESTROY { |
1622 | sub AnyEvent::Base::idle::DESTROY { |
… | |
… | |
1630 | Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak}; |
1676 | Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak}; |
1631 | wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0] |
1677 | wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0] |
1632 | } |
1678 | } |
1633 | |
1679 | |
1634 | sub cb { |
1680 | sub cb { |
1635 | $_[0]{_ae_cb} = $_[1] if @_ > 1; |
1681 | my $cv = shift; |
|
|
1682 | |
|
|
1683 | @_ |
|
|
1684 | and $cv->{_ae_cb} = shift |
|
|
1685 | and $cv->{_ae_sent} |
|
|
1686 | and (delete $cv->{_ae_cb})->($cv); |
|
|
1687 | |
1636 | $_[0]{_ae_cb} |
1688 | $cv->{_ae_cb} |
1637 | } |
1689 | } |
1638 | |
1690 | |
1639 | sub begin { |
1691 | sub begin { |
1640 | ++$_[0]{_ae_counter}; |
1692 | ++$_[0]{_ae_counter}; |
1641 | $_[0]{_ae_end_cb} = $_[1] if @_ > 1; |
1693 | $_[0]{_ae_end_cb} = $_[1] if @_ > 1; |
… | |
… | |
1647 | } |
1699 | } |
1648 | |
1700 | |
1649 | # undocumented/compatibility with pre-3.4 |
1701 | # undocumented/compatibility with pre-3.4 |
1650 | *broadcast = \&send; |
1702 | *broadcast = \&send; |
1651 | *wait = \&_wait; |
1703 | *wait = \&_wait; |
1652 | |
|
|
1653 | ############################################################################# |
|
|
1654 | # "new" API, currently only emulation of it |
|
|
1655 | ############################################################################# |
|
|
1656 | |
|
|
1657 | package AE; |
|
|
1658 | |
|
|
1659 | sub io($$$) { |
|
|
1660 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
|
|
1661 | } |
|
|
1662 | |
|
|
1663 | sub timer($$$) { |
|
|
1664 | AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]); |
|
|
1665 | } |
|
|
1666 | |
|
|
1667 | sub signal($$) { |
|
|
1668 | AnyEvent->signal (signal => $_[0], cb => $_[1]); |
|
|
1669 | } |
|
|
1670 | |
|
|
1671 | sub child($$) { |
|
|
1672 | AnyEvent->child (pid => $_[0], cb => $_[1]); |
|
|
1673 | } |
|
|
1674 | |
|
|
1675 | sub idle($) { |
|
|
1676 | AnyEvent->idle (cb => $_[0]); |
|
|
1677 | } |
|
|
1678 | |
|
|
1679 | sub cv() { |
|
|
1680 | AnyEvent->condvar |
|
|
1681 | } |
|
|
1682 | |
|
|
1683 | sub now() { |
|
|
1684 | AnyEvent->now |
|
|
1685 | } |
|
|
1686 | |
|
|
1687 | sub now_update() { |
|
|
1688 | AnyEvent->now_update |
|
|
1689 | } |
|
|
1690 | |
|
|
1691 | sub time() { |
|
|
1692 | AnyEvent->time |
|
|
1693 | } |
|
|
1694 | |
1704 | |
1695 | =head1 ERROR AND EXCEPTION HANDLING |
1705 | =head1 ERROR AND EXCEPTION HANDLING |
1696 | |
1706 | |
1697 | In general, AnyEvent does not do any error handling - it relies on the |
1707 | In general, AnyEvent does not do any error handling - it relies on the |
1698 | caller to do that if required. The L<AnyEvent::Strict> module (see also |
1708 | caller to do that if required. The L<AnyEvent::Strict> module (see also |