Skip to content
Permalink
Browse files

Fix concurrency issue in async_read

  • Loading branch information...
LadyCailin committed Aug 7, 2019
1 parent f818c13 commit b624e3fa196af67b7767596b6f5fd280c5f69c7f
Showing with 20 additions and 18 deletions.
  1. +20 −18 src/main/java/com/laytonsmith/core/functions/FileHandling.java
@@ -201,27 +201,29 @@ public ParseTree optimizeDynamic(Target t, Environment env, List<ParseTree> chil
@noboilerplate
public static class async_read extends AbstractFunction {

RunnableQueue queue = new RunnableQueue("MethodScript-asyncRead");
boolean started = false;
private static RunnableQueue queue;
private static volatile boolean started = false;
private static final Object LOCK = new Object();

// It's not really nested, it's within the callback, but the IDE doesn't understand that.
@SuppressWarnings("NestedSynchronizedStatement")
private void startup() {
if(!started) {
queue.invokeLater(null, new Runnable() {

@Override
public void run() {
//This warms up the queue. Apparently.
}
});
StaticLayer.GetConvertor().addShutdownHook(new Runnable() {

@Override
public void run() {
queue.shutdown();
started = false;
synchronized(LOCK) {
if(!started) {
queue = new RunnableQueue("MethodScript-asyncRead");
queue.invokeLater(null, () -> {
//This warms up the queue.
});
StaticLayer.GetConvertor().addShutdownHook(() -> {
synchronized(LOCK) {
queue.shutdown();
started = false;
}
});
started = true;
}
});
started = true;
}
}
}

@@ -325,7 +327,7 @@ public String docs() {
+ " If @contents is null, that indicates that an exception occured, and @exception will not be null, but instead have an"
+ " exeption array. Otherwise, @contents will contain the file's contents, and @exception will be null. This method is useful"
+ " to use in two cases, either you need a remote file via SCP, or a local file is big enough that you notice a delay when"
+ " simply using the read() function.";
+ " simply using the read() function. async_read is threadsafe.";
}

@Override

0 comments on commit b624e3f

Please sign in to comment.
You can’t perform that action at this time.