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.305 by root, Sun Dec 13 05:13:15 2009 UTC vs.
Revision 1.312 by root, Mon Feb 15 18:02:35 2010 UTC

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
508Sometimes there is a need to do something, but it is not so important 508Repeatedly invoke the callback after the process becomes idle, until
509to do it instantly, but only when there is nothing better to do. This 509either the watcher is destroyed or new events have been detected.
510"nothing better to do" is usually defined to be "no other events need
511attention by the event loop".
512 510
513Idle watchers ideally get invoked when the event loop has nothing 511Idle watchers are useful when there is a need to do something, but it
514better 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
515events. 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.
516 519
517Most event loops unfortunately do not really support idle watchers (only 520Unfortunately, most event loops do not really support idle watchers (only
518EV, 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
519will simply call the callback "from time to time". 522will simply call the callback "from time to time".
520 523
521Example: read lines from STDIN, but only process them when the 524Example: read lines from STDIN, but only process them when the
522program is otherwise idle: 525program is otherwise idle:
1145 1148
1146# basically a tuned-down version of common::sense 1149# basically a tuned-down version of common::sense
1147sub common_sense { 1150sub 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
1154BEGIN { AnyEvent::common_sense } 1157BEGIN { AnyEvent::common_sense }
1155 1158
1156use Carp (); 1159use Carp ();
1157 1160
1158our $VERSION = '5.22'; 1161our $VERSION = '5.24';
1159our $MODEL; 1162our $MODEL;
1160 1163
1161our $AUTOLOAD; 1164our $AUTOLOAD;
1162our @ISA; 1165our @ISA;
1163 1166
1164our @REGISTRY; 1167our @REGISTRY;
1165 1168
1166our $VERBOSE; 1169our $VERBOSE;
1167 1170
1168BEGIN { 1171BEGIN {
1172 eval "sub CYGWIN(){" . (($^O =~ /cygwin/i) *1) . "}";
1169 eval "sub WIN32(){ " . (($^O =~ /mswin32/i)*1) ." }"; 1173 eval "sub WIN32 (){" . (($^O =~ /mswin32/i)*1) . "}";
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;
1236sub AnyEvent::Util::postdetect::DESTROY { 1240sub AnyEvent::Util::postdetect::DESTROY {
1237 @post_detect = grep $_ != ${$_[0]}, @post_detect; 1241 @post_detect = grep $_ != ${$_[0]}, @post_detect;
1238} 1242}
1239 1243
1240sub 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
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
1300sub AUTOLOAD { 1307sub 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
1379package AnyEvent::Base; 1386package AnyEvent::Base;
1380 1387
1381# default implementations for many methods 1388# default implementations for many methods
1382 1389
1383sub _time() { 1390sub _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
1397sub time { _time } 1407sub time { _time }
1418 1428
1419our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO); 1429our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO);
1420our (%SIG_ASY, %SIG_ASY_W); 1430our (%SIG_ASY, %SIG_ASY_W);
1421our ($SIG_COUNT, $SIG_TW); 1431our ($SIG_COUNT, $SIG_TW);
1422 1432
1423sub _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
1437sub _sig_add() { 1435sub _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
1572 # print weird messages, or just unconditionally exit 1570 # print weird messages, or just unconditionally exit
1573 # instead of getting the default action. 1571 # instead of getting the default action.
1574 undef $SIG{$signal} 1572 undef $SIG{$signal}
1575 unless keys %{ $SIG_CB{$signal} }; 1573 unless keys %{ $SIG_CB{$signal} };
1576 }; 1574 };
1575
1576 *_signal_exec = sub {
1577 $HAVE_ASYNC_INTERRUPT
1578 ? $SIGPIPE_R->drain
1579 : sysread $SIGPIPE_R, (my $dummy), 9;
1580
1581 while (%SIG_EV) {
1582 for (keys %SIG_EV) {
1583 delete $SIG_EV{$_};
1584 $_->() for values %{ $SIG_CB{$_} || {} };
1585 }
1586 }
1587 };
1577 }; 1588 };
1578 die if $@; 1589 die if $@;
1590
1579 &signal 1591 &signal
1580} 1592}
1581 1593
1582# default implementation for ->child 1594# default implementation for ->child
1583 1595
1584our %PID_CB; 1596our %PID_CB;
1585our $CHLD_W; 1597our $CHLD_W;
1586our $CHLD_DELAY_W; 1598our $CHLD_DELAY_W;
1587our $WNOHANG; 1599our $WNOHANG;
1588 1600
1601# used by many Impl's
1589sub _emit_childstatus($$) { 1602sub _emit_childstatus($$) {
1590 my (undef, $rpid, $rstatus) = @_; 1603 my (undef, $rpid, $rstatus) = @_;
1591 1604
1592 $_->($rpid, $rstatus) 1605 $_->($rpid, $rstatus)
1593 for values %{ $PID_CB{$rpid} || {} }, 1606 for values %{ $PID_CB{$rpid} || {} },
1594 values %{ $PID_CB{0} || {} }; 1607 values %{ $PID_CB{0} || {} };
1595} 1608}
1596 1609
1597sub _sigchld {
1598 my $pid;
1599
1600 AnyEvent->_emit_childstatus ($pid, $?)
1601 while ($pid = waitpid -1, $WNOHANG) > 0;
1602}
1603
1604sub child { 1610sub child {
1611 eval q{ # poor man's autoloading {}
1612 *_sigchld = sub {
1613 my $pid;
1614
1615 AnyEvent->_emit_childstatus ($pid, $?)
1616 while ($pid = waitpid -1, $WNOHANG) > 0;
1617 };
1618
1619 *child = sub {
1605 my (undef, %arg) = @_; 1620 my (undef, %arg) = @_;
1606 1621
1607 defined (my $pid = $arg{pid} + 0) 1622 defined (my $pid = $arg{pid} + 0)
1608 or Carp::croak "required option 'pid' is missing"; 1623 or Carp::croak "required option 'pid' is missing";
1609 1624
1610 $PID_CB{$pid}{$arg{cb}} = $arg{cb}; 1625 $PID_CB{$pid}{$arg{cb}} = $arg{cb};
1611 1626
1612 # WNOHANG is almost cetrainly 1 everywhere 1627 # WNOHANG is almost cetrainly 1 everywhere
1613 $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/ 1628 $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/
1614 ? 1 1629 ? 1
1615 : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; 1630 : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1;
1616 1631
1617 unless ($CHLD_W) { 1632 unless ($CHLD_W) {
1618 $CHLD_W = AE::signal CHLD => \&_sigchld; 1633 $CHLD_W = AE::signal CHLD => \&_sigchld;
1619 # child could be a zombie already, so make at least one round 1634 # child could be a zombie already, so make at least one round
1620 &_sigchld; 1635 &_sigchld;
1621 } 1636 }
1622 1637
1623 bless [$pid, $arg{cb}], "AnyEvent::Base::child" 1638 bless [$pid, $arg{cb}], "AnyEvent::Base::child"
1624} 1639 };
1625 1640
1626sub AnyEvent::Base::child::DESTROY { 1641 *AnyEvent::Base::child::DESTROY = sub {
1627 my ($pid, $cb) = @{$_[0]}; 1642 my ($pid, $cb) = @{$_[0]};
1628 1643
1629 delete $PID_CB{$pid}{$cb}; 1644 delete $PID_CB{$pid}{$cb};
1630 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; 1645 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} };
1631 1646
1632 undef $CHLD_W unless keys %PID_CB; 1647 undef $CHLD_W unless keys %PID_CB;
1648 };
1649 };
1650 die if $@;
1651
1652 &child
1633} 1653}
1634 1654
1635# idle emulation is done by simply using a timer, regardless 1655# idle emulation is done by simply using a timer, regardless
1636# of whether the process is idle or not, and not letting 1656# of whether the process is idle or not, and not letting
1637# the callback use more than 50% of the time. 1657# the callback use more than 50% of the time.
1638sub idle { 1658sub idle {
1659 eval q{ # poor man's autoloading {}
1660 *idle = sub {
1639 my (undef, %arg) = @_; 1661 my (undef, %arg) = @_;
1640 1662
1641 my ($cb, $w, $rcb) = $arg{cb}; 1663 my ($cb, $w, $rcb) = $arg{cb};
1642 1664
1643 $rcb = sub { 1665 $rcb = sub {
1644 if ($cb) { 1666 if ($cb) {
1645 $w = _time; 1667 $w = _time;
1646 &$cb; 1668 &$cb;
1647 $w = _time - $w; 1669 $w = _time - $w;
1648 1670
1649 # never use more then 50% of the time for the idle watcher, 1671 # never use more then 50% of the time for the idle watcher,
1650 # within some limits 1672 # within some limits
1651 $w = 0.0001 if $w < 0.0001; 1673 $w = 0.0001 if $w < 0.0001;
1652 $w = 5 if $w > 5; 1674 $w = 5 if $w > 5;
1653 1675
1654 $w = AE::timer $w, 0, $rcb; 1676 $w = AE::timer $w, 0, $rcb;
1655 } else { 1677 } else {
1656 # clean up... 1678 # clean up...
1657 undef $w; 1679 undef $w;
1658 undef $rcb; 1680 undef $rcb;
1681 }
1682 };
1683
1684 $w = AE::timer 0.05, 0, $rcb;
1685
1686 bless \\$cb, "AnyEvent::Base::idle"
1659 } 1687 };
1688
1689 *AnyEvent::Base::idle::DESTROY = sub {
1690 undef $${$_[0]};
1691 };
1660 }; 1692 };
1693 die if $@;
1661 1694
1662 $w = AE::timer 0.05, 0, $rcb; 1695 &idle
1663
1664 bless \\$cb, "AnyEvent::Base::idle"
1665}
1666
1667sub AnyEvent::Base::idle::DESTROY {
1668 undef $${$_[0]};
1669} 1696}
1670 1697
1671package AnyEvent::CondVar; 1698package AnyEvent::CondVar;
1672 1699
1673our @ISA = AnyEvent::CondVar::Base::; 1700our @ISA = AnyEvent::CondVar::Base::;
2534 2561
2535 2562
2536=head1 FORK 2563=head1 FORK
2537 2564
2538Most event libraries are not fork-safe. The ones who are usually are 2565Most event libraries are not fork-safe. The ones who are usually are
2539because they rely on inefficient but fork-safe C<select> or C<poll> 2566because they rely on inefficient but fork-safe C<select> or C<poll> calls
2540calls. Only L<EV> is fully fork-aware. 2567- higher performance APIs such as BSD's kqueue or the dreaded Linux epoll
2568are usually badly thought-out hacks that are incompatible with fork in
2569one way or another. Only L<EV> is fully fork-aware and ensures that you
2570continue event-processing in both parent and child (or both, if you know
2571what you are doing).
2541 2572
2542This means that, in general, you cannot fork and do event processing 2573This means that, in general, you cannot fork and do event processing in
2543in the child if a watcher was created before the fork (which in turn 2574the child if the event library was initialised before the fork (which
2544initialises the event library). 2575usually happens when the first AnyEvent watcher is created, or the library
2576is loaded).
2545 2577
2546If you have to fork, you must either do so I<before> creating your first 2578If you have to fork, you must either do so I<before> creating your first
2547watcher OR you must not use AnyEvent at all in the child OR you must do 2579watcher OR you must not use AnyEvent at all in the child OR you must do
2548something completely out of the scope of AnyEvent. 2580something completely out of the scope of AnyEvent.
2549 2581
2550The problem of doing event processing in the parent I<and> the child 2582The problem of doing event processing in the parent I<and> the child
2551is much more complicated: even for backends that I<are> fork-aware or 2583is much more complicated: even for backends that I<are> fork-aware or
2552fork-safe, their behaviour is not usually what you want: fork clones all 2584fork-safe, their behaviour is not usually what you want: fork clones all
2553watchers, that means all timers, I/O watchers etc. are active in both 2585watchers, that means all timers, I/O watchers etc. are active in both
2554parent and child, which is almost never what you want. 2586parent and child, which is almost never what you want. USing C<exec>
2587to start worker children from some kind of manage rprocess is usually
2588preferred, because it is much easier and cleaner, at the expense of having
2589to have another binary.
2555 2590
2556 2591
2557=head1 SECURITY CONSIDERATIONS 2592=head1 SECURITY CONSIDERATIONS
2558 2593
2559AnyEvent can be forced to load any event model via 2594AnyEvent can be forced to load any event model via

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines