Skip to content
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

Clean up async script loading #3784

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/main/java/ch/njol/skript/ScriptLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,9 @@ public String toString() {

// Load scripts in separate (one) thread
static final BlockingQueue<Runnable> loadQueue = new ArrayBlockingQueue<>(20, true);
static final Thread loaderThread;
static boolean loadAsync; // See below
@Nullable
static Thread loaderThread;
private static boolean loadAsync; // See below

/**
* Checks if scripts are loaded in separate thread. If true,
Expand Down Expand Up @@ -309,23 +310,32 @@ private static void updateDisabledScripts(Path path) {
}
}

// Initialize and start load thread
static {
loaderThread = new AsyncLoaderThread();
loaderThread.start();
static void setLoadAsync(boolean loadAsync) {
ScriptLoader.loadAsync = loadAsync;

// Shutdown old thread in case of config reload
if (loaderThread != null) {
loaderThread.interrupt();
}

if (ScriptLoader.loadAsync) {
// Initialize and start load thread
loaderThread = new Thread(new AsyncLoaderTask(), "Skript async script loader thread");
loaderThread.start();
}
}

private static class AsyncLoaderThread extends Thread {
private static class AsyncLoaderTask implements Runnable {

public AsyncLoaderThread() { }
public AsyncLoaderTask() { }

@Override
public void run() {
while (true) {
while (!Thread.currentThread().isInterrupted()) {
try {
loadQueue.take().run();
} catch (InterruptedException e) {
Skript.exception(e); // Bubble it up with instructions on how to report it
Thread.currentThread().interrupt();
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ static void disableScripts() {
* Prints errors from reloading the config & scripts
*/
static void reload() {
if (!ScriptLoader.loadAsync)
if (!ScriptLoader.isAsync())
disableScripts();
reloadMainConfig();
reloadAliases();
Expand All @@ -989,7 +989,7 @@ static void reload() {
* Prints errors
*/
static void reloadScripts() {
if (!ScriptLoader.loadAsync)
if (!ScriptLoader.isAsync())
disableScripts();
ScriptLoader.loadScripts();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/SkriptConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public void set(Boolean t) {

@Override
public void set(Boolean t) {
ScriptLoader.loadAsync = t;
ScriptLoader.setLoadAsync(t);
}

})
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/config.sk
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ keep command last usage dates: false
# but when the player uses the command again after the cooldown period is over, the last usage will be deleted as it's no longer needed,
# If you need to use the expression 'last usage date', then you'll want to enable this.

asynchronous script loading: false
# Enables asynchronous script loading, which will load scripts in the background instead of blocking the server to load scripts.
# This will not necessarily make loading faster since it will still load the scripts on one thread and not in parallel.
# Do note that though, this option may cause issues with addons and possibly some scripts! Do NOT enable this option unless you have really long
# script load times AND you take the risk of lost data and full responsibility!

# ==== Variables ====

databases:
Expand Down