Auxiliary libraries for Scala Native runtime providing essential runtime support including boxed types, concurrent collections, and utility functions
—
Base classes and atomic field updaters for building lock-free concurrent data structures. This infrastructure provides the foundation for Scala's concurrent collections, implementing atomic operations and memory management patterns.
import scala.collection.concurrent.{BasicNode, MainNode, INodeBase, CNodeBase}
import java.util.concurrent.atomic.{AtomicReferenceFieldUpdater, AtomicIntegerFieldUpdater}
import scala.scalanative.unsafe.PtrAbstract base class for all concurrent collection nodes.
abstract class BasicNode {
def string(lev: Int): String
}Base class for main nodes in concurrent collections with atomic previous node management.
abstract class MainNode[K <: AnyRef, V <: AnyRef] extends BasicNode {
@volatile var prev: MainNode[K, V]
def cachedSize(ct: Object): Int
def knownSize(): Int
def CAS_PREV(oldval: MainNode[K, V], nval: MainNode[K, V]): Boolean
def WRITE_PREV(nval: MainNode[K, V]): Unit
@deprecated def READ_PREV(): MainNode[K, V]
}
object MainNode {
val updater: AtomicReferenceFieldUpdater[MainNode[_, _], MainNode[_, _]]
}Base class for I-nodes (indirection nodes) in concurrent trie-based collections.
abstract class INodeBase[K <: AnyRef, V <: AnyRef](generation: Gen) extends BasicNode {
@volatile var mainnode: MainNode[K, V]
final var gen: Gen
def prev(): BasicNode
}
object INodeBase {
val updater: AtomicReferenceFieldUpdater[INodeBase[_, _], MainNode[_, _]]
val RESTART: Object
val NO_SUCH_ELEMENT_SENTINEL: Object
}Base class for C-nodes (collision nodes) with atomic size management.
abstract class CNodeBase[K <: AnyRef, V <: AnyRef] extends MainNode[K, V] {
@volatile var csize: Int
final val updater: AtomicIntegerFieldUpdater[CNodeBase[_, _]]
def CAS_SIZE(oldval: Int, nval: Int): Boolean
def WRITE_SIZE(nval: Int): Unit
def READ_SIZE: Int
}Generation marker for concurrent collections.
final class Gen // private[concurrent]Intrinsic atomic field updaters providing lock-free operations.
class IntrinsicAtomicReferenceFieldUpdater[T <: AnyRef, V <: AnyRef](
selector: T => Ptr[V]
) extends AtomicReferenceFieldUpdater[T, V] {
def compareAndSet(obj: T, expect: V, update: V): Boolean
def weakCompareAndSet(obj: T, expect: V, update: V): Boolean
def set(obj: T, newValue: V): Unit
def lazySet(obj: T, newValue: V): Unit
def get(obj: T): V
}
class IntrinsicAtomicIntegerFieldUpdater[T <: AnyRef](
selector: T => Ptr[Int]
) extends AtomicIntegerFieldUpdater[T] {
def compareAndSet(obj: T, expect: Int, update: Int): Boolean
def weakCompareAndSet(obj: T, expect: Int, update: Int): Boolean
def set(obj: T, newValue: Int): Unit
def lazySet(obj: T, newValue: Int): Unit
def get(obj: T): Int
}import scala.collection.concurrent.{BasicNode, MainNode, INodeBase, CNodeBase}
// Extending BasicNode for custom node types
class MyNode extends BasicNode {
def string(lev: Int): String = s"MyNode(level=$lev)"
}
// Using atomic operations on MainNode
class MyMainNode[K <: AnyRef, V <: AnyRef] extends MainNode[K, V] {
def cachedSize(ct: Object): Int = 0
def knownSize(): Int = 0
// Atomic compare-and-swap operation
def updatePrevious(expected: MainNode[K, V], newNode: MainNode[K, V]): Boolean = {
CAS_PREV(expected, newNode)
}
}
// Using CNodeBase for size management
class MyCNode[K <: AnyRef, V <: AnyRef] extends CNodeBase[K, V] {
def cachedSize(ct: Object): Int = READ_SIZE
def knownSize(): Int = READ_SIZE
// Atomic size update
def incrementSize(): Boolean = {
val currentSize = READ_SIZE
CAS_SIZE(currentSize, currentSize + 1)
}
}All operations in this infrastructure are designed for concurrent access:
@volatile fields ensure memory visibilityInstall with Tessl CLI
npx tessl i tessl/maven-org-scala-native--auxlib-native0-5-3