diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DFGBuilder.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DFGBuilder.kt index 998cb35c7c6..99ef3a845a3 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DFGBuilder.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DFGBuilder.kt @@ -380,11 +380,17 @@ internal class ModuleDFGBuilder(val context: Context, val irModule: IrModuleFrag } if (expression is IrCall && expression.symbol == scheduleImplSymbol) { - // Producer of scheduleImpl is called externally, we need to reflect this somehow. + // Producer and job of scheduleImpl are called externally, we need to reflect this somehow. val producerInvocation = IrCallImpl(expression.startOffset, expression.endOffset, scheduleImplProducerInvoke.symbol, scheduleImplProducerInvoke.descriptor) producerInvocation.dispatchReceiver = expression.getValueArgument(2) - expressions += producerInvocation + val jobFunctionReference = expression.getValueArgument(3) as? IrFunctionReference + ?: error("A function reference expected") + val jobInvocation = IrCallImpl(expression.startOffset, expression.endOffset, + jobFunctionReference.symbol, jobFunctionReference.descriptor) + jobInvocation.putValueArgument(0, producerInvocation) + + expressions += jobInvocation } if (expression is IrReturnableBlock) { diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 91049b27b14..0fa1828e0aa 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -631,6 +631,12 @@ task worker8(type: RunKonanTest) { source = "runtime/workers/worker8.kt" } +task worker9(type: RunKonanTest) { + disabled = (project.testTarget == 'wasm32') // Workers need pthreads. + goldValue = "zzz\n42\nOK\n" + source = "runtime/workers/worker9.kt" +} + task freeze0(type: RunKonanTest) { disabled = (project.testTarget == 'wasm32') // No workers on WASM. goldValue = "frozen bit is true\n" + diff --git a/backend.native/tests/runtime/workers/worker9.kt b/backend.native/tests/runtime/workers/worker9.kt new file mode 100644 index 00000000000..98c36a6ffa6 --- /dev/null +++ b/backend.native/tests/runtime/workers/worker9.kt @@ -0,0 +1,22 @@ +package runtime.workers.worker9 + +import kotlin.test.* + +import konan.worker.* + +@Test fun runTest() { + withLock { println("zzz") } + val worker = startWorker() + val future = worker.schedule(TransferMode.CHECKED, {}) { + withLock { + println("42") + } + } + future.result() + worker.requestTermination().result() + println("OK") +} + +fun withLock(op: () -> Unit) { + op() +} \ No newline at end of file