Package kotlin.native.concurrent

Types

AtomicInt
Link copied to clipboard
class AtomicInt(value_: Int)

Atomic values and freezing: atomics AtomicInt, AtomicLong, AtomicNativePtr and AtomicReference are unique types with regard to freezing. Namely, they provide mutating operations, while can participate in frozen subgraphs. So shared frozen objects can have fields of atomic types.

AtomicLong
Link copied to clipboard
class AtomicLong(value_: Long)
AtomicNativePtr
Link copied to clipboard
class AtomicNativePtr(value_: NativePtr)
AtomicReference
Link copied to clipboard
class AtomicReference<T>

An atomic reference to a frozen Kotlin object. Can be used in concurrent scenarious but frequently shall be of nullable type and be zeroed out once no longer needed. Otherwise memory leak could happen. To detect such leaks kotlin.native.internal.GC.detectCycles in debug mode could be helpful.

Continuation0
Link copied to clipboard
class Continuation0(block: () -> Unit, invoker: CPointer<CFunction<(COpaquePointer?) -> Unit>>, singleShot: Boolean) : Function0<Unit>
Continuation1
Link copied to clipboard
class Continuation1<T1>(block: (T1) -> Unit, invoker: CPointer<CFunction<(COpaquePointer?) -> Unit>>, singleShot: Boolean) : Function1<T1, Unit>
Continuation2
Link copied to clipboard
class Continuation2<T1, T2>(block: (T1, T2) -> Unit, invoker: CPointer<CFunction<(COpaquePointer?) -> Unit>>, singleShot: Boolean) : Function2<T1, T2, Unit>
DetachedObjectGraph
Link copied to clipboard
class DetachedObjectGraph<T>

Detached object graph encapsulates transferrable detached subgraph which cannot be accessed externally, until it is attached with the attach extension function.

FreezableAtomicReference
Link copied to clipboard
class FreezableAtomicReference<T>(value_: T)

An atomic reference to a Kotlin object. Can be used in concurrent scenarious, but must be frozen first, otherwise behaves as regular box for the value. If frozen, shall be zeroed out once no longer needed. Otherwise memory leak could happen. To detect such leaks kotlin.native.internal.GC.detectCycles in debug mode could be helpful.

FreezingException
Link copied to clipboard
class FreezingException(toFreeze: Any, blocker: Any) : RuntimeException

Exception thrown whenever freezing is not possible.

Future
Link copied to clipboard
inline class Future<T>

Class representing abstract computation, whose result may become available in the future.

FutureState
Link copied to clipboard
enum FutureState : Enum<FutureState>

State of the future object.

InvalidMutabilityException
Link copied to clipboard
class InvalidMutabilityException(message: String) : RuntimeException

Exception thrown whenever we attempt to mutate frozen objects.

MutableData
Link copied to clipboard
class MutableData(capacity: Int)

Mutable concurrently accessible data buffer. Could be accessed from several workers simulteniously.

SharedImmutable
Link copied to clipboard
@Target(allowedTargets = [AnnotationTarget.PROPERTY])
expect annotation class SharedImmutable

Marks a top level property with a backing field as immutable. It is possible to share the value of such property between multiple threads, but it becomes deeply frozen, so no changes can be made to its state or the state of objects it refers to.

@Target(allowedTargets = [AnnotationTarget.PROPERTY])
actual annotation class SharedImmutable

Marks a top level property with a backing field as immutable. It is possible to share the value of such property between multiple threads, but it becomes deeply frozen, so no changes can be made to its state or the state of objects it refers to.

ThreadLocal
Link copied to clipboard
expect annotation class ThreadLocal

Marks a top level property with a backing field or an object as thread local. The object remains mutable and it is possible to change its state, but every thread will have a distinct copy of this object, so changes in one thread are not reflected in another.

actual annotation class ThreadLocal

Marks a top level property with a backing field or an object as thread local. The object remains mutable and it is possible to change its state, but every thread will have a distinct copy of this object, so changes in one thread are not reflected in another.

TransferMode
Link copied to clipboard
enum TransferMode : Enum<TransferMode>

Objects can be passed between threads in one of two possible modes.

Worker
Link copied to clipboard
inline class Worker

Class representing worker.

WorkerBoundReference
Link copied to clipboard
class WorkerBoundReference<out T : Any>(value: T)

A shared reference to a Kotlin object that doesn't freeze the referred object when it gets frozen itself.

Functions

atomicLazy
Link copied to clipboard
fun <T> atomicLazy(initializer: () -> T): Lazy<T>

Atomic lazy initializer, could be used in frozen objects, freezes initializing lambda, so use very carefully. Also, as with other uses of an AtomicReference may potentially leak memory, so it is recommended to use atomicLazy in cases of objects living forever, such as object signletons, or in cases where it's guaranteed not to have cyclical garbage.

attach
Link copied to clipboard
inline fun <T> DetachedObjectGraph<T>.attach(): T

Attaches previously detached object subgraph created by DetachedObjectGraph. Please note, that once object graph is attached, the DetachedObjectGraph.stable pointer does not make sense anymore, and shall be discarded, so attach of one DetachedObjectGraph object can only happen once.

callContinuation0
Link copied to clipboard
fun COpaquePointer.callContinuation0()
callContinuation1
Link copied to clipboard
fun <T1> COpaquePointer.callContinuation1()
callContinuation2
Link copied to clipboard
fun <T1, T2> COpaquePointer.callContinuation2()
ensureNeverFrozen
Link copied to clipboard
external fun Any.ensureNeverFrozen()

This function ensures that if we see such an object during freezing attempt - freeze fails and FreezingException is thrown.

freeze
Link copied to clipboard
fun <T> T.freeze(): T

Freezes object subgraph reachable from this object. Frozen objects can be freely shared between threads/workers.

waitForMultipleFutures
Link copied to clipboard
fun <T> Collection<Future<T>>.waitForMultipleFutures(millis: Int): Set<Future<T>>

fun <T> waitForMultipleFutures(futures: Collection<Future<T>>, timeoutMillis: Int): Set<Future<T>>

Wait for availability of futures in the collection. Returns set with all futures which have value available for the consumption, i.e. FutureState.COMPUTED.

waitWorkerTermination
Link copied to clipboard
external fun waitWorkerTermination(worker: Worker)
withWorker
Link copied to clipboard
inline fun <R> withWorker(name: String? = null, errorReporting: Boolean = true, block: Worker.() -> R): R

Executes block with new Worker as resource, by starting the new worker, calling provided block (in current context) with newly started worker as this and terminating worker after the block completes. Note that this operation is pretty heavyweight, use preconfigured worker or worker pool if need to execute it frequently.

Properties

isFrozen
Link copied to clipboard
val Any?.isFrozen: Boolean

Checks if given object is null or frozen or permanent (i.e. instantiated at compile-time).