fixed disabled concurrency in CLI

This commit is contained in:
Sergey Chernov 2026-04-03 23:42:43 +03:00
parent 1cead7822a
commit 161f3f74e2
4 changed files with 92 additions and 18 deletions

View File

@ -50,23 +50,18 @@ fn piSpigot(iThread: Int, n: Int) {
println(iThread, " - done: ", s)
}
var counter = 0
val t0 = Instant()
(1..TASK_COUNT).map { n ->
val counterState = counter
val t = launch {
piSpigot(counterState, WORK_SIZE)
}
++counter
t
}.forEach { (it as Deferred).await() }
for( repeat in 1..30) {
val t0 = Instant()
(1..TASK_COUNT).map {
val counterState = counter
val t = launch {
piSpigot(counterState, WORK_SIZE)
}
++counter
t
}.forEach { (it as Deferred).await() }
val dt = Instant() - t0
val dt = Instant() - t0
println("$repeat: all done, dt = ", dt)
delay(1000)
}
println("all done, dt = ", dt)

View File

@ -26,7 +26,9 @@ import com.github.ajalt.clikt.parameters.arguments.multiple
import com.github.ajalt.clikt.parameters.arguments.optional
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import net.sergeych.lyng.EvalSession
import net.sergeych.lyng.LyngVersion
import net.sergeych.lyng.Script
@ -245,12 +247,17 @@ fun executeFileWithArgs(fileName: String, args: List<String>) {
suspend fun executeSource(source: Source) {
val session = EvalSession(baseScopeDefer.await())
try {
session.eval(source)
evalOnCliDispatcher(session, source)
} finally {
session.cancelAndJoin()
}
}
internal suspend fun evalOnCliDispatcher(session: EvalSession, source: Source): Obj =
withContext(Dispatchers.Default) {
session.eval(source)
}
suspend fun executeFile(fileName: String) {
var text = FileSystem.SYSTEM.source(fileName.toPath()).use { fileSource ->
fileSource.buffer().use { bs ->

View File

@ -0,0 +1,72 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.EvalSession
import net.sergeych.lyng.Script
import net.sergeych.lyng.Source
import net.sergeych.lyng.obj.ObjList
import net.sergeych.lyng.obj.ObjString
import org.junit.Test
import kotlin.test.assertNotEquals
class CliDispatcherJvmTest {
@Test
fun executeSourceRunsOnDefaultDispatcher() = runBlocking {
val callerThread = Thread.currentThread()
val callerThreadKey = "${System.identityHashCode(callerThread)}:${callerThread.name}"
val scope = Script.newScope().apply {
addFn("threadKey") { ObjString("${System.identityHashCode(Thread.currentThread())}:${Thread.currentThread().name}") }
addFn("threadName") { ObjString(Thread.currentThread().name) }
}
val session = EvalSession(scope)
try {
val result = evalOnCliDispatcher(
session,
Source(
"<test>",
"""
val task = launch { [threadKey(), threadName()] }
val child = task.await()
[threadKey(), threadName(), child]
""".trimIndent()
)
) as ObjList
val topLevelThreadKey = (result.list[0] as ObjString).value
val topLevelThreadName = (result.list[1] as ObjString).value
val child = result.list[2] as ObjList
val childThreadKey = (child.list[0] as ObjString).value
val childThreadName = (child.list[1] as ObjString).value
assertNotEquals(
callerThreadKey,
topLevelThreadKey,
"CLI top-level script body should not run on the runBlocking caller thread: $topLevelThreadName"
)
assertNotEquals(
callerThreadKey,
childThreadKey,
"CLI launch child should not inherit the runBlocking caller thread: $childThreadName"
)
} finally {
session.cancelAndJoin()
}
}
}

View File

@ -21,7 +21,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
group = "net.sergeych"
version = "1.5.4"
version = "1.5.4-SNAPSHOT"
// Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below