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.352 by root, Thu Aug 4 09:14:01 2011 UTC vs.
Revision 1.353 by root, Thu Aug 11 20:56:07 2011 UTC

1004 } else { 1004 } else {
1005 # AnyEvent not yet initialised, so make sure to load Coro::AnyEvent 1005 # AnyEvent not yet initialised, so make sure to load Coro::AnyEvent
1006 # as soon as it is 1006 # as soon as it is
1007 push @AnyEvent::post_detect, sub { require Coro::AnyEvent }; 1007 push @AnyEvent::post_detect, sub { require Coro::AnyEvent };
1008 } 1008 }
1009
1010=item AnyEvent::postpone BLOCK
1011
1012Arranges for the block to be executed as soon as possible, but not before
1013the call itself returns. In practise, the block will be executed just
1014before the event loop polls for new events, or shortly afterwards.
1015
1016This function never returns anything (to make the C<return postpone { ...
1017}> idiom more useful.
1018
1019To understand the usefulness of this function, consider a function that
1020asynchronously does something for you and returns some transaction
1021object or guard to let you cancel the operation. For example,
1022C<AnyEvent::Socket::tcp_connect>:
1023
1024 # start a conenction attempt unless one is active
1025 $self->{connect_guard} ||= AnyEvent::Socket::tcp_connect "www.example.net", 80, sub {
1026 delete $self->{connect_guard};
1027 ...
1028 };
1029
1030Imagine that this function could instantly call the callback, for
1031example, because it detects an obvious error such as a negative port
1032number. Invoking the callback before the function returns causes problems
1033however: the callback will be called and will try to delete the guard
1034object. But since the function hasn't returned yet, there is nothing to
1035delete. When the function eventually returns it will assign the guard
1036object to C<< $self->{connect_guard} >>, where it will likely never be
1037deleted, so the program thinks it is still trying to connect.
1038
1039This is where C<AnyEvent::postpone> should be used. Instead of calling the
1040callback directly on error:
1041
1042 $cb->(undef), return # signal error to callback, BAD!
1043 if $some_error_condition;
1044
1045It should use C<postpone>:
1046
1047 AnyEvent::postpone { $cb->(undef) }, return # signal error to callback, later
1048 if $some_error_condition;
1009 1049
1010=back 1050=back
1011 1051
1012=head1 WHAT TO DO IN A MODULE 1052=head1 WHAT TO DO IN A MODULE
1013 1053
1326 1366
1327 my $class = shift; 1367 my $class = shift;
1328 $class->$func (@_); 1368 $class->$func (@_);
1329} 1369}
1330 1370
1371our $POSTPONE_W;
1372our @POSTPONE;
1373
1374sub _postpone_exec {
1375 undef $POSTPONE_W;
1376 (pop @POSTPONE)->()
1377 while @POSTPONE;
1378}
1379
1380sub postpone(&) {
1381 push @POSTPONE, shift;
1382
1383 $POSTPONE_W ||= AE::timer (0, 0, \&_postpone_exec);
1384
1385 ()
1386}
1387
1331# utility function to dup a filehandle. this is used by many backends 1388# utility function to dup a filehandle. this is used by many backends
1332# to support binding more than one watcher per filehandle (they usually 1389# to support binding more than one watcher per filehandle (they usually
1333# allow only one watcher per fd, so we dup it to get a different one). 1390# allow only one watcher per fd, so we dup it to get a different one).
1334sub _dupfh($$;$$) { 1391sub _dupfh($$;$$) {
1335 my ($poll, $fh, $r, $w) = @_; 1392 my ($poll, $fh, $r, $w) = @_;
1428sub _poll { 1485sub _poll {
1429 Carp::croak "$AnyEvent::MODEL does not support blocking waits. Caught"; 1486 Carp::croak "$AnyEvent::MODEL does not support blocking waits. Caught";
1430} 1487}
1431 1488
1432# default implementation for ->condvar 1489# default implementation for ->condvar
1433# in fact,t he default should not be overwritten 1490# in fact, the default should not be overwritten
1434 1491
1435sub condvar { 1492sub condvar {
1436 eval q{ # poor man's autoloading {} 1493 eval q{ # poor man's autoloading {}
1437 *condvar = sub { 1494 *condvar = sub {
1438 bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" 1495 bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar"

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines