avoid recursion in dpool

This commit is contained in:
Simon Cruanes 2024-10-09 00:26:30 -04:00
parent 40e97d969a
commit a40ea8b41b
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4

View file

@ -15,19 +15,23 @@ module Bb_queue = struct
if was_empty then Condition.broadcast self.cond; if was_empty then Condition.broadcast self.cond;
Mutex.unlock self.mutex Mutex.unlock self.mutex
let pop (self : 'a t) : 'a = let pop (type a) (self : a t) : a =
let module M = struct
exception Found of a
end in
try
Mutex.lock self.mutex; Mutex.lock self.mutex;
let rec loop () = while true do
if Queue.is_empty self.q then ( if Queue.is_empty self.q then
Condition.wait self.cond self.mutex; Condition.wait self.cond self.mutex
(loop [@tailcall]) () else (
) else (
let x = Queue.pop self.q in let x = Queue.pop self.q in
Mutex.unlock self.mutex; Mutex.unlock self.mutex;
x raise (M.Found x)
) )
in done;
loop () assert false
with M.Found x -> x
end end
module Lock = struct module Lock = struct