From 8c36b657863118b9d58dccaeab428951249fa372 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 23 Apr 2024 10:16:31 -0400 Subject: [PATCH] refactor: move functional queue into private, from chan --- src/core/chan.ml | 52 +---------------------------------------- src/fib/moonpool_fib.ml | 1 + src/private/fqueue.ml | 37 +++++++++++++++++++++++++++++ src/private/fqueue.mli | 13 +++++++++++ 4 files changed, 52 insertions(+), 51 deletions(-) create mode 100644 src/private/fqueue.ml create mode 100644 src/private/fqueue.mli diff --git a/src/core/chan.ml b/src/core/chan.ml index 5ce82376..068b55f2 100644 --- a/src/core/chan.ml +++ b/src/core/chan.ml @@ -1,59 +1,9 @@ module A = Atomic_ +module Q = Moonpool_private.Fqueue type 'a or_error = 'a Fut.or_error type 'a waiter = 'a Fut.promise -let[@inline] list_is_empty_ = function - | [] -> true - | _ :: _ -> false - -(** Simple functional queue *) -module Q : sig - type 'a t - - val return : 'a -> 'a t - val is_empty : _ t -> bool - - exception Empty - - val pop_exn : 'a t -> 'a * 'a t - val push : 'a t -> 'a -> 'a t - val iter : ('a -> unit) -> 'a t -> unit -end = struct - type 'a t = { - hd: 'a list; - tl: 'a list; - } - (** Queue containing elements of type 'a. - - invariant: if hd=[], then tl=[] *) - - let[@inline] return x : _ t = { hd = [ x ]; tl = [] } - - let[@inline] make_ hd tl = - match hd with - | [] -> { hd = List.rev tl; tl = [] } - | _ :: _ -> { hd; tl } - - let[@inline] is_empty self = list_is_empty_ self.hd - let[@inline] push self x : _ t = make_ self.hd (x :: self.tl) - - let iter f (self : _ t) : unit = - List.iter f self.hd; - List.iter f self.tl - - exception Empty - - let pop_exn self = - match self.hd with - | [] -> - assert (list_is_empty_ self.tl); - raise Empty - | x :: hd' -> - let self' = make_ hd' self.tl in - x, self' -end - exception Closed type 'a state = diff --git a/src/fib/moonpool_fib.ml b/src/fib/moonpool_fib.ml index ec89c075..9359303f 100644 --- a/src/fib/moonpool_fib.ml +++ b/src/fib/moonpool_fib.ml @@ -8,5 +8,6 @@ module Fiber = Fiber module Fls = Fls module Handle = Handle module Main = Main +module Coop_lock = Coop_lock include Fiber include Main diff --git a/src/private/fqueue.ml b/src/private/fqueue.ml new file mode 100644 index 00000000..a4fcbcab --- /dev/null +++ b/src/private/fqueue.ml @@ -0,0 +1,37 @@ +type 'a t = { + hd: 'a list; + tl: 'a list; +} +(** Queue containing elements of type 'a. + + invariant: if hd=[], then tl=[] *) + +let[@inline] list_is_empty_ = function + | [] -> true + | _ :: _ -> false + +let empty = { hd = []; tl = [] } +let[@inline] return x : _ t = { hd = [ x ]; tl = [] } + +let[@inline] make_ hd tl = + match hd with + | [] -> { hd = List.rev tl; tl = [] } + | _ :: _ -> { hd; tl } + +let[@inline] is_empty self = list_is_empty_ self.hd +let[@inline] push self x : _ t = make_ self.hd (x :: self.tl) + +let iter f (self : _ t) : unit = + List.iter f self.hd; + List.iter f self.tl + +exception Empty + +let pop_exn self = + match self.hd with + | [] -> + assert (list_is_empty_ self.tl); + raise Empty + | x :: hd' -> + let self' = make_ hd' self.tl in + x, self' diff --git a/src/private/fqueue.mli b/src/private/fqueue.mli new file mode 100644 index 00000000..22d8316f --- /dev/null +++ b/src/private/fqueue.mli @@ -0,0 +1,13 @@ +(** Simple functional queue *) + +type +'a t + +val empty : 'a t +val return : 'a -> 'a t +val is_empty : _ t -> bool + +exception Empty + +val pop_exn : 'a t -> 'a * 'a t +val push : 'a t -> 'a -> 'a t +val iter : ('a -> unit) -> 'a t -> unit