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.314 by root, Fri Mar 12 23:30:06 2010 UTC vs.
Revision 1.320 by root, Mon Apr 12 02:50:31 2010 UTC

7 7
8=head1 SYNOPSIS 8=head1 SYNOPSIS
9 9
10 use AnyEvent; 10 use AnyEvent;
11 11
12 # if you prefer function calls, look at the L<AE> manpage for
13 # an alternative API.
14
12 # file descriptor readable 15 # file handle or descriptor readable
13 my $w = AnyEvent->io (fh => $fh, poll => "r", cb => sub { ... }); 16 my $w = AnyEvent->io (fh => $fh, poll => "r", cb => sub { ... });
14 17
15 # one-shot or repeating timers 18 # one-shot or repeating timers
16 my $w = AnyEvent->timer (after => $seconds, cb => sub { ... }); 19 my $w = AnyEvent->timer (after => $seconds, cb => sub { ... });
17 my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ... 20 my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ...
606eventually calls C<< -> send >>, and the "consumer side", which waits 609eventually calls C<< -> send >>, and the "consumer side", which waits
607for the send to occur. 610for the send to occur.
608 611
609Example: wait for a timer. 612Example: wait for a timer.
610 613
611 # wait till the result is ready 614 # condition: "wait till the timer is fired"
612 my $result_ready = AnyEvent->condvar; 615 my $timer_fired = AnyEvent->condvar;
613 616
614 # do something such as adding a timer 617 # create the timer - we could wait for, say
615 # or socket watcher the calls $result_ready->send 618 # a handle becomign ready, or even an
616 # when the "result" is ready. 619 # AnyEvent::HTTP request to finish, but
617 # in this case, we simply use a timer: 620 # in this case, we simply use a timer:
618 my $w = AnyEvent->timer ( 621 my $w = AnyEvent->timer (
619 after => 1, 622 after => 1,
620 cb => sub { $result_ready->send }, 623 cb => sub { $timer_fired->send },
621 ); 624 );
622 625
623 # this "blocks" (while handling events) till the callback 626 # this "blocks" (while handling events) till the callback
624 # calls ->send 627 # calls ->send
625 $result_ready->recv; 628 $timer_fired->recv;
626 629
627Example: wait for a timer, but take advantage of the fact that condition 630Example: wait for a timer, but take advantage of the fact that condition
628variables are also callable directly. 631variables are also callable directly.
629 632
630 my $done = AnyEvent->condvar; 633 my $done = AnyEvent->condvar;
1156 1159
1157BEGIN { AnyEvent::common_sense } 1160BEGIN { AnyEvent::common_sense }
1158 1161
1159use Carp (); 1162use Carp ();
1160 1163
1161our $VERSION = '5.25'; 1164our $VERSION = '5.26';
1162our $MODEL; 1165our $MODEL;
1163 1166
1164our $AUTOLOAD; 1167our $AUTOLOAD;
1165our @ISA; 1168our @ISA;
1166 1169
1169our $VERBOSE; 1172our $VERBOSE;
1170 1173
1171BEGIN { 1174BEGIN {
1172 require "AnyEvent/constants.pl"; 1175 require "AnyEvent/constants.pl";
1173 1176
1174 eval "sub TAINT (){" . (${^TAINT} *1) . "}"; 1177 eval "sub TAINT (){" . (${^TAINT}*1) . "}";
1175 1178
1176 delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} 1179 delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV}
1177 if ${^TAINT}; 1180 if ${^TAINT};
1178 1181
1179 $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; 1182 $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1;
1222our @post_detect; 1225our @post_detect;
1223 1226
1224sub post_detect(&) { 1227sub post_detect(&) {
1225 my ($cb) = @_; 1228 my ($cb) = @_;
1226 1229
1227 if ($MODEL) {
1228 $cb->();
1229
1230 undef
1231 } else {
1232 push @post_detect, $cb; 1230 push @post_detect, $cb;
1233 1231
1234 defined wantarray 1232 defined wantarray
1235 ? bless \$cb, "AnyEvent::Util::postdetect" 1233 ? bless \$cb, "AnyEvent::Util::postdetect"
1236 : () 1234 : ()
1237 }
1238} 1235}
1239 1236
1240sub AnyEvent::Util::postdetect::DESTROY { 1237sub AnyEvent::Util::postdetect::DESTROY {
1241 @post_detect = grep $_ != ${$_[0]}, @post_detect; 1238 @post_detect = grep $_ != ${$_[0]}, @post_detect;
1242} 1239}
1295 @models = (); # free probe data 1292 @models = (); # free probe data
1296 1293
1297 push @{"$MODEL\::ISA"}, "AnyEvent::Base"; 1294 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
1298 unshift @ISA, $MODEL; 1295 unshift @ISA, $MODEL;
1299 1296
1297 # now nuke some methods that are overriden by the backend.
1298 # SUPER is not allowed.
1299 for (qw(time signal child idle)) {
1300 undef &{"AnyEvent::Base::$_"}
1301 if defined &{"$MODEL\::$_"};
1302 }
1303
1300 require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT}; 1304 require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT};
1301 1305
1302 (shift @post_detect)->() while @post_detect; 1306 (shift @post_detect)->() while @post_detect;
1307
1308 *post_detect = sub(&) {
1309 shift->();
1310
1311 undef
1312 };
1303 1313
1304 $MODEL 1314 $MODEL
1305} 1315}
1306 1316
1307sub AUTOLOAD { 1317sub AUTOLOAD {
1335 1345
1336=head1 SIMPLIFIED AE API 1346=head1 SIMPLIFIED AE API
1337 1347
1338Starting with version 5.0, AnyEvent officially supports a second, much 1348Starting with version 5.0, AnyEvent officially supports a second, much
1339simpler, API that is designed to reduce the calling, typing and memory 1349simpler, API that is designed to reduce the calling, typing and memory
1340overhead. 1350overhead by using function call syntax and a fixed number of parameters.
1341 1351
1342See the L<AE> manpage for details. 1352See the L<AE> manpage for details.
1343 1353
1344=cut 1354=cut
1345 1355
1346package AE; 1356package AE;
1347 1357
1348our $VERSION = $AnyEvent::VERSION; 1358our $VERSION = $AnyEvent::VERSION;
1359
1360# fall back to the main API by default - backends and AnyEvent::Base
1361# implementations can overwrite these.
1349 1362
1350sub io($$$) { 1363sub io($$$) {
1351 AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) 1364 AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2])
1352} 1365}
1353 1366
1385 1398
1386package AnyEvent::Base; 1399package AnyEvent::Base;
1387 1400
1388# default implementations for many methods 1401# default implementations for many methods
1389 1402
1390sub _time() { 1403sub time {
1391 eval q{ # poor man's autoloading 1404 eval q{ # poor man's autoloading {}
1392 # probe for availability of Time::HiRes 1405 # probe for availability of Time::HiRes
1393 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { 1406 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") {
1394 warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; 1407 warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8;
1395 *_time = \&Time::HiRes::time; 1408 *AE::time = \&Time::HiRes::time;
1396 # if (eval "use POSIX (); (POSIX::times())... 1409 # if (eval "use POSIX (); (POSIX::times())...
1397 } else { 1410 } else {
1398 warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; 1411 warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE;
1399 *_time = sub (){ time }; # epic fail 1412 *AE::time = sub (){ time }; # epic fail
1400 } 1413 }
1414
1415 *time = sub { AE::time }; # different prototypes
1401 }; 1416 };
1402 die if $@; 1417 die if $@;
1403 1418
1404 &_time 1419 &time
1405} 1420}
1406 1421
1407sub time { _time } 1422*now = \&time;
1408sub now { _time } 1423
1409sub now_update { } 1424sub now_update { }
1410 1425
1411# default implementation for ->condvar 1426# default implementation for ->condvar
1412 1427
1413sub condvar { 1428sub condvar {
1429 eval q{ # poor man's autoloading {}
1430 *condvar = sub {
1414 bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" 1431 bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar"
1432 };
1433
1434 *AE::cv = sub (;&) {
1435 bless { @_ ? (_ae_cb => shift) : () }, "AnyEvent::CondVar"
1436 };
1437 };
1438 die if $@;
1439
1440 &condvar
1415} 1441}
1416 1442
1417# default implementation for ->signal 1443# default implementation for ->signal
1418 1444
1419our $HAVE_ASYNC_INTERRUPT; 1445our $HAVE_ASYNC_INTERRUPT;
1449 undef $SIG_TW 1475 undef $SIG_TW
1450 unless --$SIG_COUNT; 1476 unless --$SIG_COUNT;
1451} 1477}
1452 1478
1453our $_sig_name_init; $_sig_name_init = sub { 1479our $_sig_name_init; $_sig_name_init = sub {
1454 eval q{ # poor man's autoloading 1480 eval q{ # poor man's autoloading {}
1455 undef $_sig_name_init; 1481 undef $_sig_name_init;
1456 1482
1457 if (_have_async_interrupt) { 1483 if (_have_async_interrupt) {
1458 *sig2num = \&Async::Interrupt::sig2num; 1484 *sig2num = \&Async::Interrupt::sig2num;
1459 *sig2name = \&Async::Interrupt::sig2name; 1485 *sig2name = \&Async::Interrupt::sig2name;
1513 or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; 1539 or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n";
1514 1540
1515 $SIG_IO = AE::io $SIGPIPE_R, 0, \&_signal_exec; 1541 $SIG_IO = AE::io $SIGPIPE_R, 0, \&_signal_exec;
1516 } 1542 }
1517 1543
1518 *signal = sub { 1544 *signal = $HAVE_ASYNC_INTERRUPT
1545 ? sub {
1519 my (undef, %arg) = @_; 1546 my (undef, %arg) = @_;
1520 1547
1521 my $signal = uc $arg{signal}
1522 or Carp::croak "required option 'signal' is missing";
1523
1524 if ($HAVE_ASYNC_INTERRUPT) {
1525 # async::interrupt 1548 # async::interrupt
1526
1527 $signal = sig2num $signal; 1549 my $signal = sig2num $arg{signal};
1528 $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; 1550 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
1529 1551
1530 $SIG_ASY{$signal} ||= new Async::Interrupt 1552 $SIG_ASY{$signal} ||= new Async::Interrupt
1531 cb => sub { undef $SIG_EV{$signal} }, 1553 cb => sub { undef $SIG_EV{$signal} },
1532 signal => $signal, 1554 signal => $signal,
1533 pipe => [$SIGPIPE_R->filenos], 1555 pipe => [$SIGPIPE_R->filenos],
1534 pipe_autodrain => 0, 1556 pipe_autodrain => 0,
1535 ; 1557 ;
1536 1558
1537 } else { 1559 bless [$signal, $arg{cb}], "AnyEvent::Base::signal"
1560 }
1561 : sub {
1562 my (undef, %arg) = @_;
1563
1538 # pure perl 1564 # pure perl
1539
1540 # AE::Util has been loaded in signal
1541 $signal = sig2name $signal; 1565 my $signal = sig2name $arg{signal};
1542 $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; 1566 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
1543 1567
1544 $SIG{$signal} ||= sub { 1568 $SIG{$signal} ||= sub {
1545 local $!; 1569 local $!;
1546 syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; 1570 syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV;
1547 undef $SIG_EV{$signal}; 1571 undef $SIG_EV{$signal};
1548 }; 1572 };
1549 1573
1550 # can't do signal processing without introducing races in pure perl, 1574 # can't do signal processing without introducing races in pure perl,
1551 # so limit the signal latency. 1575 # so limit the signal latency.
1552 _sig_add; 1576 _sig_add;
1553 }
1554 1577
1555 bless [$signal, $arg{cb}], "AnyEvent::Base::signal" 1578 bless [$signal, $arg{cb}], "AnyEvent::Base::signal"
1579 }
1556 }; 1580 ;
1557 1581
1558 *AnyEvent::Base::signal::DESTROY = sub { 1582 *AnyEvent::Base::signal::DESTROY = sub {
1559 my ($signal, $cb) = @{$_[0]}; 1583 my ($signal, $cb) = @{$_[0]};
1560 1584
1561 _sig_del; 1585 _sig_del;
2057 2081
2058The actual code goes further and collects all errors (C<die>s, exceptions) 2082The actual code goes further and collects all errors (C<die>s, exceptions)
2059that occurred during request processing. The C<result> method detects 2083that occurred during request processing. The C<result> method detects
2060whether an exception as thrown (it is stored inside the $txn object) 2084whether an exception as thrown (it is stored inside the $txn object)
2061and just throws the exception, which means connection errors and other 2085and just throws the exception, which means connection errors and other
2062problems get reported tot he code that tries to use the result, not in a 2086problems get reported to the code that tries to use the result, not in a
2063random callback. 2087random callback.
2064 2088
2065All of this enables the following usage styles: 2089All of this enables the following usage styles:
2066 2090
20671. Blocking: 20911. Blocking:
2524automatic timer adjustments even when no monotonic clock is available, 2548automatic timer adjustments even when no monotonic clock is available,
2525can take avdantage of advanced kernel interfaces such as C<epoll> and 2549can take avdantage of advanced kernel interfaces such as C<epoll> and
2526C<kqueue>, and is the fastest backend I<by far>. You can even embed 2550C<kqueue>, and is the fastest backend I<by far>. You can even embed
2527L<Glib>/L<Gtk2> in it (or vice versa, see L<EV::Glib> and L<Glib::EV>). 2551L<Glib>/L<Gtk2> in it (or vice versa, see L<EV::Glib> and L<Glib::EV>).
2528 2552
2553If you only use backends that rely on another event loop (e.g. C<Tk>),
2554then this module will do nothing for you.
2555
2529=item L<Guard> 2556=item L<Guard>
2530 2557
2531The guard module, when used, will be used to implement 2558The guard module, when used, will be used to implement
2532C<AnyEvent::Util::guard>. This speeds up guards considerably (and uses a 2559C<AnyEvent::Util::guard>. This speeds up guards considerably (and uses a
2533lot less memory), but otherwise doesn't affect guard operation much. It is 2560lot less memory), but otherwise doesn't affect guard operation much. It is
2534purely used for performance. 2561purely used for performance.
2535 2562
2536=item L<JSON> and L<JSON::XS> 2563=item L<JSON> and L<JSON::XS>
2537 2564
2538One of these modules is required when you want to read or write JSON data 2565One of these modules is required when you want to read or write JSON data
2539via L<AnyEvent::Handle>. It is also written in pure-perl, but can take 2566via L<AnyEvent::Handle>. L<JSON> is also written in pure-perl, but can take
2540advantage of the ultra-high-speed L<JSON::XS> module when it is installed. 2567advantage of the ultra-high-speed L<JSON::XS> module when it is installed.
2541
2542In fact, L<AnyEvent::Handle> will use L<JSON::XS> by default if it is
2543installed.
2544 2568
2545=item L<Net::SSLeay> 2569=item L<Net::SSLeay>
2546 2570
2547Implementing TLS/SSL in Perl is certainly interesting, but not very 2571Implementing TLS/SSL in Perl is certainly interesting, but not very
2548worthwhile: If this module is installed, then L<AnyEvent::Handle> (with 2572worthwhile: If this module is installed, then L<AnyEvent::Handle> (with

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines