-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mixin fields with trait setters shouldn't be JVM final #7028
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package scala.runtime; | ||
|
||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.MethodType; | ||
import java.lang.reflect.Field; | ||
|
||
public class VM { | ||
public static final MethodHandle RELEASE_FENCE; | ||
|
||
|
||
static { | ||
RELEASE_FENCE = mkHandle(); | ||
} | ||
|
||
private static MethodHandle mkHandle() { | ||
MethodHandles.Lookup lookup = MethodHandles.lookup(); | ||
try { | ||
return lookup.findStatic(Class.forName("java.lang.invoke.VarHandle"), "releaseFence", MethodType.methodType(Void.TYPE)); | ||
} catch (ClassNotFoundException e) { | ||
try { | ||
Class<?> unsafeClass = Class.forName("sun.misc.Unsafe"); | ||
return lookup.findVirtual(unsafeClass, "storeFence", MethodType.methodType(void.class)).bindTo(findUnsafe(unsafeClass)); | ||
} catch (NoSuchMethodException | ClassNotFoundException | IllegalAccessException e1) { | ||
ExceptionInInitializerError error = new ExceptionInInitializerError(e1); | ||
error.addSuppressed(e); | ||
throw error; | ||
} | ||
} catch (NoSuchMethodException | IllegalAccessException e) { | ||
throw new ExceptionInInitializerError(e); | ||
} | ||
} | ||
|
||
private static Object findUnsafe(Class<?> unsafeClass) throws IllegalAccessException { | ||
Object found = null; | ||
for (Field field : unsafeClass.getDeclaredFields()) { | ||
if (field.getType() == unsafeClass) { | ||
field.setAccessible(true); | ||
found = field.get(null); | ||
break; | ||
} | ||
} | ||
if (found == null) throw new IllegalStateException("No instance of Unsafe found"); | ||
return found; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.gz | ||
results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
enablePlugins(JCStressPlugin) | ||
|
||
scalaVersion := "2.13.0-pre-SNAPSHOT" | ||
|
||
scalaHome := Some(baseDirectory.value / "../../build/pack") | ||
version in Jcstress := "0.4" | ||
resolvers in Global += Resolver.mavenLocal | ||
crossPaths := false | ||
|
||
// workaround https://github.com/ktoso/sbt-jcstress/issues/12 | ||
internalDependencyClasspath in Test := Nil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version=1.1.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
addSbtPlugin("pl.project13.scala" % "sbt-jcstress" % "0.2.0") |
86 changes: 86 additions & 0 deletions
86
test/jcstress/src/test/scala/example/APISample_01_Simple.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package example | ||
|
||
/* | ||
* Copyright (c) 2016, Red Hat Inc. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Oracle designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Oracle in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import java.util.concurrent.atomic._ | ||
|
||
import org.openjdk.jcstress.annotations.Outcome.Outcomes | ||
import org.openjdk.jcstress.annotations._ | ||
import org.openjdk.jcstress.infra.results.II_Result | ||
|
||
/* | ||
This is our first concurrency test. It is deliberately simplistic to show | ||
testing approaches, introduce JCStress APIs, etc. | ||
Suppose we want to see if the field increment is atomic. We can make test | ||
with two actors, both actors incrementing the field and recording what | ||
value they observed into the result object. As JCStress runs, it will | ||
invoke these methods on the objects holding the field once per each actor | ||
and instance, and record what results are coming from there. | ||
Done enough times, we will get the history of observed results, and that | ||
would tell us something about the concurrent behavior. For example, running | ||
this test would yield: | ||
[OK] o.o.j.t.JCStressSample_01_Simple | ||
(JVM args: [-server]) | ||
Observed state Occurrences Expectation Interpretation | ||
1, 1 54,734,140 ACCEPTABLE Both threads came up with the same value: atomicity failure. | ||
1, 2 47,037,891 ACCEPTABLE actor1 incremented, then actor2. | ||
2, 1 53,204,629 ACCEPTABLE actor2 incremented, then actor1. | ||
How to run this test: | ||
$ java -jar jcstress-samples/target/jcstress.jar -t JCStress_APISample_01_Simple | ||
*/ | ||
|
||
// Mark the class as JCStress test. | ||
@JCStressTest | ||
@Description("Simple test, checking AtomicInteger") | ||
@Outcomes(Array( | ||
new Outcome( | ||
id = Array("1, 1"), | ||
expect = Expect.ACCEPTABLE_INTERESTING, | ||
desc = "Both actors came up with the same value: atomicity failure."), | ||
new Outcome( | ||
id = Array("1, 2"), | ||
expect = Expect.ACCEPTABLE, | ||
desc = "actor1 incremented, then actor2."), | ||
new Outcome(id = Array("2, 1"), | ||
expect = Expect.ACCEPTABLE, desc = "actor2 incremented, then actor1.") | ||
) | ||
) | ||
@State | ||
class APISample_01_Simple { | ||
|
||
var v = new AtomicInteger(0) | ||
|
||
@Actor | ||
def actor1(r: II_Result): Unit = { | ||
r.r1 = v.incrementAndGet() // record result from actor1 to field r1 | ||
} | ||
|
||
@Actor | ||
def actor2(r: II_Result): Unit = { | ||
r.r2 = v.incrementAndGet() // record result from actor1 to field r1 | ||
} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
test/jcstress/src/test/scala/scala/collection/immutable/ListLikeStressTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package scala.collection.immutable | ||
|
||
import org.openjdk.jcstress.annotations._ | ||
import org.openjdk.jcstress.annotations.Outcome.Outcomes | ||
import org.openjdk.jcstress.infra.results._ | ||
|
||
@JCStressTest | ||
@Outcomes(Array( | ||
new Outcome(id = Array("-1, 0"), expect = Expect.ACCEPTABLE, desc = "Read before write"), | ||
new Outcome(id = Array("16, 0"), expect = Expect.ACCEPTABLE, desc = "Read after all writes") | ||
)) | ||
@State | ||
class ListLikeStressTest { | ||
|
||
var v: ListLike = _ | ||
|
||
@Actor | ||
def actor1(r: II_Result): Unit = { | ||
val list = new ListLike("a") | ||
var l = list | ||
l.tail = new ListLike("a") | ||
l = l.tail | ||
l.tail = new ListLike("a") | ||
l = l.tail | ||
l.tail = new ListLike("a") | ||
l = l.tail | ||
l.tail = new ListLike("a") | ||
l = l.tail | ||
l.tail = new ListLike("a") | ||
l = l.tail | ||
l.tail = new ListLike("a") | ||
l = l.tail | ||
l.tail = new ListLike("a") | ||
l = l.tail | ||
l.tail = new ListLike("a") | ||
l = l.tail | ||
l.tail = new ListLike("a") | ||
l = l.tail | ||
l.tail = new ListLike("a") | ||
l = l.tail | ||
l.tail = new ListLike("a") | ||
l = l.tail | ||
l.tail = new ListLike("a") | ||
l = l.tail | ||
l.tail = new ListLike("a") | ||
l = l.tail | ||
l.tail = new ListLike("a") | ||
l = l.tail | ||
l.tail = new ListLike("a") | ||
l = l.tail | ||
l.tail = NilLike | ||
//java.lang.invoke.VarHandle.releaseFence() | ||
v = list | ||
|
||
} | ||
|
||
@Actor | ||
def actor2(r: II_Result): Unit = { | ||
var l = v | ||
if (l eq null) { | ||
r.r1 = -1 | ||
r.r2 = 0 | ||
return | ||
} | ||
var len = 0 | ||
var nulls = 0 | ||
while (l ne NilLike) { | ||
if (l eq null) {r.r1 = len; r.r2 = nulls + 1; return} | ||
if (l.head eq null) nulls += 1 | ||
assert(l ne l.tail) | ||
l = l.tail | ||
len += 1 | ||
} | ||
r.r1 = len | ||
r.r2 = nulls | ||
|
||
} | ||
|
||
} | ||
|
||
class ListLike(var head: AnyRef) { | ||
var tail: ListLike = _ | ||
} | ||
|
||
object NilLike extends ListLike(null) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could use a comment: If the field is mutable, it won't be final, so we can write to it in a setter. If it's not, we still need to initialize it, and make sure it's safely published. Since initialization is performed (lexically) outside of the constructor (in the trait setter), we have to make the field mutable starting with classfile format 53 (it was never allowed, but the verifier enforces this now).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated with this comment