From 8c4c68f24a85b7f46dc3c3d7bdc3092e4f4aeb8b Mon Sep 17 00:00:00 2001 From: crschnick Date: Thu, 3 Aug 2023 18:30:46 +0000 Subject: [PATCH] Use platform threads in development mode instead of virtual ones --- app/build.gradle | 1 + .../io/xpipe/app/core/AppFileWatcher.java | 2 +- .../java/io/xpipe/app/core/AppMainWindow.java | 2 +- .../java/io/xpipe/app/core/AppProperties.java | 19 +++++++------------ .../io/xpipe/app/core/mode/OperationMode.java | 2 +- .../io/xpipe/app/core/mode/PlatformMode.java | 2 +- .../app/fxcomps/util/BindingsHelper.java | 2 +- .../io/xpipe/app/update/UpdateHandler.java | 2 +- .../java/io/xpipe/app/util/ThreadHelper.java | 11 ++++++++--- dist/debug/debug_arguments.txt | 3 ++- 10 files changed, 24 insertions(+), 22 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index f4049fdbb..57e2ec8f2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -142,6 +142,7 @@ application { } run { + systemProperty 'io.xpipe.app.useVirtualThreads', 'false' systemProperty 'io.xpipe.app.mode', 'gui' systemProperty 'io.xpipe.app.dataDir', "$projectDir/local7/" systemProperty 'io.xpipe.app.writeLogs', "true" diff --git a/app/src/main/java/io/xpipe/app/core/AppFileWatcher.java b/app/src/main/java/io/xpipe/app/core/AppFileWatcher.java index 4c2a5f178..fa91f5407 100644 --- a/app/src/main/java/io/xpipe/app/core/AppFileWatcher.java +++ b/app/src/main/java/io/xpipe/app/core/AppFileWatcher.java @@ -56,7 +56,7 @@ public class AppFileWatcher { } active = true; - watcherThread = ThreadHelper.create("file watcher", true, () -> { + watcherThread = ThreadHelper.createPlatformThread("file watcher", true, () -> { while (active) { WatchKey key; try { diff --git a/app/src/main/java/io/xpipe/app/core/AppMainWindow.java b/app/src/main/java/io/xpipe/app/core/AppMainWindow.java index 0515f5de4..19fc762eb 100644 --- a/app/src/main/java/io/xpipe/app/core/AppMainWindow.java +++ b/app/src/main/java/io/xpipe/app/core/AppMainWindow.java @@ -53,7 +53,7 @@ public class AppMainWindow { private synchronized void onChange() { lastUpdate = Instant.now(); if (thread == null) { - thread = ThreadHelper.create("window change timeout", true, () -> { + thread = ThreadHelper.createPlatformThread("window change timeout", true, () -> { while (true) { var toStop = lastUpdate.plus(Duration.of(1, ChronoUnit.SECONDS)); if (Instant.now().isBefore(toStop)) { diff --git a/app/src/main/java/io/xpipe/app/core/AppProperties.java b/app/src/main/java/io/xpipe/app/core/AppProperties.java index 47e8ceb11..bfb58920f 100644 --- a/app/src/main/java/io/xpipe/app/core/AppProperties.java +++ b/app/src/main/java/io/xpipe/app/core/AppProperties.java @@ -21,14 +21,17 @@ public class AppProperties { private static final String EXTENSION_PATHS_PROP = "io.xpipe.app.extensions"; private static AppProperties INSTANCE; boolean fullVersion; + @Getter String version; + @Getter String build; UUID buildUuid; String sentryUrl; String arch; + @Getter boolean image; boolean staging; - @Getter + boolean useVirtualThreads; Path dataDir; boolean showcase; @@ -48,6 +51,9 @@ public class AppProperties { staging = Optional.ofNullable(System.getProperty("io.xpipe.app.staging")) .map(Boolean::parseBoolean) .orElse(false); + useVirtualThreads = Optional.ofNullable(System.getProperty("io.xpipe.app.useVirtualThreads")) + .map(Boolean::parseBoolean) + .orElse(true); dataDir = parseDataDir(); showcase = Optional.ofNullable(System.getProperty("io.xpipe.app.showcase")) .map(Boolean::parseBoolean) @@ -106,10 +112,6 @@ public class AppProperties { return Path.of(System.getProperty("user.home"), isStaging() ? ".xpipe_stage" : ".xpipe"); } - public String getVersion() { - return version; - } - public boolean isDeveloperMode() { if (AppPrefs.get() == null) { return false; @@ -118,11 +120,4 @@ public class AppProperties { return AppPrefs.get().developerMode().getValue(); } - public boolean isImage() { - return image; - } - - public String getBuild() { - return build; - } } diff --git a/app/src/main/java/io/xpipe/app/core/mode/OperationMode.java b/app/src/main/java/io/xpipe/app/core/mode/OperationMode.java index 6cbd9a856..d1675d651 100644 --- a/app/src/main/java/io/xpipe/app/core/mode/OperationMode.java +++ b/app/src/main/java/io/xpipe/app/core/mode/OperationMode.java @@ -122,7 +122,7 @@ public abstract class OperationMode { } public static void switchToAsync(OperationMode newMode) { - ThreadHelper.create("mode switcher", false, () -> switchTo(newMode)).start(); + ThreadHelper.createPlatformThread("mode switcher", false, () -> switchTo(newMode)).start(); } public static void switchTo(OperationMode newMode) { diff --git a/app/src/main/java/io/xpipe/app/core/mode/PlatformMode.java b/app/src/main/java/io/xpipe/app/core/mode/PlatformMode.java index e2357d14f..a4d741714 100644 --- a/app/src/main/java/io/xpipe/app/core/mode/PlatformMode.java +++ b/app/src/main/java/io/xpipe/app/core/mode/PlatformMode.java @@ -40,7 +40,7 @@ public abstract class PlatformMode extends OperationMode { TrackEvent.info("mode", "Finished essential component initialization before platform"); TrackEvent.info("mode", "Launching application ..."); - ThreadHelper.create("app", false, () -> { + ThreadHelper.createPlatformThread("app", false, () -> { TrackEvent.info("mode", "Application thread started"); Application.launch(App.class); }) diff --git a/app/src/main/java/io/xpipe/app/fxcomps/util/BindingsHelper.java b/app/src/main/java/io/xpipe/app/fxcomps/util/BindingsHelper.java index 48e6851fb..8042ee07a 100644 --- a/app/src/main/java/io/xpipe/app/fxcomps/util/BindingsHelper.java +++ b/app/src/main/java/io/xpipe/app/fxcomps/util/BindingsHelper.java @@ -43,7 +43,7 @@ public class BindingsHelper { } static { - ThreadHelper.create("referenceGC", true, () -> { + ThreadHelper.createPlatformThread("referenceGC", true, () -> { while (true) { for (ReferenceEntry reference : REFERENCES) { if (reference.canGc()) { diff --git a/app/src/main/java/io/xpipe/app/update/UpdateHandler.java b/app/src/main/java/io/xpipe/app/update/UpdateHandler.java index 4d6fe6644..0b6f7e7ec 100644 --- a/app/src/main/java/io/xpipe/app/update/UpdateHandler.java +++ b/app/src/main/java/io/xpipe/app/update/UpdateHandler.java @@ -89,7 +89,7 @@ public abstract class UpdateHandler { } private void startBackgroundUpdater() { - ThreadHelper.create("updater", true, () -> { + ThreadHelper.createPlatformThread("updater", true, () -> { ThreadHelper.sleep(Duration.ofMinutes(5).toMillis()); event("Starting background updater thread"); while (true) { diff --git a/app/src/main/java/io/xpipe/app/util/ThreadHelper.java b/app/src/main/java/io/xpipe/app/util/ThreadHelper.java index af4668be8..6da6b661e 100644 --- a/app/src/main/java/io/xpipe/app/util/ThreadHelper.java +++ b/app/src/main/java/io/xpipe/app/util/ThreadHelper.java @@ -1,19 +1,24 @@ package io.xpipe.app.util; +import io.xpipe.app.core.AppProperties; import io.xpipe.app.issue.ErrorEvent; import org.apache.commons.lang3.function.FailableRunnable; public class ThreadHelper { + private static Thread unstarted(Runnable r) { + return AppProperties.get().isUseVirtualThreads() ? Thread.ofVirtual().unstarted(r) : Thread.ofPlatform().unstarted(r); + } + public static Thread runAsync(Runnable r) { - var t = Thread.ofVirtual().unstarted(r); + var t = unstarted(r); t.setDaemon(true); t.start(); return t; } public static Thread runFailableAsync(FailableRunnable r) { - var t = Thread.ofVirtual().unstarted(() -> { + var t = unstarted(() -> { try { r.run(); } catch (Throwable e) { @@ -25,7 +30,7 @@ public class ThreadHelper { return t; } - public static Thread create(String name, boolean daemon, Runnable r) { + public static Thread createPlatformThread(String name, boolean daemon, Runnable r) { var t = new Thread(r); t.setDaemon(daemon); t.setName(name); diff --git a/dist/debug/debug_arguments.txt b/dist/debug/debug_arguments.txt index 6fa7a642f..f46088f60 100644 --- a/dist/debug/debug_arguments.txt +++ b/dist/debug/debug_arguments.txt @@ -2,4 +2,5 @@ -Dio.xpipe.app.writeLogs=false -Dio.xpipe.app.logLevel=trace -Dio.xpipe.app.developerMode=true --Dio.xpipe.beacon.debugOutput=true \ No newline at end of file +-Dio.xpipe.beacon.debugOutput=true +-Dio.xpipe.app.useVirtualThreads=false \ No newline at end of file