0
0
mirror of https://hub.spigotmc.org/stash/scm/spigot/craftbukkit.git synced 2024-11-21 22:26:17 +00:00
craftbukkit/nms-patches/net/minecraft/util/TickThrottler.patch
2024-10-23 02:15:00 +11:00

54 lines
1.6 KiB
Diff

--- a/net/minecraft/util/TickThrottler.java
+++ b/net/minecraft/util/TickThrottler.java
@@ -1,10 +1,14 @@
package net.minecraft.util;
+// CraftBukkit start
+import java.util.concurrent.atomic.AtomicInteger;
+// CraftBukkit end
+
public class TickThrottler {
private final int incrementStep;
private final int threshold;
- private int count;
+ private final AtomicInteger count = new AtomicInteger(); // CraftBukkit - multithreaded field
public TickThrottler(int i, int j) {
this.incrementStep = i;
@@ -12,17 +16,32 @@
}
public void increment() {
- this.count += this.incrementStep;
+ this.count.addAndGet(this.incrementStep); // CraftBukkit - use thread-safe field access instead
}
public void tick() {
+ // CraftBukkit start
+ for (int val; (val = this.count.get()) > 0 && !count.compareAndSet(val, val - 1); ) ;
+ /* Use thread-safe field access instead
if (this.count > 0) {
--this.count;
}
+ */
+ // CraftBukkit end
}
public boolean isUnderThreshold() {
- return this.count < this.threshold;
+ // CraftBukkit start - use thread-safe field access instead
+ return this.count.get() < this.threshold;
+ }
+
+ public boolean isIncrementAndUnderThreshold() {
+ return isIncrementAndUnderThreshold(this.incrementStep, this.threshold);
+ }
+
+ public boolean isIncrementAndUnderThreshold(int incrementStep, int threshold) {
+ return this.count.addAndGet(incrementStep) < threshold;
+ // CraftBukkit end
}
}