*******************************************************************************/
package org.simantics.charts.editor;
+import java.util.concurrent.atomic.AtomicInteger;
+
import org.simantics.databoard.annotations.Optional;
import org.simantics.db.Resource;
import org.simantics.history.Collector;
@Optional
public Collector collector;
+ /**
+ * This (shared instance) is used to track the amount of users of this ChartData
+ * instance. The same instance may end up in many ChartData instances through
+ * {@link #readFrom(ChartData)}.
+ */
+ public AtomicInteger refCount = new AtomicInteger();
+
public ChartData(Resource model, Resource run, IExperiment experiment, Datasource datasource, HistoryManager history, Collector collector) {
this.model = model;
this.run = run;
this.datasource = null;
this.history = null;
this.collector = null;
+ this.refCount = null;
} else {
this.model = other.model;
this.run = other.run;
this.datasource = other.datasource;
this.history = other.history;
this.collector = other.collector;
+ this.refCount = other.refCount;
}
}
}
}
+ public int reference() {
+ AtomicInteger i = refCount;
+ if (i == null)
+ return 0;
+ int result = i.incrementAndGet();
+ //System.out.println(this + ": reference: " + (result-1) + " -> " + result + " (" + System.identityHashCode(refCount) + ")");
+ return result;
+ }
+
+ public int dereference() {
+ AtomicInteger i = refCount;
+ if (i == null)
+ return 0;
+ int result = i.decrementAndGet();
+ //System.out.println(this + ": dereference: " + (result+1) + " -> " + result + " (" + System.identityHashCode(refCount) + ")");
+ if (result <= 0) {
+ synchronized (i) {
+ i.notifyAll();
+ }
+ }
+ return result;
+ }
+
+ public void waitUntilNotReferenced() throws InterruptedException {
+ AtomicInteger i = refCount;
+ if (i == null)
+ return;
+ synchronized (i) {
+ while (i.get() > 0) {
+ i.wait();
+ }
+ }
+ }
+
}