-package org.simantics.db.layer0;
+package org.simantics.simulator.toolkit;
import java.util.List;
-import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
-import org.simantics.db.common.utils.Logger;
import org.simantics.scl.runtime.SCLContext;
import org.simantics.scl.runtime.tuple.Tuple0;
import org.simantics.simulator.variable.Realm;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
-abstract public class StandardRealm<Node, Engine extends StandardEngine<Node>> implements Realm {
+abstract public class StandardRealm<Node, Engine extends StandardNodeManagerSupport<Node>> implements Realm {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(StandardRealm.class);
private String id;
- private Thread executorThread;
+ protected Thread executorThread;
private StandardRealmThreadFactory factory = new StandardRealmThreadFactory(this);
private ThreadPoolExecutor executor = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS,
- new LinkedBlockingQueue<Runnable>(), factory);
+ new LinkedBlockingQueue<>(), factory);
private Semaphore beginSyncExec = new Semaphore(0);
private Semaphore endSyncExec = new Semaphore(0);
-
+
private Engine engine;
- private StandardNodeManager<Node, Engine> nodeManager;
-
+ protected StandardNodeManager<Node, Engine> nodeManager;
+
private Runnable scheduleSyncExec = new Runnable() {
@Override
public void run() {
}
}
};
-
+
protected StandardRealm(Engine engine, String id) {
this.engine = engine;
this.id = id;
}
abstract protected StandardNodeManager<Node, Engine> createManager();
-
+
protected String getSCLContextKey() {
- return getClass().getSimpleName();
+ return getClass().getSimpleName();
}
public String getId() {
return id;
}
-
+
public Engine getEngine() {
return engine;
}
public Thread getThread() {
return executorThread;
}
-
+
@SuppressWarnings({ "rawtypes", "unchecked" })
public Object syncExec(Function fun) throws InterruptedException {
-
executor.execute(scheduleSyncExec);
SCLContext context = SCLContext.getCurrent();
Engine oldConnection = (Engine)context.put(getSCLContextKey(), engine);
-
+
try {
beginSyncExec.acquire();
Thread oldThread = executorThread;
context.put(getSCLContextKey(), oldConnection);
}
}
-
+
@SuppressWarnings("rawtypes")
public void asyncExec(final Function fun) {
executor.execute(new Runnable() {
@Override
public void syncExec(Runnable runnable) throws InterruptedException {
-
if(executorThread == Thread.currentThread()) {
try {
runnable.run();
} catch (Throwable t) {
- Logger.defaultLogError(t);
+ LOGGER.error("Error executing runnable in realm", t);
} finally {
}
return;
}
- executor.execute(scheduleSyncExec);
-
+ executor.execute(scheduleSyncExec);
+
beginSyncExec.acquire();
Thread oldThread = executorThread;
executorThread = Thread.currentThread();
try {
runnable.run();
} catch (Throwable t) {
- Logger.defaultLogError(t);
+ LOGGER.error("Error executing runnable in realm", t);
} finally {
executorThread = oldThread;
endSyncExec.release();
@Override
public void asyncExec(Runnable runnable) {
-
- if(executorThread == Thread.currentThread()) {
+ if(executorThread == Thread.currentThread()) {
try {
runnable.run();
} catch (Throwable t) {
- Logger.defaultLogError(t);
+ LOGGER.error("Error executing runnable in realm", t);
} finally {
}
return;
}
-
+
executor.execute(runnable);
}
-
+
public void close() {
executor.shutdown();
try {
} catch (InterruptedException e) {
getLogger().info("Could not shutdown executor " + executor + " for realm " + this, e);
}
-
+
factory.clear();
factory = null;
// Should never be true
executorThread.interrupt();
executorThread = null;
executor = null;
-
+
// Clear nodeManager
nodeManager.clear();
nodeManager = null;
}
private static class StandardRealmThreadFactory implements ThreadFactory {
-
+
private StandardRealm<?, ?> realm;
public StandardRealmThreadFactory(StandardRealm<?, ?> realm) {
this.realm = realm;
}
-
+
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
realm.setExecutorThread(t);
return t;
}
-
+
void clear() {
realm = null;
}
}
-}
+
+}
\ No newline at end of file