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.260 by root, Tue Jul 28 11:02:19 2009 UTC vs.
Revision 1.263 by root, Wed Jul 29 12:39:21 2009 UTC

1289} 1289}
1290 1290
1291# default implementation for ->signal 1291# default implementation for ->signal
1292 1292
1293our $HAVE_ASYNC_INTERRUPT; 1293our $HAVE_ASYNC_INTERRUPT;
1294
1295sub _have_async_interrupt() {
1296 $HAVE_ASYNC_INTERRUPT = 1*(!$ENV{PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT}
1297 && eval "use Async::Interrupt 1.0 (); 1")
1298 unless defined $HAVE_ASYNC_INTERRUPT;
1299
1300 $HAVE_ASYNC_INTERRUPT
1301}
1302
1294our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO); 1303our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO);
1295our (%SIG_ASY, %SIG_ASY_W); 1304our (%SIG_ASY, %SIG_ASY_W);
1296our ($SIG_COUNT, $SIG_TW); 1305our ($SIG_COUNT, $SIG_TW);
1297 1306
1298sub _signal_exec { 1307sub _signal_exec {
1306 $_->() for values %{ $SIG_CB{$_} || {} }; 1315 $_->() for values %{ $SIG_CB{$_} || {} };
1307 } 1316 }
1308 } 1317 }
1309} 1318}
1310 1319
1311# install a dumym wakeupw atcher to reduce signal catching latency 1320# install a dummy wakeup watcher to reduce signal catching latency
1312sub _sig_add() { 1321sub _sig_add() {
1313 unless ($SIG_COUNT++) { 1322 unless ($SIG_COUNT++) {
1314 # try to align timer on a full-second boundary, if possible 1323 # try to align timer on a full-second boundary, if possible
1315 my $NOW = AnyEvent->now; 1324 my $NOW = AnyEvent->now;
1316 1325
1325sub _sig_del { 1334sub _sig_del {
1326 undef $SIG_TW 1335 undef $SIG_TW
1327 unless --$SIG_COUNT; 1336 unless --$SIG_COUNT;
1328} 1337}
1329 1338
1339our %SIGNAME2NUM;
1340our @SIGNUM2NAME;
1341our $_sig_name_init; $_sig_name_init = sub {
1342 undef $_sig_name_init;
1343
1344 if (_have_async_interrupt) {
1345 *sig2num = \&Async::Interrupt::sig2num;
1346 *sig2name = \&Async::Interrupt::sig2name;
1347 } else {
1348 require Config;
1349
1350 @SIGNAME2NUM{ split ' ', $Config::Config{sig_name} }
1351 = split ' ', $Config::Config{sig_num};
1352 @SIGNUM2NAME[values %SIGNAME2NUM] = keys %SIGNAME2NUM;
1353
1354 *sig2num = sub($) {
1355 $_[0] > 0 ? shift : $SIGNAME2NUM{+shift}
1356 };
1357 *sig2name = sub ($) {
1358 $_[0] > 0 ? $SIGNUM2NAME[+shift] : shift
1359 };
1360 }
1361};
1362
1363sub sig2num ($) { &$_sig_name_init; &sig2num }
1364sub sig2name($) { &$_sig_name_init; &sig2name }
1365
1330sub _signal { 1366sub _signal {
1331 my (undef, %arg) = @_; 1367 my (undef, %arg) = @_;
1332 1368
1333 my $signal = uc $arg{signal} 1369 my $signal = uc $arg{signal}
1334 or Carp::croak "required option 'signal' is missing"; 1370 or Carp::croak "required option 'signal' is missing";
1335 1371
1336 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
1337
1338 if ($HAVE_ASYNC_INTERRUPT) { 1372 if ($HAVE_ASYNC_INTERRUPT) {
1339 # async::interrupt 1373 # async::interrupt
1340 1374
1341 $SIG_ASY{$signal} ||= do { 1375 $signal = sig2num $signal;
1342 my $asy = new Async::Interrupt 1376 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
1377
1378 $SIG_ASY{$signal} ||= new Async::Interrupt
1343 cb => sub { undef $SIG_EV{$signal} }, 1379 cb => sub { undef $SIG_EV{$signal} },
1344 signal => $signal, 1380 signal => $signal,
1345 pipe => [$SIGPIPE_R->filenos], 1381 pipe => [$SIGPIPE_R->filenos],
1346 ;
1347 $asy->pipe_autodrain (0); 1382 pipe_autodrain => 0,
1348
1349 $asy
1350 }; 1383 ;
1351 1384
1352 } else { 1385 } else {
1353 # pure perl 1386 # pure perl
1387
1388 # AE::Util has been loaded in signal
1389 $signal = sig2name $signal;
1390 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
1354 1391
1355 $SIG{$signal} ||= sub { 1392 $SIG{$signal} ||= sub {
1356 local $!; 1393 local $!;
1357 syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; 1394 syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV;
1358 undef $SIG_EV{$signal}; 1395 undef $SIG_EV{$signal};
1366 bless [$signal, $arg{cb}], "AnyEvent::Base::signal" 1403 bless [$signal, $arg{cb}], "AnyEvent::Base::signal"
1367} 1404}
1368 1405
1369sub signal { 1406sub signal {
1370 # probe for availability of Async::Interrupt 1407 # probe for availability of Async::Interrupt
1371 if (!$ENV{PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT} && eval "use Async::Interrupt 0.6 (); 1") { 1408 if (_have_async_interrupt) {
1372 warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8; 1409 warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8;
1373 1410
1374 $HAVE_ASYNC_INTERRUPT = 1;
1375 $SIGPIPE_R = new Async::Interrupt::EventPipe; 1411 $SIGPIPE_R = new Async::Interrupt::EventPipe;
1376 $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R->fileno, poll => "r", cb => \&_signal_exec); 1412 $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R->fileno, poll => "r", cb => \&_signal_exec);
1377 1413
1378 } else { 1414 } else {
1379 warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; 1415 warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines