SYNOPSIS

 my $future = CPS::Future->new;
 $future->on_ready( sub {
    say "The operation is complete";
 } );

 kperform_some_operation( sub {
    $future->done( @_ );
 } );

DESCRIPTION

An \*(C`CPS::Future\*(C' object represents an operation that is currently in progress, or has recently completed. It can be used in a variety of ways to manage the flow of control, and data, through an asynchronous program.

Some futures represent a single operation (returned by the \*(C`new\*(C' constructor), and are explicitly marked as ready by calling the \*(C`done\*(C' method. Others represent a tree of sub-tasks (returned by the \*(C`wait_all\*(C' or \*(C`needs_all\*(C' constructors), and are implicitly marked as ready when all of their component futures are ready.

It is intended that library functions that perform asynchonous operations would use \*(C`CPS::Future\*(C' objects to represent outstanding operations, and allow their calling programs to control or wait for these operations to complete. The implementation and the user of such an interface would typically make use of different methods on the class. The methods below are documented in two sections; those of interest to each side of the interface.

CONSTRUCTORS

Returns a new \*(C`CPS::Future\*(C' instance to represent a leaf future. It will be marked as ready by any of the \*(C`done\*(C', \*(C`fail\*(C', or \*(C`cancel\*(C' methods.

This constructor would primarily be used by implementations of asynchronous interfaces. Returns a new \*(C`CPS::Future\*(C' instance that will indicate it is ready once all of the sub future objects given to it indicate that they are ready.

This constructor would primarily be used by users of asynchronous interfaces. Returns a new \*(C`CPS::Future\*(C' instance that will indicate it is ready once all of the sub future objects given to it indicate that they have completed successfully, or when any of them indicates that they have failed. If any sub future fails, then this will fail immediately, and the remaining subs not yet ready will be cancelled.

This constructor would primarily be used by users of asynchronous interfaces. Returns a new \*(C`CPS::Future\*(C' instance that allows a sequence of dependent operations to be performed. Once $f1 indicates a successful completion, the code reference will be invoked and is passed one argument, being $f1. It should return a new future, $f2. Once $f2 indicates completion the combined future $future will then be marked as complete. The result of calling \*(C`get\*(C' on the combined future will return whatever was passed to the \*(C`done\*(C' method of $f2.

$f2 = $code->( $f1 )

If $f1 fails then $future will indicate this failure immediately and the block of code will not be invoked.

If $future is cancelled before $f1 completes, then $f1 will be cancelled. If it is cancelled after completion then $f2 is cancelled instead. Returns a new \*(C`CPS::Future\*(C' instance that wraps the one given as $f1. With no arguments this will be a trivial wrapper; $future will complete or fail when $f1 does, and $f1 will be cancelled when $future is.

By passing the following named argmuents, the returned $future can be made to behave differently to $f1:

done => \s-1CODE\s0

Provides a function to use to modify the result of a successful completion. When $f1 completes successfully, the result of its \*(C`get\*(C' method is passed into this function, and whatever it returns is passed to the \*(C`done\*(C' method of $future

fail => \s-1CODE\s0

Provides a function to use to modify the result of a failure. When $f1 fails, the result of its \*(C`failure\*(C' method is passed into this function, and whatever it returns is passed to the \*(C`fail\*(C' method of $future.

IMPLEMENTATION METHODS

These methods would primarily be used by implementations of asynchronous interfaces. Marks that the leaf future is now ready, and provides a list of values as a result. (The empty list is allowed, and still indicates the future as ready). Cannot be called on a non-leaf future.

Returns the $future. This method is used to overload the calling operator, so simply invoking the future object itself as if it were a \*(C`CODE\*(C' reference is equivalent to calling the \*(C`done\*(C' method. This makes it simple to pass as a callback function to other code.

It turns out however, that this behaviour is too subtle and can lead to bugs when futures are accidentally used as plain \*(C`CODE\*(C' references. See the \*(C`done_cb\*(C' method instead. This overload behaviour will be removed in a later version. Returns a \*(C`CODE\*(C' reference that, when invoked, calls the \*(C`done\*(C' method. This makes it simple to pass as a callback function to other code. Marks that the leaf future has failed, and provides an exception value. This exception will be thrown by the \*(C`get\*(C' method if called. If the exception is a non-reference that does not end in a linefeed, its value will be extended by the file and line number of the caller, similar to the logic that \*(C`die\*(C' uses.

The exception must evaluate as a true value; false exceptions are not allowed. Further details may be provided that will be returned by the \*(C`failure\*(C' method in list context. These details will not be part of the exception string raised by \*(C`get\*(C'.

Returns the $future. Returns a \*(C`CODE\*(C' reference that, when invoked, calls the \*(C`fail\*(C' method. This makes it simple to pass as a callback function to other code. If the future is not yet ready, adds a callback to be invoked if the future is cancelled by the \*(C`cancel\*(C' method. If the future is already ready, throws an exception.

If the future is cancelled, the callbacks will be invoked in the reverse order to that in which they were registered.

$on_cancel->( $future ) If passed another \*(C`CPS::Future\*(C' instance, the passed instance will be cancelled when the original future is cancelled. Returns true if the future has been cancelled by \*(C`cancel\*(C'.

USER METHODS

These methods would primarily be used by users of asynchronous interfaces, on objects returned by such an interface. Returns true on a leaf future if a result has been provided to the \*(C`done\*(C' method, failed using the \*(C`fail\*(C' method, or cancelled using the \*(C`cancel\*(C' method.

Returns true on a \*(C`wait_all\*(C' future if all the sub-tasks are ready.

Returns true on a \*(C`needs_all\*(C' future if all the sub-tasks have completed successfully or if any of them have failed. If the future is not yet ready, adds a callback to be invoked when the future is ready. If the future is already ready, invokes it immediately.

In either case, the callback will be passed the future object itself. The invoked code can then obtain the list of results by calling the \*(C`get\*(C' method.

$on_ready->( $future )

Returns the $future. If passed another \*(C`CPS::Future\*(C' instance, the passed instance will have its \*(C`done\*(C' or \*(C`fail\*(C' methods invoked when the original future completes successfully or fails respectively. If the future is ready, returns the list of results that had earlier been given to the \*(C`done\*(C' method. If not, will raise an exception.

If called on a \*(C`wait_all\*(C' or \*(C`needs_all\*(C' future, it will return a list of the futures it was waiting on, in the order they were passed to the constructor. If the future is not yet ready, adds a callback to be invoked when the future is ready, if it completes successfully. If the future completed successfully, invokes it immediately. If it failed or was cancelled, it is not invoked at all.

The callback will be passed the result passed to the \*(C`done\*(C' method.

$on_done->( @result )

Returns the $future. If passed another \*(C`CPS::Future\*(C' instance, the passed instance will have its \*(C`done\*(C' method invoked when the original future completes successfully. Returns the exception passed to the \*(C`fail\*(C' method, \*(C`undef\*(C' if the future completed successfully via the \*(C`done\*(C' method, or raises an exception if called on a future that is not yet ready.

If called in list context, will additionally yield a list of the details provided to the \*(C`fail\*(C' method.

Because the exception value must be true, this can be used in a simple \*(C`if\*(C' statement:

if( my $exception = $future->failure ) { ... } else { my @result = $future->get; ... } If the future is not yet ready, adds a callback to be invoked when the future is ready, if it fails. If the future has already failed, invokes it immediately. If it completed successfully or was cancelled, it is not invoked at all.

The callback will be passed the exception and details passed to the \*(C`fail\*(C' method.

$on_fail->( $exception, @details )

Returns the $future. If passed another \*(C`CPS::Future\*(C' instance, the passed instance will have its \*(C`fail\*(C' method invoked when the original future fails.

To invoke a \*(C`done\*(C' method on a future when another one fails, use a \s-1CODE\s0 reference:

$future->on_fail( sub { $f->done( @_ ) } ); Requests that the future be cancelled, immediately marking it as ready. This will invoke all of the code blocks registered by \*(C`on_cancel\*(C', in the reverse order. When called on a non-leaf future, all its sub-tasks are also cancelled. Returns a \*(C`CODE\*(C' reference that, when invoked, calls the \*(C`cancel\*(C' method. This makes it simple to pass as a callback function to other code.

EXAMPLES

The following examples all demonstrate possible uses of a \*(C`CPS::Future\*(C' object to provide a fictional asynchronous \s-1API\s0 function called simply \*(C`koperation\*(C'.

Providing Results

By returning a new \*(C`CPS::Future\*(C' object each time the asynchronous function is called, it provides a placeholder for its eventual result, and a way to indicate when it is complete.

sub foperation { my %args = @_;

my $future = CPS::Future->new;

kdo_something( foo => $args{foo}, on_done => sub { $future->done( @_ ); }, );

return $future; }

In most cases, the \*(C`done\*(C' method will simply be invoked with the entire result list as its arguments. In that case, it is simpler to pass the $future object itself as if it was a \*(C`CODE\*(C' reference; this will invoke the \*(C`done\*(C' method.

my $future = CPS::Future->new;

kdo_something( foo => $args{foo}, on_done => $future, );

The caller may then use this future to wait for a result using the \*(C`on_ready\*(C' method, and obtain the result using \*(C`get\*(C'.

my $f = foperation( foo => "something" );

$f->on_ready( sub { my $f = shift; say "The operation returned: ", $f->get; } );

Indicating Success or Failure

Because the stored exception value of a failued \*(C`CPS::Future\*(C' may not be false, the \*(C`failure\*(C' method can be used in a conditional statement to detect success or failure.

my $f = koperation( foo => "something" );

$f->on_ready( sub { my $f = shift; if( not my $e = $f->failure ) { say "The operation succeeded with: ", $f->get; } else { say "The operation failed with: ", $e; } } );

By using \*(C`not\*(C' in the condition, the order of the \*(C`if\*(C' blocks can be arranged to put the successful case first, similar to a \*(C`try\*(C'/\*(C`catch\*(C' block.

Because the \*(C`get\*(C' method re-raises the passed exception if the future failed, it can be used to control a \*(C`try\*(C'/\*(C`catch\*(C' block directly. (This is sometimes called Exception Hoisting).

use Try::Tiny;

$f->on_ready( sub { my $f = shift; try { say "The operation succeeded with: ", $f->get; } catch { say "The operation failed with: ", $_; }; } );

Merging Control Flow

A \*(C`wait_all\*(C' future may be used to resynchronise control flow, while waiting for multiple concurrent operations to finish.

my $f1 = koperation( foo => "something" ); my $f2 = koperation( bar => "something else" );

my $f = CPS::Future->wait_all( $f1, $f2 );

$f->on_ready( sub { say "Operations are ready:"; say " foo: ", $f1->get; say " bar: ", $f2->get; } );

This provides an ability somewhat similar to \*(C`CPS::kpar()\*(C' or Async::MergePoint.

TODO

Lots of things still need adding. \s-1API\s0 or semantics is somewhat unclear in places.

  • \*(C`CPS::Future->needs_first\*(C', which succeeds on the first success of dependent futures and cancels the outstanding ones, only fails if all the dependents do.

  • Some way to do deferred futures that don't even start their operation until invoked somehow. Ability to chain these together in a sequence, like \*(C`CPS::kseq()\*(C'.

AUTHOR

Paul Evans <[email protected]>