Skip to content

Commit

Permalink
Add a simple float test and prevent inlining
Browse files Browse the repository at this point in the history
This prevents Flamda from noticing that we are potentially storing float values
into a non-float array.
  • Loading branch information
polytypic committed Apr 2, 2024
1 parent beaec4f commit b6e62a1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src_lockfree/spsc_queue.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ let create ~size_exponent =

type _ mono = Unit : unit mono | Bool : bool mono

let push_as (type r) t element (mono : r mono) : r =
(* NOTE: Uses of [@inline never] prevent Flambda from noticing that we might be
storing float values into a non-float array. *)

let[@inline never] push_as (type r) t element (mono : r mono) : r =
let size = Array.length t.array in
let tail = Atomic.fenceless_get t.tail in
let head_cache = !(t.head_cache) in
Expand All @@ -75,7 +78,7 @@ exception Empty
type ('a, _) poly = Option : ('a, 'a option) poly | Value : ('a, 'a) poly
type op = Peek | Pop

let pop_or_peek_as (type a r) (t : a t) op (poly : (a, r) poly) : r =
let[@inline never] pop_or_peek_as (type a r) t op (poly : (a, r) poly) : r =
let head = Atomic.fenceless_get t.head in
let tail_cache = !(t.tail_cache) in
if
Expand Down
8 changes: 7 additions & 1 deletion test/spsc_queue/test_spsc_queue.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ let test_parallel () =
Domain.join producer;
Printf.printf "test_spsc_queue_parallel: ok (transferred = %d)\n" !last_num

let test_float () =
let q = Spsc_queue.create ~size_exponent:1 in
assert (Spsc_queue.try_push q 1.01);
assert (Spsc_queue.pop_opt q = Some 1.01)

let _ =
test_empty ();
test_full ();
test_parallel ()
test_parallel ();
test_float ()

0 comments on commit b6e62a1

Please sign in to comment.