Skip to content

Commit 9e633e3

Browse files
committed
use AtomicReference in DataEntry
this resolves a race-condition situation on multithreading cases
1 parent 9951401 commit 9e633e3

File tree

6 files changed

+20
-16
lines changed

6 files changed

+20
-16
lines changed

agent/core/src/main/java/me/hsgamer/topper/agent/core/DataEntryAgent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ default void onCreate(DataEntry<K, V> entry) {
77
// EMPTY
88
}
99

10-
default void onUpdate(DataEntry<K, V> entry, V oldValue) {
10+
default void onUpdate(DataEntry<K, V> entry, V oldValue, V newValue) {
1111
// EMPTY
1212
}
1313

agent/holder/src/main/java/me/hsgamer/topper/agent/holder/AgentDataHolder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ protected final void onCreate(DataEntry<K, V> entry) {
3939
}
4040

4141
@Override
42-
protected void onUpdate(DataEntry<K, V> entry, V oldValue) {
43-
entryAgentList.forEach(agent -> agent.onUpdate(entry, oldValue));
42+
protected void onUpdate(DataEntry<K, V> entry, V oldValue, V newValue) {
43+
entryAgentList.forEach(agent -> agent.onUpdate(entry, oldValue, newValue));
4444
}
4545

4646
@Override

agent/storage/src/main/java/me/hsgamer/topper/agent/storage/StorageAgent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ public void beforeStop() {
111111
}
112112

113113
@Override
114-
public void onUpdate(DataEntry<K, V> entry, V oldValue) {
115-
storeMap.get().put(entry.getKey(), entry.getValue());
114+
public void onUpdate(DataEntry<K, V> entry, V oldValue, V newValue) {
115+
storeMap.get().put(entry.getKey(), newValue);
116116
}
117117

118118
@Override

core/src/main/java/me/hsgamer/topper/core/DataEntry.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
package me.hsgamer.topper.core;
22

33
import java.util.Objects;
4+
import java.util.concurrent.atomic.AtomicReference;
45
import java.util.function.UnaryOperator;
56

67
public final class DataEntry<K, V> {
78
private final K key;
89
private final DataHolder<K, V> holder;
9-
private volatile V value;
10+
private final AtomicReference<V> value;
1011

1112
public DataEntry(K key, DataHolder<K, V> holder) {
1213
this.key = key;
1314
this.holder = holder;
14-
this.value = holder.getDefaultValue();
15+
this.value = new AtomicReference<>(holder.getDefaultValue());
1516
}
1617

1718
public K getKey() {
1819
return key;
1920
}
2021

2122
public V getValue() {
22-
return value;
23+
return value.get();
2324
}
2425

2526
public void setValue(V value) {
@@ -31,14 +32,17 @@ public void setValue(UnaryOperator<V> operator) {
3132
}
3233

3334
public void setValue(V value, boolean notify) {
34-
if (Objects.equals(this.value, value)) return;
35-
V oldValue = this.value;
36-
this.value = value;
37-
if (notify) holder.onUpdate(this, oldValue);
35+
setValue(v -> value, notify);
3836
}
3937

4038
public void setValue(UnaryOperator<V> operator, boolean notify) {
41-
setValue(operator.apply(value), notify);
39+
this.value.updateAndGet(oldValue -> {
40+
V newValue = operator.apply(oldValue);
41+
if (notify && !Objects.equals(oldValue, newValue)) {
42+
holder.onUpdate(this, oldValue, newValue);
43+
}
44+
return newValue;
45+
});
4246
}
4347

4448
public DataHolder<K, V> getHolder() {

core/src/main/java/me/hsgamer/topper/core/DataHolder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected void onCreate(DataEntry<K, V> entry) {
2323
protected void onRemove(DataEntry<K, V> entry) {
2424
}
2525

26-
protected void onUpdate(DataEntry<K, V> entry, V oldValue) {
26+
protected void onUpdate(DataEntry<K, V> entry, V oldValue, V newValue) {
2727
}
2828

2929
public final String getName() {

spigot/plugin/src/main/java/me/hsgamer/topper/spigot/plugin/holder/NumberTopHolder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public void start() {
9393
});
9494
addEntryAgent(new DataEntryAgent<UUID, Double>() {
9595
@Override
96-
public void onUpdate(DataEntry<UUID, Double> entry, Double oldValue) {
97-
Bukkit.getPluginManager().callEvent(new GenericEntryUpdateEvent(name, entry.getKey(), oldValue, entry.getValue(), true));
96+
public void onUpdate(DataEntry<UUID, Double> entry, Double oldValue, Double newValue) {
97+
Bukkit.getPluginManager().callEvent(new GenericEntryUpdateEvent(name, entry.getKey(), oldValue, newValue, true));
9898
}
9999
});
100100
}

0 commit comments

Comments
 (0)