mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
Futures.choose implemented
This commit is contained in:
parent
c8b7b7dfed
commit
81e7ee2c04
1 changed files with 19 additions and 1 deletions
20
futures.ml
20
futures.ml
|
|
@ -389,7 +389,25 @@ let sequence futures =
|
|||
future'
|
||||
|
||||
let choose futures =
|
||||
failwith "not implemented"
|
||||
let future' = make () in
|
||||
let one_finished = MVar.full false in
|
||||
(* handlers. The first handler to be called will update [one_finished]
|
||||
to true, see that it was false (hence know it is the first)
|
||||
and propagate its result to [future'] *)
|
||||
let one_succeeded x =
|
||||
let one_finished, _ = MVar.update one_finished (fun _ -> true) in
|
||||
if not one_finished then send future' x
|
||||
and one_failed e =
|
||||
let one_finished, _ = MVar.update one_finished (fun _ -> true) in
|
||||
if not one_finished then fail future' e
|
||||
in
|
||||
(* add handlers to all futures *)
|
||||
List.iter
|
||||
(fun future ->
|
||||
on_success future one_succeeded;
|
||||
on_failure future one_failed; )
|
||||
futures;
|
||||
future'
|
||||
|
||||
let map f future =
|
||||
let future' = make () in
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue