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.242 by root, Fri Jul 17 22:05:12 2009 UTC vs.
Revision 1.251 by root, Mon Jul 20 22:39:57 2009 UTC

1=head1 NAME 1=head1 NAME
2 2
3AnyEvent - provide framework for multiple event loops 3AnyEvent - events independent of event loop implementation
4 4
5EV, Event, Glib, Tk, Perl, Event::Lib, Qt and POE are various supported 5EV, Event, Glib, Tk, Perl, Event::Lib, Qt and POE are various supported
6event loops. 6event loops.
7 7
8=head1 SYNOPSIS 8=head1 SYNOPSIS
40=head1 INTRODUCTION/TUTORIAL 40=head1 INTRODUCTION/TUTORIAL
41 41
42This manpage is mainly a reference manual. If you are interested 42This manpage is mainly a reference manual. If you are interested
43in a tutorial or some gentle introduction, have a look at the 43in a tutorial or some gentle introduction, have a look at the
44L<AnyEvent::Intro> manpage. 44L<AnyEvent::Intro> manpage.
45
46=head1 SUPPORT
47
48There is a mailinglist for discussing all things AnyEvent, and an IRC
49channel, too.
50
51See the AnyEvent project page at the B<Schmorpforge Ta-Sa Software
52Respository>, at L<http://anyevent.schmorp.de>, for more info.
45 53
46=head1 WHY YOU SHOULD USE THIS MODULE (OR NOT) 54=head1 WHY YOU SHOULD USE THIS MODULE (OR NOT)
47 55
48Glib, POE, IO::Async, Event... CPAN offers event models by the dozen 56Glib, POE, IO::Async, Event... CPAN offers event models by the dozen
49nowadays. So what is different about AnyEvent? 57nowadays. So what is different about AnyEvent?
368 376
369This watcher might use C<%SIG> (depending on the event loop used), 377This watcher might use C<%SIG> (depending on the event loop used),
370so programs overwriting those signals directly will likely not work 378so programs overwriting those signals directly will likely not work
371correctly. 379correctly.
372 380
381Example: exit on SIGINT
382
383 my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 });
384
385=head3 Signal Races, Delays and Workarounds
386
373Also note that many event loops (e.g. Glib, Tk, Qt, IO::Async) do not 387Many event loops (e.g. Glib, Tk, Qt, IO::Async) do not support attaching
374support attaching callbacks to signals, which is a pity, as you cannot do 388callbacks to signals in a generic way, which is a pity, as you cannot do
375race-free signal handling in perl. AnyEvent will try to do it's best, but 389race-free signal handling in perl. AnyEvent will try to do it's best, but
376in some cases, signals will be delayed. The maximum time a signal might 390in some cases, signals will be delayed. The maximum time a signal might
377be delayed is specified in C<$AnyEvent::MAX_SIGNAL_LATENCY> (default: 10 391be delayed is specified in C<$AnyEvent::MAX_SIGNAL_LATENCY> (default: 10
378seconds). This variable can be changed only before the first signal 392seconds). This variable can be changed only before the first signal
379watcher is created, and should be left alone otherwise. Higher values 393watcher is created, and should be left alone otherwise. Higher values
380will cause fewer spurious wake-ups, which is better for power and CPU 394will cause fewer spurious wake-ups, which is better for power and CPU
381saving. All these problems can be avoided by installing the optional 395saving. All these problems can be avoided by installing the optional
382L<Async::Interrupt> module. 396L<Async::Interrupt> module. This will not work with inherently broken
383 397event loops such as L<Event> or L<Event::Lib> (and not with L<POE>
384Example: exit on SIGINT 398currently, as POE does it's own workaround with one-second latency). With
385 399those, you just have to suffer the delays.
386 my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 });
387 400
388=head2 CHILD PROCESS WATCHERS 401=head2 CHILD PROCESS WATCHERS
389 402
390You can also watch on a child process exit and catch its exit status. 403You can also watch on a child process exit and catch its exit status.
391 404
504Condition variables are similar to callbacks, except that you can 517Condition variables are similar to callbacks, except that you can
505optionally wait for them. They can also be called merge points - points 518optionally wait for them. They can also be called merge points - points
506in time where multiple outstanding events have been processed. And yet 519in time where multiple outstanding events have been processed. And yet
507another way to call them is transactions - each condition variable can be 520another way to call them is transactions - each condition variable can be
508used to represent a transaction, which finishes at some point and delivers 521used to represent a transaction, which finishes at some point and delivers
509a result. 522a result. And yet some people know them as "futures" - a promise to
523compute/deliver something that you can wait for.
510 524
511Condition variables are very useful to signal that something has finished, 525Condition variables are very useful to signal that something has finished,
512for example, if you write a module that does asynchronous http requests, 526for example, if you write a module that does asynchronous http requests,
513then a condition variable would be the ideal candidate to signal the 527then a condition variable would be the ideal candidate to signal the
514availability of results. The user can either act when the callback is 528availability of results. The user can either act when the callback is
1041 1055
1042=cut 1056=cut
1043 1057
1044package AnyEvent; 1058package AnyEvent;
1045 1059
1060# basically a tuned-down version of common::sense
1061sub common_sense {
1046no warnings; 1062 # no warnings
1063 ${^WARNING_BITS} ^= ${^WARNING_BITS};
1047use strict qw(vars subs); 1064 # use strict vars subs
1065 $^H |= 0x00000600;
1066}
1067
1068BEGIN { AnyEvent::common_sense }
1048 1069
1049use Carp (); 1070use Carp ();
1050 1071
1051our $VERSION = 4.83; 1072our $VERSION = 4.86;
1052our $MODEL; 1073our $MODEL;
1053 1074
1054our $AUTOLOAD; 1075our $AUTOLOAD;
1055our @ISA; 1076our @ISA;
1056 1077
1130 @post_detect = grep $_ != ${$_[0]}, @post_detect; 1151 @post_detect = grep $_ != ${$_[0]}, @post_detect;
1131} 1152}
1132 1153
1133sub detect() { 1154sub detect() {
1134 unless ($MODEL) { 1155 unless ($MODEL) {
1135 no strict 'refs';
1136 local $SIG{__DIE__}; 1156 local $SIG{__DIE__};
1137 1157
1138 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { 1158 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) {
1139 my $model = "AnyEvent::Impl::$1"; 1159 my $model = "AnyEvent::Impl::$1";
1140 if (eval "require $model") { 1160 if (eval "require $model") {
1264 $_->() for values %{ $SIG_CB{$_} || {} }; 1284 $_->() for values %{ $SIG_CB{$_} || {} };
1265 } 1285 }
1266 } 1286 }
1267} 1287}
1268 1288
1289# install a dumym wakeupw atcher to reduce signal catching latency
1290sub _sig_add() {
1291 unless ($SIG_COUNT++) {
1292 # try to align timer on a full-second boundary, if possible
1293 my $NOW = AnyEvent->now;
1294
1295 $SIG_TW = AnyEvent->timer (
1296 after => $MAX_SIGNAL_LATENCY - ($NOW - int $NOW),
1297 interval => $MAX_SIGNAL_LATENCY,
1298 cb => sub { }, # just for the PERL_ASYNC_CHECK
1299 );
1300 }
1301}
1302
1303sub _sig_del {
1304 undef $SIG_TW
1305 unless --$SIG_COUNT;
1306}
1307
1269sub _signal { 1308sub _signal {
1270 my (undef, %arg) = @_; 1309 my (undef, %arg) = @_;
1271 1310
1272 my $signal = uc $arg{signal} 1311 my $signal = uc $arg{signal}
1273 or Carp::croak "required option 'signal' is missing"; 1312 or Carp::croak "required option 'signal' is missing";
1297 undef $SIG_EV{$signal}; 1336 undef $SIG_EV{$signal};
1298 }; 1337 };
1299 1338
1300 # can't do signal processing without introducing races in pure perl, 1339 # can't do signal processing without introducing races in pure perl,
1301 # so limit the signal latency. 1340 # so limit the signal latency.
1302 ++$SIG_COUNT; 1341 _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 } 1342 }
1309 1343
1310 bless [$signal, $arg{cb}], "AnyEvent::Base::signal" 1344 bless [$signal, $arg{cb}], "AnyEvent::Base::signal"
1311} 1345}
1312 1346
1351} 1385}
1352 1386
1353sub AnyEvent::Base::signal::DESTROY { 1387sub AnyEvent::Base::signal::DESTROY {
1354 my ($signal, $cb) = @{$_[0]}; 1388 my ($signal, $cb) = @{$_[0]};
1355 1389
1356 undef $SIG_TW 1390 _sig_del;
1357 unless --$SIG_COUNT;
1358 1391
1359 delete $SIG_CB{$signal}{$cb}; 1392 delete $SIG_CB{$signal}{$cb};
1360 1393
1394 $HAVE_ASYNC_INTERRUPT
1395 ? delete $SIG_ASY{$signal}
1361 # delete doesn't work with older perls - they then 1396 : # delete doesn't work with older perls - they then
1362 # print weird messages, or just unconditionally exit 1397 # print weird messages, or just unconditionally exit
1363 # instead of getting the default action. 1398 # instead of getting the default action.
1364 undef $SIG{$signal} 1399 undef $SIG{$signal}
1365 unless keys %{ $SIG_CB{$signal} }; 1400 unless keys %{ $SIG_CB{$signal} };
1366} 1401}
1367 1402
1368# default implementation for ->child 1403# default implementation for ->child
1369 1404
1386 defined (my $pid = $arg{pid} + 0) 1421 defined (my $pid = $arg{pid} + 0)
1387 or Carp::croak "required option 'pid' is missing"; 1422 or Carp::croak "required option 'pid' is missing";
1388 1423
1389 $PID_CB{$pid}{$arg{cb}} = $arg{cb}; 1424 $PID_CB{$pid}{$arg{cb}} = $arg{cb};
1390 1425
1426 # WNOHANG is almost cetrainly 1 everywhere
1427 $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/
1428 ? 1
1391 $WNOHANG ||= eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; 1429 : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1;
1392 1430
1393 unless ($CHLD_W) { 1431 unless ($CHLD_W) {
1394 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); 1432 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld);
1395 # child could be a zombie already, so make at least one round 1433 # child could be a zombie already, so make at least one round
1396 &_sigchld; 1434 &_sigchld;
1448 1486
1449our @ISA = AnyEvent::CondVar::Base::; 1487our @ISA = AnyEvent::CondVar::Base::;
1450 1488
1451package AnyEvent::CondVar::Base; 1489package AnyEvent::CondVar::Base;
1452 1490
1453use overload 1491#use overload
1454 '&{}' => sub { my $self = shift; sub { $self->send (@_) } }, 1492# '&{}' => sub { my $self = shift; sub { $self->send (@_) } },
1455 fallback => 1; 1493# fallback => 1;
1494
1495# save 300+ kilobytes by dirtily hardcoding overloading
1496${"AnyEvent::CondVar::Base::OVERLOAD"}{dummy}++; # Register with magic by touching.
1497*{'AnyEvent::CondVar::Base::()'} = sub { }; # "Make it findable via fetchmethod."
1498*{'AnyEvent::CondVar::Base::(&{}'} = sub { my $self = shift; sub { $self->send (@_) } }; # &{}
1499${'AnyEvent::CondVar::Base::()'} = 1; # fallback
1456 1500
1457our $WAITING; 1501our $WAITING;
1458 1502
1459sub _send { 1503sub _send {
1460 # nop 1504 # nop
1551C<PERL_ANYEVENT_MODEL>. 1595C<PERL_ANYEVENT_MODEL>.
1552 1596
1553When set to C<2> or higher, cause AnyEvent to report to STDERR which event 1597When set to C<2> or higher, cause AnyEvent to report to STDERR which event
1554model it chooses. 1598model it chooses.
1555 1599
1600When set to C<8> or higher, then AnyEvent will report extra information on
1601which optional modules it loads and how it implements certain features.
1602
1556=item C<PERL_ANYEVENT_STRICT> 1603=item C<PERL_ANYEVENT_STRICT>
1557 1604
1558AnyEvent does not do much argument checking by default, as thorough 1605AnyEvent does not do much argument checking by default, as thorough
1559argument checking is very costly. Setting this variable to a true value 1606argument checking is very costly. Setting this variable to a true value
1560will cause AnyEvent to load C<AnyEvent::Strict> and then to thoroughly 1607will cause AnyEvent to load C<AnyEvent::Strict> and then to thoroughly
1561check the arguments passed to most method calls. If it finds any problems, 1608check the arguments passed to most method calls. If it finds any problems,
1562it will croak. 1609it will croak.
1563 1610
1564In other words, enables "strict" mode. 1611In other words, enables "strict" mode.
1565 1612
1566Unlike C<use strict>, it is definitely recommended to keep it off in 1613Unlike C<use strict> (or it's modern cousin, C<< use L<common::sense>
1567production. Keeping C<PERL_ANYEVENT_STRICT=1> in your environment while 1614>>, it is definitely recommended to keep it off in production. Keeping
1568developing programs can be very useful, however. 1615C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs
1616can be very useful, however.
1569 1617
1570=item C<PERL_ANYEVENT_MODEL> 1618=item C<PERL_ANYEVENT_MODEL>
1571 1619
1572This can be used to specify the event model to be used by AnyEvent, before 1620This can be used to specify the event model to be used by AnyEvent, before
1573auto detection and -probing kicks in. It must be a string consisting 1621auto detection and -probing kicks in. It must be a string consisting
1635 1683
1636When neither C<ca_file> nor C<ca_path> was specified during 1684When neither C<ca_file> nor C<ca_path> was specified during
1637L<AnyEvent::TLS> context creation, and either of these environment 1685L<AnyEvent::TLS> context creation, and either of these environment
1638variables exist, they will be used to specify CA certificate locations 1686variables exist, they will be used to specify CA certificate locations
1639instead of a system-dependent default. 1687instead of a system-dependent default.
1688
1689=item C<PERL_ANYEVENT_AVOID_GUARD> and C<PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT>
1690
1691When these are set to C<1>, then the respective modules are not
1692loaded. Mostly good for testing AnyEvent itself.
1640 1693
1641=back 1694=back
1642 1695
1643=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE 1696=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE
1644 1697
2231 2284
2232This slightly arcane module is used to implement fast signal handling: To 2285This slightly arcane module is used to implement fast signal handling: To
2233my knowledge, there is no way to do completely race-free and quick 2286my knowledge, there is no way to do completely race-free and quick
2234signal handling in pure perl. To ensure that signals still get 2287signal handling in pure perl. To ensure that signals still get
2235delivered, AnyEvent will start an interval timer to wake up perl (and 2288delivered, AnyEvent will start an interval timer to wake up perl (and
2236catch the signals) with soemd elay (default is 10 seconds, look for 2289catch the signals) with some delay (default is 10 seconds, look for
2237C<$AnyEvent::MAX_SIGNAL_LATENCY>). 2290C<$AnyEvent::MAX_SIGNAL_LATENCY>).
2238 2291
2239If this module is available, then it will be used to implement signal 2292If this module is available, then it will be used to implement signal
2240catching, which means that signals will not be delayed, and the event loop 2293catching, which means that signals will not be delayed, and the event loop
2241will not be interrupted regularly, which is more efficient (And good for 2294will not be interrupted regularly, which is more efficient (And good for
2242battery life on laptops). 2295battery life on laptops).
2243 2296
2244This affects not just the pure-perl event loop, but also other event loops 2297This affects not just the pure-perl event loop, but also other event loops
2245that have no signal handling on their own (e.g. Glib, Tk, Qt). 2298that have no signal handling on their own (e.g. Glib, Tk, Qt).
2299
2300Some event loops (POE, Event, Event::Lib) offer signal watchers natively,
2301and either employ their own workarounds (POE) or use AnyEvent's workaround
2302(using C<$AnyEvent::MAX_SIGNAL_LATENCY>). Installing L<Async::Interrupt>
2303does nothing for those backends.
2246 2304
2247=item L<EV> 2305=item L<EV>
2248 2306
2249This module isn't really "optional", as it is simply one of the backend 2307This module isn't really "optional", as it is simply one of the backend
2250event loops that AnyEvent can use. However, it is simply the best event 2308event loops that AnyEvent can use. However, it is simply the best event
2264 2322
2265=item L<JSON> and L<JSON::XS> 2323=item L<JSON> and L<JSON::XS>
2266 2324
2267This module is required when you want to read or write JSON data via 2325This module is required when you want to read or write JSON data via
2268L<AnyEvent::Handle>. It is also written in pure-perl, but can take 2326L<AnyEvent::Handle>. It is also written in pure-perl, but can take
2269advantage of the ulta-high-speed L<JSON::XS> module when it is installed. 2327advantage of the ultra-high-speed L<JSON::XS> module when it is installed.
2270 2328
2271In fact, L<AnyEvent::Handle> will use L<JSON::XS> by default if it is 2329In fact, L<AnyEvent::Handle> will use L<JSON::XS> by default if it is
2272installed. 2330installed.
2273 2331
2274=item L<Net::SSLeay> 2332=item L<Net::SSLeay>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines