-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add multithreaded event support for measurements and dataset proc
* Port Measurements and MathDataSets to new event system * indicators: use and set dirty flags * event processors: thread and animation timer based * split trending measurements from dataset measurements * feedback from @ennerf: * changed to fireInvalidated shorthand * removed locks from bitstate * ValueIndicators: move from bitstate to directly calling postListener * YIndicator: fix drag offset
- Loading branch information
Showing
34 changed files
with
1,275 additions
and
622 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
chartfx-chart/src/main/java/io/fair_acc/chartfx/events/FxEventProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package io.fair_acc.chartfx.events; | ||
|
||
import io.fair_acc.dataset.events.BitState; | ||
import io.fair_acc.dataset.events.ChartBits; | ||
import io.fair_acc.dataset.events.EventProcessor; | ||
import javafx.animation.AnimationTimer; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
/** | ||
* An event processor class which processes dataset events un the UI thread of the chart. | ||
* All datasets added to this processor will be processed whenever they are invalidated. | ||
* <p> | ||
* TODO: check how to ensure that everything gets garbage-collected correctly | ||
*/ | ||
public class FxEventProcessor extends AnimationTimer implements EventProcessor { | ||
BitState localState = BitState.initDirty(this, ChartBits.DataSetMask); | ||
BitState stateRoot = BitState.initDirtyMultiThreaded(this, ChartBits.DataSetMask); | ||
List<Pair<BitState, Runnable>> actions = new ArrayList<>(); | ||
private static final AtomicReference<FxEventProcessor> INSTANCE = new AtomicReference<>(); | ||
|
||
public static FxEventProcessor getInstance() { | ||
FxEventProcessor result = INSTANCE.get(); | ||
if (result != null) { | ||
return result; | ||
} | ||
// probably does not exist yet, but initialise in thread safe way | ||
result = new FxEventProcessor(); | ||
if (INSTANCE.compareAndSet(null, result)) { | ||
return result; | ||
} else { | ||
return INSTANCE.get(); | ||
} | ||
} | ||
|
||
public FxEventProcessor() { | ||
start(); | ||
} | ||
|
||
@Override | ||
public void handle(final long now) { | ||
localState.setDirty(stateRoot.clear()); | ||
if (localState.isDirty()) { | ||
for (final var action : actions) { | ||
if (action.getLeft().isDirty(ChartBits.DataSetMask)) { | ||
action.getLeft().clear(); | ||
try { | ||
action.getRight().run(); | ||
} catch (Exception ignored) {} | ||
} | ||
} | ||
} | ||
localState.clear(); | ||
// TODO: perform multiple iterations if there are changes to handle wrongly ordered processing chains and break after timeout | ||
} | ||
|
||
public BitState getBitState() { | ||
return stateRoot; | ||
} | ||
|
||
@Override | ||
public void addAction(final BitState obj, final Runnable action) { | ||
obj.addInvalidateListener(stateRoot); | ||
actions.add(Pair.of(obj, action)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.