]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.acorn/src/org/simantics/acorn/lru/ClusterUpdateState.java
Merge commit '728147df5d63a3333daff3d8c0e9bfd4f5597e3a'
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / lru / ClusterUpdateState.java
1 package org.simantics.acorn.lru;
2
3 import java.util.concurrent.Semaphore;
4
5 import org.simantics.db.service.ClusterUID;
6
7 public class ClusterUpdateState {
8         
9         final ClusterUID uid;
10         final Semaphore lock = new Semaphore(0);
11         int referenceCount = 0;
12         boolean inUpdate = false;
13         
14         ClusterUpdateState(ClusterUID uid) {
15                 this.uid = uid;
16         }
17         
18         public void waitForUpdates() {
19                 try {
20                         lock.acquire();
21                 } catch (InterruptedException e) {
22                         e.printStackTrace();
23                 }
24         }
25         
26         public synchronized void beginUpdate() {
27 //          System.err.println("ClusterUpdateState.beginUpdate() for " + uid + ", inUpdate=" + inUpdate);
28                 assert(!inUpdate);
29                 inUpdate = true;
30         }
31         
32         public synchronized void endUpdate() {
33 //          System.err.println("ClusterUpdateState.endUpdate() for " + uid + ", inUpdate=" + inUpdate);
34                 assert(inUpdate);
35                 inUpdate = false;
36         }
37         
38         public synchronized void incRef() {
39                 referenceCount++;
40         }
41         
42         public synchronized ClusterUpdateState decRef() {
43                 referenceCount--;
44                 if(referenceCount == 0) {
45                         lock.release(Integer.MAX_VALUE);
46                         return null;
47                 }
48                 return this;
49         }
50         
51 }