Spaces:
Build error
Build error
File size: 4,181 Bytes
d46f4a3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
package net.minecraft.util.thread;
import com.google.common.collect.ImmutableList;
import com.mojang.logging.LogUtils;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicReference;
import net.minecraft.Util;
import net.minecraft.util.profiling.metrics.MetricCategory;
import net.minecraft.util.profiling.metrics.MetricSampler;
import net.minecraft.util.profiling.metrics.MetricsRegistry;
import net.minecraft.util.profiling.metrics.ProfilerMeasured;
import org.slf4j.Logger;
public abstract class AbstractConsecutiveExecutor<T extends Runnable> implements ProfilerMeasured, TaskScheduler<T>, Runnable {
private static final Logger LOGGER = LogUtils.getLogger();
private final AtomicReference<AbstractConsecutiveExecutor.Status> status = new AtomicReference<>(AbstractConsecutiveExecutor.Status.SLEEPING);
private final StrictQueue<T> queue;
private final Executor executor;
private final String name;
public AbstractConsecutiveExecutor(StrictQueue<T> p_363900_, Executor p_367154_, String p_369151_) {
this.executor = p_367154_;
this.queue = p_363900_;
this.name = p_369151_;
MetricsRegistry.INSTANCE.add(this);
}
private boolean canBeScheduled() {
return !this.isClosed() && !this.queue.isEmpty();
}
@Override
public void close() {
this.status.set(AbstractConsecutiveExecutor.Status.CLOSED);
}
private boolean pollTask() {
if (!this.isRunning()) {
return false;
} else {
Runnable runnable = this.queue.pop();
if (runnable == null) {
return false;
} else {
Util.runNamed(runnable, this.name);
return true;
}
}
}
@Override
public void run() {
try {
this.pollTask();
} finally {
this.setSleeping();
this.registerForExecution();
}
}
public void runAll() {
try {
while (this.pollTask()) {
}
} finally {
this.setSleeping();
this.registerForExecution();
}
}
@Override
public void schedule(T p_364361_) {
this.queue.push(p_364361_);
this.registerForExecution();
}
private void registerForExecution() {
if (this.canBeScheduled() && this.setRunning()) {
try {
this.executor.execute(this);
} catch (RejectedExecutionException rejectedexecutionexception1) {
try {
this.executor.execute(this);
} catch (RejectedExecutionException rejectedexecutionexception) {
LOGGER.error("Could not schedule ConsecutiveExecutor", (Throwable)rejectedexecutionexception);
}
}
}
}
public int size() {
return this.queue.size();
}
public boolean hasWork() {
return this.isRunning() && !this.queue.isEmpty();
}
@Override
public String toString() {
return this.name + " " + this.status.get() + " " + this.queue.isEmpty();
}
@Override
public String name() {
return this.name;
}
@Override
public List<MetricSampler> profiledMetrics() {
return ImmutableList.of(MetricSampler.create(this.name + "-queue-size", MetricCategory.CONSECUTIVE_EXECUTORS, this::size));
}
private boolean setRunning() {
return this.status.compareAndSet(AbstractConsecutiveExecutor.Status.SLEEPING, AbstractConsecutiveExecutor.Status.RUNNING);
}
private void setSleeping() {
this.status.compareAndSet(AbstractConsecutiveExecutor.Status.RUNNING, AbstractConsecutiveExecutor.Status.SLEEPING);
}
private boolean isRunning() {
return this.status.get() == AbstractConsecutiveExecutor.Status.RUNNING;
}
private boolean isClosed() {
return this.status.get() == AbstractConsecutiveExecutor.Status.CLOSED;
}
static enum Status {
SLEEPING,
RUNNING,
CLOSED;
}
} |