X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2FGraphSemaphore.java;fp=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2FGraphSemaphore.java;h=2924f0275a394cb57e160a752c8323cb3c84f03d;hb=1a8971ec56f21b4532e22d787c3f5a15df0b5f2b;hp=0000000000000000000000000000000000000000;hpb=8744cc98e176b7f74f166fde5df76c84089a63b9;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/GraphSemaphore.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/GraphSemaphore.java new file mode 100644 index 000000000..2924f0275 --- /dev/null +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/GraphSemaphore.java @@ -0,0 +1,48 @@ +package org.simantics.db.common; + +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; + +import org.simantics.db.AsyncReadGraph; +import org.simantics.db.exception.DatabaseException; + +public class GraphSemaphore extends Semaphore { + + private static final long serialVersionUID = 2114861433176831938L; + + private final AsyncReadGraph graph; + + public GraphSemaphore(AsyncReadGraph graph, int permits) { + super(permits); + this.graph = graph; + } + + public void waitFor(int permits) throws DatabaseException, InterruptedException { + + boolean success = false; + success = tryAcquire(permits); + if(success) return; + + while(!success) { + + if(graph.performPending()) { + // Some task was done + success = tryAcquire(permits); + } else { + // Nothing to do - just wait + try { + success = tryAcquire(permits, 10, TimeUnit.SECONDS); + if(!success) throw new DatabaseException("Timeout while waiting for async request to complete."); + } catch (InterruptedException e) { + throw new DatabaseException(e); + } + } + + } + + + } + + + +}