Module Picos_std_structured.Run

Operations for running fibers in specific patterns.

val all : (unit -> unit) list -> unit

all actions starts the actions as separate fibers and waits until they all return. If any of the actions raises an exception other than Terminate the remaining fibers will be canceled and the exception, if any, will be raised.

⚠️ One of the actions may be run on the current fiber.

⚠️ It is not guaranteed that any of the actions in the list are called. In particular, after any action raises an exception other than Terminate or after the main fiber is canceled, the actions that have not yet started may be skipped entirely.

all is roughly equivalent to

let all actions =
  Bundle.join_after @@ fun bundle ->
  List.iter (Bundle.fork bundle) actions

but treats the list of actions as a single computation.

val any : (unit -> unit) list -> unit

any actions starts the actions as separate fibers and waits until one of them returns or raises an exception other than Terminate after which the remaining started fibers will be canceled and the exception, if any, will be raised.

⚠️ One of the actions may be run on the current fiber.

⚠️ It is not guaranteed that any of the actions in the list are called. In particular, after the first action returns successfully or after any action raises an exception other than Terminate or after the main fiber is canceled, the actions that have not yet started may be skipped entirely.

any is roughly equivalent to

let any actions =
  Bundle.join_after @@ fun bundle ->
  try
    actions
    |> List.iter @@ fun action ->
       Bundle.fork bundle @@ fun () ->
       action ();
       Bundle.terminate bundle
  with Control.Terminate -> ()

but treats the list of actions as a single computation.

val first_or_terminate : (unit -> 'a) list -> 'a

first_or_terminate actions starts the actions as separate fibers and waits until one of them returns a value or raises an exception other than Terminate after which the remaining started fibers will be canceled and the value will be returned or the exception, if any, will be raised. If none of the actions returned or raised an exception other than Terminate, then Terminate will be raised.

⚠️ One of the actions may be run on the current fiber.

⚠️ It is not guaranteed that any of the actions in the list are called. In particular, after the first action returns successfully or after any action raises an exception other than Terminate or after the main fiber is canceled, the actions that have not yet started may be skipped entirely.

  • raises Control.Terminate

    in case none of the actions returned a value or raised an exception other than Terminate.

    first_or_terminate is roughly equivalent to

    let first_or_terminate actions =
      let result = Atomic.make None in
      begin
        Bundle.join_after @@ fun bundle ->
        try
          actions
          |> List.iter @@ fun action ->
             Bundle.fork bundle @@ fun () ->
             let value = action () in
             if Atomic.compare_and_set result None (Some value) then
               Bundle.terminate bundle
        with Control.Terminate -> ()
      end;
      match Atomic.get result with
      | None -> raise Control.Terminate
      | Some value -> value

    but treats the list of actions as a single computation.