Proofread Watson (#2055)
This commit is contained in:
committed by
Nikolay Igotti
parent
30f5eb0bfa
commit
985385e2fa
+19
-19
@@ -1,31 +1,31 @@
|
|||||||
# Immutability in Kotlin/Native
|
# Immutability in Kotlin/Native
|
||||||
|
|
||||||
Kotlin/Native implements strict mutability checks, ensuring
|
Kotlin/Native implements strict mutability checks, ensuring
|
||||||
important invariant that object is either immutable or
|
the important invariant that the object is either immutable or
|
||||||
accessible from the single thread at the moment (`mutable XOR global`).
|
accessible from the single thread at that moment in time (`mutable XOR global`).
|
||||||
|
|
||||||
Immutability is the runtime property in Kotlin/Native, and can be applied
|
Immutability is a runtime property in Kotlin/Native, and can be applied
|
||||||
to an arbitrary object subgraph using `kotlin.native.concurrent.freeze` function.
|
to an arbitrary object subgraph using the `kotlin.native.concurrent.freeze` function.
|
||||||
It makes all objects reachable from the given one immutable, and
|
It makes all the objects reachable from the given one immutable,
|
||||||
such a transition is a one way operation (object cannot be unfrozen later).
|
such a transition is a one-way operation (i.e., objects cannot be unfrozen later).
|
||||||
Some naturally immutable objects, such as `kotlin.String`, `kotlin.Int` and
|
Some naturally immutable objects such as `kotlin.String`, `kotlin.Int`, and
|
||||||
other primitive types, along with `AtomicInt` and `AtomicReference` are frozen
|
other primitive types, along with `AtomicInt` and `AtomicReference` are frozen
|
||||||
by default. If mutating operation is applied to a frozen object,
|
by default. If a mutating operation is applied to a frozen object,
|
||||||
an `InvalidMutabilityException` is thrown.
|
an `InvalidMutabilityException` is thrown.
|
||||||
|
|
||||||
To achieve `mutable XOR global` invariant all globally visible state (currently,
|
To achieve `mutable XOR global` invariant, all globally visible state (currently,
|
||||||
`object` singletons and enums) are automatically frozen. If an object freezing
|
`object` singletons and enums) are automatically frozen. If object freezing
|
||||||
is not desirable, `kotlin.native.ThreadLocal` annotation could be used, which will make
|
is not desired, a `kotlin.native.ThreadLocal` annotation can be used, which will make
|
||||||
object state thread local, and thus, mutable (but changed state not visible to
|
the object state thread local, and so, mutable (but the changed state is not visible to
|
||||||
other threads).
|
other threads).
|
||||||
|
|
||||||
Top level/global variables of non-primitive types are by default accessible in the
|
Top level/global variables of non-primitive types are by default accessible in the
|
||||||
main thread (i.e. thread which initialized _Kotlin/Native_ runtime first) only.
|
main thread (i.e., the thread which initialized _Kotlin/Native_ runtime first) only.
|
||||||
Access from another thread leads to an `IncorrectDereferenceException` being thrown.
|
Access from another thread will lead to an `IncorrectDereferenceException` being thrown.
|
||||||
To make such variables accessible in other threads either `@ThreadLocal` annotation,
|
To make such variables accessible in other threads, you can use either the `@ThreadLocal` annotation,
|
||||||
marking value thread local or `@SharedImmutable`, making value frozen, and accessible
|
and mark the value thread local or `@SharedImmutable`, which will make the value frozen and accessible
|
||||||
from other threads, can be used.
|
from other threads.
|
||||||
|
|
||||||
Class `AtomicReference` could be used to publish changed frozen state to
|
Class `AtomicReference` can be used to publish the changed frozen state to
|
||||||
other threads, and thus build patterns like shared caches.
|
other threads, and so build patterns like shared caches.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user