From 05b58b55a42692db44387f14facbd9065d871bc0 Mon Sep 17 00:00:00 2001
From: William Walker <wnwalker@ucsc.edu>
Date: Wed, 19 Mar 2025 18:05:19 -0700
Subject: [PATCH] wip

---
 .../temerity/di/PlatformDispatcher.android.kt | 24 +++++++++++++++++++
 .../ucsc/its/temerity/core/TemClientConfig.kt |  9 +++++++
 .../edu/ucsc/its/temerity/core/Temerity.kt    | 21 ++++++++++++++--
 .../its/temerity/di/PlatformDispatcher.kt     | 22 +++++++++++++++++
 .../its/temerity/di/PlatformDispatcher.jvm.kt | 24 +++++++++++++++++++
 5 files changed, 98 insertions(+), 2 deletions(-)
 create mode 100644 temerity/src/androidMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.android.kt
 create mode 100644 temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.kt
 create mode 100644 temerity/src/jvmMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.jvm.kt

diff --git a/temerity/src/androidMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.android.kt b/temerity/src/androidMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.android.kt
new file mode 100644
index 0000000..69dabdb
--- /dev/null
+++ b/temerity/src/androidMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.android.kt
@@ -0,0 +1,24 @@
+/*
+ *     Designed and developed in 2022-2024 by William Walker (wnwalker@ucsc.edu)
+ *     Copyright 2022-2024 The Regents of the University of California. All rights reserved.
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation; version 2.1 of the License.
+ *
+ *     This library is distributed in the hope that it will be useful,
+ *     but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *     Lesser General Public License for more details.
+ *
+ *     You should have received a copy of the GNU Lesser General Public
+ *     License along with this library; if not, write to the Free Software
+ *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+package edu.ucsc.its.temerity.di
+
+import kotlinx.coroutines.CoroutineDispatcher
+
+internal actual fun createLibraryDispatcher(): CoroutineDispatcher {
+  TODO("Not yet implemented")
+}
diff --git a/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/TemClientConfig.kt b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/TemClientConfig.kt
index a31ac91..f0b2a6f 100644
--- a/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/TemClientConfig.kt
+++ b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/TemClientConfig.kt
@@ -39,6 +39,7 @@ import kotlin.time.Duration
  * @property optWebTimeoutDuration Specifies the timeout [kotlin.time.Duration] to use when making web requests
  * @property optCacheTimeoutDuration Specifies the timeout [kotlin.time.Duration] to use when storing cache entries. Affects keep-alive time for cached instance data like user role type fields.
  * @property optThreadCount Specifies the number of threads to use for each client instance. Defaults to 2 at a minimum
+ * @property optUseVirtualThreads Reserved for future use. Will configure library dispatchers to use virtual threads when available
  * @property optEnableCompression Specifies whether to turn on compression for HTTP requests. Enabled by default.
  */
 @TemDsl
@@ -62,6 +63,14 @@ public class TemClientConfig {
 
   public var optThreadCount: Int? = null
 
+  /**
+   *    [optUseVirtualThreads] Option flag to be used in future to switch to using Project Loom virtual thread-backed coroutine dispatchers
+   *    Inop for now, as Android Gradle Plugin does not yet support compiling with JDK > 17
+   *    Not to be converted to mutable field until JDK build target >= 21; required for Project Loom availability
+   *    Treated as enum: null = unsupported by current library, false means disabled, true means enabled
+   */
+  public val optUseVirtualThreads: Boolean? = null
+
   public var optEnableCompression: Boolean = true
 
   /**
diff --git a/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/Temerity.kt b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/Temerity.kt
index 422a74f..cb06e78 100644
--- a/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/Temerity.kt
+++ b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/Temerity.kt
@@ -234,8 +234,25 @@ public class Temerity internal constructor(
       "Service url must be provided. Set it using TemClientConfig.serviceUrl(url)"
     }
 
-    libraryCoroutineDispatcher = get<CoroutineDispatcher>(named("libraryCoroutineDispatcher")) {
-      parametersOf(availableThreads(config.optThreadCount), "Temerity Library Dispatcher")
+    libraryCoroutineDispatcher = when (config.optUseVirtualThreads) {
+      // Case 1: User requests to enable virtual threads
+      // Need to check if JDK supports virtual threads
+      true -> {
+        val jvmVersion = Version.parse(BuildConfig.JVM_VERSION)
+        if (jvmVersion >= Version(21, 0, 0)) {
+          TODO("Configure virtual thread per task executor, create dispatcher")
+        } else {
+          get<CoroutineDispatcher>(named("libraryCoroutineDispatcher")) {
+            parametersOf(availableThreads(config.optThreadCount), "Temerity Library Dispatcher")
+          }
+        }
+      }
+      // Case 2: Virtual threads are identified as unsupported by null value as developer constant, or user requests to disable them: null | false
+      else -> {
+        get<CoroutineDispatcher>(named("libraryCoroutineDispatcher")) {
+          parametersOf(availableThreads(config.optThreadCount), "Temerity Library Dispatcher")
+        }
+      }
     }
     libraryCoroutineScope = get<CoroutineScope>(named("libraryCoroutineScope")) {
       parametersOf(libraryCoroutineDispatcher)
diff --git a/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.kt b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.kt
new file mode 100644
index 0000000..13fbbf1
--- /dev/null
+++ b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.kt
@@ -0,0 +1,22 @@
+/*
+ *     Designed and developed in 2022-2024 by William Walker (wnwalker@ucsc.edu)
+ *     Copyright 2022-2024 The Regents of the University of California. All rights reserved.
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation; version 2.1 of the License.
+ *
+ *     This library is distributed in the hope that it will be useful,
+ *     but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *     Lesser General Public License for more details.
+ *
+ *     You should have received a copy of the GNU Lesser General Public
+ *     License along with this library; if not, write to the Free Software
+ *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+package edu.ucsc.its.temerity.di
+
+import kotlinx.coroutines.CoroutineDispatcher
+
+internal expect fun createLibraryDispatcher(): CoroutineDispatcher
diff --git a/temerity/src/jvmMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.jvm.kt b/temerity/src/jvmMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.jvm.kt
new file mode 100644
index 0000000..69dabdb
--- /dev/null
+++ b/temerity/src/jvmMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.jvm.kt
@@ -0,0 +1,24 @@
+/*
+ *     Designed and developed in 2022-2024 by William Walker (wnwalker@ucsc.edu)
+ *     Copyright 2022-2024 The Regents of the University of California. All rights reserved.
+ *
+ *     This library is free software; you can redistribute it and/or
+ *     modify it under the terms of the GNU Lesser General Public
+ *     License as published by the Free Software Foundation; version 2.1 of the License.
+ *
+ *     This library is distributed in the hope that it will be useful,
+ *     but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *     Lesser General Public License for more details.
+ *
+ *     You should have received a copy of the GNU Lesser General Public
+ *     License along with this library; if not, write to the Free Software
+ *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+package edu.ucsc.its.temerity.di
+
+import kotlinx.coroutines.CoroutineDispatcher
+
+internal actual fun createLibraryDispatcher(): CoroutineDispatcher {
+  TODO("Not yet implemented")
+}
-- 
GitLab