Skip to content
Permalink
Browse files
8271855: [TESTBUG] Wrong weakCompareAndSet assumption in UnsafeIntrin…
…sicsTest

Backport-of: c4d115701d102c33af937ca25dda8ac50117ac6b
  • Loading branch information
TheRealMDoerr committed Oct 18, 2021
1 parent 8b62d54 commit b0e49bb95698a6cc8a306547b7a09e91ab24ee6b
Showing 1 changed file with 15 additions and 9 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -24,7 +24,7 @@
/*
* @test id=z
* @key randomness
* @bug 8059022
* @bug 8059022 8271855
* @modules java.base/jdk.internal.misc:+open
* @summary Validate barriers after Unsafe getReference, CAS and swap (GetAndSet)
* @requires vm.gc.Z
@@ -302,15 +302,15 @@ private Node mergeImplSwap(Node startNode, Node expectedNext, Node head) {
private Node mergeImplCAS(Node startNode, Node expectedNext, Node head) {
// CAS - should always be true within a single thread - no other thread can have overwritten
if (!UNSAFE.compareAndSetReference(startNode, offset, expectedNext, head)) {
throw new Error("CAS should always succeed on thread local objects, check you barrier implementation");
throw new Error("CAS should always succeed on thread local objects, check your barrier implementation");
}
return expectedNext; // continue on old circle
}

private Node mergeImplCASFail(Node startNode, Node expectedNext, Node head) {
// Force a fail
if (UNSAFE.compareAndSetReference(startNode, offset, "fail", head)) {
throw new Error("This CAS should always fail, check you barrier implementation");
throw new Error("This CAS should always fail, check your barrier implementation");
}
if (startNode.next() != expectedNext) {
throw new Error("Shouldn't have changed");
@@ -319,17 +319,23 @@ private Node mergeImplCASFail(Node startNode, Node expectedNext, Node head) {
}

private Node mergeImplWeakCAS(Node startNode, Node expectedNext, Node head) {
// Weak CAS - should always be true within a single thread - no other thread can have overwritten
if (!UNSAFE.weakCompareAndSetReference(startNode, offset, expectedNext, head)) {
throw new Error("Weak CAS should always succeed on thread local objects, check you barrier implementation");
// Weak CAS - should almost always be true within a single thread - no other thread can have overwritten
// Spurious failures are allowed. So, we retry a couple of times on failure.
boolean ok = false;
for (int i = 0; i < 3; ++i) {
ok = UNSAFE.weakCompareAndSetReference(startNode, offset, expectedNext, head);
if (ok) break;
}
if (!ok) {
throw new Error("Weak CAS should almost always succeed on thread local objects, check your barrier implementation");
}
return expectedNext; // continue on old circle
}

private Node mergeImplWeakCASFail(Node startNode, Node expectedNext, Node head) {
// Force a fail
if (UNSAFE.weakCompareAndSetReference(startNode, offset, "fail", head)) {
throw new Error("This weak CAS should always fail, check you barrier implementation");
throw new Error("This weak CAS should always fail, check your barrier implementation");
}
if (startNode.next() != expectedNext) {
throw new Error("Shouldn't have changed");
@@ -341,7 +347,7 @@ private Node mergeImplCMPX(Node startNode, Node expectedNext, Node head) {
// CmpX - should always be true within a single thread - no other thread can have overwritten
Object res = UNSAFE.compareAndExchangeReference(startNode, offset, expectedNext, head);
if (!res.equals(expectedNext)) {
throw new Error("Fail CmpX should always succeed on thread local objects, check you barrier implementation");
throw new Error("Fail CmpX should always succeed on thread local objects, check your barrier implementation");
}
return expectedNext; // continue on old circle
}

1 comment on commit b0e49bb

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on b0e49bb Oct 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.