… | |
… | |
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 | |
|
|
1012 | Arranges for the block to be executed as soon as possible, but not before |
|
|
1013 | the call itself returns. In practise, the block will be executed just |
|
|
1014 | before the event loop polls for new events, or shortly afterwards. |
|
|
1015 | |
|
|
1016 | This function never returns anything (to make the C<return postpone { ... |
|
|
1017 | }> idiom more useful. |
|
|
1018 | |
|
|
1019 | To understand the usefulness of this function, consider a function that |
|
|
1020 | asynchronously does something for you and returns some transaction |
|
|
1021 | object or guard to let you cancel the operation. For example, |
|
|
1022 | C<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 | |
|
|
1030 | Imagine that this function could instantly call the callback, for |
|
|
1031 | example, because it detects an obvious error such as a negative port |
|
|
1032 | number. Invoking the callback before the function returns causes problems |
|
|
1033 | however: the callback will be called and will try to delete the guard |
|
|
1034 | object. But since the function hasn't returned yet, there is nothing to |
|
|
1035 | delete. When the function eventually returns it will assign the guard |
|
|
1036 | object to C<< $self->{connect_guard} >>, where it will likely never be |
|
|
1037 | deleted, so the program thinks it is still trying to connect. |
|
|
1038 | |
|
|
1039 | This is where C<AnyEvent::postpone> should be used. Instead of calling the |
|
|
1040 | callback directly on error: |
|
|
1041 | |
|
|
1042 | $cb->(undef), return # signal error to callback, BAD! |
|
|
1043 | if $some_error_condition; |
|
|
1044 | |
|
|
1045 | It 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 | |
|
|
1371 | our $POSTPONE_W; |
|
|
1372 | our @POSTPONE; |
|
|
1373 | |
|
|
1374 | sub _postpone_exec { |
|
|
1375 | undef $POSTPONE_W; |
|
|
1376 | (pop @POSTPONE)->() |
|
|
1377 | while @POSTPONE; |
|
|
1378 | } |
|
|
1379 | |
|
|
1380 | sub 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). |
1334 | sub _dupfh($$;$$) { |
1391 | sub _dupfh($$;$$) { |
1335 | my ($poll, $fh, $r, $w) = @_; |
1392 | my ($poll, $fh, $r, $w) = @_; |
… | |
… | |
1428 | sub _poll { |
1485 | sub _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 | |
1435 | sub condvar { |
1492 | sub 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" |