cfuture-2.0: A Future type that is interruptible anytime and exportable to C/C++.
Safe HaskellNone
LanguageHaskell2010

Control.Concurrent.CFuture

Description

Defines a type similar to C++ futures, that can be passed to C and used to interrupt asynchronous calls or to get their results.

From within Haskell, you can use forkFuture to start a calculation, get to wait on it and abort to abort it.

From C, interruption happens via calling a C-native FFI function, without the cost of a full FFI call. See the C source for more information.

Synopsis

Documentation

data Future a Source #

An object representing an asynchronous calculation. Filling the first MVar activates a thread that aborts the calculation and writes Nothing to the other MVar (which would otherwise contain the result). It is recommended not to manipulate the MVars directly, but to use the functions in the library instead.

type CFuturePtr = Ptr (StablePtr PrimMVar) Source #

Gets translated to HsStablePtr* (i.e. void**) in C.

forkFuture :: IO a -> IO (Future a) Source #

Starts an asynchronous calculation and returns a Future to it.

writeFutureC :: CFuturePtr -> Future a -> IO () Source #

Creates StablePtrs and writes them to a memory area provided by a C caller. Use this in functions where the C frontend provides a CFuturePtr to write the future to.

Note: it is the responsibility of the C side to free the StablePtrs.

forkFutureC :: CFuturePtr -> IO a -> IO () Source #

Similar to forkFuture, but we write the Future into a location given by the caller. This makes it easier to create C exports for actions.

Use this in functions where the C frontend provides a CFuturePtr to write the future to.

Note: it is the responsibility of the C side to free the StablePtrs.

get :: Future a -> IO (Maybe a) Source #

Reads the result from the Future. This is a blocking call, waiting for the result (or the Nothing signalling interruption) until it is ready.

getC :: Storable a => CFuturePtr -> Ptr a -> IO Bool Source #

A variant of get to call from C which writes the result to the memory location defined by the pointer. If there is Nothing instead of a result, it writes nothing to the pointer and returns False; on success, it returns True.

Note: do _not_ call this on a freed Future (the abortC function of the C side frees it).

waitC :: CFuturePtr -> IO Bool Source #

Only waits until the calculation gets finished; then returns True if it was successful and False otherwise. To be called from C. Differs from getC in that it ignores the result.

Note: do _not_ call this on a freed Future (the abortC function of the C side frees it).

abort :: Future a -> IO Bool Source #

Interrupts the calculation behind the Future. Do not call this from C; use hs_try_putmvar instead (that frees the first MVar, too). Returns False if it has already been interrupted and True otherwise.