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.263 by root, Wed Jul 29 12:39:21 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
1334sub _sig_del { 1359sub _sig_del {
1335 undef $SIG_TW 1360 undef $SIG_TW
1336 unless --$SIG_COUNT; 1361 unless --$SIG_COUNT;
1337} 1362}
1338 1363
1339our %SIGNAME2NUM;
1340our @SIGNUM2NAME;
1341our $_sig_name_init; $_sig_name_init = sub { 1364our $_sig_name_init; $_sig_name_init = sub {
1365 eval q{ # poor man's autoloading
1342 undef $_sig_name_init; 1366 undef $_sig_name_init;
1343 1367
1344 if (_have_async_interrupt) { 1368 if (_have_async_interrupt) {
1345 *sig2num = \&Async::Interrupt::sig2num; 1369 *sig2num = \&Async::Interrupt::sig2num;
1346 *sig2name = \&Async::Interrupt::sig2name; 1370 *sig2name = \&Async::Interrupt::sig2name;
1347 } else { 1371 } else {
1348 require Config; 1372 require Config;
1349 1373
1374 my %signame2num;
1350 @SIGNAME2NUM{ split ' ', $Config::Config{sig_name} } 1375 @signame2num{ split ' ', $Config::Config{sig_name} }
1351 = split ' ', $Config::Config{sig_num}; 1376 = split ' ', $Config::Config{sig_num};
1352 @SIGNUM2NAME[values %SIGNAME2NUM] = keys %SIGNAME2NUM;
1353 1377
1378 my @signum2name;
1379 @signum2name[values %signame2num] = keys %signame2num;
1380
1354 *sig2num = sub($) { 1381 *sig2num = sub($) {
1355 $_[0] > 0 ? shift : $SIGNAME2NUM{+shift} 1382 $_[0] > 0 ? shift : $signame2num{+shift}
1356 }; 1383 };
1357 *sig2name = sub ($) { 1384 *sig2name = sub ($) {
1358 $_[0] > 0 ? $SIGNUM2NAME[+shift] : shift 1385 $_[0] > 0 ? $signum2name[+shift] : shift
1386 };
1359 }; 1387 }
1360 } 1388 };
1389 die if $@;
1361}; 1390};
1362 1391
1363sub sig2num ($) { &$_sig_name_init; &sig2num } 1392sub sig2num ($) { &$_sig_name_init; &sig2num }
1364sub sig2name($) { &$_sig_name_init; &sig2name } 1393sub sig2name($) { &$_sig_name_init; &sig2name }
1365 1394
1366sub _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 {
1367 my (undef, %arg) = @_; 1432 my (undef, %arg) = @_;
1368 1433
1369 my $signal = uc $arg{signal} 1434 my $signal = uc $arg{signal}
1370 or Carp::croak "required option 'signal' is missing"; 1435 or Carp::croak "required option 'signal' is missing";
1371 1436
1372 if ($HAVE_ASYNC_INTERRUPT) { 1437 if ($HAVE_ASYNC_INTERRUPT) {
1373 # async::interrupt 1438 # async::interrupt
1374 1439
1375 $signal = sig2num $signal; 1440 $signal = sig2num $signal;
1376 $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; 1441 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
1377 1442
1378 $SIG_ASY{$signal} ||= new Async::Interrupt 1443 $SIG_ASY{$signal} ||= new Async::Interrupt
1379 cb => sub { undef $SIG_EV{$signal} }, 1444 cb => sub { undef $SIG_EV{$signal} },
1380 signal => $signal, 1445 signal => $signal,
1381 pipe => [$SIGPIPE_R->filenos], 1446 pipe => [$SIGPIPE_R->filenos],
1382 pipe_autodrain => 0, 1447 pipe_autodrain => 0,
1383 ; 1448 ;
1384 1449
1385 } else { 1450 } else {
1386 # pure perl 1451 # pure perl
1387 1452
1388 # AE::Util has been loaded in signal 1453 # AE::Util has been loaded in signal
1389 $signal = sig2name $signal; 1454 $signal = sig2name $signal;
1390 $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; 1455 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
1391 1456
1392 $SIG{$signal} ||= sub { 1457 $SIG{$signal} ||= sub {
1393 local $!; 1458 local $!;
1394 syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; 1459 syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV;
1395 undef $SIG_EV{$signal}; 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;
1466 }
1467
1468 bless [$signal, $arg{cb}], "AnyEvent::Base::signal"
1396 }; 1469 };
1397 1470
1398 # can't do signal processing without introducing races in pure perl, 1471 *AnyEvent::Base::signal::DESTROY = sub {
1399 # so limit the signal latency. 1472 my ($signal, $cb) = @{$_[0]};
1473
1400 _sig_add; 1474 _sig_del;
1401 }
1402 1475
1403 bless [$signal, $arg{cb}], "AnyEvent::Base::signal" 1476 delete $SIG_CB{$signal}{$cb};
1404}
1405 1477
1406sub signal { 1478 $HAVE_ASYNC_INTERRUPT
1407 # probe for availability of Async::Interrupt 1479 ? delete $SIG_ASY{$signal}
1408 if (_have_async_interrupt) { 1480 : # delete doesn't work with older perls - they then
1409 warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8; 1481 # print weird messages, or just unconditionally exit
1410 1482 # instead of getting the default action.
1411 $SIGPIPE_R = new Async::Interrupt::EventPipe; 1483 undef $SIG{$signal}
1412 $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R->fileno, poll => "r", cb => \&_signal_exec); 1484 unless keys %{ $SIG_CB{$signal} };
1413
1414 } else {
1415 warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8;
1416
1417 require Fcntl;
1418
1419 if (AnyEvent::WIN32) {
1420 require AnyEvent::Util;
1421
1422 ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe ();
1423 AnyEvent::Util::fh_nonblocking ($SIGPIPE_R) if $SIGPIPE_R;
1424 AnyEvent::Util::fh_nonblocking ($SIGPIPE_W) if $SIGPIPE_W; # just in case
1425 } else {
1426 pipe $SIGPIPE_R, $SIGPIPE_W;
1427 fcntl $SIGPIPE_R, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_R;
1428 fcntl $SIGPIPE_W, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_W; # just in case
1429
1430 # not strictly required, as $^F is normally 2, but let's make sure...
1431 fcntl $SIGPIPE_R, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC;
1432 fcntl $SIGPIPE_W, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC;
1433 } 1485 };
1434
1435 $SIGPIPE_R
1436 or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n";
1437
1438 $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R, poll => "r", cb => \&_signal_exec);
1439 } 1486 };
1440 1487 die if $@;
1441 *signal = \&_signal;
1442 &signal 1488 &signal
1443}
1444
1445sub AnyEvent::Base::signal::DESTROY {
1446 my ($signal, $cb) = @{$_[0]};
1447
1448 _sig_del;
1449
1450 delete $SIG_CB{$signal}{$cb};
1451
1452 $HAVE_ASYNC_INTERRUPT
1453 ? delete $SIG_ASY{$signal}
1454 : # delete doesn't work with older perls - they then
1455 # print weird messages, or just unconditionally exit
1456 # instead of getting the default action.
1457 undef $SIG{$signal}
1458 unless keys %{ $SIG_CB{$signal} };
1459} 1489}
1460 1490
1461# default implementation for ->child 1491# default implementation for ->child
1462 1492
1463our %PID_CB; 1493our %PID_CB;
1617} 1647}
1618 1648
1619# undocumented/compatibility with pre-3.4 1649# undocumented/compatibility with pre-3.4
1620*broadcast = \&send; 1650*broadcast = \&send;
1621*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}
1622 1694
1623=head1 ERROR AND EXCEPTION HANDLING 1695=head1 ERROR AND EXCEPTION HANDLING
1624 1696
1625In 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
1626caller 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