-
Notifications
You must be signed in to change notification settings - Fork 81
Refactor AsynchronousSceneManager to use a single thread ExecutorService. #1558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ShirleyNekoDev
wants to merge
1
commit into
chunky-dev:master
Choose a base branch
from
ShirleyNekoDev:refactor/better-async-scene-manager
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,7 +30,8 @@ | |
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.Collection; | ||
| import java.util.concurrent.LinkedBlockingQueue; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
|
|
||
| /** | ||
| * This scene manager is used for asynchronous loading and saving of scenes. | ||
|
|
@@ -39,16 +40,39 @@ | |
| * | ||
| * @author Jesper Öqvist <[email protected]> | ||
| */ | ||
| public class AsynchronousSceneManager extends Thread implements SceneManager { | ||
| public class AsynchronousSceneManager implements SceneManager { | ||
|
|
||
| private final SynchronousSceneManager sceneManager; | ||
| private final LinkedBlockingQueue<Runnable> taskQueue; | ||
| private final ExecutorService executorService = Executors.newSingleThreadExecutor(runnable -> { | ||
| Thread thread = new Thread(runnable, "AsynchronousSceneManager-Thread"); | ||
| thread.setUncaughtExceptionHandler((t, exception) -> { | ||
| if (exception instanceof OutOfMemoryError) { | ||
| Log.error( | ||
| "Chunky has run out of memory! Increase the memory given to Chunky in the launcher.", | ||
| exception); | ||
| throw (Error) exception; | ||
| } else if(exception instanceof InterruptedException) { | ||
| Thread.currentThread().interrupt(); | ||
| } else { | ||
| Log.error("Scene manager has crashed due to an uncaught exception. \n" + | ||
| "Chunky might not work properly until you restart it.\n" + | ||
| "If you think this is a bug, please report it and the following lines to the developers.", | ||
| exception); | ||
| // a new thread will be created by the executor after the current one terminated and a new task is enqueued | ||
| } | ||
| }); | ||
| return thread; | ||
| }); | ||
|
|
||
| public AsynchronousSceneManager(RenderContext context, RenderManager renderManager) { | ||
| super("Scene Manager"); | ||
|
|
||
| sceneManager = new SynchronousSceneManager(context, renderManager); | ||
| taskQueue = new LinkedBlockingQueue<>(); | ||
| } | ||
|
|
||
| /** | ||
| * Schedule a task to be run soon. | ||
| */ | ||
| public void enqueueTask(Runnable task) { | ||
| executorService.execute(task); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -85,26 +109,6 @@ public void setOnChunksLoaded(Runnable onChunksLoaded) { | |
| return sceneManager.getScene(); | ||
| } | ||
|
|
||
| @Override public void run() { | ||
| try { | ||
| while (!isInterrupted()) { | ||
| Runnable task = taskQueue.take(); | ||
| task.run(); | ||
| } | ||
| } catch (InterruptedException ignored) { | ||
| // Interrupted. | ||
| } catch (Throwable e) { | ||
| if (e instanceof OutOfMemoryError) { | ||
| Log.error("Chunky has run out of memory! Increase the memory given to Chunky in the launcher.", e); | ||
| } else { | ||
| Log.error("Scene manager has crashed due to an uncaught exception. " + | ||
| "Chunky will not work properly until you restart it. " + | ||
| "If you think this is a bug, please report it to the developers.", e); | ||
| } | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Load the given scene. | ||
| * | ||
|
|
@@ -229,84 +233,6 @@ public void mergeRenderDump(File renderDump) { | |
| enqueueTask(() -> sceneManager.mergeDump(renderDump)); | ||
| } | ||
|
|
||
| /** | ||
| * Schedule a task to be run soon. | ||
| */ | ||
| public void enqueueTask(Runnable task) { | ||
| taskQueue.add(task); | ||
| } | ||
|
|
||
| /** | ||
| * Remove problematic characters from scene name. | ||
| * | ||
| * @return sanitized scene name | ||
| */ | ||
| public static String sanitizedSceneName(String name, String fallbackName) { | ||
| name = name.trim(); | ||
| StringBuilder sb = new StringBuilder(); | ||
| for (int i = 0; i < name.length(); ++i) { | ||
| char c = name.charAt(i); | ||
| if (isValidSceneNameChar(c)) { | ||
| sb.append(c); | ||
| } else if (c >= '\u0020' && c <= '\u007e') { | ||
| sb.append('_'); | ||
| } | ||
| } | ||
| String stripped = sb.toString().trim(); | ||
| if (stripped.isEmpty()) { | ||
| return fallbackName; | ||
| } else { | ||
| return stripped; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Remove problematic characters from scene name. | ||
| * | ||
| * @return sanitized scene name | ||
| */ | ||
| public static String sanitizedSceneName(String name) { | ||
| return sanitizedSceneName(name, "Scene"); | ||
| } | ||
|
|
||
| /** | ||
| * @return <code>false</code> if the character can cause problems on any | ||
| * supported platform. | ||
| */ | ||
| public static boolean isValidSceneNameChar(char c) { | ||
| switch (c) { | ||
| case '/': | ||
| case ':': | ||
| case ';': | ||
| case '\\': // Windows file separator. | ||
| case '*': | ||
| case '?': | ||
| case '"': | ||
| case '<': | ||
| case '>': | ||
| case '|': | ||
| return false; | ||
| } | ||
| if (c < '\u0020') { | ||
| return false; | ||
| } | ||
| return c <= '\u007e' || c >= '\u00a0'; | ||
| } | ||
|
|
||
| /** | ||
| * Check for scene name validity. | ||
| * | ||
| * @return <code>true</code> if the scene name contains only legal characters | ||
| */ | ||
| public static boolean sceneNameIsValid(String name) { | ||
| for (int i = 0; i < name.length(); ++i) { | ||
| if (!isValidSceneNameChar(name.charAt(i))) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| public void discardSceneChanges() { | ||
| sceneManager.discardSceneChanges(); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
chunky/src/java/se/llbit/chunky/renderer/scene/SceneUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package se.llbit.chunky.renderer.scene; | ||
|
|
||
| public class SceneUtils { | ||
|
|
||
| /** | ||
| * Remove problematic characters from scene name. | ||
| * | ||
| * @return sanitized scene name | ||
| */ | ||
| public static String sanitizedSceneName(String name, String fallbackName) { | ||
| name = name.trim(); | ||
| StringBuilder sb = new StringBuilder(); | ||
| for (int i = 0; i < name.length(); ++i) { | ||
| char c = name.charAt(i); | ||
| if (isValidSceneNameChar(c)) { | ||
| sb.append(c); | ||
| } else if (c >= '\u0020' && c <= '\u007e') { | ||
| sb.append('_'); | ||
| } | ||
| } | ||
| String stripped = sb.toString().trim(); | ||
| if (stripped.isEmpty()) { | ||
| return fallbackName; | ||
| } else { | ||
| return stripped; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Remove problematic characters from scene name. | ||
| * | ||
| * @return sanitized scene name | ||
| */ | ||
| public static String sanitizedSceneName(String name) { | ||
| return sanitizedSceneName(name, "Scene"); | ||
| } | ||
|
|
||
| /** | ||
| * @return <code>false</code> if the character can cause problems on any | ||
| * supported platform. | ||
| */ | ||
| public static boolean isValidSceneNameChar(char c) { | ||
| switch (c) { | ||
| case '/': | ||
| case ':': | ||
| case ';': | ||
| case '\\': // Windows file separator. | ||
| case '*': | ||
| case '?': | ||
| case '"': | ||
| case '<': | ||
| case '>': | ||
| case '|': | ||
| return false; | ||
| } | ||
| if (c < '\u0020') { | ||
| return false; | ||
| } | ||
| return c <= '\u007e' || c >= '\u00a0'; | ||
| } | ||
|
|
||
| /** | ||
| * Check for scene name validity. | ||
| * | ||
| * @return <code>true</code> if the scene name contains only legal characters | ||
| */ | ||
| public static boolean sceneNameIsValid(String name) { | ||
| for (int i = 0; i < name.length(); ++i) { | ||
| if (!isValidSceneNameChar(name.charAt(i))) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.