X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fgraph%2FAsyncBarrierImpl.java;fp=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fgraph%2FAsyncBarrierImpl.java;h=637db3f26f8e119173fbf29cbe54b46ed9b17002;hp=060fc22aefb866d15a17bb0f70e7cd4692846d90;hb=a88c02c2d0f4250caf887a130b9f4314c6564722;hpb=785f638bab44e70ec6103c3891daea95bcda9a07 diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/AsyncBarrierImpl.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/AsyncBarrierImpl.java index 060fc22ae..637db3f26 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/AsyncBarrierImpl.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/AsyncBarrierImpl.java @@ -18,209 +18,206 @@ import org.simantics.db.common.utils.Logger; import org.simantics.db.exception.RuntimeDatabaseException; import org.simantics.db.impl.query.CacheEntry; import org.simantics.db.impl.query.QueryProcessor.AsyncBarrier; -import org.simantics.db.impl.query.QueryProcessor.SessionTask; -final public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrier { +public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrier { - private static final long serialVersionUID = 4724463372850048672L; + private static final long serialVersionUID = 4724463372850048672L; - static final int WAIT_TIME = 60000; + static final int WAIT_TIME = 60000; - public static final boolean PRINT = false; + public static final boolean PRINT = false; - final public AsyncBarrierImpl caller; - - final public Runnable callback; + final AsyncBarrierImpl caller; - public AsyncBarrierImpl(AsyncBarrierImpl caller, CacheEntry entry, Runnable callback) { - super(0); - this.caller = caller; - this.callback = callback; + private final Runnable callback; + + private final boolean needsToBlock; + + public AsyncBarrierImpl(AsyncBarrierImpl caller, CacheEntry entry, Runnable callback, boolean needsToBlock) { + super(0); + this.caller = caller; + this.callback = callback; + this.needsToBlock = needsToBlock; if (BarrierTracing.BOOKKEEPING) { BarrierTracing.trace(this, entry); } - } - - - public void inc() { + } + + public AsyncBarrier getBlockingBarrier() { + if(needsToBlock) + return this; + if(caller == null) + return null; + else return caller.getBlockingBarrier(); + } + + @Override + public boolean isBlocking() { + return needsToBlock; + } + + @Override + public void inc() { + + if(BarrierTracing.BOOKKEEPING) { + BarrierTracing.inc(this); + } else { + inc(null, null); + } - if(BarrierTracing.BOOKKEEPING) { - BarrierTracing.inc(this); - } else { - inc(null, null); - } - - } + } - void inc(Object id, String info) { + void inc(Object id, String info) { - if(PRINT) { - System.err.println("inc barrier[" + get() + "] " + this); - StackTraceElement[] elems = new Exception().getStackTrace(); - for(int i=0;i<4;i++) System.err.println(elems[i]); - } + if(PRINT) { + System.err.println("inc barrier[" + get() + "] " + this); + StackTraceElement[] elems = new Exception().getStackTrace(); + for(int i=0;i<4;i++) System.err.println(elems[i]); + } - if (incrementAndGet() == 1) { - if (caller != null) { - if(BarrierTracing.BOOKKEEPING) { + if (incrementAndGet() == 1) { + if (caller != null) { + if(BarrierTracing.BOOKKEEPING) { caller.inc(this, "Child"); - } else { + } else { caller.inc(null, null); - } - } - } + } + } + } - } + } - public void dec() { + @Override + public void dec() { - if(PRINT) { - System.err.println("dec barrier[" + get() + "] " + this); - StackTraceElement[] elems = new Exception().getStackTrace(); - for(int i=0;i<3;i++) System.err.println(elems[i]); - } + if(PRINT) { + System.err.println("dec barrier[" + get() + "] " + this); + StackTraceElement[] elems = new Exception().getStackTrace(); + for(int i=0;i<3;i++) System.err.println(elems[i]); + } - int count = decrementAndGet(); - if (count < 1) { + int count = decrementAndGet(); + if (count < 1) { if(BarrierTracing.BOOKKEEPING) { BarrierTracing.dec(this, count); } - if (count == 0) { - if (caller != null) { - caller.dec(); - } - } - if (count < 0) { - Logger.defaultLogError( - "Database request processing error. The application code has performed illegal actions (probably called multiple times the execute or exception method of a single result request.", - new Exception()); - } - assert (count >= 0); - - if(callback != null) - callback.run(); - - } - - } - - public static String report(AsyncBarrierImpl barrier) { - CacheEntry e = BarrierTracing.entryMap.get(barrier); - if(e != null) return e.toString(); - else return "Barrier@" + System.identityHashCode(barrier); - } - - public static void printReverse(AsyncBarrierImpl barrier, int indent) { - - if (barrier.get() == 0) - return; - for (int i = 0; i < indent; i++) - System.err.print(" "); - System.err.println("[" + barrier.get() + " requests]: " + report(barrier)); -// if (BOOKKEEPING) { -// Debugger debugger = debuggerMap.get(barrier); -// debugger.toErr(indent + 2); -// } - - Collection children = BarrierTracing.reverseLookup.get(barrier); - if (children != null) { - for (AsyncBarrierImpl child : children) - printReverse(child, indent + 2); - } - - } - - public void waitBarrier(Object request, ReadGraphImpl impl) { - - if (get() > 0) { - - long waitCount = 0; - - while (get() != 0) { - - boolean executed = impl.performPending(); - if(executed) waitCount = 0; - - ++waitCount; - if(waitCount > 100) Thread.yield(); - if(waitCount > 1000) { - try { - Thread.sleep(1); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - if(waitCount > WAIT_TIME) { - - System.err.println("AsyncBarrierImpl.waitBarrier(" - + request - + ") is taking long to execute, so far " - + (waitCount / 1000) + " s."); - - if (BarrierTracing.BOOKKEEPING) { - synchronized (BarrierTracing.reverseLookup) { - printReverse(this, 0); - } - BarrierTracing.printBAPS(); - } - - for(SessionTask t : impl.processor.freeScheduling) { - System.err.println("Pending task:" + t); - } - -// if(Development.DEVELOPMENT) { -// impl.processor.threadLocks[0].lock(); -// System.err.println("-queues=" + impl.processor.queues[0].size()); -// impl.processor.threadLocks[0].unlock(); -// System.err.println("-own=" + impl.processor.ownTasks[0].size()); -// System.err.println("-ownSync=" + impl.processor.ownSyncTasks[0].size()); -// for(SessionTask task : impl.processor.ownSyncTasks[0]) { -// System.err.println("--" + task); -// } -// } - - throw new RuntimeDatabaseException("Request timed out."); - //waitCount = 0; - - } - - } - - } - - } - - public void restart() { - assertReady(); - if(BarrierTracing.BOOKKEEPING) { - BarrierTracing.restart(this); - } - } - - public void assertReady() { - int current = get(); - if (current != 0) - throw new AssertionError("Barrier was not finished (pending=" - + current + ")."); - } - - public void report() { - // System.out.println("Barrier log:"); - // for(Map.Entry entry : sources.entrySet()) { - // System.out.println(entry.getKey() + " " + entry.getValue()); - // } - // System.out.println("SyncIntProcedure log:"); - // for(Map.Entry entry : - // SyncIntProcedure.counters.entrySet()) { - // System.out.println(entry.getKey() + " " + entry.getValue()); - // } - } - - @Override - public String toString() { - return report(this); -// return "AsyncBarrierImpl@" + System.identityHashCode(this) -// + " - counter = " + get() + " - caller = " + caller; - } + if (count == 0) { + if (caller != null) { + caller.dec(); + } + } + if (count < 0) { + Logger.defaultLogError( + "Database request processing error. The application code has performed illegal actions (probably called multiple times the execute or exception method of a single result request.", + new Exception()); + } + assert (count >= 0); + + if(callback != null) + callback.run(); + + } + + } + + public static String report(AsyncBarrierImpl barrier) { + CacheEntry e = BarrierTracing.entryMap.get(barrier); + if(e != null) return e.toString(); + else return "Barrier@" + System.identityHashCode(barrier); + } + + public static void printReverse(AsyncBarrierImpl barrier, int indent) { + + if (barrier.get() == 0) + return; + for (int i = 0; i < indent; i++) + System.err.print(" "); + System.err.println("[" + barrier.get() + " requests]: " + report(barrier)); + + Collection children = BarrierTracing.reverseLookup.get(barrier); + if (children != null) { + for (AsyncBarrierImpl child : children) + printReverse(child, indent + 2); + } + + } + + public void waitBarrier(Object request, ReadGraphImpl impl) { + + if (get() > 0) { + + long waitCount = 0; + + while (get() != 0) { + + boolean executed = impl.performPending(); + if(executed) waitCount = 0; + + ++waitCount; + if(waitCount > 100) Thread.yield(); + if(waitCount > 1000) { + try { + Thread.sleep(1); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + if(waitCount > WAIT_TIME) { + + System.err.println("AsyncBarrierImpl.waitBarrier(" + + request + + ") is taking long to execute, so far " + + (waitCount / 1000) + " s."); + + if (BarrierTracing.BOOKKEEPING) { + synchronized (BarrierTracing.reverseLookup) { + printReverse(this, 0); + } + BarrierTracing.printBAPS(); + } + + throw new RuntimeDatabaseException("Request timed out."); + + } + + } + + } + + } + + public void restart() { + assertReady(); + if(BarrierTracing.BOOKKEEPING) { + BarrierTracing.restart(this); + } + } + + public void assertReady() { + int current = get(); + if (current != 0) + throw new AssertionError("Barrier was not finished (pending=" + + current + ")."); + } + + public void report() { + // System.out.println("Barrier log:"); + // for(Map.Entry entry : sources.entrySet()) { + // System.out.println(entry.getKey() + " " + entry.getValue()); + // } + // System.out.println("SyncIntProcedure log:"); + // for(Map.Entry entry : + // SyncIntProcedure.counters.entrySet()) { + // System.out.println(entry.getKey() + " " + entry.getValue()); + // } + } + + @Override + public String toString() { + return report(this); + // return "AsyncBarrierImpl@" + System.identityHashCode(this) + // + " - counter = " + get() + " - caller = " + caller; + } + }