refactor picos: move whole setup into Background_thread mod

This commit is contained in:
Simon Cruanes 2025-05-01 12:05:51 -04:00
parent c2b7c0e39d
commit 14d744c369
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 14 additions and 13 deletions

View file

@ -66,13 +66,15 @@ module Global_ = struct
Thread.join st.th
end
let has_bg_thread = Global_.has_bg_thread
let setup_bg_thread = Global_.setup_bg_thread
let shutdown_bg_thread = Global_.shutdown_bg_thread
module Background_thread = struct
let is_setup = Global_.has_bg_thread
let setup = Global_.setup_bg_thread
let shutdown = Global_.shutdown_bg_thread
let with_setup_bg_thread ev f =
setup_bg_thread ev;
Fun.protect ~finally:shutdown_bg_thread f
let with_setup ev f =
setup ev;
Fun.protect ~finally:shutdown f
end
let[@inline] get_loop_exn_ () : Nanoev.t =
match Atomic.get Global_.st with

View file

@ -1,17 +1,16 @@
(** Basic interface with picos *)
module Background_thread : sig
val setup_bg_thread : Nanoev.t -> unit
val setup : Nanoev.t -> unit
(** Install this event loop in a background thread *)
val shutdown_bg_thread : unit -> unit
(** Shutdown background thread, assuming {! has_bg_thread} returns [true] *)
val shutdown : unit -> unit
(** Shutdown background thread, assuming {! is_setup} returns [true] *)
val with_setup_bg_thread : Nanoev.t -> (unit -> 'a) -> 'a
val with_setup : Nanoev.t -> (unit -> 'a) -> 'a
val has_bg_thread : unit -> bool
(** [has_bg_thread ()] is [true] iff a background thread is running a nanoev
loop *)
val is_setup : unit -> bool
(** [is_setup()] is [true] iff a background thread is running a nanoev loop *)
end
(** {2 Non blocking IO primitives} *)