X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.charts%2Fsrc%2Forg%2Fsimantics%2Fcharts%2Feditor%2FChartData.java;h=e85c080370e0b2d774aa0b9c174b8424fae84c13;hp=b44556b633713f976ce7ec437f715d86919dd287;hb=908f3683f6cd21dcff70a8f3b1a9d1e3368ca5af;hpb=74045ae2a729ae2ec57db0816709a948eea7d392 diff --git a/bundles/org.simantics.charts/src/org/simantics/charts/editor/ChartData.java b/bundles/org.simantics.charts/src/org/simantics/charts/editor/ChartData.java index b44556b63..e85c08037 100644 --- a/bundles/org.simantics.charts/src/org/simantics/charts/editor/ChartData.java +++ b/bundles/org.simantics.charts/src/org/simantics/charts/editor/ChartData.java @@ -11,6 +11,8 @@ *******************************************************************************/ 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; @@ -58,6 +60,13 @@ public final class ChartData { @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; @@ -75,6 +84,7 @@ public final class ChartData { this.datasource = null; this.history = null; this.collector = null; + this.refCount = null; } else { this.model = other.model; this.run = other.run; @@ -82,6 +92,7 @@ public final class ChartData { this.datasource = other.datasource; this.history = other.history; this.collector = other.collector; + this.refCount = other.refCount; } } @@ -99,4 +110,38 @@ public final class ChartData { } } + 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(); + } + } + } + }