]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/BlockingAsyncProcedure.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / BlockingAsyncProcedure.java
index 2d3e2804c6ebf4d630984892195c6fe2e2e4b0fa..9f89a0d8fa2a4f2ede8474513ac9f8ae5ce33de1 100644 (file)
 package org.simantics.db.impl;
 
 import org.simantics.db.AsyncReadGraph;
-import org.simantics.db.common.utils.Logger;
 import org.simantics.db.exception.DatabaseException;
-import org.simantics.db.impl.graph.AsyncBarrierImpl;
+import org.simantics.db.impl.graph.BarrierTracing;
 import org.simantics.db.impl.graph.ReadGraphImpl;
+import org.simantics.db.impl.query.AsyncReadEntry;
+import org.simantics.db.impl.query.PendingTaskSupport;
 import org.simantics.db.procedure.AsyncProcedure;
+import org.simantics.db.request.AsyncRead;
 
-public class BlockingAsyncProcedure<Result> implements AsyncProcedure<Result> {
+public class BlockingAsyncProcedure<Result> implements AsyncProcedure<Result>, Runnable {
 
     private static final Object NO_RESULT = new Object();
 
     private final Object key;
-    private final AsyncBarrierImpl barrier;
-    private final ReadGraphImpl procedureGraph;
+    private final ReadGraphImpl queryGraph;
+    private final ReadGraphImpl callerGraph;
+    private final AsyncReadEntry<Result> entry;
     private final AsyncProcedure<Result> procedure;
-
+    private PendingTaskSupport pendingTaskSupport;
+    private final boolean needsToBlock;
     private Object result = NO_RESULT;
     private Throwable exception = null;
-
-    public BlockingAsyncProcedure(AsyncBarrierImpl barrier, ReadGraphImpl procedureGraph, AsyncProcedure<Result> procedure, Object key) {
+    
+    public BlockingAsyncProcedure(ReadGraphImpl callerGraph, AsyncReadEntry<Result> entry, AsyncProcedure<Result> procedure, Object key, boolean needsToBlock) {
+        
+        // A new graph for evaluating the query with correct parent and asyncBarrier
+        queryGraph = callerGraph.withParent(entry, this, needsToBlock);
+        queryGraph.asyncBarrier.inc();
+        
+        // This makes sure that caller does not quit before dispatch
+        callerGraph.asyncBarrier.inc();
+
+        this.entry = entry;
         this.procedure = procedure;
         this.key = key;
-        this.barrier = barrier;
-        this.barrier.inc();
-        this.procedureGraph = procedureGraph;
+        this.queryGraph.asyncBarrier.inc();
+        this.callerGraph = callerGraph;
+        this.needsToBlock = needsToBlock;
+        if (BarrierTracing.BOOKKEEPING) {
+            BarrierTracing.registerBAP(this);
+        }
     }
 
     @Override
     public void execute(AsyncReadGraph graph_, Result result) {
+        
         this.result = result;
-        try {
-            if(procedure != null) procedure.execute(procedureGraph, result);
-        } catch (Throwable throwable) {
-            Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
-        } finally {
-            barrier.dec();
-        }
+        queryGraph.asyncBarrier.dec();
+        
     }
 
     @Override
     public void exception(AsyncReadGraph graph_, Throwable t) {
+
         this.exception = t;
-        try {
-            if(procedure != null) procedure.exception(procedureGraph, t);
-        } catch (Throwable throwable) {
-            Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
-        } finally {
-            barrier.dec();
-        }
+        queryGraph.asyncBarrier.dec();
+        
     }
-
+    
     public void waitBarrier() {
-        barrier.waitBarrier(key, procedureGraph);
+        queryGraph.asyncBarrier.waitBarrier(key, queryGraph);
+    }
+    
+    public void dec() {
+
+        queryGraph.asyncBarrier.dec();
+        
     }
 
     @SuppressWarnings("unchecked")
     public Result get() throws DatabaseException {
 
-        barrier.waitBarrier(key, procedureGraph);
+        if(needsToBlock)
+            queryGraph.asyncBarrier.waitBarrier(key, queryGraph);
 
         if(exception != null) {
             if(exception instanceof DatabaseException) throw (DatabaseException)exception;
@@ -80,10 +95,6 @@ public class BlockingAsyncProcedure<Result> implements AsyncProcedure<Result> {
 
     }
 
-    public boolean isDone() {
-        return barrier.get() == 0;
-    }
-
     @SuppressWarnings("unchecked")
     public Result getResult() {
         return (Result)result;
@@ -97,5 +108,74 @@ public class BlockingAsyncProcedure<Result> implements AsyncProcedure<Result> {
     public String toString() {
         return "." + procedure; 
     }
+    
+    @Override
+    public void run() {
+        
+        AsyncProcedure<Result> procedure__ = entry != null ? entry : procedure;
+
+        ReadGraphImpl executeGraph = callerGraph.withParent(callerGraph.parent, null, needsToBlock);
+        executeGraph.asyncBarrier.inc();
+        
+        // This counters the inc in the constructor 
+        callerGraph.asyncBarrier.dec();
+        
+        try {
+            if(procedure__ != null) {
+                procedure__.execute(executeGraph, get());
+            }
+        } catch (DatabaseException e) {
+            if(procedure__ != null) procedure__.exception(executeGraph, e);
+            exception = e;
+        } catch (Throwable t) {
+            DatabaseException dbe = new DatabaseException(t);
+            if(procedure__ != null) procedure__.exception(executeGraph, dbe);
+            exception = dbe;
+        } finally {
+
+            if (entry != null) {
+                assert(entry.isReady());
+                // This does not throw
+                entry.performFromCache(executeGraph, procedure);
+            }
+
+            executeGraph.asyncBarrier.dec();
+            if(needsToBlock)
+                executeGraph.asyncBarrier.waitBarrier(procedure__, executeGraph);
+        }
+
+        if (BarrierTracing.BOOKKEEPING) {
+            BarrierTracing.unregisterBAP(this);
+        }
+
+    }
+    
+    public void print() {
+        System.err.println("BlockingAsyncProcedure");
+        System.err.println("-key: " + key);
+        System.err.println("-queryGraph: " + queryGraph);
+        System.err.println("-callerGraph: " + callerGraph);
+        System.err.println("-procedure: " + procedure);
+        System.err.println("-pendingTaskSupport: " + pendingTaskSupport);
+        System.err.println("-result: " + result);
+        System.err.println("-exception: " + exception);
+    }
+    
+    public Result performSync(AsyncRead<Result> request) throws DatabaseException {
+        try {
+            request.perform(queryGraph, this);
+        } finally {
+            dec();
+        }
+        return get();
+    }
+
+    public void performAsync(AsyncRead<Result> request) throws DatabaseException {
+        try {
+            request.perform(queryGraph, this);
+        } finally {
+            dec();
+        }
+    }
 
 }