Module Current.Switch

Like Lwt_switch, but the cleanup functions are called in sequence, not in parallel.

type t

A switch limits the lifetime of an operation. Cleanup operations can be registered against the switch and will be called (in reverse order) when the switch is turned off.

val create : label:string -> unit -> t

create ~label () is a fresh switch, initially on.

  • parameter label

    If the switch is GC'd while on, this is logged in the error message.

val create_off : unit -> t

create_off () is a fresh switch, initially (and always) off.

val add_hook_or_exec : t -> (unit -> unit Lwt.t) -> unit Lwt.t

add_hook_or_exec switch fn pushes fn on to the stack of functions to call when t is turned off. If t is already off, calls fn immediately. If t is in the process of being turned off, waits for that to complete and then runs fn.

val turn_off : t -> unit Lwt.t

turn_off t marks the switch as being turned off, then pops and calls clean-up functions in order. When the last one finishes, the switch is marked as off and cannot be used again. If the switch is already off, this does nothing. If the switch is already being turned off, it just waits for that to complete.

val is_on : t -> bool

is_on t is true if turn_off t hasn't yet been called.

val pp : t Fmt.t

Prints the state of the switch (for debugging).