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.266 by root, Thu Jul 30 03:41:56 2009 UTC vs.
Revision 1.275 by root, Sun Aug 9 00:24:35 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines