Skip to content

Commit

Permalink
removed locks from bitstate
Browse files Browse the repository at this point in the history
  • Loading branch information
ennerf committed Sep 22, 2023
1 parent bf17521 commit 24768c2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,6 @@ public static StringBuilder appendStackTrace(StringBuilder builder, int from, in
private static final int DEFAULT_MIN_STACK_TRACE = 6;
private static final int DEFAULT_MAX_STACK_TRACE = 25;

public abstract void waitForFlag();

private static class FilteredListener implements StateListener {

private FilteredListener(int filter, StateListener listener) {
Expand Down Expand Up @@ -379,11 +377,6 @@ public int clear(final int bits) {
return previous;
}

@Override
public void waitForFlag() {
throw new IllegalStateException("cannot wait on single threaded bitstate");
}

@Override
public int getBits() {
return state;
Expand All @@ -402,7 +395,6 @@ protected static class MultiThreadedBitState extends BitState {
protected MultiThreadedBitState(Object source, int filter, int initial) {
super(source, filter);
state.set(initial);
//state.notifyAll();
}

@Override
Expand All @@ -413,12 +405,7 @@ public int setDirtyAndGetDelta(int bits) {
final int oldState = getBits();
final int newState = oldState | bits;
final int delta = (oldState ^ newState);
if (delta == 0) {
return delta;
} else if ( state.compareAndSet(oldState, oldState | bits)) {
synchronized (state) {
state.notifyAll();
}
if (oldState == newState || state.compareAndSet(oldState, newState)) {
return delta;
}
}
Expand All @@ -427,13 +414,10 @@ public int setDirtyAndGetDelta(int bits) {
@Override
public int clear(int bits) {
while (true) {
final int current = getBits();
final int newState = current & ~bits;
if (state.compareAndSet(current, newState)) {
synchronized (state) {
state.notifyAll();
}
return current;
final int oldState = getBits();
final int newState = oldState & ~bits;
if (oldState == newState || state.compareAndSet(oldState, newState)) {
return oldState;
}
}
}
Expand All @@ -443,17 +427,6 @@ public int getBits() {
return state.get();
}

@Override
public void waitForFlag() { // todo check if/where lock is needed
while (state.get() == 0) {
try {
synchronized (state) {
state.wait();
}
} catch (InterruptedException ignored) { }
}
}

private final AtomicInteger state = new AtomicInteger();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public class ThreadEventProcessor implements EventProcessor, Runnable {
private static final AtomicReference<ThreadEventProcessor> INSTANCE = new AtomicReference<>();
private static EventProcessor userInstance;

private final BitState localState = BitState.initDirty(this, ChartBits.DataSetMask);
private final BitState stateRoot = BitState.initDirtyMultiThreaded(this, ChartBits.DataSetMask);
private final Object changeLock = new Object();
private final BitState state = BitState.initDirtyMultiThreaded(this, ChartBits.DataSetMask)
.addChangeListener((src, bits) -> notifyChanged());
private final List<Pair<BitState, Runnable>> actions = new CopyOnWriteArrayList<>();

public static EventProcessor getUserInstance() {
Expand Down Expand Up @@ -58,8 +59,8 @@ public static ThreadEventProcessor getInstance() {
public void run() {
//noinspection InfiniteLoopStatement
while (true) {
localState.setDirty(stateRoot.clear());
if (localState.isDirty()) {
boolean isDirty = state.clear() != 0;
if (isDirty) {
for (final var action : actions) {
if (action.getLeft().isDirty(ChartBits.DataSetMask)) {
action.getLeft().clear();
Expand All @@ -69,19 +70,35 @@ public void run() {
}
}
}
localState.clear();
stateRoot.waitForFlag();
// Todo: add optional rate limiting
waitForChanges();
}
}

private void notifyChanged() {
synchronized (changeLock) {
changeLock.notifyAll();
}
}

private void waitForChanges() {
synchronized (changeLock) {
if (state.isClean()) {
try {
changeLock.wait();
} catch (InterruptedException ignored) {
}
}
}
}

public BitState getBitState() {
return stateRoot;
return state;
}

@Override
public void addAction(final BitState obj, final Runnable action) {
obj.addInvalidateListener(stateRoot);
obj.addInvalidateListener(state);
actions.add(Pair.of(obj, action));
}
}

0 comments on commit 24768c2

Please sign in to comment.