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.258 by root, Sun Jul 26 05:57:05 2009 UTC vs.
Revision 1.266 by root, Thu Jul 30 03:41:56 2009 UTC

181my variables are only visible after the statement in which they are 181my variables are only visible after the statement in which they are
182declared. 182declared.
183 183
184=head2 I/O WATCHERS 184=head2 I/O WATCHERS
185 185
186 $w = AnyEvent->io (
187 fh => <filehandle_or_fileno>,
188 poll => <"r" or "w">,
189 cb => <callback>,
190 );
191
186You can create an I/O watcher by calling the C<< AnyEvent->io >> method 192You can create an I/O watcher by calling the C<< AnyEvent->io >> method
187with the following mandatory key-value pairs as arguments: 193with the following mandatory key-value pairs as arguments:
188 194
189C<fh> is the Perl I<file handle> (or a naked file descriptor) to watch 195C<fh> is the Perl I<file handle> (or a naked file descriptor) to watch
190for events (AnyEvent might or might not keep a reference to this file 196for events (AnyEvent might or might not keep a reference to this file
219 undef $w; 225 undef $w;
220 }); 226 });
221 227
222=head2 TIME WATCHERS 228=head2 TIME WATCHERS
223 229
230 $w = AnyEvent->timer (after => <seconds>, cb => <callback>);
231
232 $w = AnyEvent->timer (
233 after => <fractional_seconds>,
234 interval => <fractional_seconds>,
235 cb => <callback>,
236 );
237
224You can create a time watcher by calling the C<< AnyEvent->timer >> 238You can create a time watcher by calling the C<< AnyEvent->timer >>
225method with the following mandatory arguments: 239method with the following mandatory arguments:
226 240
227C<after> specifies after how many seconds (fractional values are 241C<after> specifies after how many seconds (fractional values are
228supported) the callback should be invoked. C<cb> is the callback to invoke 242supported) the callback should be invoked. C<cb> is the callback to invoke
354Note that updating the time I<might> cause some events to be handled. 368Note that updating the time I<might> cause some events to be handled.
355 369
356=back 370=back
357 371
358=head2 SIGNAL WATCHERS 372=head2 SIGNAL WATCHERS
373
374 $w = AnyEvent->signal (signal => <uppercase_signal_name>, cb => <callback>);
359 375
360You can watch for signals using a signal watcher, C<signal> is the signal 376You can watch for signals using a signal watcher, C<signal> is the signal
361I<name> in uppercase and without any C<SIG> prefix, C<cb> is the Perl 377I<name> in uppercase and without any C<SIG> prefix, C<cb> is the Perl
362callback to be invoked whenever a signal occurs. 378callback to be invoked whenever a signal occurs.
363 379
397event loops such as L<Event> or L<Event::Lib> (and not with L<POE> 413event loops such as L<Event> or L<Event::Lib> (and not with L<POE>
398currently, as POE does it's own workaround with one-second latency). With 414currently, as POE does it's own workaround with one-second latency). With
399those, you just have to suffer the delays. 415those, you just have to suffer the delays.
400 416
401=head2 CHILD PROCESS WATCHERS 417=head2 CHILD PROCESS WATCHERS
418
419 $w = AnyEvent->child (pid => <process id>, cb => <callback>);
402 420
403You can also watch on a child process exit and catch its exit status. 421You can also watch on a child process exit and catch its exit status.
404 422
405The child process is specified by the C<pid> argument (one some backends, 423The child process is specified by the C<pid> argument (one some backends,
406using C<0> watches for any child process exit, on others this will 424using C<0> watches for any child process exit, on others this will
455 # do something else, then wait for process exit 473 # do something else, then wait for process exit
456 $done->recv; 474 $done->recv;
457 475
458=head2 IDLE WATCHERS 476=head2 IDLE WATCHERS
459 477
478 $w = AnyEvent->idle (cb => <callback>);
479
460Sometimes there is a need to do something, but it is not so important 480Sometimes there is a need to do something, but it is not so important
461to do it instantly, but only when there is nothing better to do. This 481to do it instantly, but only when there is nothing better to do. This
462"nothing better to do" is usually defined to be "no other events need 482"nothing better to do" is usually defined to be "no other events need
463attention by the event loop". 483attention by the event loop".
464 484
490 }); 510 });
491 }); 511 });
492 512
493=head2 CONDITION VARIABLES 513=head2 CONDITION VARIABLES
494 514
515 $cv = AnyEvent->condvar;
516
517 $cv->send (<list>);
518 my @res = $cv->recv;
519
495If you are familiar with some event loops you will know that all of them 520If you are familiar with some event loops you will know that all of them
496require you to run some blocking "loop", "run" or similar function that 521require you to run some blocking "loop", "run" or similar function that
497will actively watch for new events and call your callbacks. 522will actively watch for new events and call your callbacks.
498 523
499AnyEvent is slightly different: it expects somebody else to run the event 524AnyEvent is slightly different: it expects somebody else to run the event
1086 1111
1087BEGIN { AnyEvent::common_sense } 1112BEGIN { AnyEvent::common_sense }
1088 1113
1089use Carp (); 1114use Carp ();
1090 1115
1091our $VERSION = 4.87; 1116our $VERSION = 4.881;
1092our $MODEL; 1117our $MODEL;
1093 1118
1094our $AUTOLOAD; 1119our $AUTOLOAD;
1095our @ISA; 1120our @ISA;
1096 1121
1289} 1314}
1290 1315
1291# default implementation for ->signal 1316# default implementation for ->signal
1292 1317
1293our $HAVE_ASYNC_INTERRUPT; 1318our $HAVE_ASYNC_INTERRUPT;
1319
1320sub _have_async_interrupt() {
1321 $HAVE_ASYNC_INTERRUPT = 1*(!$ENV{PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT}
1322 && eval "use Async::Interrupt 1.0 (); 1")
1323 unless defined $HAVE_ASYNC_INTERRUPT;
1324
1325 $HAVE_ASYNC_INTERRUPT
1326}
1327
1294our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO); 1328our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO);
1295our (%SIG_ASY, %SIG_ASY_W); 1329our (%SIG_ASY, %SIG_ASY_W);
1296our ($SIG_COUNT, $SIG_TW); 1330our ($SIG_COUNT, $SIG_TW);
1297 1331
1298sub _signal_exec { 1332sub _signal_exec {
1306 $_->() for values %{ $SIG_CB{$_} || {} }; 1340 $_->() for values %{ $SIG_CB{$_} || {} };
1307 } 1341 }
1308 } 1342 }
1309} 1343}
1310 1344
1311# install a dumym wakeupw atcher to reduce signal catching latency 1345# install a dummy wakeup watcher to reduce signal catching latency
1312sub _sig_add() { 1346sub _sig_add() {
1313 unless ($SIG_COUNT++) { 1347 unless ($SIG_COUNT++) {
1314 # try to align timer on a full-second boundary, if possible 1348 # try to align timer on a full-second boundary, if possible
1315 my $NOW = AnyEvent->now; 1349 my $NOW = AnyEvent->now;
1316 1350
1325sub _sig_del { 1359sub _sig_del {
1326 undef $SIG_TW 1360 undef $SIG_TW
1327 unless --$SIG_COUNT; 1361 unless --$SIG_COUNT;
1328} 1362}
1329 1363
1364our $_sig_name_init; $_sig_name_init = sub {
1365 eval q{ # poor man's autoloading
1366 undef $_sig_name_init;
1367
1368 if (_have_async_interrupt) {
1369 *sig2num = \&Async::Interrupt::sig2num;
1370 *sig2name = \&Async::Interrupt::sig2name;
1371 } else {
1372 require Config;
1373
1374 my %signame2num;
1375 @signame2num{ split ' ', $Config::Config{sig_name} }
1376 = split ' ', $Config::Config{sig_num};
1377
1378 my @signum2name;
1379 @signum2name[values %signame2num] = keys %signame2num;
1380
1381 *sig2num = sub($) {
1382 $_[0] > 0 ? shift : $signame2num{+shift}
1383 };
1384 *sig2name = sub ($) {
1385 $_[0] > 0 ? $signum2name[+shift] : shift
1386 };
1387 }
1388 };
1389 die if $@;
1390};
1391
1392sub sig2num ($) { &$_sig_name_init; &sig2num }
1393sub sig2name($) { &$_sig_name_init; &sig2name }
1394
1330sub _signal { 1395sub signal {
1396 eval q{ # poor man's autoloading {}
1397 # probe for availability of Async::Interrupt
1398 if (_have_async_interrupt) {
1399 warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8;
1400
1401 $SIGPIPE_R = new Async::Interrupt::EventPipe;
1402 $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R->fileno, poll => "r", cb => \&_signal_exec);
1403
1404 } else {
1405 warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8;
1406
1407 require Fcntl;
1408
1409 if (AnyEvent::WIN32) {
1410 require AnyEvent::Util;
1411
1412 ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe ();
1413 AnyEvent::Util::fh_nonblocking ($SIGPIPE_R, 1) if $SIGPIPE_R;
1414 AnyEvent::Util::fh_nonblocking ($SIGPIPE_W, 1) if $SIGPIPE_W; # just in case
1415 } else {
1416 pipe $SIGPIPE_R, $SIGPIPE_W;
1417 fcntl $SIGPIPE_R, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_R;
1418 fcntl $SIGPIPE_W, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_W; # just in case
1419
1420 # not strictly required, as $^F is normally 2, but let's make sure...
1421 fcntl $SIGPIPE_R, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC;
1422 fcntl $SIGPIPE_W, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC;
1423 }
1424
1425 $SIGPIPE_R
1426 or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n";
1427
1428 $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R, poll => "r", cb => \&_signal_exec);
1429 }
1430
1431 *signal = sub {
1331 my (undef, %arg) = @_; 1432 my (undef, %arg) = @_;
1332 1433
1333 my $signal = uc $arg{signal} 1434 my $signal = uc $arg{signal}
1334 or Carp::croak "required option 'signal' is missing"; 1435 or Carp::croak "required option 'signal' is missing";
1335 1436
1336 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
1337
1338 if ($HAVE_ASYNC_INTERRUPT) { 1437 if ($HAVE_ASYNC_INTERRUPT) {
1339 # async::interrupt 1438 # async::interrupt
1340 1439
1341 $SIG_ASY{$signal} ||= do { 1440 $signal = sig2num $signal;
1342 my $asy = new Async::Interrupt 1441 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
1442
1443 $SIG_ASY{$signal} ||= new Async::Interrupt
1343 cb => sub { undef $SIG_EV{$signal} }, 1444 cb => sub { undef $SIG_EV{$signal} },
1344 signal => $signal, 1445 signal => $signal,
1345 pipe => [$SIGPIPE_R->filenos], 1446 pipe => [$SIGPIPE_R->filenos],
1447 pipe_autodrain => 0,
1448 ;
1449
1450 } else {
1451 # pure perl
1452
1453 # AE::Util has been loaded in signal
1454 $signal = sig2name $signal;
1455 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
1456
1457 $SIG{$signal} ||= sub {
1458 local $!;
1459 syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV;
1460 undef $SIG_EV{$signal};
1461 };
1462
1463 # can't do signal processing without introducing races in pure perl,
1464 # so limit the signal latency.
1465 _sig_add;
1346 ; 1466 }
1347 $asy->pipe_autodrain (0);
1348 1467
1349 $asy 1468 bless [$signal, $arg{cb}], "AnyEvent::Base::signal"
1350 }; 1469 };
1351 1470
1352 } else { 1471 *AnyEvent::Base::signal::DESTROY = sub {
1353 # pure perl 1472 my ($signal, $cb) = @{$_[0]};
1354 1473
1355 $SIG{$signal} ||= sub { 1474 _sig_del;
1356 local $!; 1475
1357 syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; 1476 delete $SIG_CB{$signal}{$cb};
1477
1478 $HAVE_ASYNC_INTERRUPT
1479 ? delete $SIG_ASY{$signal}
1480 : # delete doesn't work with older perls - they then
1481 # print weird messages, or just unconditionally exit
1482 # instead of getting the default action.
1358 undef $SIG_EV{$signal}; 1483 undef $SIG{$signal}
1484 unless keys %{ $SIG_CB{$signal} };
1359 }; 1485 };
1360
1361 # can't do signal processing without introducing races in pure perl,
1362 # so limit the signal latency.
1363 _sig_add;
1364 } 1486 };
1365 1487 die if $@;
1366 bless [$signal, $arg{cb}], "AnyEvent::Base::signal"
1367}
1368
1369sub signal {
1370 # probe for availability of Async::Interrupt
1371 if (!$ENV{PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT} && eval "use Async::Interrupt 0.6 (); 1") {
1372 warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8;
1373
1374 $HAVE_ASYNC_INTERRUPT = 1;
1375 $SIGPIPE_R = new Async::Interrupt::EventPipe;
1376 $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R->fileno, poll => "r", cb => \&_signal_exec);
1377
1378 } else {
1379 warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8;
1380
1381 require Fcntl;
1382
1383 if (AnyEvent::WIN32) {
1384 require AnyEvent::Util;
1385
1386 ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe ();
1387 AnyEvent::Util::fh_nonblocking ($SIGPIPE_R) if $SIGPIPE_R;
1388 AnyEvent::Util::fh_nonblocking ($SIGPIPE_W) if $SIGPIPE_W; # just in case
1389 } else {
1390 pipe $SIGPIPE_R, $SIGPIPE_W;
1391 fcntl $SIGPIPE_R, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_R;
1392 fcntl $SIGPIPE_W, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_W; # just in case
1393
1394 # not strictly required, as $^F is normally 2, but let's make sure...
1395 fcntl $SIGPIPE_R, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC;
1396 fcntl $SIGPIPE_W, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC;
1397 }
1398
1399 $SIGPIPE_R
1400 or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n";
1401
1402 $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R, poll => "r", cb => \&_signal_exec);
1403 }
1404
1405 *signal = \&_signal;
1406 &signal 1488 &signal
1407}
1408
1409sub AnyEvent::Base::signal::DESTROY {
1410 my ($signal, $cb) = @{$_[0]};
1411
1412 _sig_del;
1413
1414 delete $SIG_CB{$signal}{$cb};
1415
1416 $HAVE_ASYNC_INTERRUPT
1417 ? delete $SIG_ASY{$signal}
1418 : # delete doesn't work with older perls - they then
1419 # print weird messages, or just unconditionally exit
1420 # instead of getting the default action.
1421 undef $SIG{$signal}
1422 unless keys %{ $SIG_CB{$signal} };
1423} 1489}
1424 1490
1425# default implementation for ->child 1491# default implementation for ->child
1426 1492
1427our %PID_CB; 1493our %PID_CB;
1581} 1647}
1582 1648
1583# undocumented/compatibility with pre-3.4 1649# undocumented/compatibility with pre-3.4
1584*broadcast = \&send; 1650*broadcast = \&send;
1585*wait = \&_wait; 1651*wait = \&_wait;
1652
1653#############################################################################
1654# "new" API, currently only emulation of it
1655#############################################################################
1656
1657package AE;
1658
1659sub io($$$) {
1660 AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2])
1661}
1662
1663sub timer($$$) {
1664 AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]);
1665}
1666
1667sub signal($$) {
1668 AnyEvent->signal (signal => $_[0], cb => $_[1]);
1669}
1670
1671sub child($$) {
1672 AnyEvent->child (pid => $_[0], cb => $_[1]);
1673}
1674
1675sub idle($) {
1676 AnyEvent->idle (cb => $_[0]);
1677}
1678
1679sub cv() {
1680 AnyEvent->condvar
1681}
1682
1683sub now() {
1684 AnyEvent->now
1685}
1686
1687sub now_update() {
1688 AnyEvent->now_update
1689}
1690
1691sub time() {
1692 AnyEvent->time
1693}
1586 1694
1587=head1 ERROR AND EXCEPTION HANDLING 1695=head1 ERROR AND EXCEPTION HANDLING
1588 1696
1589In general, AnyEvent does not do any error handling - it relies on the 1697In general, AnyEvent does not do any error handling - it relies on the
1590caller to do that if required. The L<AnyEvent::Strict> module (see also 1698caller to do that if required. The L<AnyEvent::Strict> module (see also

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines