]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/AsyncBarrierImpl.java
Trying to wait for procedures
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / graph / AsyncBarrierImpl.java
index af0604ae6c6abe6d3dee3a573509b4a304471b54..b94ec6f6857cb0e0ca913ef920a5231c9ecc3577 100644 (file)
  *******************************************************************************/
 package org.simantics.db.impl.graph;
 
-import java.util.ArrayList;
 import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.simantics.db.common.utils.Logger;
@@ -26,122 +23,46 @@ final public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrie
 
        private static final long serialVersionUID = 4724463372850048672L;
 
-       static final HashMap<AsyncBarrierImpl, Collection<AsyncBarrierImpl>> reverseLookup = new HashMap<>();
-       public static final HashMap<AsyncBarrierImpl, Debugger> debuggerMap = new HashMap<>();
-       static final HashMap<AsyncBarrierImpl, CacheEntry<?>> entryMap = new HashMap<>();
-       static final HashMap<AsyncBarrierImpl, Boolean> restartMap = new HashMap<>();
-
        static final int WAIT_TIME = 600;
 
-       public static final boolean BOOKKEEPING = false;
        public static final boolean PRINT = false;
-       static final boolean RESTART_GUARD = true;
 
        final public AsyncBarrierImpl caller;
 
-       //private final Semaphore sema = new Semaphore(0);
-
        public AsyncBarrierImpl(AsyncBarrierImpl caller, CacheEntry<?> entry) {
                super(0);
-               if (BOOKKEEPING) {
-                       synchronized (entryMap) {
-                               entryMap.put(this, entry);
-                       }
-                       synchronized (debuggerMap) {
-                               debuggerMap.put(this, new Debugger());
-                       }
-                       synchronized (reverseLookup) {
-                               Collection<AsyncBarrierImpl> barriers = reverseLookup
-                                               .get(caller);
-                               if (barriers == null) {
-                                       barriers = new ArrayList<AsyncBarrierImpl>();
-                                       reverseLookup.put(caller, barriers);
-                               }
-                               barriers.add(this);
-                       }
-               }
                this.caller = caller;
+        if (BarrierTracing.BOOKKEEPING) {
+            BarrierTracing.trace(this, entry);
+        }
        }
 
-       public class Debugger {
-               
-               public Map<AsyncBarrierImpl, String> infos = new HashMap<>();
-
-               public synchronized void inc(AsyncBarrierImpl id, String info) {
-                       if (id == null)
-                               return;
-                       String exist = infos.get(id);
-                       if (exist != null)
-                               throw new IllegalStateException("Already existing info " + id + " " + info);
-                       infos.put(id, exist);
-               }
-
-               public synchronized void dec(AsyncBarrierImpl id) {
-                       if (id == null)
-                               return;
-                       String exist = infos.get(id);
-                       if (exist == null) {
-                               System.err.println("No data for " + id);
-                       } else {
-                               infos.remove(id);
-                       }
-               }
-
-               @Override
-               public synchronized String toString() {
-                       StringBuilder b = new StringBuilder();
-                       for (String s : infos.values()) {
-                               b.append("info " + s + "\r\n");
-                       }
-                       return b.toString();
-               }
-               
-               public boolean isEmpty() {
-                       return infos.isEmpty();
-               }
-
-       }
 
        public void inc() {
 
-               if (BOOKKEEPING)
-                       inc(this, new Exception().getStackTrace()[2].toString());
-               else
-                       inc(null, null);
-               
-               if (RESTART_GUARD)
-                       if(restartMap.containsKey(this))
-                               throw new IllegalStateException("Unplanned restart");
-
+           if(BarrierTracing.BOOKKEEPING) {
+               BarrierTracing.inc(this);
+           } else {
+               inc(null, null);
+           }
+           
        }
 
-       private void inc(Object id, String info) {
-
-               //              if (PRINT) {
-               //                      if (get() < 5)
-               //                              new Exception("inc " + get() + " " + this).printStackTrace();
-               //              }
-
-               if (BOOKKEEPING) {
-//                     Debugger debugger = debuggerMap.get(this);
-//                     if (debugger != null)
-//                             debugger.inc(id, 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 (incrementAndGet() == 1) {
                        if (caller != null) {
-                               if (BOOKKEEPING)
-                                       caller.inc(this, "Child");
-                               else
-                                       caller.inc(null, null);
+                       if(BarrierTracing.BOOKKEEPING) {
+                    caller.inc(this, "Child");
+                       } else {
+                    caller.inc(null, null);
+                       }
                        }
                }
 
@@ -157,10 +78,13 @@ final public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrie
 
                int count = decrementAndGet();
                if (count < 1) {
+            if(BarrierTracing.BOOKKEEPING) {
+                BarrierTracing.dec(this, count);
+            }
                        if (count == 0) {
-                               if (caller != null)
-                                       caller.dec(this);
-                               //                              sema.release();
+                               if (caller != null) {
+                                       caller.dec();
+                               }
                        }
                        if (count < 0) {
                                Logger.defaultLogError(
@@ -172,43 +96,8 @@ final public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrie
 
        }
 
-       public void dec(Object id) {
-
-               if (PRINT) {
-                       if (get() < 5)
-                               new Exception("dec" + get() + " " + this).printStackTrace();
-               }
-
-               if (BOOKKEEPING) {
-//                     Debugger debugger = debuggerMap.get(this);
-//                     if (debugger != null) {
-//                             debugger.dec(id);
-//                             if(debugger.isEmpty())
-//                                     debuggerMap.remove(this);
-//                     }
-               }
-
-               int count = decrementAndGet();
-               if (count < 1) {
-                       if (count == 0) {
-                               debuggerMap.remove(this);
-                               if (caller != null)
-                                       caller.dec(this);
-                               if (RESTART_GUARD)
-                                       restartMap.put(this, true);
-                       }
-                       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());
-                               System.exit(-1);
-                       }
-                       assert (count >= 0);
-               }
-       }
-
        public static String report(AsyncBarrierImpl barrier) {
-               CacheEntry<?> e = entryMap.get(barrier);
+               CacheEntry<?> e = BarrierTracing.entryMap.get(barrier);
                if(e != null) return e.toString();
                else return "Barrier@" + System.identityHashCode(barrier);
        }
@@ -225,7 +114,7 @@ final public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrie
 //                     debugger.toErr(indent + 2);
 //             }
 
-               Collection<AsyncBarrierImpl> children = reverseLookup.get(barrier);
+               Collection<AsyncBarrierImpl> children = BarrierTracing.reverseLookup.get(barrier);
                if (children != null) {
                        for (AsyncBarrierImpl child : children)
                                printReverse(child, indent + 2);
@@ -260,12 +149,10 @@ final public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrie
                                                        + ") is taking long to execute, so far "
                                                        + (waitCount / 1000) + " s.");
 
-                                       if (BOOKKEEPING) {
-
-                                               synchronized (reverseLookup) {
+                                       if (BarrierTracing.BOOKKEEPING) {
+                                               synchronized (BarrierTracing.reverseLookup) {
                                                        printReverse(this, 0);
                                                }
-
                                        }
 
 //                                     if(Development.DEVELOPMENT) {
@@ -292,12 +179,9 @@ final public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrie
 
        public void restart() {
                assertReady();
-               // log.clear();
-               //              sema.drainPermits();
-               if (RESTART_GUARD)
-                       restartMap.remove(this);
-               if (BOOKKEEPING)
-                       debuggerMap.put(this, new Debugger());
+               if(BarrierTracing.BOOKKEEPING) {
+                   BarrierTracing.restart(this);
+               }
        }
 
        public void assertReady() {