-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Replace synchronized(this) with lock-free initState in LazyList #26100
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
Open
lrytz
wants to merge
4
commits into
scala:main
Choose a base branch
from
lrytz:i25777
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 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
51 changes: 51 additions & 0 deletions
51
library-js/src/scala/collection/immutable/LazyListBase.scala
This file contains hidden or 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,51 @@ | ||
| /* | ||
| * Scala (https://www.scala-lang.org) | ||
| * | ||
| * Copyright EPFL and Lightbend, Inc. dba Akka | ||
| * | ||
| * Licensed under Apache License 2.0 | ||
| * (http://www.apache.org/licenses/LICENSE-2.0). | ||
| * | ||
| * See the NOTICE file distributed with this work for | ||
| * additional information regarding copyright ownership. | ||
| */ | ||
|
|
||
| package scala.collection.immutable | ||
|
|
||
| import scala.language.`2.13` | ||
|
|
||
| /** | ||
| * Base class for [[LazyList]] to split out code that uses concurrency utilities that are not available on Scala.js. | ||
| */ | ||
| abstract class LazyListBase[+A] private[immutable] (initialTail: AnyRef | Null) extends AbstractSeq[A] with Serializable { | ||
| /** See [[LazyList._head]] for the possible states of this field. */ | ||
| @volatile private var _tail: AnyRef | Null /* () => LazyList[A] | Thread | InRace | LazyList[A] | Null */ = initialTail | ||
|
|
||
| private[immutable] def rawTail: AnyRef | Null = _tail | ||
|
|
||
| private[immutable] def setRawTail(value: AnyRef): Unit = _tail = value | ||
|
|
||
| private[immutable] def makeTailUpdater: LazyListBase.TailUpdater = LazyListBase.TailUpdater() | ||
| } | ||
|
|
||
| private[immutable] object LazyListBase { | ||
| final class TailUpdater { | ||
| @inline def compareAndSet(ll: LazyListBase[?], expected: AnyRef, value: AnyRef): Boolean = | ||
| if (ll._tail eq expected) { ll._tail = value; true } else false | ||
|
|
||
| @inline def getAndSet(ll: LazyListBase[?], value: AnyRef | Null): AnyRef | Null = { | ||
| val old = ll._tail | ||
| ll._tail = value | ||
| old | ||
| } | ||
| } | ||
|
|
||
| def isCurrentThread(t: Thread): Boolean = true | ||
|
|
||
| def InRace(t: Thread): InRace = throw new Exception("unreachable") | ||
|
|
||
| final class InRace private[LazyListBase] (val owner: Thread) { | ||
| def await(): Unit = () | ||
| def countDown(): Unit = () | ||
| } | ||
| } | ||
54 changes: 54 additions & 0 deletions
54
library-js/src/scala/collection/immutable/LazyListIterableBase.scala
This file contains hidden or 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,54 @@ | ||
| /* | ||
| * Scala (https://www.scala-lang.org) | ||
| * | ||
| * Copyright EPFL and Lightbend, Inc. dba Akka | ||
| * | ||
| * Licensed under Apache License 2.0 | ||
| * (http://www.apache.org/licenses/LICENSE-2.0). | ||
| * | ||
| * See the NOTICE file distributed with this work for | ||
| * additional information regarding copyright ownership. | ||
| */ | ||
|
|
||
| package scala.collection.immutable | ||
|
|
||
| import scala.language.`2.13` | ||
| import language.experimental.captureChecking | ||
|
|
||
| /** | ||
| * Base class for [[LazyList]] to split out code that uses concurrency utilities that are not available on Scala.js. | ||
| */ | ||
| abstract class LazyListIterableBase[+A] private[immutable] (initialTail: (AnyRef | Null)^) extends Iterable[A] with Serializable { | ||
| /** See [[LazyList._head]] for the possible states of this field. */ | ||
| @volatile private var _tail: AnyRef^{this} | Null /* () => LazyList[A] | Thread | InRace | LazyList[A] | Null */ = | ||
| caps.unsafe.unsafeAssumePure(initialTail) | ||
|
|
||
| private[immutable] def rawTail: AnyRef^{this} | Null = _tail | ||
|
|
||
| private[immutable] def setRawTail(value: AnyRef^): Unit = _tail = caps.unsafe.unsafeAssumePure(value) | ||
|
|
||
| private[immutable] def makeTailUpdater: LazyListIterableBase.TailUpdater = LazyListIterableBase.TailUpdater() | ||
| } | ||
|
|
||
| private[immutable] object LazyListIterableBase { | ||
| import caps.unsafe.unsafeAssumePure | ||
|
|
||
| final class TailUpdater { | ||
| @inline def compareAndSet(ll: LazyListIterableBase[?]^, expected: AnyRef^, value: AnyRef^): Boolean = | ||
| if (ll._tail eq expected) { ll._tail = unsafeAssumePure(value); true } else false | ||
| @inline def getAndSet(ll: LazyListIterableBase[?]^, value: (AnyRef | Null)^): (AnyRef | Null)^ = { | ||
| val old = ll._tail | ||
| ll._tail = value.asInstanceOf[AnyRef | Null] | ||
| old | ||
| } | ||
| } | ||
|
|
||
| def isCurrentThread(t: Thread^): Boolean = true | ||
|
|
||
| def InRace(t: Thread^): InRace = throw new Exception("unreachable") | ||
|
|
||
| final class InRace private[LazyListIterableBase] (val owner: Thread) { | ||
| def await(): Unit = () | ||
| def countDown(): Unit = () | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,66 @@ | ||
| /* | ||
| * Scala (https://www.scala-lang.org) | ||
| * | ||
| * Copyright EPFL and Lightbend, Inc. dba Akka | ||
| * | ||
| * Licensed under Apache License 2.0 | ||
| * (http://www.apache.org/licenses/LICENSE-2.0). | ||
| * | ||
| * See the NOTICE file distributed with this work for | ||
| * additional information regarding copyright ownership. | ||
| */ | ||
|
|
||
| package scala.collection.immutable | ||
|
|
||
| import scala.language.`2.13` | ||
|
|
||
| import java.util.concurrent.CountDownLatch | ||
| import java.util.concurrent.atomic.AtomicReferenceFieldUpdater | ||
|
|
||
| /** | ||
| * Base class for [[LazyList]] to split out code that uses concurrency utilities that are not available | ||
| * on Scala.js. This way, Scala.js does not need to override all of LazyList. | ||
| * | ||
| * This class cannot be a trait because `AtomicReferenceFieldUpdater.newUpdater` checks if the caller | ||
| * class has access to the corresponding field. So it needs to be called in the class where the field is | ||
| * declared (fields are always private in Scala). | ||
| */ | ||
| abstract class LazyListBase[+A] private[immutable] (initialTail: AnyRef | Null) extends AbstractSeq[A] with Serializable { | ||
| /** See [[LazyList._head]] for the possible states of this field. */ | ||
| @volatile private var _tail: AnyRef | Null /* () => LazyList[A] | Thread | InRace | LazyList[A] | Null */ = initialTail | ||
|
|
||
| private[immutable] def rawTail: AnyRef | Null = _tail | ||
|
|
||
| private[immutable] def setRawTail(value: AnyRef): Unit = _tail = value | ||
|
|
||
| @noinline private[immutable] def makeTailUpdater: LazyListBase.TailUpdater = | ||
| new LazyListBase.TailUpdater(AtomicReferenceFieldUpdater.newUpdater(classOf[LazyListBase[?]], classOf[AnyRef], "_tail")) | ||
| } | ||
|
|
||
| private[immutable] object LazyListBase { | ||
| final class TailUpdater(u: AtomicReferenceFieldUpdater[LazyListBase[?], AnyRef]) { | ||
| def compareAndSet(ll: LazyListBase[?], expected: AnyRef, value: AnyRef): Boolean = u.compareAndSet(ll, expected, value) | ||
| def getAndSet(ll: LazyListBase[?], value: AnyRef | Null): AnyRef | Null = u.getAndSet(ll, value) | ||
| } | ||
|
|
||
| // this utility is constant `true` on Scala.js -> enables DCE in LazyList | ||
| def isCurrentThread(t: Thread): Boolean = t eq Thread.currentThread | ||
| // also for Scala.js | ||
| def InRace(t: Thread): InRace = new InRace(t) | ||
|
|
||
| final class InRace private[LazyListBase] (val owner: Thread) { | ||
| private val done: CountDownLatch = new CountDownLatch(1) | ||
|
|
||
| def await(): Unit = { | ||
| var interrupted = false | ||
| while (done.getCount > 0) { | ||
| try done.await() catch { | ||
| case _: InterruptedException => interrupted = true | ||
| } | ||
| } | ||
| if (interrupted) Thread.currentThread().interrupt() | ||
| } | ||
|
|
||
| def countDown(): Unit = done.countDown() | ||
| } | ||
| } |
Oops, something went wrong.
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.
As suggested in the Scala 2.js version of this PR (scala-js/scala-js#5358):