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.268 by root, Thu Jul 30 16:39:19 2009 UTC vs.
Revision 1.274 by root, Sun Aug 9 00:20:35 2009 UTC

790=item $cb = $cv->cb ($cb->($cv)) 790=item $cb = $cv->cb ($cb->($cv))
791 791
792This is a mutator function that returns the callback set and optionally 792This is a mutator function that returns the callback set and optionally
793replaces it before doing so. 793replaces it before doing so.
794 794
795The callback will be called when the condition becomes "true", i.e. when 795The callback will be called when the condition becomes (or already was)
796C<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
797variable itself. Calling C<recv> inside the callback or at any later time 797the only argument being the condition variable itself. Calling C<recv>
798is guaranteed not to block. 798inside the callback or at any later time is guaranteed not to block.
799 799
800=back 800=back
801 801
802=head1 SUPPORTED EVENT LOOPS/BACKENDS 802=head1 SUPPORTED EVENT LOOPS/BACKENDS
803 803
1115 1115
1116BEGIN { AnyEvent::common_sense } 1116BEGIN { AnyEvent::common_sense }
1117 1117
1118use Carp (); 1118use Carp ();
1119 1119
1120our $VERSION = 4.9; 1120our $VERSION = 4.92;
1121our $MODEL; 1121our $MODEL;
1122 1122
1123our $AUTOLOAD; 1123our $AUTOLOAD;
1124our @ISA; 1124our @ISA;
1125 1125
1287 # 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
1288 1288
1289 ($fh2, $rw) 1289 ($fh2, $rw)
1290} 1290}
1291 1291
1292#############################################################################
1293# "new" API, currently only emulation of it
1294#############################################################################
1295
1296package AE;
1297
1298sub io($$$) {
1299 AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2])
1300}
1301
1302sub timer($$$) {
1303 AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]);
1304}
1305
1306sub signal($$) {
1307 AnyEvent->signal (signal => $_[0], cb => $_[1]);
1308}
1309
1310sub child($$) {
1311 AnyEvent->child (pid => $_[0], cb => $_[1]);
1312}
1313
1314sub idle($) {
1315 AnyEvent->idle (cb => $_[0]);
1316}
1317
1318sub cv(;&) {
1319 AnyEvent->condvar (@_ ? (cb => $_[0]) : ())
1320}
1321
1322sub now() {
1323 AnyEvent->now
1324}
1325
1326sub now_update() {
1327 AnyEvent->now_update
1328}
1329
1330sub time() {
1331 AnyEvent->time
1332}
1333
1292package AnyEvent::Base; 1334package AnyEvent::Base;
1293 1335
1294# default implementations for many methods 1336# default implementations for many methods
1295 1337
1296sub _time { 1338sub _time {
1348 1390
1349# install a dummy wakeup watcher to reduce signal catching latency 1391# install a dummy wakeup watcher to reduce signal catching latency
1350sub _sig_add() { 1392sub _sig_add() {
1351 unless ($SIG_COUNT++) { 1393 unless ($SIG_COUNT++) {
1352 # try to align timer on a full-second boundary, if possible 1394 # try to align timer on a full-second boundary, if possible
1353 my $NOW = AnyEvent->now; 1395 my $NOW = AE::now;
1354 1396
1355 $SIG_TW = AnyEvent->timer ( 1397 $SIG_TW = AE::timer
1356 after => $MAX_SIGNAL_LATENCY - ($NOW - int $NOW), 1398 $MAX_SIGNAL_LATENCY - ($NOW - int $NOW),
1357 interval => $MAX_SIGNAL_LATENCY, 1399 $MAX_SIGNAL_LATENCY,
1358 cb => sub { }, # just for the PERL_ASYNC_CHECK 1400 sub { } # just for the PERL_ASYNC_CHECK
1359 ); 1401 ;
1360 } 1402 }
1361} 1403}
1362 1404
1363sub _sig_del { 1405sub _sig_del {
1364 undef $SIG_TW 1406 undef $SIG_TW
1401 # probe for availability of Async::Interrupt 1443 # probe for availability of Async::Interrupt
1402 if (_have_async_interrupt) { 1444 if (_have_async_interrupt) {
1403 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;
1404 1446
1405 $SIGPIPE_R = new Async::Interrupt::EventPipe; 1447 $SIGPIPE_R = new Async::Interrupt::EventPipe;
1406 $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R->fileno, poll => "r", cb => \&_signal_exec); 1448 $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec;
1407 1449
1408 } else { 1450 } else {
1409 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;
1410 1452
1411 require Fcntl; 1453 require Fcntl;
1427 } 1469 }
1428 1470
1429 $SIGPIPE_R 1471 $SIGPIPE_R
1430 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";
1431 1473
1432 $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R, poll => "r", cb => \&_signal_exec); 1474 $SIG_IO = AE::io $SIGPIPE_R, 0, \&_signal_exec;
1433 } 1475 }
1434 1476
1435 *signal = sub { 1477 *signal = sub {
1436 my (undef, %arg) = @_; 1478 my (undef, %arg) = @_;
1437 1479
1526 $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/ 1568 $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/
1527 ? 1 1569 ? 1
1528 : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; 1570 : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1;
1529 1571
1530 unless ($CHLD_W) { 1572 unless ($CHLD_W) {
1531 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); 1573 $CHLD_W = AE::signal CHLD => \&_sigchld;
1532 # 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
1533 &_sigchld; 1575 &_sigchld;
1534 } 1576 }
1535 1577
1536 bless [$pid, $arg{cb}], "AnyEvent::Base::child" 1578 bless [$pid, $arg{cb}], "AnyEvent::Base::child"
1562 # 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,
1563 # within some limits 1605 # within some limits
1564 $w = 0.0001 if $w < 0.0001; 1606 $w = 0.0001 if $w < 0.0001;
1565 $w = 5 if $w > 5; 1607 $w = 5 if $w > 5;
1566 1608
1567 $w = AnyEvent->timer (after => $w, cb => $rcb); 1609 $w = AE::timer $w, 0, $rcb;
1568 } else { 1610 } else {
1569 # clean up... 1611 # clean up...
1570 undef $w; 1612 undef $w;
1571 undef $rcb; 1613 undef $rcb;
1572 } 1614 }
1573 }; 1615 };
1574 1616
1575 $w = AnyEvent->timer (after => 0.05, cb => $rcb); 1617 $w = AE::timer 0.05, 0, $rcb;
1576 1618
1577 bless \\$cb, "AnyEvent::Base::idle" 1619 bless \\$cb, "AnyEvent::Base::idle"
1578} 1620}
1579 1621
1580sub AnyEvent::Base::idle::DESTROY { 1622sub AnyEvent::Base::idle::DESTROY {
1634 Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak}; 1676 Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak};
1635 wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0] 1677 wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0]
1636} 1678}
1637 1679
1638sub cb { 1680sub cb {
1639 $_[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
1640 $_[0]{_ae_cb} 1688 $cv->{_ae_cb}
1641} 1689}
1642 1690
1643sub begin { 1691sub begin {
1644 ++$_[0]{_ae_counter}; 1692 ++$_[0]{_ae_counter};
1645 $_[0]{_ae_end_cb} = $_[1] if @_ > 1; 1693 $_[0]{_ae_end_cb} = $_[1] if @_ > 1;
1651} 1699}
1652 1700
1653# undocumented/compatibility with pre-3.4 1701# undocumented/compatibility with pre-3.4
1654*broadcast = \&send; 1702*broadcast = \&send;
1655*wait = \&_wait; 1703*wait = \&_wait;
1656
1657#############################################################################
1658# "new" API, currently only emulation of it
1659#############################################################################
1660
1661package AE;
1662
1663sub io($$$) {
1664 AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2])
1665}
1666
1667sub timer($$$) {
1668 AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]);
1669}
1670
1671sub signal($$) {
1672 AnyEvent->signal (signal => $_[0], cb => $_[1]);
1673}
1674
1675sub child($$) {
1676 AnyEvent->child (pid => $_[0], cb => $_[1]);
1677}
1678
1679sub idle($) {
1680 AnyEvent->idle (cb => $_[0]);
1681}
1682
1683sub cv() {
1684 AnyEvent->condvar
1685}
1686
1687sub now() {
1688 AnyEvent->now
1689}
1690
1691sub now_update() {
1692 AnyEvent->now_update
1693}
1694
1695sub time() {
1696 AnyEvent->time
1697}
1698 1704
1699=head1 ERROR AND EXCEPTION HANDLING 1705=head1 ERROR AND EXCEPTION HANDLING
1700 1706
1701In general, AnyEvent does not do any error handling - it relies on the 1707In general, AnyEvent does not do any error handling - it relies on the
1702caller to do that if required. The L<AnyEvent::Strict> module (see also 1708caller to do that if required. The L<AnyEvent::Strict> module (see also

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines