… | |
… | |
1156 | |
1156 | |
1157 | BEGIN { AnyEvent::common_sense } |
1157 | BEGIN { AnyEvent::common_sense } |
1158 | |
1158 | |
1159 | use Carp (); |
1159 | use Carp (); |
1160 | |
1160 | |
1161 | our $VERSION = '5.24'; |
1161 | our $VERSION = '5.251'; |
1162 | our $MODEL; |
1162 | our $MODEL; |
1163 | |
1163 | |
1164 | our $AUTOLOAD; |
1164 | our $AUTOLOAD; |
1165 | our @ISA; |
1165 | our @ISA; |
1166 | |
1166 | |
1167 | our @REGISTRY; |
1167 | our @REGISTRY; |
1168 | |
1168 | |
1169 | our $VERBOSE; |
1169 | our $VERBOSE; |
1170 | |
1170 | |
1171 | BEGIN { |
1171 | BEGIN { |
1172 | eval "sub CYGWIN(){" . (($^O =~ /cygwin/i) *1) . "}"; |
1172 | require "AnyEvent/constants.pl"; |
1173 | eval "sub WIN32 (){" . (($^O =~ /mswin32/i)*1) . "}"; |
1173 | |
1174 | eval "sub TAINT (){" . (${^TAINT} *1) . "}"; |
1174 | eval "sub TAINT (){" . (${^TAINT}*1) . "}"; |
1175 | |
1175 | |
1176 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1176 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1177 | if ${^TAINT}; |
1177 | if ${^TAINT}; |
1178 | |
1178 | |
1179 | $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
1179 | $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
… | |
… | |
1222 | our @post_detect; |
1222 | our @post_detect; |
1223 | |
1223 | |
1224 | sub post_detect(&) { |
1224 | sub post_detect(&) { |
1225 | my ($cb) = @_; |
1225 | my ($cb) = @_; |
1226 | |
1226 | |
1227 | if ($MODEL) { |
|
|
1228 | $cb->(); |
|
|
1229 | |
|
|
1230 | undef |
|
|
1231 | } else { |
|
|
1232 | push @post_detect, $cb; |
1227 | push @post_detect, $cb; |
1233 | |
1228 | |
1234 | defined wantarray |
1229 | defined wantarray |
1235 | ? bless \$cb, "AnyEvent::Util::postdetect" |
1230 | ? bless \$cb, "AnyEvent::Util::postdetect" |
1236 | : () |
1231 | : () |
1237 | } |
|
|
1238 | } |
1232 | } |
1239 | |
1233 | |
1240 | sub AnyEvent::Util::postdetect::DESTROY { |
1234 | sub AnyEvent::Util::postdetect::DESTROY { |
1241 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
1235 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
1242 | } |
1236 | } |
… | |
… | |
1295 | @models = (); # free probe data |
1289 | @models = (); # free probe data |
1296 | |
1290 | |
1297 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
1291 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
1298 | unshift @ISA, $MODEL; |
1292 | unshift @ISA, $MODEL; |
1299 | |
1293 | |
|
|
1294 | # now nuke some methods that are overriden by the backend. |
|
|
1295 | # SUPER is not allowed. |
|
|
1296 | for (qw(time signal child idle)) { |
|
|
1297 | undef &{"AnyEvent::Base::$_"} |
|
|
1298 | if defined &{"$MODEL\::$_"}; |
|
|
1299 | } |
|
|
1300 | |
1300 | require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT}; |
1301 | require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT}; |
1301 | |
1302 | |
1302 | (shift @post_detect)->() while @post_detect; |
1303 | (shift @post_detect)->() while @post_detect; |
|
|
1304 | |
|
|
1305 | *post_detect = sub(&) { |
|
|
1306 | shift->(); |
|
|
1307 | |
|
|
1308 | undef |
|
|
1309 | }; |
1303 | |
1310 | |
1304 | $MODEL |
1311 | $MODEL |
1305 | } |
1312 | } |
1306 | |
1313 | |
1307 | sub AUTOLOAD { |
1314 | sub AUTOLOAD { |
… | |
… | |
1385 | |
1392 | |
1386 | package AnyEvent::Base; |
1393 | package AnyEvent::Base; |
1387 | |
1394 | |
1388 | # default implementations for many methods |
1395 | # default implementations for many methods |
1389 | |
1396 | |
1390 | sub _time() { |
1397 | sub time { |
1391 | eval q{ # poor man's autoloading |
1398 | eval q{ # poor man's autoloading {} |
1392 | # probe for availability of Time::HiRes |
1399 | # probe for availability of Time::HiRes |
1393 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1400 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1394 | warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; |
1401 | warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; |
1395 | *_time = \&Time::HiRes::time; |
1402 | *AE::time = \&Time::HiRes::time; |
1396 | # if (eval "use POSIX (); (POSIX::times())... |
1403 | # if (eval "use POSIX (); (POSIX::times())... |
1397 | } else { |
1404 | } else { |
1398 | warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; |
1405 | warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; |
1399 | *_time = sub (){ time }; # epic fail |
1406 | *AE::time = sub (){ time }; # epic fail |
1400 | } |
1407 | } |
|
|
1408 | |
|
|
1409 | *time = sub { AE::time }; # different prototypes |
1401 | }; |
1410 | }; |
1402 | die if $@; |
1411 | die if $@; |
1403 | |
1412 | |
1404 | &_time |
1413 | &time |
1405 | } |
1414 | } |
1406 | |
1415 | |
1407 | sub time { _time } |
1416 | *now = \&time; |
1408 | sub now { _time } |
1417 | |
1409 | sub now_update { } |
1418 | sub now_update { } |
1410 | |
1419 | |
1411 | # default implementation for ->condvar |
1420 | # default implementation for ->condvar |
1412 | |
1421 | |
1413 | sub condvar { |
1422 | sub condvar { |
|
|
1423 | eval q{ # poor man's autoloading {} |
|
|
1424 | *condvar = sub { |
1414 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" |
1425 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" |
|
|
1426 | }; |
|
|
1427 | |
|
|
1428 | *AE::cv = sub (;&) { |
|
|
1429 | bless { @_ ? (_ae_cb => shift) : () }, "AnyEvent::CondVar" |
|
|
1430 | }; |
|
|
1431 | }; |
|
|
1432 | die if $@; |
|
|
1433 | |
|
|
1434 | &condvar |
1415 | } |
1435 | } |
1416 | |
1436 | |
1417 | # default implementation for ->signal |
1437 | # default implementation for ->signal |
1418 | |
1438 | |
1419 | our $HAVE_ASYNC_INTERRUPT; |
1439 | our $HAVE_ASYNC_INTERRUPT; |
… | |
… | |
1449 | undef $SIG_TW |
1469 | undef $SIG_TW |
1450 | unless --$SIG_COUNT; |
1470 | unless --$SIG_COUNT; |
1451 | } |
1471 | } |
1452 | |
1472 | |
1453 | our $_sig_name_init; $_sig_name_init = sub { |
1473 | our $_sig_name_init; $_sig_name_init = sub { |
1454 | eval q{ # poor man's autoloading |
1474 | eval q{ # poor man's autoloading {} |
1455 | undef $_sig_name_init; |
1475 | undef $_sig_name_init; |
1456 | |
1476 | |
1457 | if (_have_async_interrupt) { |
1477 | if (_have_async_interrupt) { |
1458 | *sig2num = \&Async::Interrupt::sig2num; |
1478 | *sig2num = \&Async::Interrupt::sig2num; |
1459 | *sig2name = \&Async::Interrupt::sig2name; |
1479 | *sig2name = \&Async::Interrupt::sig2name; |
… | |
… | |
1491 | $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; |
1511 | $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; |
1492 | |
1512 | |
1493 | } else { |
1513 | } else { |
1494 | warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; |
1514 | warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; |
1495 | |
1515 | |
1496 | require Fcntl; |
|
|
1497 | |
|
|
1498 | if (AnyEvent::WIN32) { |
1516 | if (AnyEvent::WIN32) { |
1499 | require AnyEvent::Util; |
1517 | require AnyEvent::Util; |
1500 | |
1518 | |
1501 | ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); |
1519 | ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); |
1502 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_R, 1) if $SIGPIPE_R; |
1520 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_R, 1) if $SIGPIPE_R; |
1503 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_W, 1) if $SIGPIPE_W; # just in case |
1521 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_W, 1) if $SIGPIPE_W; # just in case |
1504 | } else { |
1522 | } else { |
1505 | pipe $SIGPIPE_R, $SIGPIPE_W; |
1523 | pipe $SIGPIPE_R, $SIGPIPE_W; |
1506 | fcntl $SIGPIPE_R, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_R; |
1524 | fcntl $SIGPIPE_R, AnyEvent::F_SETFL, AnyEvent::O_NONBLOCK if $SIGPIPE_R; |
1507 | fcntl $SIGPIPE_W, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_W; # just in case |
1525 | fcntl $SIGPIPE_W, AnyEvent::F_SETFL, AnyEvent::O_NONBLOCK if $SIGPIPE_W; # just in case |
1508 | |
1526 | |
1509 | # not strictly required, as $^F is normally 2, but let's make sure... |
1527 | # not strictly required, as $^F is normally 2, but let's make sure... |
1510 | fcntl $SIGPIPE_R, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; |
1528 | fcntl $SIGPIPE_R, AnyEvent::F_SETFD, AnyEvent::FD_CLOEXEC; |
1511 | fcntl $SIGPIPE_W, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; |
1529 | fcntl $SIGPIPE_W, AnyEvent::F_SETFD, AnyEvent::FD_CLOEXEC; |
1512 | } |
1530 | } |
1513 | |
1531 | |
1514 | $SIGPIPE_R |
1532 | $SIGPIPE_R |
1515 | or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; |
1533 | or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; |
1516 | |
1534 | |
1517 | $SIG_IO = AE::io $SIGPIPE_R, 0, \&_signal_exec; |
1535 | $SIG_IO = AE::io $SIGPIPE_R, 0, \&_signal_exec; |
1518 | } |
1536 | } |
1519 | |
1537 | |
1520 | *signal = sub { |
1538 | *signal = $HAVE_ASYNC_INTERRUPT |
|
|
1539 | ? sub { |
1521 | my (undef, %arg) = @_; |
1540 | my (undef, %arg) = @_; |
1522 | |
1541 | |
1523 | my $signal = uc $arg{signal} |
|
|
1524 | or Carp::croak "required option 'signal' is missing"; |
|
|
1525 | |
|
|
1526 | if ($HAVE_ASYNC_INTERRUPT) { |
|
|
1527 | # async::interrupt |
1542 | # async::interrupt |
1528 | |
|
|
1529 | $signal = sig2num $signal; |
1543 | my $signal = sig2num $arg{signal}; |
1530 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
1544 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
1531 | |
1545 | |
1532 | $SIG_ASY{$signal} ||= new Async::Interrupt |
1546 | $SIG_ASY{$signal} ||= new Async::Interrupt |
1533 | cb => sub { undef $SIG_EV{$signal} }, |
1547 | cb => sub { undef $SIG_EV{$signal} }, |
1534 | signal => $signal, |
1548 | signal => $signal, |
1535 | pipe => [$SIGPIPE_R->filenos], |
1549 | pipe => [$SIGPIPE_R->filenos], |
1536 | pipe_autodrain => 0, |
1550 | pipe_autodrain => 0, |
1537 | ; |
1551 | ; |
1538 | |
1552 | |
1539 | } else { |
1553 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
|
|
1554 | } |
|
|
1555 | : sub { |
|
|
1556 | my (undef, %arg) = @_; |
|
|
1557 | |
1540 | # pure perl |
1558 | # pure perl |
1541 | |
|
|
1542 | # AE::Util has been loaded in signal |
|
|
1543 | $signal = sig2name $signal; |
1559 | my $signal = sig2name $arg{signal}; |
1544 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
1560 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
1545 | |
1561 | |
1546 | $SIG{$signal} ||= sub { |
1562 | $SIG{$signal} ||= sub { |
1547 | local $!; |
1563 | local $!; |
1548 | syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; |
1564 | syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; |
1549 | undef $SIG_EV{$signal}; |
1565 | undef $SIG_EV{$signal}; |
1550 | }; |
1566 | }; |
1551 | |
1567 | |
1552 | # can't do signal processing without introducing races in pure perl, |
1568 | # can't do signal processing without introducing races in pure perl, |
1553 | # so limit the signal latency. |
1569 | # so limit the signal latency. |
1554 | _sig_add; |
1570 | _sig_add; |
1555 | } |
|
|
1556 | |
1571 | |
1557 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
1572 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
|
|
1573 | } |
1558 | }; |
1574 | ; |
1559 | |
1575 | |
1560 | *AnyEvent::Base::signal::DESTROY = sub { |
1576 | *AnyEvent::Base::signal::DESTROY = sub { |
1561 | my ($signal, $cb) = @{$_[0]}; |
1577 | my ($signal, $cb) = @{$_[0]}; |
1562 | |
1578 | |
1563 | _sig_del; |
1579 | _sig_del; |
… | |
… | |
2526 | automatic timer adjustments even when no monotonic clock is available, |
2542 | automatic timer adjustments even when no monotonic clock is available, |
2527 | can take avdantage of advanced kernel interfaces such as C<epoll> and |
2543 | can take avdantage of advanced kernel interfaces such as C<epoll> and |
2528 | C<kqueue>, and is the fastest backend I<by far>. You can even embed |
2544 | C<kqueue>, and is the fastest backend I<by far>. You can even embed |
2529 | L<Glib>/L<Gtk2> in it (or vice versa, see L<EV::Glib> and L<Glib::EV>). |
2545 | L<Glib>/L<Gtk2> in it (or vice versa, see L<EV::Glib> and L<Glib::EV>). |
2530 | |
2546 | |
|
|
2547 | If you only use backends that rely on another event loop (e.g. C<Tk>), |
|
|
2548 | then this module will do nothing for you. |
|
|
2549 | |
2531 | =item L<Guard> |
2550 | =item L<Guard> |
2532 | |
2551 | |
2533 | The guard module, when used, will be used to implement |
2552 | The guard module, when used, will be used to implement |
2534 | C<AnyEvent::Util::guard>. This speeds up guards considerably (and uses a |
2553 | C<AnyEvent::Util::guard>. This speeds up guards considerably (and uses a |
2535 | lot less memory), but otherwise doesn't affect guard operation much. It is |
2554 | lot less memory), but otherwise doesn't affect guard operation much. It is |
2536 | purely used for performance. |
2555 | purely used for performance. |
2537 | |
2556 | |
2538 | =item L<JSON> and L<JSON::XS> |
2557 | =item L<JSON> and L<JSON::XS> |
2539 | |
2558 | |
2540 | One of these modules is required when you want to read or write JSON data |
2559 | One of these modules is required when you want to read or write JSON data |
2541 | via L<AnyEvent::Handle>. It is also written in pure-perl, but can take |
2560 | via L<AnyEvent::Handle>. L<JSON> is also written in pure-perl, but can take |
2542 | advantage of the ultra-high-speed L<JSON::XS> module when it is installed. |
2561 | advantage of the ultra-high-speed L<JSON::XS> module when it is installed. |
2543 | |
|
|
2544 | In fact, L<AnyEvent::Handle> will use L<JSON::XS> by default if it is |
|
|
2545 | installed. |
|
|
2546 | |
2562 | |
2547 | =item L<Net::SSLeay> |
2563 | =item L<Net::SSLeay> |
2548 | |
2564 | |
2549 | Implementing TLS/SSL in Perl is certainly interesting, but not very |
2565 | Implementing TLS/SSL in Perl is certainly interesting, but not very |
2550 | worthwhile: If this module is installed, then L<AnyEvent::Handle> (with |
2566 | worthwhile: If this module is installed, then L<AnyEvent::Handle> (with |