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.296 by root, Tue Nov 17 01:19:49 2009 UTC vs.
Revision 1.314 by root, Fri Mar 12 23:30:06 2010 UTC

402correctly. 402correctly.
403 403
404Example: exit on SIGINT 404Example: exit on SIGINT
405 405
406 my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); 406 my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 });
407
408=head3 Restart Behaviour
409
410While restart behaviour is up to the event loop implementation, most will
411not restart syscalls (that includes L<Async::Interrupt> and AnyEvent's
412pure perl implementation).
413
414=head3 Safe/Unsafe Signals
415
416Perl signals can be either "safe" (synchronous to opcode handling) or
417"unsafe" (asynchronous) - the former might get delayed indefinitely, the
418latter might corrupt your memory.
419
420AnyEvent signal handlers are, in addition, synchronous to the event loop,
421i.e. they will not interrupt your running perl program but will only be
422called as part of the normal event handling (just like timer, I/O etc.
423callbacks, too).
407 424
408=head3 Signal Races, Delays and Workarounds 425=head3 Signal Races, Delays and Workarounds
409 426
410Many event loops (e.g. Glib, Tk, Qt, IO::Async) do not support attaching 427Many event loops (e.g. Glib, Tk, Qt, IO::Async) do not support attaching
411callbacks to signals in a generic way, which is a pity, as you cannot 428callbacks to signals in a generic way, which is a pity, as you cannot
486 503
487=head2 IDLE WATCHERS 504=head2 IDLE WATCHERS
488 505
489 $w = AnyEvent->idle (cb => <callback>); 506 $w = AnyEvent->idle (cb => <callback>);
490 507
491Sometimes there is a need to do something, but it is not so important 508Repeatedly invoke the callback after the process becomes idle, until
492to do it instantly, but only when there is nothing better to do. This 509either the watcher is destroyed or new events have been detected.
493"nothing better to do" is usually defined to be "no other events need
494attention by the event loop".
495 510
496Idle watchers ideally get invoked when the event loop has nothing 511Idle watchers are useful when there is a need to do something, but it
497better to do, just before it would block the process to wait for new 512is not so important (or wise) to do it instantly. The callback will be
498events. Instead of blocking, the idle watcher is invoked. 513invoked only when there is "nothing better to do", which is usually
514defined as "all outstanding events have been handled and no new events
515have been detected". That means that idle watchers ideally get invoked
516when the event loop has just polled for new events but none have been
517detected. Instead of blocking to wait for more events, the idle watchers
518will be invoked.
499 519
500Most event loops unfortunately do not really support idle watchers (only 520Unfortunately, most event loops do not really support idle watchers (only
501EV, Event and Glib do it in a usable fashion) - for the rest, AnyEvent 521EV, Event and Glib do it in a usable fashion) - for the rest, AnyEvent
502will simply call the callback "from time to time". 522will simply call the callback "from time to time".
503 523
504Example: read lines from STDIN, but only process them when the 524Example: read lines from STDIN, but only process them when the
505program is otherwise idle: 525program is otherwise idle:
949You should check C<$AnyEvent::MODEL> before adding to this array, though: 969You should check C<$AnyEvent::MODEL> before adding to this array, though:
950if it is defined then the event loop has already been detected, and the 970if it is defined then the event loop has already been detected, and the
951array will be ignored. 971array will be ignored.
952 972
953Best use C<AnyEvent::post_detect { BLOCK }> when your application allows 973Best use C<AnyEvent::post_detect { BLOCK }> when your application allows
954it,as it takes care of these details. 974it, as it takes care of these details.
955 975
956This variable is mainly useful for modules that can do something useful 976This variable is mainly useful for modules that can do something useful
957when AnyEvent is used and thus want to know when it is initialised, but do 977when AnyEvent is used and thus want to know when it is initialised, but do
958not need to even load it by default. This array provides the means to hook 978not need to even load it by default. This array provides the means to hook
959into AnyEvent passively, without loading it. 979into AnyEvent passively, without loading it.
980
981Example: To load Coro::AnyEvent whenever Coro and AnyEvent are used
982together, you could put this into Coro (this is the actual code used by
983Coro to accomplish this):
984
985 if (defined $AnyEvent::MODEL) {
986 # AnyEvent already initialised, so load Coro::AnyEvent
987 require Coro::AnyEvent;
988 } else {
989 # AnyEvent not yet initialised, so make sure to load Coro::AnyEvent
990 # as soon as it is
991 push @AnyEvent::post_detect, sub { require Coro::AnyEvent };
992 }
960 993
961=back 994=back
962 995
963=head1 WHAT TO DO IN A MODULE 996=head1 WHAT TO DO IN A MODULE
964 997
1114package AnyEvent; 1147package AnyEvent;
1115 1148
1116# basically a tuned-down version of common::sense 1149# basically a tuned-down version of common::sense
1117sub common_sense { 1150sub common_sense {
1118 # from common:.sense 1.0 1151 # from common:.sense 1.0
1119 ${^WARNING_BITS} = "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03"; 1152 ${^WARNING_BITS} = "\xfc\x3f\x33\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x00";
1120 # use strict vars subs 1153 # use strict vars subs - NO UTF-8, as Util.pm doesn't like this atm. (uts46data.pl)
1121 $^H |= 0x00000600; 1154 $^H |= 0x00000600;
1122} 1155}
1123 1156
1124BEGIN { AnyEvent::common_sense } 1157BEGIN { AnyEvent::common_sense }
1125 1158
1126use Carp (); 1159use Carp ();
1127 1160
1128our $VERSION = '5.202'; 1161our $VERSION = '5.25';
1129our $MODEL; 1162our $MODEL;
1130 1163
1131our $AUTOLOAD; 1164our $AUTOLOAD;
1132our @ISA; 1165our @ISA;
1133 1166
1134our @REGISTRY; 1167our @REGISTRY;
1135 1168
1136our $WIN32;
1137
1138our $VERBOSE; 1169our $VERBOSE;
1139 1170
1140BEGIN { 1171BEGIN {
1141 eval "sub WIN32(){ " . (($^O =~ /mswin32/i)*1) ." }"; 1172 require "AnyEvent/constants.pl";
1173
1142 eval "sub TAINT(){ " . (${^TAINT}*1) . " }"; 1174 eval "sub TAINT (){" . (${^TAINT} *1) . "}";
1143 1175
1144 delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} 1176 delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV}
1145 if ${^TAINT}; 1177 if ${^TAINT};
1146 1178
1147 $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; 1179 $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1;
1208sub AnyEvent::Util::postdetect::DESTROY { 1240sub AnyEvent::Util::postdetect::DESTROY {
1209 @post_detect = grep $_ != ${$_[0]}, @post_detect; 1241 @post_detect = grep $_ != ${$_[0]}, @post_detect;
1210} 1242}
1211 1243
1212sub detect() { 1244sub detect() {
1245 # free some memory
1246 *detect = sub () { $MODEL };
1247
1248 local $!; # for good measure
1249 local $SIG{__DIE__};
1250
1251 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) {
1252 my $model = "AnyEvent::Impl::$1";
1253 if (eval "require $model") {
1254 $MODEL = $model;
1255 warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2;
1256 } else {
1257 warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE;
1258 }
1259 }
1260
1261 # check for already loaded models
1213 unless ($MODEL) { 1262 unless ($MODEL) {
1214 local $SIG{__DIE__}; 1263 for (@REGISTRY, @models) {
1215 1264 my ($package, $model) = @$_;
1216 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { 1265 if (${"$package\::VERSION"} > 0) {
1217 my $model = "AnyEvent::Impl::$1";
1218 if (eval "require $model") { 1266 if (eval "require $model") {
1219 $MODEL = $model; 1267 $MODEL = $model;
1220 warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2; 1268 warn "AnyEvent: autodetected model '$model', using it.\n" if $VERBOSE >= 2;
1221 } else { 1269 last;
1222 warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE; 1270 }
1223 } 1271 }
1224 } 1272 }
1225 1273
1226 # check for already loaded models
1227 unless ($MODEL) { 1274 unless ($MODEL) {
1275 # try to autoload a model
1228 for (@REGISTRY, @models) { 1276 for (@REGISTRY, @models) {
1229 my ($package, $model) = @$_; 1277 my ($package, $model, $autoload) = @$_;
1278 if (
1279 $autoload
1280 and eval "require $package"
1230 if (${"$package\::VERSION"} > 0) { 1281 and ${"$package\::VERSION"} > 0
1231 if (eval "require $model") { 1282 and eval "require $model"
1283 ) {
1232 $MODEL = $model; 1284 $MODEL = $model;
1233 warn "AnyEvent: autodetected model '$model', using it.\n" if $VERBOSE >= 2; 1285 warn "AnyEvent: autoloaded model '$model', using it.\n" if $VERBOSE >= 2;
1234 last; 1286 last;
1235 }
1236 } 1287 }
1237 } 1288 }
1238 1289
1239 unless ($MODEL) {
1240 # try to autoload a model
1241 for (@REGISTRY, @models) {
1242 my ($package, $model, $autoload) = @$_;
1243 if (
1244 $autoload
1245 and eval "require $package"
1246 and ${"$package\::VERSION"} > 0
1247 and eval "require $model"
1248 ) {
1249 $MODEL = $model;
1250 warn "AnyEvent: autoloaded model '$model', using it.\n" if $VERBOSE >= 2;
1251 last;
1252 }
1253 }
1254
1255 $MODEL 1290 $MODEL
1256 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib.\n"; 1291 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib.\n";
1257 }
1258 } 1292 }
1259
1260 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
1261
1262 unshift @ISA, $MODEL;
1263
1264 require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT};
1265
1266 (shift @post_detect)->() while @post_detect;
1267 } 1293 }
1294
1295 @models = (); # free probe data
1296
1297 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
1298 unshift @ISA, $MODEL;
1299
1300 require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT};
1301
1302 (shift @post_detect)->() while @post_detect;
1268 1303
1269 $MODEL 1304 $MODEL
1270} 1305}
1271 1306
1272sub AUTOLOAD { 1307sub AUTOLOAD {
1273 (my $func = $AUTOLOAD) =~ s/.*://; 1308 (my $func = $AUTOLOAD) =~ s/.*://;
1274 1309
1275 $method{$func} 1310 $method{$func}
1276 or Carp::croak "$func: not a valid method for AnyEvent objects"; 1311 or Carp::croak "$func: not a valid AnyEvent class method";
1277 1312
1278 detect unless $MODEL; 1313 detect;
1279 1314
1280 my $class = shift; 1315 my $class = shift;
1281 $class->$func (@_); 1316 $class->$func (@_);
1282} 1317}
1283 1318
1351package AnyEvent::Base; 1386package AnyEvent::Base;
1352 1387
1353# default implementations for many methods 1388# default implementations for many methods
1354 1389
1355sub _time() { 1390sub _time() {
1391 eval q{ # poor man's autoloading
1356 # probe for availability of Time::HiRes 1392 # probe for availability of Time::HiRes
1357 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { 1393 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") {
1358 warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; 1394 warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8;
1359 *_time = \&Time::HiRes::time; 1395 *_time = \&Time::HiRes::time;
1360 # if (eval "use POSIX (); (POSIX::times())... 1396 # if (eval "use POSIX (); (POSIX::times())...
1361 } else { 1397 } else {
1362 warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; 1398 warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE;
1363 *_time = sub { time }; # epic fail 1399 *_time = sub (){ time }; # epic fail
1400 }
1364 } 1401 };
1402 die if $@;
1365 1403
1366 &_time 1404 &_time
1367} 1405}
1368 1406
1369sub time { _time } 1407sub time { _time }
1390 1428
1391our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO); 1429our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO);
1392our (%SIG_ASY, %SIG_ASY_W); 1430our (%SIG_ASY, %SIG_ASY_W);
1393our ($SIG_COUNT, $SIG_TW); 1431our ($SIG_COUNT, $SIG_TW);
1394 1432
1395sub _signal_exec {
1396 $HAVE_ASYNC_INTERRUPT
1397 ? $SIGPIPE_R->drain
1398 : sysread $SIGPIPE_R, (my $dummy), 9;
1399
1400 while (%SIG_EV) {
1401 for (keys %SIG_EV) {
1402 delete $SIG_EV{$_};
1403 $_->() for values %{ $SIG_CB{$_} || {} };
1404 }
1405 }
1406}
1407
1408# install a dummy wakeup watcher to reduce signal catching latency 1433# install a dummy wakeup watcher to reduce signal catching latency
1434# used by Impls
1409sub _sig_add() { 1435sub _sig_add() {
1410 unless ($SIG_COUNT++) { 1436 unless ($SIG_COUNT++) {
1411 # try to align timer on a full-second boundary, if possible 1437 # try to align timer on a full-second boundary, if possible
1412 my $NOW = AE::now; 1438 my $NOW = AE::now;
1413 1439
1465 $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; 1491 $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec;
1466 1492
1467 } else { 1493 } else {
1468 warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; 1494 warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8;
1469 1495
1470 require Fcntl;
1471
1472 if (AnyEvent::WIN32) { 1496 if (AnyEvent::WIN32) {
1473 require AnyEvent::Util; 1497 require AnyEvent::Util;
1474 1498
1475 ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); 1499 ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe ();
1476 AnyEvent::Util::fh_nonblocking ($SIGPIPE_R, 1) if $SIGPIPE_R; 1500 AnyEvent::Util::fh_nonblocking ($SIGPIPE_R, 1) if $SIGPIPE_R;
1477 AnyEvent::Util::fh_nonblocking ($SIGPIPE_W, 1) if $SIGPIPE_W; # just in case 1501 AnyEvent::Util::fh_nonblocking ($SIGPIPE_W, 1) if $SIGPIPE_W; # just in case
1478 } else { 1502 } else {
1479 pipe $SIGPIPE_R, $SIGPIPE_W; 1503 pipe $SIGPIPE_R, $SIGPIPE_W;
1480 fcntl $SIGPIPE_R, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_R; 1504 fcntl $SIGPIPE_R, AnyEvent::F_SETFL, AnyEvent::O_NONBLOCK if $SIGPIPE_R;
1481 fcntl $SIGPIPE_W, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_W; # just in case 1505 fcntl $SIGPIPE_W, AnyEvent::F_SETFL, AnyEvent::O_NONBLOCK if $SIGPIPE_W; # just in case
1482 1506
1483 # not strictly required, as $^F is normally 2, but let's make sure... 1507 # not strictly required, as $^F is normally 2, but let's make sure...
1484 fcntl $SIGPIPE_R, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; 1508 fcntl $SIGPIPE_R, AnyEvent::F_SETFD, AnyEvent::FD_CLOEXEC;
1485 fcntl $SIGPIPE_W, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; 1509 fcntl $SIGPIPE_W, AnyEvent::F_SETFD, AnyEvent::FD_CLOEXEC;
1486 } 1510 }
1487 1511
1488 $SIGPIPE_R 1512 $SIGPIPE_R
1489 or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; 1513 or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n";
1490 1514
1544 # print weird messages, or just unconditionally exit 1568 # print weird messages, or just unconditionally exit
1545 # instead of getting the default action. 1569 # instead of getting the default action.
1546 undef $SIG{$signal} 1570 undef $SIG{$signal}
1547 unless keys %{ $SIG_CB{$signal} }; 1571 unless keys %{ $SIG_CB{$signal} };
1548 }; 1572 };
1573
1574 *_signal_exec = sub {
1575 $HAVE_ASYNC_INTERRUPT
1576 ? $SIGPIPE_R->drain
1577 : sysread $SIGPIPE_R, (my $dummy), 9;
1578
1579 while (%SIG_EV) {
1580 for (keys %SIG_EV) {
1581 delete $SIG_EV{$_};
1582 $_->() for values %{ $SIG_CB{$_} || {} };
1583 }
1584 }
1585 };
1549 }; 1586 };
1550 die if $@; 1587 die if $@;
1588
1551 &signal 1589 &signal
1552} 1590}
1553 1591
1554# default implementation for ->child 1592# default implementation for ->child
1555 1593
1556our %PID_CB; 1594our %PID_CB;
1557our $CHLD_W; 1595our $CHLD_W;
1558our $CHLD_DELAY_W; 1596our $CHLD_DELAY_W;
1559our $WNOHANG; 1597our $WNOHANG;
1560 1598
1599# used by many Impl's
1561sub _emit_childstatus($$) { 1600sub _emit_childstatus($$) {
1562 my (undef, $rpid, $rstatus) = @_; 1601 my (undef, $rpid, $rstatus) = @_;
1563 1602
1564 $_->($rpid, $rstatus) 1603 $_->($rpid, $rstatus)
1565 for values %{ $PID_CB{$rpid} || {} }, 1604 for values %{ $PID_CB{$rpid} || {} },
1566 values %{ $PID_CB{0} || {} }; 1605 values %{ $PID_CB{0} || {} };
1567} 1606}
1568 1607
1569sub _sigchld {
1570 my $pid;
1571
1572 AnyEvent->_emit_childstatus ($pid, $?)
1573 while ($pid = waitpid -1, $WNOHANG) > 0;
1574}
1575
1576sub child { 1608sub child {
1609 eval q{ # poor man's autoloading {}
1610 *_sigchld = sub {
1611 my $pid;
1612
1613 AnyEvent->_emit_childstatus ($pid, $?)
1614 while ($pid = waitpid -1, $WNOHANG) > 0;
1615 };
1616
1617 *child = sub {
1577 my (undef, %arg) = @_; 1618 my (undef, %arg) = @_;
1578 1619
1579 defined (my $pid = $arg{pid} + 0) 1620 defined (my $pid = $arg{pid} + 0)
1580 or Carp::croak "required option 'pid' is missing"; 1621 or Carp::croak "required option 'pid' is missing";
1581 1622
1582 $PID_CB{$pid}{$arg{cb}} = $arg{cb}; 1623 $PID_CB{$pid}{$arg{cb}} = $arg{cb};
1583 1624
1584 # WNOHANG is almost cetrainly 1 everywhere 1625 # WNOHANG is almost cetrainly 1 everywhere
1585 $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/ 1626 $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/
1586 ? 1 1627 ? 1
1587 : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; 1628 : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1;
1588 1629
1589 unless ($CHLD_W) { 1630 unless ($CHLD_W) {
1590 $CHLD_W = AE::signal CHLD => \&_sigchld; 1631 $CHLD_W = AE::signal CHLD => \&_sigchld;
1591 # child could be a zombie already, so make at least one round 1632 # child could be a zombie already, so make at least one round
1592 &_sigchld; 1633 &_sigchld;
1593 } 1634 }
1594 1635
1595 bless [$pid, $arg{cb}], "AnyEvent::Base::child" 1636 bless [$pid, $arg{cb}], "AnyEvent::Base::child"
1596} 1637 };
1597 1638
1598sub AnyEvent::Base::child::DESTROY { 1639 *AnyEvent::Base::child::DESTROY = sub {
1599 my ($pid, $cb) = @{$_[0]}; 1640 my ($pid, $cb) = @{$_[0]};
1600 1641
1601 delete $PID_CB{$pid}{$cb}; 1642 delete $PID_CB{$pid}{$cb};
1602 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; 1643 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} };
1603 1644
1604 undef $CHLD_W unless keys %PID_CB; 1645 undef $CHLD_W unless keys %PID_CB;
1646 };
1647 };
1648 die if $@;
1649
1650 &child
1605} 1651}
1606 1652
1607# idle emulation is done by simply using a timer, regardless 1653# idle emulation is done by simply using a timer, regardless
1608# of whether the process is idle or not, and not letting 1654# of whether the process is idle or not, and not letting
1609# the callback use more than 50% of the time. 1655# the callback use more than 50% of the time.
1610sub idle { 1656sub idle {
1657 eval q{ # poor man's autoloading {}
1658 *idle = sub {
1611 my (undef, %arg) = @_; 1659 my (undef, %arg) = @_;
1612 1660
1613 my ($cb, $w, $rcb) = $arg{cb}; 1661 my ($cb, $w, $rcb) = $arg{cb};
1614 1662
1615 $rcb = sub { 1663 $rcb = sub {
1616 if ($cb) { 1664 if ($cb) {
1617 $w = _time; 1665 $w = _time;
1618 &$cb; 1666 &$cb;
1619 $w = _time - $w; 1667 $w = _time - $w;
1620 1668
1621 # never use more then 50% of the time for the idle watcher, 1669 # never use more then 50% of the time for the idle watcher,
1622 # within some limits 1670 # within some limits
1623 $w = 0.0001 if $w < 0.0001; 1671 $w = 0.0001 if $w < 0.0001;
1624 $w = 5 if $w > 5; 1672 $w = 5 if $w > 5;
1625 1673
1626 $w = AE::timer $w, 0, $rcb; 1674 $w = AE::timer $w, 0, $rcb;
1627 } else { 1675 } else {
1628 # clean up... 1676 # clean up...
1629 undef $w; 1677 undef $w;
1630 undef $rcb; 1678 undef $rcb;
1679 }
1680 };
1681
1682 $w = AE::timer 0.05, 0, $rcb;
1683
1684 bless \\$cb, "AnyEvent::Base::idle"
1631 } 1685 };
1686
1687 *AnyEvent::Base::idle::DESTROY = sub {
1688 undef $${$_[0]};
1689 };
1632 }; 1690 };
1691 die if $@;
1633 1692
1634 $w = AE::timer 0.05, 0, $rcb; 1693 &idle
1635
1636 bless \\$cb, "AnyEvent::Base::idle"
1637}
1638
1639sub AnyEvent::Base::idle::DESTROY {
1640 undef $${$_[0]};
1641} 1694}
1642 1695
1643package AnyEvent::CondVar; 1696package AnyEvent::CondVar;
1644 1697
1645our @ISA = AnyEvent::CondVar::Base::; 1698our @ISA = AnyEvent::CondVar::Base::;
2433it's built-in modules) are required to use it. 2486it's built-in modules) are required to use it.
2434 2487
2435That does not mean that AnyEvent won't take advantage of some additional 2488That does not mean that AnyEvent won't take advantage of some additional
2436modules if they are installed. 2489modules if they are installed.
2437 2490
2438This section epxlains which additional modules will be used, and how they 2491This section explains which additional modules will be used, and how they
2439affect AnyEvent's operetion. 2492affect AnyEvent's operation.
2440 2493
2441=over 4 2494=over 4
2442 2495
2443=item L<Async::Interrupt> 2496=item L<Async::Interrupt>
2444 2497
2449catch the signals) with some delay (default is 10 seconds, look for 2502catch the signals) with some delay (default is 10 seconds, look for
2450C<$AnyEvent::MAX_SIGNAL_LATENCY>). 2503C<$AnyEvent::MAX_SIGNAL_LATENCY>).
2451 2504
2452If this module is available, then it will be used to implement signal 2505If this module is available, then it will be used to implement signal
2453catching, which means that signals will not be delayed, and the event loop 2506catching, which means that signals will not be delayed, and the event loop
2454will not be interrupted regularly, which is more efficient (And good for 2507will not be interrupted regularly, which is more efficient (and good for
2455battery life on laptops). 2508battery life on laptops).
2456 2509
2457This affects not just the pure-perl event loop, but also other event loops 2510This affects not just the pure-perl event loop, but also other event loops
2458that have no signal handling on their own (e.g. Glib, Tk, Qt). 2511that have no signal handling on their own (e.g. Glib, Tk, Qt).
2459 2512
2506 2559
2507 2560
2508=head1 FORK 2561=head1 FORK
2509 2562
2510Most event libraries are not fork-safe. The ones who are usually are 2563Most event libraries are not fork-safe. The ones who are usually are
2511because they rely on inefficient but fork-safe C<select> or C<poll> 2564because they rely on inefficient but fork-safe C<select> or C<poll> calls
2512calls. Only L<EV> is fully fork-aware. 2565- higher performance APIs such as BSD's kqueue or the dreaded Linux epoll
2566are usually badly thought-out hacks that are incompatible with fork in
2567one way or another. Only L<EV> is fully fork-aware and ensures that you
2568continue event-processing in both parent and child (or both, if you know
2569what you are doing).
2570
2571This means that, in general, you cannot fork and do event processing in
2572the child if the event library was initialised before the fork (which
2573usually happens when the first AnyEvent watcher is created, or the library
2574is loaded).
2513 2575
2514If you have to fork, you must either do so I<before> creating your first 2576If you have to fork, you must either do so I<before> creating your first
2515watcher OR you must not use AnyEvent at all in the child OR you must do 2577watcher OR you must not use AnyEvent at all in the child OR you must do
2516something completely out of the scope of AnyEvent. 2578something completely out of the scope of AnyEvent.
2579
2580The problem of doing event processing in the parent I<and> the child
2581is much more complicated: even for backends that I<are> fork-aware or
2582fork-safe, their behaviour is not usually what you want: fork clones all
2583watchers, that means all timers, I/O watchers etc. are active in both
2584parent and child, which is almost never what you want. USing C<exec>
2585to start worker children from some kind of manage rprocess is usually
2586preferred, because it is much easier and cleaner, at the expense of having
2587to have another binary.
2517 2588
2518 2589
2519=head1 SECURITY CONSIDERATIONS 2590=head1 SECURITY CONSIDERATIONS
2520 2591
2521AnyEvent can be forced to load any event model via 2592AnyEvent can be forced to load any event model via

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines