From 70872854ad01134339c77db7687b754aff813510 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Thu, 25 Jul 2019 17:41:30 +0300 Subject: [PATCH] Document potentially cyclic cases in ownership transfer and add test. (#3207) --- .../tests/runtime/workers/worker9.kt | 22 +++++++++++++++++++ .../kotlin/kotlin/native/concurrent/Worker.kt | 3 +++ 2 files changed, 25 insertions(+) diff --git a/backend.native/tests/runtime/workers/worker9.kt b/backend.native/tests/runtime/workers/worker9.kt index cd99c5a594a..67819215e0e 100644 --- a/backend.native/tests/runtime/workers/worker9.kt +++ b/backend.native/tests/runtime/workers/worker9.kt @@ -58,5 +58,27 @@ fun withLock(op: () -> Unit) { println("frozen OK") }.freeze()) + worker.requestTermination().result +} + +class Node(var node: Node?, var outher: Node?) + +fun makeCyclic(): Node { + val inner = Node(null, null) + inner.node = inner + val outer = Node(null, null) + inner.outher = outer + return outer +} + +@Test fun runTest4() { + val worker = Worker.start() + + val future = worker.execute(TransferMode.SAFE, { }) { + makeCyclic().also { + kotlin.native.internal.GC.collect() + } + } + assert(future.result != null) worker.requestTermination().result } \ No newline at end of file diff --git a/runtime/src/main/kotlin/kotlin/native/concurrent/Worker.kt b/runtime/src/main/kotlin/kotlin/native/concurrent/Worker.kt index f04eaec0375..bb1c1a0c666 100644 --- a/runtime/src/main/kotlin/kotlin/native/concurrent/Worker.kt +++ b/runtime/src/main/kotlin/kotlin/native/concurrent/Worker.kt @@ -76,6 +76,9 @@ public inline class Worker @PublishedApi internal constructor(val id: Int) { * explicitly stored in object produced by [producer]. Scheduled job is being executed by the worker, * and result of such a execution is being disconnected from worker's object graph. Whoever will consume * the future, can use result of worker's computations. + * Note, that some technically disjoint subgraphs may lead to `kotlin.IllegalStateException` + * so `kotlin.native.internal.GC.collect()` could be called in the end of `producer` and `job` + * if garbage cyclic structures or other uncollected objects refer to the value being transferred. * * @return the future with the computation result of [job] */