]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryProcessor.java
Trying to wait for procedures
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / QueryProcessor.java
index 65b277e6c8d2985410b82e12ce4edca3f1fea09d..b2951cb27e3c122322aeca97aabe124cac8b9702 100644 (file)
@@ -51,6 +51,7 @@ import org.simantics.db.exception.NoInverseException;
 import org.simantics.db.exception.ResourceNotFoundException;
 import org.simantics.db.impl.DebugPolicy;
 import org.simantics.db.impl.ResourceImpl;
+import org.simantics.db.impl.graph.BarrierTracing;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.impl.graph.ReadGraphSupport;
 import org.simantics.db.impl.graph.WriteGraphImpl;
@@ -171,7 +172,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
        public void close() {
        }
 
-       SessionTask getOwnTask(ReadGraphImpl impl) {
+       public SessionTask getOwnTask(ReadGraphImpl impl) {
                Set<ReadGraphImpl> ancestors = impl.ancestorSet();
                synchronized(querySupportLock) {
                        int index = 0;
@@ -186,6 +187,21 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                return null;
        }
 
+    public SessionTask getSubTask(ReadGraphImpl impl) {
+        Set<ReadGraphImpl> onlyThis = Collections.singleton(impl);
+        synchronized(querySupportLock) {
+            int index = 0;
+            while(index < freeScheduling.size()) {
+                SessionTask task = freeScheduling.get(index);
+                if(task.hasCommonParent(onlyThis)) {
+                    return freeScheduling.remove(index);
+                }
+                index++;
+            }
+        }
+        return null;
+    }
+
        public boolean performPending(ReadGraphImpl graph) {
                SessionTask task = getOwnTask(graph);
                if(task != null) {
@@ -201,7 +217,7 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 //     }
 
        final public void schedule(SessionTask request) {
-
+           
                //int performer = request.thread;
 
 //             if(DebugPolicy.SCHEDULE)
@@ -218,24 +234,20 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 //                     if(performer == THREADS) {
                                
                                synchronized(querySupportLock) {
-                                       
-                                       //new Exception().printStackTrace();
-                                       
+
+                                       if(BarrierTracing.BOOKKEEPING) {
+                                               Exception current = new Exception();
+                                               Exception previous = BarrierTracing.tasks.put(request, current);
+                                               if(previous != null) {
+                                                       previous.printStackTrace();
+                                                       current.printStackTrace();
+                                               }
+                                       }
+                                   
                                        freeScheduling.add(request);
                                        
                                        querySupportLock.notifyAll();
 
-                                       //System.err.println("schedule free task " + request + " => " + freeScheduling.size());
-
-//                                     for(int i=0;i<THREADS;i++) {
-//                                             ReentrantLock queueLock = threadLocks[i];
-//                                             queueLock.lock();
-//                                             //queues[performer].add(request);
-//                                             //if(ThreadState.SLEEP == threadStates[i]) sleepers.decrementAndGet();
-//                                             threadConditions[i].signalAll();
-//                                             queueLock.unlock();
-//                                     }
-
                                }
 
                                return;
@@ -265,9 +277,12 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
 
                public final ReadGraphImpl graph;
                private Set<ReadGraphImpl> ancestors;
+               private int counter = 0;
+               private Exception trace;
 
                public SessionTask(ReadGraphImpl graph) {
                        this.graph = graph;
+                       if(graph != null) graph.asyncBarrier.inc();
                }
 
                public boolean hasCommonParent(Set<ReadGraphImpl> otherAncestors) {
@@ -276,7 +291,22 @@ final public class QueryProcessor extends AbstractDisposable implements ReadGrap
                        return !Collections.disjoint(ancestors, otherAncestors);
                }
 
-               public abstract void run(int thread);
+               public abstract void run0(int thread);
+
+               public final void run(int thread) {
+                   if(counter++ > 0) {
+                       if(BarrierTracing.BOOKKEEPING) {
+                           trace.printStackTrace();
+                           new Exception().printStackTrace();
+                       }
+                       throw new IllegalStateException("Multiple invocations of SessionTask!");
+                   }
+                   if(BarrierTracing.BOOKKEEPING) {
+                       trace = new Exception();
+                   }
+                   run0(thread);
+                   if(graph != null) graph.asyncBarrier.dec();
+               }
 
                @Override
                public String toString() {