]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AsyncReadEntry.java
Trying to remove synchronization problems
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / AsyncReadEntry.java
index 75169d02e9946ca0a1dea3f1e03a3dba2ce49b97..69a85220b954cac8d1dc0e24ef25ca07edd0c407 100644 (file)
@@ -14,84 +14,72 @@ package org.simantics.db.impl.query;
 import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.common.GraphSemaphore;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.RuntimeDatabaseException;
 import org.simantics.db.impl.DebugPolicy;
 import org.simantics.db.impl.graph.ReadGraphImpl;
 import org.simantics.db.procedure.AsyncProcedure;
 import org.simantics.db.request.AsyncRead;
 
-final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
+final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> implements AsyncProcedure<T> {
 
     protected AsyncRead<T> request;
 
     AsyncReadEntry(AsyncRead<T> request) {
-       this.request = request;
-       if(DebugPolicy.QUERY_STATE) System.out.println("[QUERY STATE]: created " + this);
+        this.request = request;
+        if (DebugPolicy.QUERY_STATE)
+            System.out.println("[QUERY STATE]: created " + this);
     }
 
     @Override
     int makeHash() {
-       return request.hashCode();
+        return request.hashCode();
     }
-    
+
     @Override
     public Object getOriginalRequest() {
         return request;
     }
-    
+
     @Override
     public void discard() {
-       super.discard();
-       setResult(null);
+        super.discard();
+        setResult(null);
     }
-    
-    final public void addOrSet(AsyncReadGraph graph, Object item) {
 
-//     System.err.println("addOrSet " + request + " " + Thread.currentThread() + " " + item);
-       
-       assert(isPending());
-
-        synchronized(this) {
-            setResult(item);
-               setReady();
-        }
-        
-    }
-    
-    
     public void except(AsyncReadGraph graph, Throwable t) {
-       
-       assert(isPending());
 
-        synchronized(this) {
+        assert (isPending());
+
+        synchronized (this) {
             except(t);
         }
-       
+
     }
-    
-    
+
     @Override
     final public Query getQuery() {
-       
+
         return new Query() {
 
-                       @Override
-                       public void recompute(ReadGraphImpl graph) {
+            @Override
+            public void recompute(ReadGraphImpl graph) {
 
-                               try {
+                try {
 
-                                       GraphSemaphore s = new GraphSemaphore(graph, 0);
-                                       
-                                   request.perform(graph , new AsyncProcedure<T>() {
+                    GraphSemaphore s = new GraphSemaphore(graph, 0);
+
+                    request.perform(graph, new AsyncProcedure<T>() {
 
                         @Override
                         public void execute(AsyncReadGraph graph, T result) {
-                            addOrSet(graph, result);
+                            setResult(result);
+                            setReady();
                             s.release();
                         }
-                               
-                               @Override
-                               public void exception(AsyncReadGraph graph, Throwable t) {
-                                   except(t);
+
+                        @Override
+                        public void exception(AsyncReadGraph graph, Throwable t) {
+                            except(t);
                             s.release();
                         }
 
@@ -99,144 +87,129 @@ final public class AsyncReadEntry<T> extends CacheEntryBase<AsyncProcedure<T>> {
 
                     s.waitFor(1);
 
-                               } catch (Throwable t) {
-                                   except(t);
+                } catch (Throwable t) {
+                    except(t);
                 }
-                               
-                       }
-
-                       @Override
-                       public void removeEntry(QueryProcessor qp) {
-                       qp.cache.remove(AsyncReadEntry.this);
-                       }
-
-                       @Override
-                       public int type() {
-                               return request.getFlags();
-                       }
-                       
-                       @Override
-                       public String toString() {
-                               if(request == null) return "DISCARDED";
-                               else if(isExcepted()) return request.toString() + " " + getResult();
-                               else return request.toString() + " " + statusOrException;
-                       }
-               
+
+            }
+
+            @Override
+            public void removeEntry(QueryProcessor qp) {
+                qp.cache.remove(AsyncReadEntry.this);
+            }
+
+            @Override
+            public int type() {
+                return request.getFlags();
+            }
+
+            @Override
+            public String toString() {
+                if (request == null)
+                    return "DISCARDED";
+                else if (isExcepted())
+                    return request.toString() + " " + getResult();
+                else
+                    return request.toString() + " " + statusOrException;
+            }
+
         };
-        
+
     }
 
     @Override
-       public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
-               
-           if(isExcepted()) {
-            
+    public Object performFromCache(ReadGraphImpl graph, AsyncProcedure<T> proc) {
+
+        if (isExcepted()) {
+
             try {
-                proc.exception(graph, (Throwable)getResult());
+                proc.exception(graph, (Throwable) getResult());
             } catch (Throwable t) {
                 t.printStackTrace();
             }
-            
+
         } else {
-            
+
             try {
-                proc.execute(graph, (T)getResult());
+                proc.execute(graph, (T) getResult());
             } catch (Throwable t) {
                 t.printStackTrace();
             }
-            
+
+        }
+
+        return getResult();
+
+    }
+
+    public static <T> void computeForEach(ReadGraphImpl parentGraph, AsyncRead<T> request, AsyncReadEntry<T> entry,
+            AsyncProcedure<T> procedure_) throws DatabaseException {
+
+        AsyncProcedure<T> procedure = entry != null ? entry : procedure_;
+
+        GraphSemaphore s = new GraphSemaphore(parentGraph, 0);
+
+        ReadGraphImpl queryGraph = parentGraph.withParent(entry);
+
+        request.perform(queryGraph, new AsyncProcedure<T>() {
+
+            @Override
+            public void execute(AsyncReadGraph returnGraph, T result) {
+                try {
+                    procedure.execute(parentGraph, result);
+                } catch (Throwable t) {
+                    t.printStackTrace();
+                }
+                s.release();
+            }
+
+            @Override
+            public void exception(AsyncReadGraph returnGraph, Throwable t) {
+                try {
+                    procedure.exception(parentGraph, t);
+                } catch (Throwable t2) {
+                    t2.printStackTrace();
+                }
+                s.release();
+            }
+
+            @Override
+            public String toString() {
+                return procedure.toString();
+            }
+
+        });
+
+        try {
+            s.waitFor(1);
+        } catch (InterruptedException e) {
+            throw new RuntimeDatabaseException(e);
         }
-               
-           return getResult();
-           
-       }
-
-    //@Override
-    public Object compute(ReadGraphImpl graph, AsyncProcedure<T> procedure) throws DatabaseException {
-
-       ReadGraphImpl queryGraph = graph.withParent(this);
-
-       request.perform(queryGraph, new AsyncProcedure<T>() {
-
-               @Override
-               public void execute(AsyncReadGraph returnGraph, T result) {
-                       ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
-                       AsyncReadEntry.this.addOrSet(graph, result);
-                       try {
-                               procedure.execute(graph, result);
-                       } catch (Throwable t) {
-                               t.printStackTrace();
-                       }
-                       //                                      parentBarrier.dec(query);
-               }
-
-               @Override
-               public void exception(AsyncReadGraph returnGraph, Throwable t) {
-                       ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
-                       //                                      AsyncReadGraph resumeGraph = finalParentGraph.newAsync();
-                       AsyncReadEntry.this.except(graph, t);
-                       try {
-                               procedure.exception(graph, t);
-                       } catch (Throwable t2) {
-                               t2.printStackTrace();
-                       }
-                       //                                      parentBarrier.dec(query);
-               }
-
-               @Override
-               public String toString() {
-                       return procedure.toString();
-               }
-
-       });
-
-               return getResult();
-       
+
+        if (entry != null)
+            entry.performFromCache(parentGraph, procedure_);
+
+    }
+
+    @Override
+    public String toString() {
+        if (isDiscarded())
+            return "DISCARDED " + request.toString();
+        else if (isExcepted())
+            return request.toString() + " " + getResult();
+        else
+            return request.toString() + " " + statusOrException;
     }
-    
-    public static <T> void computeForEach(ReadGraphImpl parentGraph, AsyncRead<T> request, AsyncReadEntry<T> entry, AsyncProcedure<T> procedure) throws DatabaseException {
-
-               ReadGraphImpl queryGraph = parentGraph.withParent(entry);
-
-               request.perform(queryGraph, new AsyncProcedure<T>() {
-
-                       @Override
-                       public void execute(AsyncReadGraph returnGraph, T result) {
-                               ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
-                               if(entry != null) entry.addOrSet(parentGraph, result);
-                               try {
-                                       procedure.execute(parentGraph, result);
-                               } catch (Throwable t) {
-                                       t.printStackTrace();
-                               }
-                       }
-
-                       @Override
-                       public void exception(AsyncReadGraph returnGraph, Throwable t) {
-                               ReadGraphImpl impl = (ReadGraphImpl)returnGraph;
-                               if(entry != null) entry.except(parentGraph, t);
-                               try {
-                                       procedure.exception(parentGraph, t);
-                               } catch (Throwable t2) {
-                                       t2.printStackTrace();
-                               }
-                       }
-
-                       @Override
-                       public String toString() {
-                               return procedure.toString();
-                       }
-
-               });
-       
+
+    @Override
+    public void execute(AsyncReadGraph graph, T result) {
+        setResult(result);
+        setReady();
+    }
+
+    @Override
+    public void exception(AsyncReadGraph graph, Throwable throwable) {
+        except(throwable);
     }
-    
-    
-       @Override
-       public String toString() {
-               if(isDiscarded()) return "DISCARDED " + request.toString();
-               else if(isExcepted()) return request.toString() + " " + getResult();
-               else return request.toString() + " " + statusOrException;
-       }
 
 }