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.262 by root, Tue Jul 28 13:08: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} ||= new Async::Interrupt 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],
1346 pipe_autodrain => 0, 1447 pipe_autodrain => 0,
1347 ; 1448 ;
1348 1449
1349 } else { 1450 } else {
1350 # pure perl 1451 # pure perl
1351 1452
1352 # AE::Util has been loaded in signal 1453 # AE::Util has been loaded in signal
1353 $signal = AnyEvent::Util::sig2name ($signal); 1454 $signal = sig2name $signal;
1354 $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; 1455 $SIG_CB{$signal}{$arg{cb}} = $arg{cb};
1355 1456
1356 $SIG{$signal} ||= sub { 1457 $SIG{$signal} ||= sub {
1357 local $!; 1458 local $!;
1358 syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; 1459 syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV;
1359 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"
1360 }; 1469 };
1361 1470
1362 # can't do signal processing without introducing races in pure perl, 1471 *AnyEvent::Base::signal::DESTROY = sub {
1363 # so limit the signal latency. 1472 my ($signal, $cb) = @{$_[0]};
1473
1364 _sig_add; 1474 _sig_del;
1365 }
1366 1475
1367 bless [$signal, $arg{cb}], "AnyEvent::Base::signal" 1476 delete $SIG_CB{$signal}{$cb};
1368}
1369 1477
1370sub signal {
1371 # probe for availability of Async::Interrupt
1372 if (!$ENV{PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT} && eval "use Async::Interrupt 1.0 (); 1") {
1373 warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8;
1374
1375 $HAVE_ASYNC_INTERRUPT = 1; 1478 $HAVE_ASYNC_INTERRUPT
1376 $SIGPIPE_R = new Async::Interrupt::EventPipe; 1479 ? delete $SIG_ASY{$signal}
1377 $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R->fileno, poll => "r", cb => \&_signal_exec); 1480 : # delete doesn't work with older perls - they then
1378 1481 # print weird messages, or just unconditionally exit
1379 } else { 1482 # instead of getting the default action.
1380 warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; 1483 undef $SIG{$signal}
1381 1484 unless keys %{ $SIG_CB{$signal} };
1382 require Fcntl;
1383
1384 if (AnyEvent::WIN32) {
1385 require AnyEvent::Util;
1386
1387 ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe ();
1388 AnyEvent::Util::fh_nonblocking ($SIGPIPE_R) if $SIGPIPE_R;
1389 AnyEvent::Util::fh_nonblocking ($SIGPIPE_W) if $SIGPIPE_W; # just in case
1390 } else {
1391 pipe $SIGPIPE_R, $SIGPIPE_W;
1392 fcntl $SIGPIPE_R, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_R;
1393 fcntl $SIGPIPE_W, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_W; # just in case
1394
1395 # not strictly required, as $^F is normally 2, but let's make sure...
1396 fcntl $SIGPIPE_R, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC;
1397 fcntl $SIGPIPE_W, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC;
1398 } 1485 };
1399
1400 $SIGPIPE_R
1401 or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n";
1402
1403 $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R, poll => "r", cb => \&_signal_exec);
1404 } 1486 };
1405 1487 die if $@;
1406 *signal = \&_signal;
1407 &signal 1488 &signal
1408}
1409
1410sub AnyEvent::Base::signal::DESTROY {
1411 my ($signal, $cb) = @{$_[0]};
1412
1413 _sig_del;
1414
1415 delete $SIG_CB{$signal}{$cb};
1416
1417 $HAVE_ASYNC_INTERRUPT
1418 ? delete $SIG_ASY{$signal}
1419 : # delete doesn't work with older perls - they then
1420 # print weird messages, or just unconditionally exit
1421 # instead of getting the default action.
1422 undef $SIG{$signal}
1423 unless keys %{ $SIG_CB{$signal} };
1424} 1489}
1425 1490
1426# default implementation for ->child 1491# default implementation for ->child
1427 1492
1428our %PID_CB; 1493our %PID_CB;
1582} 1647}
1583 1648
1584# undocumented/compatibility with pre-3.4 1649# undocumented/compatibility with pre-3.4
1585*broadcast = \&send; 1650*broadcast = \&send;
1586*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}
1587 1694
1588=head1 ERROR AND EXCEPTION HANDLING 1695=head1 ERROR AND EXCEPTION HANDLING
1589 1696
1590In 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
1591caller 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