… | |
… | |
503 | |
503 | |
504 | =head2 IDLE WATCHERS |
504 | =head2 IDLE WATCHERS |
505 | |
505 | |
506 | $w = AnyEvent->idle (cb => <callback>); |
506 | $w = AnyEvent->idle (cb => <callback>); |
507 | |
507 | |
508 | Sometimes there is a need to do something, but it is not so important |
508 | Repeatedly invoke the callback after the process becomes idle, until |
509 | to do it instantly, but only when there is nothing better to do. This |
509 | either the watcher is destroyed or new events have been detected. |
510 | "nothing better to do" is usually defined to be "no other events need |
|
|
511 | attention by the event loop". |
|
|
512 | |
510 | |
513 | Idle watchers ideally get invoked when the event loop has nothing |
511 | Idle watchers are useful when there is a need to do something, but it |
514 | better to do, just before it would block the process to wait for new |
512 | is not so important (or wise) to do it instantly. The callback will be |
515 | events. Instead of blocking, the idle watcher is invoked. |
513 | invoked only when there is "nothing better to do", which is usually |
|
|
514 | defined as "all outstanding events have been handled and no new events |
|
|
515 | have been detected". That means that idle watchers ideally get invoked |
|
|
516 | when the event loop has just polled for new events but none have been |
|
|
517 | detected. Instead of blocking to wait for more events, the idle watchers |
|
|
518 | will be invoked. |
516 | |
519 | |
517 | Most event loops unfortunately do not really support idle watchers (only |
520 | Unfortunately, most event loops do not really support idle watchers (only |
518 | EV, Event and Glib do it in a usable fashion) - for the rest, AnyEvent |
521 | EV, Event and Glib do it in a usable fashion) - for the rest, AnyEvent |
519 | will simply call the callback "from time to time". |
522 | will simply call the callback "from time to time". |
520 | |
523 | |
521 | Example: read lines from STDIN, but only process them when the |
524 | Example: read lines from STDIN, but only process them when the |
522 | program is otherwise idle: |
525 | program is otherwise idle: |
… | |
… | |
1145 | |
1148 | |
1146 | # basically a tuned-down version of common::sense |
1149 | # basically a tuned-down version of common::sense |
1147 | sub common_sense { |
1150 | sub common_sense { |
1148 | # from common:.sense 1.0 |
1151 | # from common:.sense 1.0 |
1149 | ${^WARNING_BITS} = "\xfc\x3f\x33\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x00"; |
1152 | ${^WARNING_BITS} = "\xfc\x3f\x33\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x00"; |
1150 | # use strict vars subs |
1153 | # use strict vars subs - NO UTF-8, as Util.pm doesn't like this atm. (uts46data.pl) |
1151 | $^H |= 0x00000600; |
1154 | $^H |= 0x00000600; |
1152 | } |
1155 | } |
1153 | |
1156 | |
1154 | BEGIN { AnyEvent::common_sense } |
1157 | BEGIN { AnyEvent::common_sense } |
1155 | |
1158 | |
1156 | use Carp (); |
1159 | use Carp (); |
1157 | |
1160 | |
1158 | our $VERSION = '5.22'; |
1161 | our $VERSION = '5.25'; |
1159 | our $MODEL; |
1162 | our $MODEL; |
1160 | |
1163 | |
1161 | our $AUTOLOAD; |
1164 | our $AUTOLOAD; |
1162 | our @ISA; |
1165 | our @ISA; |
1163 | |
1166 | |
1164 | our @REGISTRY; |
1167 | our @REGISTRY; |
1165 | |
1168 | |
1166 | our $VERBOSE; |
1169 | our $VERBOSE; |
1167 | |
1170 | |
1168 | BEGIN { |
1171 | BEGIN { |
1169 | eval "sub WIN32(){ " . (($^O =~ /mswin32/i)*1) ." }"; |
1172 | require "AnyEvent/constants.pl"; |
|
|
1173 | |
1170 | eval "sub TAINT(){ " . (${^TAINT}*1) . " }"; |
1174 | eval "sub TAINT (){" . (${^TAINT} *1) . "}"; |
1171 | |
1175 | |
1172 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1176 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1173 | if ${^TAINT}; |
1177 | if ${^TAINT}; |
1174 | |
1178 | |
1175 | $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
1179 | $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
… | |
… | |
1236 | sub AnyEvent::Util::postdetect::DESTROY { |
1240 | sub AnyEvent::Util::postdetect::DESTROY { |
1237 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
1241 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
1238 | } |
1242 | } |
1239 | |
1243 | |
1240 | sub detect() { |
1244 | sub 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 |
1241 | unless ($MODEL) { |
1262 | unless ($MODEL) { |
1242 | local $SIG{__DIE__}; |
1263 | for (@REGISTRY, @models) { |
1243 | |
1264 | my ($package, $model) = @$_; |
1244 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
1265 | if (${"$package\::VERSION"} > 0) { |
1245 | my $model = "AnyEvent::Impl::$1"; |
|
|
1246 | if (eval "require $model") { |
1266 | if (eval "require $model") { |
1247 | $MODEL = $model; |
1267 | $MODEL = $model; |
1248 | 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; |
1249 | } else { |
1269 | last; |
1250 | warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE; |
1270 | } |
1251 | } |
1271 | } |
1252 | } |
1272 | } |
1253 | |
1273 | |
1254 | # check for already loaded models |
|
|
1255 | unless ($MODEL) { |
1274 | unless ($MODEL) { |
|
|
1275 | # try to autoload a model |
1256 | for (@REGISTRY, @models) { |
1276 | for (@REGISTRY, @models) { |
1257 | my ($package, $model) = @$_; |
1277 | my ($package, $model, $autoload) = @$_; |
|
|
1278 | if ( |
|
|
1279 | $autoload |
|
|
1280 | and eval "require $package" |
1258 | if (${"$package\::VERSION"} > 0) { |
1281 | and ${"$package\::VERSION"} > 0 |
1259 | if (eval "require $model") { |
1282 | and eval "require $model" |
|
|
1283 | ) { |
1260 | $MODEL = $model; |
1284 | $MODEL = $model; |
1261 | warn "AnyEvent: autodetected model '$model', using it.\n" if $VERBOSE >= 2; |
1285 | warn "AnyEvent: autoloaded model '$model', using it.\n" if $VERBOSE >= 2; |
1262 | last; |
1286 | last; |
1263 | } |
|
|
1264 | } |
1287 | } |
1265 | } |
1288 | } |
1266 | |
1289 | |
1267 | unless ($MODEL) { |
|
|
1268 | # try to autoload a model |
|
|
1269 | for (@REGISTRY, @models) { |
|
|
1270 | my ($package, $model, $autoload) = @$_; |
|
|
1271 | if ( |
|
|
1272 | $autoload |
|
|
1273 | and eval "require $package" |
|
|
1274 | and ${"$package\::VERSION"} > 0 |
|
|
1275 | and eval "require $model" |
|
|
1276 | ) { |
|
|
1277 | $MODEL = $model; |
|
|
1278 | warn "AnyEvent: autoloaded model '$model', using it.\n" if $VERBOSE >= 2; |
|
|
1279 | last; |
|
|
1280 | } |
|
|
1281 | } |
|
|
1282 | |
|
|
1283 | $MODEL |
1290 | $MODEL |
1284 | 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"; |
1285 | } |
|
|
1286 | } |
1292 | } |
1287 | |
|
|
1288 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
|
|
1289 | |
|
|
1290 | unshift @ISA, $MODEL; |
|
|
1291 | |
|
|
1292 | require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT}; |
|
|
1293 | |
|
|
1294 | (shift @post_detect)->() while @post_detect; |
|
|
1295 | } |
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; |
1296 | |
1303 | |
1297 | $MODEL |
1304 | $MODEL |
1298 | } |
1305 | } |
1299 | |
1306 | |
1300 | sub AUTOLOAD { |
1307 | sub AUTOLOAD { |
1301 | (my $func = $AUTOLOAD) =~ s/.*://; |
1308 | (my $func = $AUTOLOAD) =~ s/.*://; |
1302 | |
1309 | |
1303 | $method{$func} |
1310 | $method{$func} |
1304 | or Carp::croak "$func: not a valid method for AnyEvent objects"; |
1311 | or Carp::croak "$func: not a valid AnyEvent class method"; |
1305 | |
1312 | |
1306 | detect unless $MODEL; |
1313 | detect; |
1307 | |
1314 | |
1308 | my $class = shift; |
1315 | my $class = shift; |
1309 | $class->$func (@_); |
1316 | $class->$func (@_); |
1310 | } |
1317 | } |
1311 | |
1318 | |
… | |
… | |
1379 | package AnyEvent::Base; |
1386 | package AnyEvent::Base; |
1380 | |
1387 | |
1381 | # default implementations for many methods |
1388 | # default implementations for many methods |
1382 | |
1389 | |
1383 | sub _time() { |
1390 | sub _time() { |
|
|
1391 | eval q{ # poor man's autoloading |
1384 | # probe for availability of Time::HiRes |
1392 | # probe for availability of Time::HiRes |
1385 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1393 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1386 | 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; |
1387 | *_time = \&Time::HiRes::time; |
1395 | *_time = \&Time::HiRes::time; |
1388 | # if (eval "use POSIX (); (POSIX::times())... |
1396 | # if (eval "use POSIX (); (POSIX::times())... |
1389 | } else { |
1397 | } else { |
1390 | 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; |
1391 | *_time = sub { time }; # epic fail |
1399 | *_time = sub (){ time }; # epic fail |
|
|
1400 | } |
1392 | } |
1401 | }; |
|
|
1402 | die if $@; |
1393 | |
1403 | |
1394 | &_time |
1404 | &_time |
1395 | } |
1405 | } |
1396 | |
1406 | |
1397 | sub time { _time } |
1407 | sub time { _time } |
… | |
… | |
1418 | |
1428 | |
1419 | our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO); |
1429 | our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO); |
1420 | our (%SIG_ASY, %SIG_ASY_W); |
1430 | our (%SIG_ASY, %SIG_ASY_W); |
1421 | our ($SIG_COUNT, $SIG_TW); |
1431 | our ($SIG_COUNT, $SIG_TW); |
1422 | |
1432 | |
1423 | sub _signal_exec { |
|
|
1424 | $HAVE_ASYNC_INTERRUPT |
|
|
1425 | ? $SIGPIPE_R->drain |
|
|
1426 | : sysread $SIGPIPE_R, (my $dummy), 9; |
|
|
1427 | |
|
|
1428 | while (%SIG_EV) { |
|
|
1429 | for (keys %SIG_EV) { |
|
|
1430 | delete $SIG_EV{$_}; |
|
|
1431 | $_->() for values %{ $SIG_CB{$_} || {} }; |
|
|
1432 | } |
|
|
1433 | } |
|
|
1434 | } |
|
|
1435 | |
|
|
1436 | # 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 |
1437 | sub _sig_add() { |
1435 | sub _sig_add() { |
1438 | unless ($SIG_COUNT++) { |
1436 | unless ($SIG_COUNT++) { |
1439 | # try to align timer on a full-second boundary, if possible |
1437 | # try to align timer on a full-second boundary, if possible |
1440 | my $NOW = AE::now; |
1438 | my $NOW = AE::now; |
1441 | |
1439 | |
… | |
… | |
1493 | $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; |
1491 | $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; |
1494 | |
1492 | |
1495 | } else { |
1493 | } else { |
1496 | 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; |
1497 | |
1495 | |
1498 | require Fcntl; |
|
|
1499 | |
|
|
1500 | if (AnyEvent::WIN32) { |
1496 | if (AnyEvent::WIN32) { |
1501 | require AnyEvent::Util; |
1497 | require AnyEvent::Util; |
1502 | |
1498 | |
1503 | ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); |
1499 | ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); |
1504 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_R, 1) if $SIGPIPE_R; |
1500 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_R, 1) if $SIGPIPE_R; |
1505 | 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 |
1506 | } else { |
1502 | } else { |
1507 | pipe $SIGPIPE_R, $SIGPIPE_W; |
1503 | pipe $SIGPIPE_R, $SIGPIPE_W; |
1508 | 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; |
1509 | 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 |
1510 | |
1506 | |
1511 | # 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... |
1512 | fcntl $SIGPIPE_R, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; |
1508 | fcntl $SIGPIPE_R, AnyEvent::F_SETFD, AnyEvent::FD_CLOEXEC; |
1513 | fcntl $SIGPIPE_W, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; |
1509 | fcntl $SIGPIPE_W, AnyEvent::F_SETFD, AnyEvent::FD_CLOEXEC; |
1514 | } |
1510 | } |
1515 | |
1511 | |
1516 | $SIGPIPE_R |
1512 | $SIGPIPE_R |
1517 | 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"; |
1518 | |
1514 | |
… | |
… | |
1572 | # print weird messages, or just unconditionally exit |
1568 | # print weird messages, or just unconditionally exit |
1573 | # instead of getting the default action. |
1569 | # instead of getting the default action. |
1574 | undef $SIG{$signal} |
1570 | undef $SIG{$signal} |
1575 | unless keys %{ $SIG_CB{$signal} }; |
1571 | unless keys %{ $SIG_CB{$signal} }; |
1576 | }; |
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 | }; |
1577 | }; |
1586 | }; |
1578 | die if $@; |
1587 | die if $@; |
|
|
1588 | |
1579 | &signal |
1589 | &signal |
1580 | } |
1590 | } |
1581 | |
1591 | |
1582 | # default implementation for ->child |
1592 | # default implementation for ->child |
1583 | |
1593 | |
1584 | our %PID_CB; |
1594 | our %PID_CB; |
1585 | our $CHLD_W; |
1595 | our $CHLD_W; |
1586 | our $CHLD_DELAY_W; |
1596 | our $CHLD_DELAY_W; |
1587 | our $WNOHANG; |
1597 | our $WNOHANG; |
1588 | |
1598 | |
|
|
1599 | # used by many Impl's |
1589 | sub _emit_childstatus($$) { |
1600 | sub _emit_childstatus($$) { |
1590 | my (undef, $rpid, $rstatus) = @_; |
1601 | my (undef, $rpid, $rstatus) = @_; |
1591 | |
1602 | |
1592 | $_->($rpid, $rstatus) |
1603 | $_->($rpid, $rstatus) |
1593 | for values %{ $PID_CB{$rpid} || {} }, |
1604 | for values %{ $PID_CB{$rpid} || {} }, |
1594 | values %{ $PID_CB{0} || {} }; |
1605 | values %{ $PID_CB{0} || {} }; |
1595 | } |
1606 | } |
1596 | |
1607 | |
1597 | sub _sigchld { |
|
|
1598 | my $pid; |
|
|
1599 | |
|
|
1600 | AnyEvent->_emit_childstatus ($pid, $?) |
|
|
1601 | while ($pid = waitpid -1, $WNOHANG) > 0; |
|
|
1602 | } |
|
|
1603 | |
|
|
1604 | sub child { |
1608 | sub 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 { |
1605 | my (undef, %arg) = @_; |
1618 | my (undef, %arg) = @_; |
1606 | |
1619 | |
1607 | defined (my $pid = $arg{pid} + 0) |
1620 | defined (my $pid = $arg{pid} + 0) |
1608 | or Carp::croak "required option 'pid' is missing"; |
1621 | or Carp::croak "required option 'pid' is missing"; |
1609 | |
1622 | |
1610 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
1623 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
1611 | |
1624 | |
1612 | # WNOHANG is almost cetrainly 1 everywhere |
1625 | # WNOHANG is almost cetrainly 1 everywhere |
1613 | $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/ |
1626 | $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/ |
1614 | ? 1 |
1627 | ? 1 |
1615 | : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
1628 | : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
1616 | |
1629 | |
1617 | unless ($CHLD_W) { |
1630 | unless ($CHLD_W) { |
1618 | $CHLD_W = AE::signal CHLD => \&_sigchld; |
1631 | $CHLD_W = AE::signal CHLD => \&_sigchld; |
1619 | # 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 |
1620 | &_sigchld; |
1633 | &_sigchld; |
1621 | } |
1634 | } |
1622 | |
1635 | |
1623 | bless [$pid, $arg{cb}], "AnyEvent::Base::child" |
1636 | bless [$pid, $arg{cb}], "AnyEvent::Base::child" |
1624 | } |
1637 | }; |
1625 | |
1638 | |
1626 | sub AnyEvent::Base::child::DESTROY { |
1639 | *AnyEvent::Base::child::DESTROY = sub { |
1627 | my ($pid, $cb) = @{$_[0]}; |
1640 | my ($pid, $cb) = @{$_[0]}; |
1628 | |
1641 | |
1629 | delete $PID_CB{$pid}{$cb}; |
1642 | delete $PID_CB{$pid}{$cb}; |
1630 | delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; |
1643 | delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; |
1631 | |
1644 | |
1632 | undef $CHLD_W unless keys %PID_CB; |
1645 | undef $CHLD_W unless keys %PID_CB; |
|
|
1646 | }; |
|
|
1647 | }; |
|
|
1648 | die if $@; |
|
|
1649 | |
|
|
1650 | &child |
1633 | } |
1651 | } |
1634 | |
1652 | |
1635 | # idle emulation is done by simply using a timer, regardless |
1653 | # idle emulation is done by simply using a timer, regardless |
1636 | # of whether the process is idle or not, and not letting |
1654 | # of whether the process is idle or not, and not letting |
1637 | # the callback use more than 50% of the time. |
1655 | # the callback use more than 50% of the time. |
1638 | sub idle { |
1656 | sub idle { |
|
|
1657 | eval q{ # poor man's autoloading {} |
|
|
1658 | *idle = sub { |
1639 | my (undef, %arg) = @_; |
1659 | my (undef, %arg) = @_; |
1640 | |
1660 | |
1641 | my ($cb, $w, $rcb) = $arg{cb}; |
1661 | my ($cb, $w, $rcb) = $arg{cb}; |
1642 | |
1662 | |
1643 | $rcb = sub { |
1663 | $rcb = sub { |
1644 | if ($cb) { |
1664 | if ($cb) { |
1645 | $w = _time; |
1665 | $w = _time; |
1646 | &$cb; |
1666 | &$cb; |
1647 | $w = _time - $w; |
1667 | $w = _time - $w; |
1648 | |
1668 | |
1649 | # 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, |
1650 | # within some limits |
1670 | # within some limits |
1651 | $w = 0.0001 if $w < 0.0001; |
1671 | $w = 0.0001 if $w < 0.0001; |
1652 | $w = 5 if $w > 5; |
1672 | $w = 5 if $w > 5; |
1653 | |
1673 | |
1654 | $w = AE::timer $w, 0, $rcb; |
1674 | $w = AE::timer $w, 0, $rcb; |
1655 | } else { |
1675 | } else { |
1656 | # clean up... |
1676 | # clean up... |
1657 | undef $w; |
1677 | undef $w; |
1658 | undef $rcb; |
1678 | undef $rcb; |
|
|
1679 | } |
|
|
1680 | }; |
|
|
1681 | |
|
|
1682 | $w = AE::timer 0.05, 0, $rcb; |
|
|
1683 | |
|
|
1684 | bless \\$cb, "AnyEvent::Base::idle" |
1659 | } |
1685 | }; |
|
|
1686 | |
|
|
1687 | *AnyEvent::Base::idle::DESTROY = sub { |
|
|
1688 | undef $${$_[0]}; |
|
|
1689 | }; |
1660 | }; |
1690 | }; |
|
|
1691 | die if $@; |
1661 | |
1692 | |
1662 | $w = AE::timer 0.05, 0, $rcb; |
1693 | &idle |
1663 | |
|
|
1664 | bless \\$cb, "AnyEvent::Base::idle" |
|
|
1665 | } |
|
|
1666 | |
|
|
1667 | sub AnyEvent::Base::idle::DESTROY { |
|
|
1668 | undef $${$_[0]}; |
|
|
1669 | } |
1694 | } |
1670 | |
1695 | |
1671 | package AnyEvent::CondVar; |
1696 | package AnyEvent::CondVar; |
1672 | |
1697 | |
1673 | our @ISA = AnyEvent::CondVar::Base::; |
1698 | our @ISA = AnyEvent::CondVar::Base::; |
… | |
… | |
2534 | |
2559 | |
2535 | |
2560 | |
2536 | =head1 FORK |
2561 | =head1 FORK |
2537 | |
2562 | |
2538 | Most event libraries are not fork-safe. The ones who are usually are |
2563 | Most event libraries are not fork-safe. The ones who are usually are |
2539 | because they rely on inefficient but fork-safe C<select> or C<poll> |
2564 | because they rely on inefficient but fork-safe C<select> or C<poll> calls |
2540 | calls. Only L<EV> is fully fork-aware. |
2565 | - higher performance APIs such as BSD's kqueue or the dreaded Linux epoll |
|
|
2566 | are usually badly thought-out hacks that are incompatible with fork in |
|
|
2567 | one way or another. Only L<EV> is fully fork-aware and ensures that you |
|
|
2568 | continue event-processing in both parent and child (or both, if you know |
|
|
2569 | what you are doing). |
2541 | |
2570 | |
2542 | This means that, in general, you cannot fork and do event processing |
2571 | This means that, in general, you cannot fork and do event processing in |
2543 | in the child if a watcher was created before the fork (which in turn |
2572 | the child if the event library was initialised before the fork (which |
2544 | initialises the event library). |
2573 | usually happens when the first AnyEvent watcher is created, or the library |
|
|
2574 | is loaded). |
2545 | |
2575 | |
2546 | If you have to fork, you must either do so I<before> creating your first |
2576 | If you have to fork, you must either do so I<before> creating your first |
2547 | watcher OR you must not use AnyEvent at all in the child OR you must do |
2577 | watcher OR you must not use AnyEvent at all in the child OR you must do |
2548 | something completely out of the scope of AnyEvent. |
2578 | something completely out of the scope of AnyEvent. |
2549 | |
2579 | |
2550 | The problem of doing event processing in the parent I<and> the child |
2580 | The problem of doing event processing in the parent I<and> the child |
2551 | is much more complicated: even for backends that I<are> fork-aware or |
2581 | is much more complicated: even for backends that I<are> fork-aware or |
2552 | fork-safe, their behaviour is not usually what you want: fork clones all |
2582 | fork-safe, their behaviour is not usually what you want: fork clones all |
2553 | watchers, that means all timers, I/O watchers etc. are active in both |
2583 | watchers, that means all timers, I/O watchers etc. are active in both |
2554 | parent and child, which is almost never what you want. |
2584 | parent and child, which is almost never what you want. USing C<exec> |
|
|
2585 | to start worker children from some kind of manage rprocess is usually |
|
|
2586 | preferred, because it is much easier and cleaner, at the expense of having |
|
|
2587 | to have another binary. |
2555 | |
2588 | |
2556 | |
2589 | |
2557 | =head1 SECURITY CONSIDERATIONS |
2590 | =head1 SECURITY CONSIDERATIONS |
2558 | |
2591 | |
2559 | AnyEvent can be forced to load any event model via |
2592 | AnyEvent can be forced to load any event model via |