mirror of
https://github.com/c-cube/moonpool.git
synced 2025-12-07 19:55:39 -05:00
fixes for blocking queue
This commit is contained in:
parent
9f976f4092
commit
078e7414bc
2 changed files with 28 additions and 13 deletions
|
|
@ -70,9 +70,9 @@ let pop (self : 'a t) : 'a =
|
||||||
) else (
|
) else (
|
||||||
let was_full = is_full_ self in
|
let was_full = is_full_ self in
|
||||||
let x = Queue.pop self.q in
|
let x = Queue.pop self.q in
|
||||||
Mutex.unlock self.mutex;
|
(* wakeup pushers that were blocked *)
|
||||||
(* wakeup pushers *)
|
|
||||||
if was_full then Condition.broadcast self.cond_push;
|
if was_full then Condition.broadcast self.cond_push;
|
||||||
|
Mutex.unlock self.mutex;
|
||||||
x
|
x
|
||||||
)
|
)
|
||||||
in
|
in
|
||||||
|
|
@ -87,11 +87,16 @@ let try_pop ~force_lock (self : _ t) : _ option =
|
||||||
Mutex.try_lock self.mutex
|
Mutex.try_lock self.mutex
|
||||||
in
|
in
|
||||||
if has_lock then (
|
if has_lock then (
|
||||||
|
if self.closed then (
|
||||||
|
Mutex.unlock self.mutex;
|
||||||
|
raise Closed
|
||||||
|
);
|
||||||
let was_full_before_pop = is_full_ self in
|
let was_full_before_pop = is_full_ self in
|
||||||
match Queue.pop self.q with
|
match Queue.pop self.q with
|
||||||
| x ->
|
| x ->
|
||||||
Mutex.unlock self.mutex;
|
(* wakeup pushers that are blocked *)
|
||||||
if was_full_before_pop then Condition.broadcast self.cond_push;
|
if was_full_before_pop then Condition.broadcast self.cond_push;
|
||||||
|
Mutex.unlock self.mutex;
|
||||||
Some x
|
Some x
|
||||||
| exception Queue.Empty ->
|
| exception Queue.Empty ->
|
||||||
Mutex.unlock self.mutex;
|
Mutex.unlock self.mutex;
|
||||||
|
|
@ -99,8 +104,15 @@ let try_pop ~force_lock (self : _ t) : _ option =
|
||||||
) else
|
) else
|
||||||
None
|
None
|
||||||
|
|
||||||
let try_push (self : _ t) x : bool =
|
let try_push ~force_lock (self : _ t) x : bool =
|
||||||
if Mutex.try_lock self.mutex then (
|
let has_lock =
|
||||||
|
if force_lock then (
|
||||||
|
Mutex.lock self.mutex;
|
||||||
|
true
|
||||||
|
) else
|
||||||
|
Mutex.try_lock self.mutex
|
||||||
|
in
|
||||||
|
if has_lock then (
|
||||||
if self.closed then (
|
if self.closed then (
|
||||||
Mutex.unlock self.mutex;
|
Mutex.unlock self.mutex;
|
||||||
raise Closed
|
raise Closed
|
||||||
|
|
|
||||||
|
|
@ -31,9 +31,15 @@ val push : 'a t -> 'a -> unit
|
||||||
room for [x].
|
room for [x].
|
||||||
@raise Closed if [q] is closed. *)
|
@raise Closed if [q] is closed. *)
|
||||||
|
|
||||||
val try_push : 'a t -> 'a -> bool
|
val try_push : force_lock:bool -> 'a t -> 'a -> bool
|
||||||
(** [try_push q x] attempts to push [x] into [q], but abandons
|
(** [try_push q x] attempts to push [x] into [q], but abandons
|
||||||
if it cannot acquire [q] or if [q] is full.
|
if it cannot acquire [q] or if [q] is full.
|
||||||
|
|
||||||
|
@param force_lock if true, use {!Mutex.lock} (which can block
|
||||||
|
under contention);
|
||||||
|
if false, use {!Mutex.try_lock}, which might return [false] even
|
||||||
|
if there's room in the queue.
|
||||||
|
|
||||||
@raise Closed if [q] is closed. *)
|
@raise Closed if [q] is closed. *)
|
||||||
|
|
||||||
val pop : 'a t -> 'a
|
val pop : 'a t -> 'a
|
||||||
|
|
@ -42,7 +48,7 @@ val pop : 'a t -> 'a
|
||||||
@raise Closed if [q] is empty and closed. *)
|
@raise Closed if [q] is empty and closed. *)
|
||||||
|
|
||||||
val try_pop : force_lock:bool -> 'a t -> 'a option
|
val try_pop : force_lock:bool -> 'a t -> 'a option
|
||||||
(** [try_pop q] tries to pop the first element, or returns [None]
|
(** [try_pop ~force_lock q] tries to pop the first element, or returns [None]
|
||||||
if no element is available or if it failed to acquire [q].
|
if no element is available or if it failed to acquire [q].
|
||||||
|
|
||||||
@param force_lock if true, use {!Mutex.lock} (which can block
|
@param force_lock if true, use {!Mutex.lock} (which can block
|
||||||
|
|
@ -71,13 +77,10 @@ type 'a iter = ('a -> unit) -> unit
|
||||||
|
|
||||||
val to_iter : 'a t -> 'a iter
|
val to_iter : 'a t -> 'a iter
|
||||||
(** [to_iter q] returns an iterator over all items in the queue.
|
(** [to_iter q] returns an iterator over all items in the queue.
|
||||||
This might not terminate if [q] is never closed.
|
This might not terminate if [q] is never closed. *)
|
||||||
@since NEXT_RELEASE *)
|
|
||||||
|
|
||||||
val to_gen : 'a t -> 'a gen
|
val to_gen : 'a t -> 'a gen
|
||||||
(** [to_gen q] returns a generator from the queue.
|
(** [to_gen q] returns a generator from the queue. *)
|
||||||
@since NEXT_RELEASE *)
|
|
||||||
|
|
||||||
val to_seq : 'a t -> 'a Seq.t
|
val to_seq : 'a t -> 'a Seq.t
|
||||||
(** [to_gen q] returns a (transient) sequence from the queue.
|
(** [to_gen q] returns a (transient) sequence from the queue. *)
|
||||||
@since NEXT_RELEASE *)
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue