threading testing
This commit is contained in:
1 parent
5d32213922
commit
9ba573afe6
4 files changed
+86
-14
No files matched your search
@@ -1,5 +1,6 @@
|
||||
package carpet.script.value;
|
||||
|
||||
import carpet.CarpetSettings;
|
||||
import carpet.script.Context;
|
||||
import carpet.script.Expression;
|
||||
import carpet.script.Tokenizer;
|
||||
@@ -9,6 +10,7 @@ import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
@@ -17,9 +19,13 @@ import java.util.function.Supplier;
|
||||
public class ThreadValue extends Value
|
||||
{
|
||||
private CompletableFuture<Value> taskFuture;
|
||||
//public static ThreadLocal<Boolean> joining = ThreadLocal.withInitial(() -> false);
|
||||
public final static Map<Thread,Integer> waitingTreads = new ConcurrentHashMap<>();
|
||||
public static Map<Thread,Integer> joinRequestedThreads = new ConcurrentHashMap<>();
|
||||
private long id;
|
||||
private final Object lock;
|
||||
private final boolean[] started;
|
||||
//private final boolean[] started;
|
||||
private Thread executingThread;
|
||||
private Supplier<Value>task;
|
||||
private static long sequence = 0L;
|
||||
|
||||
@@ -29,18 +35,20 @@ public class ThreadValue extends Value
|
||||
{
|
||||
id = sequence++;
|
||||
lock = new Object();
|
||||
started = new boolean[]{false};
|
||||
executingThread = null;
|
||||
//started = new boolean[]{false};
|
||||
task = () ->
|
||||
{
|
||||
if (!started[0])
|
||||
if (executingThread == null)
|
||||
{
|
||||
started[0] = true;
|
||||
CarpetSettings.LOG.error("Setting the thread");
|
||||
executingThread = Thread.currentThread();
|
||||
return function.lazyEval(ctx, Context.NONE, expr, token, Collections.emptyList()).evalValue(ctx);
|
||||
}
|
||||
return null;
|
||||
};
|
||||
taskFuture = CompletableFuture.supplyAsync(
|
||||
() -> { synchronized (lock) { return task.get(); } },
|
||||
() -> { synchronized (lock) { CarpetSettings.LOG.error("getting in future"); Value ret = task.get(); CarpetSettings.LOG.error("gotten in future"); return ret;} },
|
||||
executorServices.computeIfAbsent(pool, (v) -> (ThreadPoolExecutor)Executors.newCachedThreadPool())
|
||||
);
|
||||
Thread.yield();
|
||||
@@ -60,26 +68,45 @@ public class ThreadValue extends Value
|
||||
|
||||
public Value join()
|
||||
{
|
||||
if (!started[0])
|
||||
CarpetSettings.LOG.error("in join "+id);
|
||||
if (executingThread == null)
|
||||
{
|
||||
synchronized (lock)
|
||||
CarpetSettings.LOG.error("not started "+id);
|
||||
Value res;
|
||||
waitingTreads.put(Thread.currentThread(), 1);
|
||||
try
|
||||
{
|
||||
if (!started[0])
|
||||
{
|
||||
Value res = task.get();
|
||||
taskFuture.complete(res);
|
||||
return res;
|
||||
}
|
||||
CarpetSettings.LOG.error(" getting result "+id);
|
||||
res = task.get();
|
||||
CarpetSettings.LOG.error(" result got "+id);
|
||||
}
|
||||
finally
|
||||
{
|
||||
waitingTreads.remove(Thread.currentThread());
|
||||
}
|
||||
taskFuture.complete(res);
|
||||
return res;
|
||||
}
|
||||
try
|
||||
{
|
||||
synchronized (waitingTreads)
|
||||
{
|
||||
if (waitingTreads.containsKey(executingThread))
|
||||
throw new InternalExpressionException("Cannot wait/join on world accessing thread");
|
||||
waitingTreads.put(executingThread, 1);
|
||||
}
|
||||
CarpetSettings.LOG.error("getting future "+id);
|
||||
return taskFuture.get();
|
||||
}
|
||||
catch (InterruptedException | ExecutionException e)
|
||||
{
|
||||
return Value.NULL;
|
||||
}
|
||||
finally
|
||||
{
|
||||
waitingTreads.remove(executingThread);
|
||||
CarpetSettings.LOG.error(" future gotten "+id);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isFinished()
|
||||
@@ -93,7 +120,7 @@ public class ThreadValue extends Value
|
||||
{
|
||||
if (!taskFuture.isDone())
|
||||
{
|
||||
started[0] = true;
|
||||
executingThread = Thread.currentThread();
|
||||
taskFuture.complete(Value.NULL);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user