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.261 by root, Tue Jul 28 12:56:17 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
489 } 509 }
490 }); 510 });
491 }); 511 });
492 512
493=head2 CONDITION VARIABLES 513=head2 CONDITION VARIABLES
514
515 $cv = AnyEvent->condvar;
516
517 $cv->send (<list>);
518 my @res = $cv->recv;
494 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
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 {
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 if ($HAVE_ASYNC_INTERRUPT) { 1437 if ($HAVE_ASYNC_INTERRUPT) {
1337 # async::interrupt 1438 # async::interrupt
1338 1439
1339 $signal = Async::Interrupt::sig2num ($signal); 1440 $signal = sig2num $signal;
1340 $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; 1441 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
1341 1442
1342 $SIG_ASY{$signal} ||= do { 1443 $SIG_ASY{$signal} ||= new Async::Interrupt
1343 my $asy = new Async::Interrupt
1344 cb => sub { undef $SIG_EV{$signal} }, 1444 cb => sub { undef $SIG_EV{$signal} },
1345 signal => $signal, 1445 signal => $signal,
1346 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;
1347 ; 1466 }
1348 $asy->pipe_autodrain (0);
1349 1467
1350 $asy 1468 bless [$signal, $arg{cb}], "AnyEvent::Base::signal"
1351 }; 1469 };
1352 1470
1353 } else { 1471 *AnyEvent::Base::signal::DESTROY = sub {
1354 # pure perl 1472 my ($signal, $cb) = @{$_[0]};
1355 1473
1356 # AE::Util has been loaded in signal 1474 _sig_del;
1357 $signal = AnyEvent::Util::sig2name ($signal);
1358 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
1359 1475
1360 $SIG{$signal} ||= sub { 1476 delete $SIG_CB{$signal}{$cb};
1361 local $!; 1477
1362 syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; 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.
1363 undef $SIG_EV{$signal}; 1483 undef $SIG{$signal}
1484 unless keys %{ $SIG_CB{$signal} };
1364 }; 1485 };
1365
1366 # can't do signal processing without introducing races in pure perl,
1367 # so limit the signal latency.
1368 _sig_add;
1369 } 1486 };
1370 1487 die if $@;
1371 bless [$signal, $arg{cb}], "AnyEvent::Base::signal"
1372}
1373
1374sub signal {
1375 # probe for availability of Async::Interrupt
1376 if (!$ENV{PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT} && eval "use Async::Interrupt 1.0 (); 1") {
1377 warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8;
1378
1379 $HAVE_ASYNC_INTERRUPT = 1;
1380 $SIGPIPE_R = new Async::Interrupt::EventPipe;
1381 $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R->fileno, poll => "r", cb => \&_signal_exec);
1382
1383 } else {
1384 warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8;
1385
1386 require Fcntl;
1387
1388 if (AnyEvent::WIN32) {
1389 require AnyEvent::Util;
1390
1391 ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe ();
1392 AnyEvent::Util::fh_nonblocking ($SIGPIPE_R) if $SIGPIPE_R;
1393 AnyEvent::Util::fh_nonblocking ($SIGPIPE_W) if $SIGPIPE_W; # just in case
1394 } else {
1395 pipe $SIGPIPE_R, $SIGPIPE_W;
1396 fcntl $SIGPIPE_R, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_R;
1397 fcntl $SIGPIPE_W, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_W; # just in case
1398
1399 # not strictly required, as $^F is normally 2, but let's make sure...
1400 fcntl $SIGPIPE_R, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC;
1401 fcntl $SIGPIPE_W, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC;
1402 }
1403
1404 $SIGPIPE_R
1405 or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n";
1406
1407 $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R, poll => "r", cb => \&_signal_exec);
1408 }
1409
1410 *signal = \&_signal;
1411 &signal 1488 &signal
1412}
1413
1414sub AnyEvent::Base::signal::DESTROY {
1415 my ($signal, $cb) = @{$_[0]};
1416
1417 _sig_del;
1418
1419 delete $SIG_CB{$signal}{$cb};
1420
1421 $HAVE_ASYNC_INTERRUPT
1422 ? delete $SIG_ASY{$signal}
1423 : # delete doesn't work with older perls - they then
1424 # print weird messages, or just unconditionally exit
1425 # instead of getting the default action.
1426 undef $SIG{$signal}
1427 unless keys %{ $SIG_CB{$signal} };
1428} 1489}
1429 1490
1430# default implementation for ->child 1491# default implementation for ->child
1431 1492
1432our %PID_CB; 1493our %PID_CB;
1586} 1647}
1587 1648
1588# undocumented/compatibility with pre-3.4 1649# undocumented/compatibility with pre-3.4
1589*broadcast = \&send; 1650*broadcast = \&send;
1590*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}
1591 1694
1592=head1 ERROR AND EXCEPTION HANDLING 1695=head1 ERROR AND EXCEPTION HANDLING
1593 1696
1594In 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
1595caller 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