X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.interop.scl%2Fsrc%2Forg%2Fsimantics%2Finterop%2Fscl%2FThreads.java;fp=org.simantics.interop.scl%2Fsrc%2Forg%2Fsimantics%2Finterop%2Fscl%2FThreads.java;h=f0774924f29a19168ed51c73a6378a054ccd8809;hb=5911a732d643327419746cf87c2dda447fe195a2;hp=0000000000000000000000000000000000000000;hpb=0403f09aa2c8b17a99acdd6f82d893bc1cdf88fb;p=simantics%2Finterop.git diff --git a/org.simantics.interop.scl/src/org/simantics/interop/scl/Threads.java b/org.simantics.interop.scl/src/org/simantics/interop/scl/Threads.java new file mode 100644 index 0000000..f077492 --- /dev/null +++ b/org.simantics.interop.scl/src/org/simantics/interop/scl/Threads.java @@ -0,0 +1,93 @@ +package org.simantics.interop.scl; + +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.atomic.AtomicInteger; + +import org.simantics.scl.runtime.SCLContext; +import org.simantics.scl.runtime.function.Function; +import org.simantics.scl.runtime.tuple.Tuple0; + +public class Threads { + + private static final AtomicInteger threadCount = new AtomicInteger(0); + + private static final ThreadFactory threadFactory = r -> { + Thread t = new Thread(r, "scl-interop-thread-" + threadCount.incrementAndGet()); + t.setDaemon(true); + return t; + }; + + + private static final ScheduledThreadPoolExecutor scheduledExecutor; + + static { + scheduledExecutor = new ScheduledThreadPoolExecutor(2, threadFactory); + scheduledExecutor.setMaximumPoolSize(2); + } + + public static void setMaximumPoolSize(int size) { + if (size < 2) + return; + scheduledExecutor.setMaximumPoolSize(size); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public static SCLThread runAsync(Function f) { + SCLContext context = SCLContext.createDerivedContext(); + SCLThread t = new SCLThread(context,f); + scheduledExecutor.submit(t); + return t; + } + + public static class SCLThread implements Runnable { + SCLContext context; + Function f; + boolean running = false; + boolean completed = false; + Object returnValue = null; + Throwable error; + + public SCLThread(SCLContext context, Function f) { + this.context = context; + this.f = f; + } + + @Override + public void run() { + SCLContext.push(context); + running = true; + try { + returnValue = f.apply(Tuple0.INSTANCE); + } catch (Throwable t) { + error = t; + } finally { + SCLContext.pop(); + } + running = false; + completed = true; + } + + public boolean isRunning() { + return running; + } + + public boolean isCompleted() { + return completed; + } + + public boolean hasErrors() { + return error != null; + } + + public Object getReturnValue() { + return returnValue; + } + + public Throwable getError() { + return error; + } + + } + +}