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