]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.acorn/src/org/simantics/acorn/MainProgram.java
Uncommon Acorn deadlock fix
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / MainProgram.java
index 78ff9e8995c4162fc347edc9b51bd6f7c69ec4ff..c69c7bea6fe7957bf3c408256a6d9f5cbfed16e4 100644 (file)
@@ -20,9 +20,13 @@ import org.simantics.acorn.lru.ClusterStreamChunk;
 import org.simantics.acorn.lru.ClusterUpdateOperation;
 import org.simantics.db.service.ClusterUID;
 import org.simantics.utils.logging.TimeLogger;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class MainProgram implements Runnable, Closeable {
 
+       private static final Logger LOGGER = LoggerFactory.getLogger(MainProgram.class);
+       
        private static final int CLUSTER_THREADS = 4;
        private static final int CHUNK_CACHE_SIZE = 100;
 
@@ -250,7 +254,45 @@ public class MainProgram implements Runnable, Closeable {
                        deathBarrier.release();
                }
        }
+       
+       static interface MainProgramRunnable {
+               
+               public void run() throws Exception;
+               public void done();
+               
+       }
 
+       public Exception runIdle(MainProgramRunnable runnable) {
+               try {
+                       long startTime = System.currentTimeMillis();
+                       while (true) {
+                               boolean hasMutex = false;
+                               try {
+                                       synchronized (MainProgram.this) {
+                                               if (hasMutex = mutex.tryAcquire()) {
+                                                       if (operations.isEmpty()) {
+                                                               runnable.run();
+                                                               return null;
+                                                       }
+                                               }
+                                       }
+                                       long endTime = System.currentTimeMillis(); 
+                                       if ((endTime - startTime) > 100) {
+                                               startTime = endTime; 
+                                               LOGGER.info("MainProgram.runIdle() retry mutex acquire!");
+                                       }
+                               } catch (Exception e) {
+                                       return e;
+                               } finally {
+                                       if (hasMutex)
+                                               mutex.release();
+                               }
+                       }
+               } finally {
+                       runnable.done();
+               }
+       }
+       
        /*
         * Mutex for streamLRU is assumed here
         * 
@@ -277,7 +319,7 @@ public class MainProgram implements Runnable, Closeable {
 
                ClusterStreamChunk last = operations.isEmpty() ? null : operations.getLast();
         if (!alive) {
-            System.err.println("Trying to commit operation after MainProgram is closed! Operation is " + last);
+            LOGGER.error("Trying to commit operation after MainProgram is closed! Operation is " + last);
 //          return;
         }
                if(last != null) last.commit();
@@ -286,7 +328,7 @@ public class MainProgram implements Runnable, Closeable {
 
        public synchronized void schedule(ClusterUpdateOperation operation) throws IllegalAcornStateException {
            if (!alive) {
-               System.err.println("Trying to schedule operation after MainProgram is closed! Operation is " + operation);
+               LOGGER.error("Trying to schedule operation after MainProgram is closed! Operation is " + operation);
 //             return;
            }
                clusters.streamLRU.acquireMutex();