Skip to content

Commit

Permalink
Merge pull request #4042 from dumarchie/cas
Browse files Browse the repository at this point in the history
Make cas($target, &code) ~17% faster
  • Loading branch information
lizmat committed Nov 21, 2020
2 parents 1cd8765 + 484f870 commit 96ab6eb
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/core.c/atomicops.pm6
Expand Up @@ -24,12 +24,15 @@ multi sub cas($target is rw, \expected, \value) {
}
multi sub cas($target is rw, &code) {
my $current := nqp::atomicload($target);
loop {
my $updated := code($current);
my $seen := nqp::cas($target, $current, $updated);
return $updated if nqp::eqaddr($seen, $current);
$current := $seen;
}
nqp::until(
nqp::stmts(
(my $updated := code($current)),
(my $seen := nqp::cas($target, $current, $updated)),
nqp::eqaddr($seen, $current)
),
$current := $seen
);
$updated
}

#== Native integer atomics only available on MoarVM ==============================
Expand Down

0 comments on commit 96ab6eb

Please sign in to comment.