Skip to content

Commit

Permalink
Rename pop to pop_exn and steal to steal_exn
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrm committed Nov 23, 2024
1 parent cbf23dc commit 9e219b7
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 33 deletions.
16 changes: 9 additions & 7 deletions bench/bench_ws_deque.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ let run_as_scheduler ~budgetf ?(n_domains = 1) () =
in

let rec try_own own =
match Ws_deque.pop (Array.unsafe_get deques own) with
match Ws_deque.pop_exn (Array.unsafe_get deques own) with
| work -> work
| exception Exit -> try_steal own (next own)
and try_steal own other =
if other = own then raise_notrace Exit
else
match Ws_deque.steal (Array.unsafe_get deques other) with
match Ws_deque.steal_exn (Array.unsafe_get deques other) with
| work -> work
| exception Exit -> try_steal own (next other)
in
Expand Down Expand Up @@ -90,14 +90,15 @@ let run_as_one_domain ~budgetf ?(n_msgs = 150 * Util.iter_factor) order =

let op_lifo push =
if push then Ws_deque.push t 101
else match Ws_deque.pop t with _ -> () | exception Exit -> ()
else match Ws_deque.pop_exn t with _ -> () | exception Exit -> ()
and op_fifo push =
if push then Ws_deque.push t 101
else match Ws_deque.steal t with _ -> () | exception Exit -> ()
else match Ws_deque.steal_exn t with _ -> () | exception Exit -> ()
in

let init _ =
assert (match Ws_deque.steal t with _ -> false | exception Exit -> true);
assert (
match Ws_deque.steal_exn t with _ -> false | exception Exit -> true);
Util.generate_push_and_pop_sequence n_msgs
in
let work _ bits =
Expand All @@ -121,7 +122,8 @@ let run_as_spmc ~budgetf ~n_thiefs () =
let n_msgs_to_steal = Atomic.make 0 |> Multicore_magic.copy_as_padded in

let init _ =
assert (match Ws_deque.steal t with _ -> false | exception Exit -> true);
assert (
match Ws_deque.steal_exn t with _ -> false | exception Exit -> true);
Atomic.set n_msgs_to_steal n_msgs
in
let work i () =
Expand All @@ -131,7 +133,7 @@ let run_as_spmc ~budgetf ~n_thiefs () =
if 0 < n then
let rec loop n =
if 0 < n then
match Ws_deque.steal t with
match Ws_deque.steal_exn t with
| exception Exit ->
Domain.cpu_relax ();
loop n
Expand Down
4 changes: 2 additions & 2 deletions src/ws_deque.ml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ let pop_as : type a r. a t -> (a, r) poly -> r =
match poly with Option -> None | Value -> raise_notrace Exit
end

let pop q = pop_as q Value
let pop_exn q = pop_as q Value
let pop_opt q = pop_as q Option

let rec steal_as : type a r. a t -> Backoff.t -> (a, r) poly -> r =
Expand All @@ -146,5 +146,5 @@ let rec steal_as : type a r. a t -> Backoff.t -> (a, r) poly -> r =
else steal_as q (Backoff.once backoff) poly
else match poly with Option -> None | Value -> raise_notrace Exit

let steal q = steal_as q Backoff.default Value
let steal_exn q = steal_as q Backoff.default Value
let steal_opt q = steal_as q Backoff.default Option
8 changes: 4 additions & 4 deletions src/ws_deque.mli
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ val push : 'a t -> 'a -> unit
(** [push t v] adds [v] to the front of the queue [q].
It should only be invoked by the domain which owns the queue [q]. *)

val pop : 'a t -> 'a
(** [pop q] removes and returns the first element in queue
val pop_exn : 'a t -> 'a
(** [pop_exn q] removes and returns the first element in queue
[q].It should only be invoked by the domain which owns the queue
[q].
Expand All @@ -39,8 +39,8 @@ val pop_opt : 'a t -> 'a option

(** {1 Stealers function} *)

val steal : 'a t -> 'a
(** [steal q] removes and returns the last element from queue
val steal_exn : 'a t -> 'a
(** [steal_exn q] removes and returns the last element from queue
[q]. It should only be invoked by domain which doesn't own the
queue [q].
Expand Down
14 changes: 7 additions & 7 deletions test/ws_deque/qcheck_ws_deque.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let tests_one_producer =
let deque = deque_of_list (l @ l') in

let pop_list =
extract_n_of_deque deque Ws_deque.pop (List.length l')
extract_n_of_deque deque Ws_deque.pop_exn (List.length l')
in
pop_list = List.rev l'));
(* TEST 2 - single producer no stealer :
Expand All @@ -49,7 +49,7 @@ let tests_one_producer =
let deque = deque_of_list l in

for _i = 0 to m - 1 do
try ignore (Ws_deque.pop deque) with Exit -> incr count
try ignore (Ws_deque.pop_exn deque) with Exit -> incr count
done;

!count = m - n));
Expand Down Expand Up @@ -77,7 +77,7 @@ let tests_one_producer_one_stealer =
let stealer =
Domain.spawn (fun () ->
let steal' deque =
match Ws_deque.steal deque with
match Ws_deque.steal_exn deque with
| value -> Some value
| exception Exit ->
Domain.cpu_relax ();
Expand Down Expand Up @@ -124,7 +124,7 @@ let tests_one_producer_one_stealer =
Domain.spawn (fun () ->
Barrier.await barrier;
let steal' deque =
match Ws_deque.steal deque with
match Ws_deque.steal_exn deque with
| value -> Some value
| exception Exit ->
Domain.cpu_relax ();
Expand Down Expand Up @@ -175,7 +175,7 @@ let tests_one_producer_one_stealer =
let barrier = Barrier.create 2 in
Random.self_init ();
let pop' deque =
match Ws_deque.pop deque with
match Ws_deque.pop_exn deque with
| value -> Some value
| exception Exit ->
Domain.cpu_relax ();
Expand All @@ -189,7 +189,7 @@ let tests_one_producer_one_stealer =
Domain.spawn (fun () ->
Barrier.await barrier;
let steal' deque =
match Ws_deque.steal deque with
match Ws_deque.steal_exn deque with
| value -> Some value
| exception Exit ->
Domain.cpu_relax ();
Expand Down Expand Up @@ -242,7 +242,7 @@ let tests_one_producer_two_stealers =

for i = 0 to nsteal - 1 do
res.(i) <-
(match Ws_deque.steal deque with
(match Ws_deque.steal_exn deque with
| value -> Some value
| exception Exit ->
Domain.cpu_relax ();
Expand Down
4 changes: 2 additions & 2 deletions test/ws_deque/stm_ws_deque.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ module Spec = struct
let run c d =
match c with
| Push i -> Res (unit, Ws_deque.push d i)
| Pop -> Res (result int exn, protect Ws_deque.pop d)
| Steal -> Res (result int exn, protect Ws_deque.steal d)
| Pop -> Res (result int exn, protect Ws_deque.pop_exn d)
| Steal -> Res (result int exn, protect Ws_deque.steal_exn d)

let postcond c (s : state) res =
match (c, res) with
Expand Down
14 changes: 7 additions & 7 deletions test/ws_deque/test_ws_deque.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ open Saturn.Work_stealing_deque

let test_empty () =
let q = create () in
match pop q with
match pop_exn q with
| exception Exit -> print_string "test_exit: ok\n"
| _ -> assert false

Expand All @@ -12,9 +12,9 @@ let test_push_and_pop () =
push q 1;
push q 10;
push q 100;
assert (pop q = 100);
assert (pop q = 10);
assert (pop q = 1);
assert (pop_exn q = 100);
assert (pop_exn q = 10);
assert (pop_exn q = 1);
print_string "test_push_and_pop: ok\n"

let test_push_and_steal () =
Expand All @@ -25,7 +25,7 @@ let test_push_and_steal () =
let domains =
Array.init 3 (fun _ ->
Domain.spawn (fun _ ->
let v = steal q in
let v = steal_exn q in
assert (v = 1 || v = 10 || v = 100)))
in
Array.iter Domain.join domains;
Expand Down Expand Up @@ -64,7 +64,7 @@ let test_concurrent_workload () =
pushed := x :: !pushed;
decr n
and pop () =
match pop q with
match pop_exn q with
| exception Exit ->
Domain.cpu_relax ();
false
Expand All @@ -89,7 +89,7 @@ let test_concurrent_workload () =
Array.init thieves (fun i ->
Domain.spawn (fun () ->
let steal () =
match steal q with
match steal_exn q with
| exception Exit -> Domain.cpu_relax ()
| x -> stolen.(i) <- x :: stolen.(i)
in
Expand Down
8 changes: 4 additions & 4 deletions test/ws_deque/ws_deque_dscheck.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let drain_remaining queue =
let remaining = ref 0 in
(try
while true do
Ws_deque.pop queue |> ignore;
Ws_deque.pop_exn queue |> ignore;
remaining := !remaining + 1
done
with _ -> ());
Expand All @@ -21,15 +21,15 @@ let owner_stealer () =
Ws_deque.push queue 0
done;
for _ = 1 to total_items / 2 do
match Ws_deque.pop queue with
match Ws_deque.pop_exn queue with
| exception _ -> ()
| _ -> popped := !popped + 1
done);

(* stealer *)
Atomic.spawn (fun () ->
for _ = 1 to total_items / 2 do
match Ws_deque.steal queue with
match Ws_deque.steal_exn queue with
| exception _ -> ()
| _ -> popped := !popped + 1
done);
Expand All @@ -50,7 +50,7 @@ let popper_stealer () =
(* stealers *)
let popped = ref 0 in
let stealer () =
match Ws_deque.steal queue with
match Ws_deque.steal_exn queue with
| exception _ -> ()
| _ -> popped := !popped + 1
in
Expand Down

0 comments on commit 9e219b7

Please sign in to comment.