9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-25 09:59:15 +00:00

remove box on SpscIntQueue#recv

This commit is contained in:
hayanesuru
2025-05-04 23:19:53 +08:00
parent ce01c9bb1f
commit 3ae994e707
3 changed files with 13 additions and 7 deletions

View File

@@ -8,6 +8,7 @@ import org.apache.logging.log4j.Logger;
import org.dreeam.leaf.config.modules.async.AsyncTargetFinding;
import org.dreeam.leaf.util.queue.SpscIntQueue;
import java.util.OptionalInt;
import java.util.concurrent.locks.LockSupport;
public class AsyncGoalExecutor {
@@ -55,10 +56,11 @@ public class AsyncGoalExecutor {
public final void midTick() {
while (true) {
Integer id = this.wake.recv();
if (id == null) {
OptionalInt result = this.wake.recv();
if (result.isEmpty()) {
break;
}
int id = result.getAsInt();
if (poll(id)) {
submit(id);
}

View File

@@ -4,6 +4,7 @@ import net.minecraft.Util;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import java.util.OptionalInt;
import java.util.concurrent.locks.LockSupport;
public class AsyncGoalThread extends Thread {
@@ -22,10 +23,11 @@ public class AsyncGoalThread extends Thread {
for (ServerLevel level : server.getAllLevels()) {
var exec = level.asyncGoalExecutor;
while (true) {
Integer id = exec.queue.recv();
if (id == null) {
OptionalInt result = exec.queue.recv();
if (result.isEmpty()) {
break;
}
int id = result.getAsInt();
retry = true;
if (exec.wake(id)) {
while (!exec.wake.send(id)) {

View File

@@ -2,6 +2,8 @@ package org.dreeam.leaf.util.queue;
import org.jetbrains.annotations.Nullable;
import java.util.OptionalInt;
/// Lock-free Single Producer Single Consumer Queue
public class SpscIntQueue {
@@ -35,14 +37,14 @@ public class SpscIntQueue {
}
public final @Nullable Integer recv() {
public final OptionalInt recv() {
final int idx = consumerIdx.getOpaque();
int cachedIdx = producerCachedIdx.getPlain();
if (idx == cachedIdx) {
cachedIdx = producerIdx.getAcquire();
producerCachedIdx.setPlain(cachedIdx);
if (idx == cachedIdx) {
return null;
return OptionalInt.empty();
}
}
int e = data[idx];
@@ -51,7 +53,7 @@ public class SpscIntQueue {
nextIdx = 0;
}
consumerIdx.setRelease(nextIdx);
return e;
return OptionalInt.of(e);
}
public final int size() {