Skip to content

Commit 15d6477

Browse files
committed
Add Picos_std_sync.Queue
1 parent fbc4124 commit 15d6477

File tree

8 files changed

+542
-0
lines changed

8 files changed

+542
-0
lines changed

Diff for: bench/bench_queue.ml

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
open Multicore_bench
2+
open Picos_std_sync
3+
4+
let run_one_domain ~budgetf ?(n_msgs = 50 * Util.iter_factor) () =
5+
let t = Queue.create ~padded:true () in
6+
7+
let op push =
8+
if push then Queue.push t 101
9+
else match Queue.pop_exn t with _ -> () | exception Queue.Empty -> ()
10+
in
11+
12+
let init _ =
13+
assert (
14+
match Queue.pop_exn t with _ -> false | exception Queue.Empty -> true);
15+
Util.generate_push_and_pop_sequence n_msgs
16+
in
17+
let work _ bits = Util.Bits.iter op bits in
18+
19+
Times.record ~budgetf ~n_domains:1 ~init ~work ()
20+
|> Times.to_thruput_metrics ~n:n_msgs ~singular:"message" ~config:"one domain"
21+
22+
let run_one ~budgetf ~n_adders ~n_takers () =
23+
let n_domains = n_adders + n_takers in
24+
25+
let n_msgs = 50 * Util.iter_factor in
26+
27+
let t = Queue.create ~padded:true () in
28+
29+
let n_msgs_to_add = Countdown.create ~n_domains:n_adders () in
30+
let n_msgs_to_take = Countdown.create ~n_domains:n_takers () in
31+
32+
let init _ =
33+
assert (
34+
match Queue.pop_exn t with _ -> false | exception Queue.Empty -> true);
35+
Countdown.non_atomic_set n_msgs_to_add n_msgs;
36+
Countdown.non_atomic_set n_msgs_to_take n_msgs
37+
in
38+
let work i () =
39+
if i < n_adders then
40+
let rec work () =
41+
let n = Countdown.alloc n_msgs_to_add ~domain_index:i ~batch:1000 in
42+
if 0 < n then begin
43+
for i = 1 to n do
44+
Queue.push t i
45+
done;
46+
work ()
47+
end
48+
in
49+
work ()
50+
else
51+
let i = i - n_adders in
52+
let rec work () =
53+
let n = Countdown.alloc n_msgs_to_take ~domain_index:i ~batch:1000 in
54+
if 0 < n then
55+
let rec loop n =
56+
if 0 < n then begin
57+
match Queue.pop_exn t with
58+
| _ -> loop (n - 1)
59+
| exception Queue.Empty ->
60+
Backoff.once Backoff.default |> ignore;
61+
loop n
62+
end
63+
else work ()
64+
in
65+
loop n
66+
in
67+
work ()
68+
in
69+
70+
let config =
71+
let format role n =
72+
Printf.sprintf "%d %s%s" n role (if n = 1 then "" else "s")
73+
in
74+
Printf.sprintf "%s, %s"
75+
(format "nb adder" n_adders)
76+
(format "nb taker" n_takers)
77+
in
78+
Times.record ~budgetf ~n_domains ~init ~work ()
79+
|> Times.to_thruput_metrics ~n:n_msgs ~singular:"message" ~config
80+
81+
let run_suite ~budgetf =
82+
run_one_domain ~budgetf ()
83+
@ (Util.cross [ 1; 2; 4 ] [ 1; 2; 4 ]
84+
|> List.concat_map @@ fun (n_adders, n_takers) ->
85+
if Picos_domain.recommended_domain_count () < n_adders + n_takers then []
86+
else run_one ~budgetf ~n_adders ~n_takers ())

Diff for: bench/dune

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
(run %{test} -brief "Picos Mutex")
1212
(run %{test} -brief "Picos Semaphore")
1313
(run %{test} -brief "Picos Spawn")
14+
(run %{test} -brief "Picos Queue")
1415
(run %{test} -brief "Picos Yield")
1516
(run %{test} -brief "Picos Cancel_after with Picos_select")
1617
(run %{test} -brief "Ref with Picos_sync.Mutex")

Diff for: bench/main.ml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ let benchmarks =
99
("Picos DLS", Bench_dls.run_suite);
1010
("Picos Mutex", Bench_mutex.run_suite);
1111
("Picos Semaphore", Bench_semaphore.run_suite);
12+
("Picos Queue", Bench_queue.run_suite);
1213
("Picos Spawn", Bench_spawn.run_suite);
1314
("Picos Yield", Bench_yield.run_suite);
1415
("Picos Cancel_after with Picos_select", Bench_cancel_after.run_suite);

Diff for: lib/picos_std.sync/picos_std_sync.ml

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ module Semaphore = Semaphore
44
module Lazy = Lazy
55
module Latch = Latch
66
module Ivar = Ivar
7+
module Queue = Queue
78
module Stream = Stream

Diff for: lib/picos_std.sync/picos_std_sync.mli

+77
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,81 @@ module Ivar : sig
351351
variable has either been assigned a value or has been poisoned. *)
352352
end
353353

354+
module Queue : sig
355+
(** A lock-free multi-producer, multi-consumer queue. *)
356+
357+
(** {1 API} *)
358+
359+
type !'a t
360+
(** A multi-producer, multi-consumer queue. *)
361+
362+
val create : ?padded:bool -> unit -> 'a t
363+
(** [create ()] returns a new empty multi-producer, multi-consumer queue. *)
364+
365+
val push : 'a t -> 'a -> unit
366+
(** [push queue value] adds the [value] to the tail of the [queue]. *)
367+
368+
val push_head : 'a t -> 'a -> unit
369+
(** [push_head queue value] adds the [value] to the head of the [queue]. *)
370+
371+
exception Empty
372+
(** Raised by {!pop_exn} in case it finds the queue empty. *)
373+
374+
val pop_exn : 'a t -> 'a
375+
(** [pop_exn queue] tries to remove the value at the head of the [queue].
376+
Returns the removed value or raises {!Empty} in case the queue was empty.
377+
378+
@raise Empty in case the queue was empty. *)
379+
380+
val pop_opt : 'a t -> 'a option
381+
(** [pop_opt queue] tries to remove the value at the head of the [queue].
382+
Returns the removed value or [None] in case the queue was empty. *)
383+
384+
val pop : 'a t -> 'a
385+
(** [pop queue] waits until the queue is not empty, removes the value at the
386+
head of the [queue], and returns it. *)
387+
388+
val length : 'a t -> int
389+
(** [length queue] returns the length or the number of values in the
390+
[queue]. *)
391+
392+
val is_empty : 'a t -> bool
393+
(** [is_empty queue] is equivalent to {{!length} [length queue = 0]}. *)
394+
395+
(** {1 Examples}
396+
397+
An example top-level session:
398+
{[
399+
# let q : int Queue.t =
400+
Queue.create ()
401+
val q : int Picos_std_sync.Queue.t = <abstr>
402+
403+
# Queue.push q 42
404+
- : unit = ()
405+
406+
# Queue.push_head q 76
407+
- : unit = ()
408+
409+
# Queue.length q
410+
- : int = 2
411+
412+
# Queue.push q 101
413+
- : unit = ()
414+
415+
# Queue.pop_exn q
416+
- : int = 76
417+
418+
# Queue.pop_exn q
419+
- : int = 42
420+
421+
# Queue.pop_exn q
422+
- : int = 101
423+
424+
# Queue.pop_exn q
425+
Exception: Picos_std_sync__Queue.Empty.
426+
]} *)
427+
end
428+
354429
module Stream : sig
355430
(** A lock-free, poisonable, many-to-many, stream.
356431
@@ -423,6 +498,8 @@ end
423498
val push : 'a t -> 'a -> unit
424499
val pop : 'a t -> 'a
425500
end = struct
501+
module Queue = Stdlib.Queue
502+
426503
type 'a t = {
427504
mutex : Mutex.t;
428505
queue : 'a Queue.t;

0 commit comments

Comments
 (0)