Skip to content

Commit

Permalink
Minor changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrm committed Nov 23, 2024
1 parent 9e219b7 commit 29a52e6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/ws_deque.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ type 'a t = {
}

let create () =
let top = Atomic.make 0 |> Multicore_magic.copy_as_padded in
let top = Atomic.make_contended 0 in
let tab = Array.make min_capacity (Obj.magic ()) in
let bottom = Atomic.make 0 |> Multicore_magic.copy_as_padded in
let bottom = Atomic.make_contended 0 in
let top_cache = ref 0 |> Multicore_magic.copy_as_padded in
{ top; bottom; top_cache; tab } |> Multicore_magic.copy_as_padded

Expand Down Expand Up @@ -83,6 +83,8 @@ let push q v =

type ('a, _) poly = Option : ('a, 'a option) poly | Value : ('a, 'a) poly

exception Empty

let pop_as : type a r. a t -> (a, r) poly -> r =
fun q poly ->
let b = Atomic.fetch_and_add q.bottom (-1) - 1 in
Expand Down Expand Up @@ -115,13 +117,13 @@ let pop_as : type a r. a t -> (a, r) poly -> r =
out := Obj.magic ();
match poly with Option -> Some res | Value -> res
end
else match poly with Option -> None | Value -> raise_notrace Exit
else match poly with Option -> None | Value -> raise Empty
end
else begin
(* This write of [bottom] requires no fence. The deque is empty and
remains so until the next [push]. *)
Atomic.fenceless_set q.bottom (b + 1);
match poly with Option -> None | Value -> raise_notrace Exit
match poly with Option -> None | Value -> raise Empty
end

let pop_exn q = pop_as q Value
Expand All @@ -144,7 +146,7 @@ let rec steal_as : type a r. a t -> Backoff.t -> (a, r) poly -> r =
match poly with Option -> Some res | Value -> res
end
else steal_as q (Backoff.once backoff) poly
else match poly with Option -> None | Value -> raise_notrace Exit
else match poly with Option -> None | Value -> raise Empty

let steal_exn q = steal_as q Backoff.default Value
let steal_opt q = steal_as q Backoff.default Option
2 changes: 2 additions & 0 deletions src/ws_deque.mli
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type 'a t
val create : unit -> 'a t
(** [create ()] returns a new empty work-stealing queue. *)

exception Empty

(** {1 Queue owner functions} *)

val push : 'a t -> 'a -> unit
Expand Down

0 comments on commit 29a52e6

Please sign in to comment.