]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/GraphSemaphore.java
Thread count was wrong
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / GraphSemaphore.java
1 package org.simantics.db.common;
2
3 import java.util.concurrent.Semaphore;
4 import java.util.concurrent.TimeUnit;
5
6 import org.simantics.db.AsyncReadGraph;
7 import org.simantics.db.exception.DatabaseException;
8
9 public class GraphSemaphore extends Semaphore {
10
11     private static final long serialVersionUID = 2114861433176831938L;
12
13     private final AsyncReadGraph graph;
14
15     public GraphSemaphore(AsyncReadGraph graph, int permits) {
16         super(permits);
17         this.graph = graph;
18     }
19
20     public void waitFor(int permits) throws DatabaseException, InterruptedException {
21         boolean success = false;
22         success = tryAcquire(permits);
23         if (success)
24             return;
25
26         while (!success) {
27             if (graph.performPending()) {
28                 // Some task was done
29                 success = tryAcquire(permits);
30             } else {
31                 // Nothing to do - just wait
32                 try {
33                     success = tryAcquire(permits, 10, TimeUnit.SECONDS);
34                     if (!success)
35                         throw new DatabaseException("Timeout while waiting for async request to complete.");
36                 } catch (InterruptedException e) {
37                     throw new DatabaseException(e);
38                 }
39             }
40         }
41     }
42 }