CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-scala-native--auxlib-native0-5-3

Auxiliary libraries for Scala Native runtime providing essential runtime support including boxed types, concurrent collections, and utility functions

Pending
Overview
Eval results
Files

concurrent-collections.mddocs/

Concurrent Collection Infrastructure

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.

Core Imports

import scala.collection.concurrent.{BasicNode, MainNode, INodeBase, CNodeBase}
import java.util.concurrent.atomic.{AtomicReferenceFieldUpdater, AtomicIntegerFieldUpdater}
import scala.scalanative.unsafe.Ptr

Capabilities

Basic Node Infrastructure

Abstract base class for all concurrent collection nodes.

abstract class BasicNode {
  def string(lev: Int): String
}

Main Node Management

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[_, _]]
}

INode Base Implementation

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
}

CNode Base Implementation

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 Management

Generation marker for concurrent collections.

final class Gen // private[concurrent]

Atomic Field Updaters

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
}

Usage Examples

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)
  }
}

Thread Safety

All operations in this infrastructure are designed for concurrent access:

  • @volatile fields ensure memory visibility
  • Atomic field updaters provide lock-free compare-and-swap operations
  • Memory ordering guarantees are maintained through proper use of atomic operations
  • The infrastructure supports building high-performance concurrent data structures without explicit locking

Install with Tessl CLI

npx tessl i tessl/maven-org-scala-native--auxlib-native0-5-3

docs

array-runtime.md

boxing-unboxing.md

concurrent-collections.md

index.md

mathematical-support.md

reference-types.md

tile.json