Skip to content

Commit 57da9d5

Browse files
authored
update: new FAQ answer (#4789)
1 parent a6948ac commit 57da9d5

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

docs/topics/native/native-faq.md

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,28 +109,22 @@ The Kotlin/Native compiler does not support bitcode embedding since Kotlin 2.0.2
109109
If you're using earlier versions of Xcode but want to upgrade to Kotlin 2.0.20 or later versions, disable bitcode
110110
embedding in your Xcode projects.
111111

112-
## Why do I see InvalidMutabilityException?
112+
## How do I reference objects safely from different coroutines?
113113

114-
> This issue is relevant for the legacy memory manager only. Check out [Kotlin/Native memory management](native-memory-manager.md)
115-
> to learn about the new memory manager, which has been enabled by default since Kotlin 1.7.20.
116-
>
117-
{style="note"}
114+
To safely access or update an object across multiple coroutines in Kotlin/Native, consider using concurrency-safe
115+
constructs, such as `@Volatile` and `AtomicReference`.
118116

119-
It likely happens, because you are trying to mutate a frozen object. An object can transfer to the
120-
frozen state either explicitly, as objects reachable from objects on which the `kotlin.native.concurrent.freeze` is called,
121-
or implicitly (i.e. reachable from `enum` or global singleton object - see the next question).
117+
Use [`@Volatile`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.concurrent/-volatile/) to annotate a `var` property.
118+
This makes all reads and writes to the property's backing field atomic. In addition, writes become immediately visible
119+
to other threads. When another thread accesses this property, it observes not only the updated value but also the changes
120+
that happened before the update.
122121

123-
## How do I make a singleton object mutable?
122+
Alternatively, use [AtomicReference](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.concurrent.atomics/-atomic-reference/),
123+
which supports atomic reads and updates. On Kotlin/Native, it wraps a volatile variable and performs atomic operations.
124+
Kotlin also provides a set of types for atomic operations tailored to specific data types. You can use `AtomicInt`,
125+
`AtomicLong`, `AtomicBoolean`, `AtomicArray`, as well as `AtomicIntArray` and `AtomicLongArray`.
124126

125-
> This issue is relevant for the legacy memory manager only. Check out [Kotlin/Native memory management](native-memory-manager.md)
126-
> to learn about the new memory manager, which has been enabled by default since Kotlin 1.7.20.
127-
>
128-
{style="note"}
129-
130-
Currently, singleton objects are immutable (i.e. frozen after creation), and it's generally considered
131-
good practise to have the global state immutable. If for some reason you need a mutable state inside such an
132-
object, use the `@konan.ThreadLocal` annotation on the object. Also, the `kotlin.native.concurrent.AtomicReference` class could be
133-
used to store different pointers to frozen objects in a frozen object and automatically update them.
127+
For more information about access to shared mutable state, see the [Coroutines documentation](shared-mutable-state-and-concurrency.md).
134128

135129
## How can I compile my project with unreleased versions of Kotlin/Native?
136130

docs/topics/native/native-migration-guide.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ To support the new memory manager, remove usages of the affected API:
6767
|-------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|
6868
| [`@SharedImmutable`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.native.concurrent/-shared-immutable/) | You can remove all usages, though there are no warnings for using this API in the new memory manager. |
6969
| [The `FreezableAtomicReference` class](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.native.concurrent/-freezable-atomic-reference/) | Use [`AtomicReference`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.native.concurrent/-atomic-reference/) instead. |
70-
| [The `FreezingException` class](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.native.concurrent/-freezing-exception/) | Remove all usages. | |
70+
| [The `FreezingException` class](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.native.concurrent/-freezing-exception/) | Remove all usages. |
7171
| [The `InvalidMutabilityException` class](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.native.concurrent/-invalid-mutability-exception/) | Remove all usages. |
7272
| [The `IncorrectDereferenceException` class](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.native/-incorrect-dereference-exception/) | Remove all usages. |
7373
| [The `freeze()` function](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.native.concurrent/freeze.html) | Remove all usages. |
@@ -81,4 +81,5 @@ To support the new memory manager, remove usages of the affected API:
8181
## What's next
8282

8383
* [Learn more about the new memory manager](native-memory-manager.md)
84-
* [Check the specifics of integration with Swift/Objective-C ARC](native-arc-integration.md)
84+
* [Check the specifics of integration with Swift/Objective-C ARC](native-arc-integration.md)
85+
* [Learn how to reference objects safely from different coroutines](native-faq.md#how-do-i-reference-objects-safely-from-different-coroutines)

0 commit comments

Comments
 (0)