Imandrakit_thread.FutFuture
type 'a or_error = ('a, Moonpool.Exn_bt.t) resulttype 'a t = 'a Moonpool.Fut.tA future with a result of type 'a.
type 'a promise = 'a Moonpool.Fut.promiseA promise, which can be fulfilled exactly once to set the corresponding future
on_result fut f registers f to be called in the future when fut is set ; or calls f immediately if fut is already set.
Fullfill the promise, setting the future at the same time.
Fullfill the promise, setting the future at the same time. Does nothing if the promise is already fulfilled.
val return : 'a -> 'a tAlready settled future, with a result
val fail : exn -> Stdlib.Printexc.raw_backtrace -> _ tAlready settled future, with a failure
val fail_exn_bt : Moonpool.Exn_bt.t -> _ tFail from a bundle of exception and backtrace
val is_resolved : _ t -> boolis_resolved fut is true iff fut is resolved.
peek fut returns Some r if fut is currently resolved with r, and None if fut is not resolved yet.
get_or_fail fut obtains the result from fut if it's fulfilled (i.e. if peek fut returns Some res, get_or_fail fut returns res).
val get_or_fail_exn : 'a t -> 'aget_or_fail_exn fut obtains the result from fut if it's fulfilled, like get_or_fail. If the result is an Error _, the exception inside is re-raised.
val is_done : _ t -> boolIs the future resolved? This is the same as peek fut |> Option.is_some.
val is_success : _ t -> boolChecks if the future is resolved with Ok _ as a result.
val is_failed : _ t -> boolChecks if the future is resolved with Error _ as a result.
val raise_if_failed : _ t -> unitraise_if_failed fut raises e if fut failed with e.
val spawn : on:Moonpool.Runner.t -> (unit -> 'a) -> 'a tspaw ~on f runs f() on the given runner on, and return a future that will hold its result.
val spawn_on_current_runner : (unit -> 'a) -> 'a tThis must be run from inside a runner, and schedules the new task on it as well.
See Runner.get_current_runner to see how the runner is found.
val map : ?on:Moonpool.Runner.t -> f:('a -> 'b) -> 'a t -> 'b tmap ?on ~f fut returns a new future fut2 that resolves with f x if fut resolved with x; and fails with e if fut fails with e or f x raises e.
val bind : ?on:Moonpool.Runner.t -> f:('a -> 'b t) -> 'a t -> 'b tbind ?on ~f fut returns a new future fut2 that resolves like the future f x if fut resolved with x; and fails with e if fut fails with e or f x raises e.
both a b succeeds with x, y if a succeeds with x and b succeeds with y, or fails if any of them fails.
choose a b succeeds Left x or Right y if a succeeds with x or b succeeds with y, or fails if both of them fails. If they both succeed, it is not specified which result is used.
choose_same a b succeeds with the value of one of a or b if they succeed, or fails if both fail. If they both succeed, it is not specified which result is used.
Wait for all the futures in the array. Fails if any future fails.
Wait for all the futures in the list. Fails if any future fails.
module Advanced = Moonpool.Fut.Advancedmap_list ~f l is like join_list @@ List.map f l.
wait_array arr waits for all futures in arr to resolve. It discards the individual results of futures in arr. It fails if any future fails.
wait_list l waits for all futures in l to resolve. It discards the individual results of futures in l. It fails if any future fails.
val for_ : on:Moonpool.Runner.t -> int -> (int -> unit) -> unit tfor_ ~on n f runs f 0, f 1, …, f (n-1) on the runner, and returns a future that resolves when all the tasks have resolved, or fails as soon as one task has failed.
val for_array :
on:Moonpool.Runner.t ->
'a array ->
(int -> 'a -> unit) ->
unit tfor_array ~on arr f runs f 0 arr.(0), …, f (n-1) arr.(n-1) in the runner (where n = Array.length arr), and returns a future that resolves when all the tasks are done, or fails if any of them fails.
val for_list : on:Moonpool.Runner.t -> 'a list -> ('a -> unit) -> unit tfor_list ~on l f is like for_array ~on (Array.of_list l) f.
NOTE This is only available on OCaml 5.
val await : 'a t -> 'aawait fut suspends the current tasks until fut is fulfilled, then resumes the task on this same runner (but possibly on a different thread/domain).
This must only be run from inside the runner itself. The runner must support Suspend_. NOTE: only on OCaml 5.x
wait_block fut blocks the current thread until fut is resolved, and returns its value.
NOTE: A word of warning: this will monopolize the calling thread until the future resolves. This can also easily cause deadlocks, if enough threads in a pool call wait_block on futures running on the same pool or a pool depending on it.
A good rule to avoid deadlocks is to run this from outside of any pool, or to have an acyclic order between pools where wait_block is only called from a pool on futures evaluated in a pool that comes lower in the hierarchy. If this rule is broken, it is possible for all threads in a pool to wait for futures that can only make progress on these same threads, hence the deadlock.
val wait_block_exn : 'a t -> 'aSame as wait_block but re-raises the exception if the future failed.
These combinators run on either the current pool (if present), or on the same thread that just fulfilled the previous future if not.
They were previously present as module Infix_local and val infix, but are now simplified.
module Infix = Moonpool.Fut.Infixinclude module type of Infixval (>|=) : 'a Moonpool.Fut.t -> ('a -> 'b) -> 'b Moonpool.Fut.tval (>>=) : 'a Moonpool.Fut.t -> ('a -> 'b Moonpool.Fut.t) -> 'b Moonpool.Fut.tval let+ : 'a Moonpool.Fut.t -> ('a -> 'b) -> 'b Moonpool.Fut.tval and+ : 'a Moonpool.Fut.t -> 'b Moonpool.Fut.t -> ('a * 'b) Moonpool.Fut.tval let* : 'a Moonpool.Fut.t -> ('a -> 'b Moonpool.Fut.t) -> 'b Moonpool.Fut.tval and* : 'a Moonpool.Fut.t -> 'b Moonpool.Fut.t -> ('a * 'b) Moonpool.Fut.tmodule Infix_local = Moonpool.Fut.Infixtype backtrace = Stdlib.Printexc.raw_backtracetype exn_with_bt = exn * Stdlib.Printexc.raw_backtraceval pp : 'a Imandrakit.Fmt.printer -> 'a t Imandrakit.Fmt.printerval peek_exn : 'a t -> 'a optionval unwrap : ('a, exn_with_bt) result -> 'aUnwrap result by raising in case of error
val raise_with_bt : exn -> backtrace -> 'aval bind_reify_error :
?on:Executor.t ->
f:('a or_error -> 'b t) ->
'a t ->
'b tval wait_vec : (_ t, _) Imandrakit.Vec.t -> unit tWait for all futures in the vector to be done
val map_vec_array : ('a t, _) Imandrakit.Vec.t -> 'a array tval pp_any : any Imandrakit.Fmt.printerval any_is_resolved : any -> boolval any_is_success : any -> boolval any_is_failed : any -> boolval any_raise_if_failed : any -> unit