Skip to content

Commit 897da96

Browse files
committed
Add ?backoff to Loc.compare_and_set
`Loc.compare_and_set` has a mutating retry loop, so it makes sense to include a backoff mechanism.
1 parent 0386004 commit 897da96

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/kcas/kcas.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ let[@inline] is_obstruction_free which loc =
508508
(* Fenceless is safe as we are accessing a private location. *)
509509
fenceless_get (root_as_atomic which) == R Mode.obstruction_free && 0 <= loc.id
510510

511-
let[@inline] rec cas_with_state loc before state state_old =
511+
let[@inline] rec cas_with_state backoff loc before state state_old =
512512
before == eval state_old
513513
&& (before == state.after
514514
||
@@ -522,7 +522,8 @@ let[@inline] rec cas_with_state loc before state state_old =
522522
having installed or removed a waiter.
523523
524524
Fenceless is safe as there was a fence before. *)
525-
cas_with_state loc before state (fenceless_get (as_atomic loc)))
525+
cas_with_state (Backoff.once backoff) loc before state
526+
(fenceless_get (as_atomic loc)))
526527

527528
let inc x = x + 1
528529
let dec x = x - 1
@@ -571,10 +572,10 @@ module Loc = struct
571572
let[@inline] get_mode loc =
572573
if loc.id < 0 then Mode.lock_free else Mode.obstruction_free
573574

574-
let compare_and_set loc before after =
575+
let compare_and_set ?(backoff = Backoff.default) loc before after =
575576
let state = new_state after in
576577
let state_old = atomic_get (as_atomic loc) in
577-
cas_with_state loc before state state_old
578+
cas_with_state backoff loc before state state_old
578579

579580
let fenceless_update ?timeoutf ?(backoff = Backoff.default) loc f =
580581
let timeout = Timeout.alloc_opt timeoutf in
@@ -898,7 +899,7 @@ module Xt = struct
898899
(* Fenceless is safe inside transactions as each log update has a
899900
fence. *)
900901
let state_old = fenceless_get (as_atomic loc) in
901-
if cas_with_state loc before state state_old then
902+
if cas_with_state Backoff.default loc before state state_old then
902903
success xt result
903904
else commit (Backoff.once backoff) mode (reset_quick xt) tx
904905
end

src/kcas/kcas.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ module Loc : sig
215215
conditional load. It is also safe for the given function [f] to raise any
216216
other exception to abort the conditional load. *)
217217

218-
val compare_and_set : 'a t -> 'a -> 'a -> bool
218+
val compare_and_set : ?backoff:Backoff.t -> 'a t -> 'a -> 'a -> bool
219219
(** [compare_and_set r before after] atomically updates the shared memory
220220
location [r] to the [after] value if the current value of [r] is the
221221
[before] value. *)

0 commit comments

Comments
 (0)