-
Notifications
You must be signed in to change notification settings - Fork 361
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
Allow to use scala.collection.concurrent.TrieMap
#3149
Merged
WojciechMazur
merged 7 commits into
scala-native:main
from
WojciechMazur:feautre/multithreading-scc.TrieMap
Feb 9, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d812010
Implement java-defined types required by `scala.collection.concurrent…
WojciechMazur 9464aa6
Add clib dependency to auxlib (requires c atomics)
WojciechMazur bcdaf07
Remove no longer needed filtering of scalalib sources
WojciechMazur 06b1f41
Fix scalalib scala.collection.immutable.VM implementation for Scala 2.12
WojciechMazur e2283e3
Update scala partest-junit tess to use now support concurreny primitives
WojciechMazur 4002773
Use TrieMap instead of threadlocal or non-concurrency-ready types
WojciechMazur 850a6d2
Fix compilation of Scala 2.12 scalalib with JDK8
WojciechMazur File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
auxlib/src/main/scala/scala/collection/concurrent/BasicNode.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,6 @@ | ||
// Ported from Scala 2.13.10 | ||
package scala.collection.concurrent | ||
|
||
abstract class BasicNode { | ||
def string(lev: Int): String | ||
} |
27 changes: 27 additions & 0 deletions
27
auxlib/src/main/scala/scala/collection/concurrent/CNodeBase.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,27 @@ | ||
package scala.collection.concurrent | ||
|
||
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater | ||
|
||
import scala.scalanative.annotation.alwaysinline | ||
import scala.scalanative.runtime.Intrinsics.classFieldRawPtr | ||
import scala.scalanative.runtime.fromRawPtr | ||
|
||
private[concurrent] abstract class CNodeBase[K <: AnyRef, V <: AnyRef] | ||
extends MainNode[K, V] { | ||
@volatile var csize: Int = -1 | ||
|
||
final val updater: AtomicIntegerFieldUpdater[CNodeBase[_, _]] = | ||
new IntrinsicAtomicIntegerFieldUpdater(obj => | ||
fromRawPtr(classFieldRawPtr(obj, "csize")) | ||
) | ||
|
||
@alwaysinline | ||
def CAS_SIZE(oldval: Int, nval: Int) = | ||
updater.compareAndSet(this, oldval, nval) | ||
|
||
@alwaysinline | ||
def WRITE_SIZE(nval: Int): Unit = updater.set(this, nval) | ||
|
||
@alwaysinline | ||
def READ_SIZE: Int = updater.get(this) | ||
} |
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,5 @@ | ||
// Ported from Scala 2.13.10 | ||
|
||
package scala.collection.concurrent | ||
|
||
private[concurrent] final class Gen {} |
28 changes: 28 additions & 0 deletions
28
auxlib/src/main/scala/scala/collection/concurrent/INodeBase.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,28 @@ | ||
// Ported from Scala 2.13.10 | ||
|
||
package scala.collection.concurrent | ||
|
||
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater | ||
|
||
import scala.scalanative.runtime.Intrinsics.classFieldRawPtr | ||
import scala.scalanative.runtime.fromRawPtr | ||
|
||
object INodeBase { | ||
final val updater | ||
: AtomicReferenceFieldUpdater[INodeBase[_, _], MainNode[_, _]] = | ||
new IntrinsicAtomicReferenceFieldUpdater(obj => | ||
fromRawPtr(classFieldRawPtr(obj, "mainnode")) | ||
) | ||
|
||
final val RESTART = new Object {} | ||
final val NO_SUCH_ELEMENT_SENTINEL = new Object {} | ||
} | ||
|
||
private[concurrent] abstract class INodeBase[K <: AnyRef, V <: AnyRef]( | ||
generation: Gen | ||
) extends BasicNode { | ||
@volatile var mainnode: MainNode[K, V] = _ | ||
final var gen: Gen = generation | ||
|
||
def prev(): BasicNode = null | ||
} |
61 changes: 61 additions & 0 deletions
61
auxlib/src/main/scala/scala/collection/concurrent/IntrinsicAtomicFieldUpdaters.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,61 @@ | ||
package scala.collection.concurrent | ||
|
||
import java.util.concurrent.atomic.{ | ||
AtomicIntegerFieldUpdater, | ||
AtomicReferenceFieldUpdater | ||
} | ||
|
||
import scala.scalanative.runtime.Intrinsics.classFieldRawPtr | ||
import scala.scalanative.runtime.{RawPtr, fromRawPtr} | ||
import scala.scalanative.annotation.alwaysinline | ||
import scala.scalanative.libc.atomic.{CAtomicRef, CAtomicInt, memory_order} | ||
import scala.scalanative.unsafe.Ptr | ||
|
||
private[concurrent] class IntrinsicAtomicReferenceFieldUpdater[ | ||
T <: AnyRef, | ||
V <: AnyRef | ||
](@alwaysinline selector: T => Ptr[V]) | ||
extends AtomicReferenceFieldUpdater[T, V]() { | ||
@alwaysinline private def atomicRef(insideObj: T) = | ||
new CAtomicRef[V](selector(insideObj)) | ||
|
||
@alwaysinline def compareAndSet(obj: T, expect: V, update: V): Boolean = | ||
atomicRef(obj).compareExchangeStrong(expect, update) | ||
|
||
@alwaysinline def weakCompareAndSet(obj: T, expect: V, update: V): Boolean = | ||
atomicRef(obj).compareExchangeWeak(expect, update) | ||
|
||
@alwaysinline def set(obj: T, newIntalue: V): Unit = | ||
atomicRef(obj).store(newIntalue) | ||
|
||
@alwaysinline def lazySet(obj: T, newIntalue: V): Unit = | ||
atomicRef(obj).store(newIntalue, memory_order.memory_order_release) | ||
|
||
@alwaysinline def get(obj: T): V = atomicRef(obj).load() | ||
} | ||
|
||
class IntrinsicAtomicIntegerFieldUpdater[T <: AnyRef]( | ||
@alwaysinline selector: T => Ptr[Int] | ||
) extends AtomicIntegerFieldUpdater[T]() { | ||
@alwaysinline private def atomicRef(insideObj: T) = new CAtomicInt( | ||
selector(insideObj) | ||
) | ||
|
||
@alwaysinline def compareAndSet(obj: T, expect: Int, update: Int): Boolean = | ||
atomicRef(obj).compareExchangeStrong(expect, update) | ||
|
||
@alwaysinline def weakCompareAndSet( | ||
obj: T, | ||
expect: Int, | ||
update: Int | ||
): Boolean = | ||
atomicRef(obj).compareExchangeWeak(expect, update) | ||
|
||
@alwaysinline def set(obj: T, newIntalue: Int): Unit = | ||
atomicRef(obj).store(newIntalue) | ||
|
||
@alwaysinline def lazySet(obj: T, newIntalue: Int): Unit = | ||
atomicRef(obj).store(newIntalue, memory_order.memory_order_release) | ||
|
||
@alwaysinline def get(obj: T): Int = atomicRef(obj).load() | ||
} |
38 changes: 38 additions & 0 deletions
38
auxlib/src/main/scala/scala/collection/concurrent/MainNode.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,38 @@ | ||
package scala.collection.concurrent | ||
|
||
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater | ||
|
||
import scala.scalanative.annotation.alwaysinline | ||
import scala.scalanative.runtime.Intrinsics.classFieldRawPtr | ||
import scala.scalanative.runtime.fromRawPtr | ||
|
||
object MainNode { | ||
final val updater | ||
: AtomicReferenceFieldUpdater[MainNode[_, _], MainNode[_, _]] = | ||
new IntrinsicAtomicReferenceFieldUpdater(obj => | ||
fromRawPtr(classFieldRawPtr(obj, "prev")) | ||
) | ||
} | ||
|
||
private[concurrent] abstract class MainNode[K <: AnyRef, V <: AnyRef] | ||
extends BasicNode { | ||
import MainNode.updater | ||
|
||
@volatile var prev: MainNode[K, V] = _ | ||
|
||
def cachedSize(ct: Object): Int | ||
|
||
// standard contract | ||
def knownSize(): Int | ||
|
||
@alwaysinline | ||
def CAS_PREV(oldval: MainNode[K, V], nval: MainNode[K, V]) = | ||
updater.compareAndSet(this, oldval, nval) | ||
|
||
@alwaysinline | ||
def WRITE_PREV(nval: MainNode[K, V]): Unit = updater.set(this, nval) | ||
|
||
@deprecated | ||
@alwaysinline def READ_PREV(): MainNode[K, V] = | ||
updater.get(this).asInstanceOf[MainNode[K, V]] | ||
} |
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
2 changes: 2 additions & 0 deletions
2
scala-partest-junit-tests/src/test/resources/2.13.10/BlacklistedTests-require-threads.txt
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 @@ | ||
scala/collection/convert/MapWrapperTest.scala | ||
scala/collection/concurrent/TrieMapTest.scala |
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.
Is there still a plan to port
ConcurrentHashMap
at a later point? I'd have to read the paper again but I thinkTrieMap
makes different performance trade-offs thanConcurrentHashMap
to support some special features.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.
Yes, at some point I wish we port all the collections from JSR-166. The implementation ported from Scala.js is great, but I'm worried that it's not safe enough for usage in a multithreading environment.
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.
Oh, definitely cannot use the current implementation in multi-threading :) we rely a lot on
ConcurrentHashMap
in Cats Effect.