mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-12 10:21:39 +01:00
Cleanup
This commit is contained in:
parent
db2550f9ab
commit
0462c8f9a7
@ -9,7 +9,6 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -32,12 +31,12 @@ public class AcquirableEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Changes the collection returned by {@link #current()}.
|
* Changes the stream returned by {@link #current()}.
|
||||||
* <p>
|
* <p>
|
||||||
* Mostly for internal use, internal calls are unrecommended as they could lead
|
* Mostly for internal use, external calls are unrecommended as they could lead
|
||||||
* to unexpected behavior.
|
* to unexpected behavior.
|
||||||
*
|
*
|
||||||
* @param entities the new entity collection
|
* @param entities the new entity stream
|
||||||
*/
|
*/
|
||||||
@ApiStatus.Internal
|
@ApiStatus.Internal
|
||||||
public static void refresh(@NotNull Stream<@NotNull Entity> entities) {
|
public static void refresh(@NotNull Stream<@NotNull Entity> entities) {
|
||||||
@ -97,17 +96,6 @@ public class AcquirableEntity {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Signals the acquisition manager to acquire 'this' at the end of the thread tick.
|
|
||||||
* <p>
|
|
||||||
* Thread-safety is guaranteed but not the order.
|
|
||||||
*
|
|
||||||
* @param consumer the consumer of the acquired object
|
|
||||||
*/
|
|
||||||
public void scheduledAcquire(@NotNull EntityConsumer consumer) {
|
|
||||||
Acquisition.scheduledAcquireRequest(this, (Consumer<Entity>) consumer);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unwrap the contained object unsafely.
|
* Unwrap the contained object unsafely.
|
||||||
* <p>
|
* <p>
|
||||||
@ -122,9 +110,12 @@ public class AcquirableEntity {
|
|||||||
/**
|
/**
|
||||||
* Gets the {@link Handler} of this acquirable element,
|
* Gets the {@link Handler} of this acquirable element,
|
||||||
* containing the currently linked thread.
|
* containing the currently linked thread.
|
||||||
|
* <p>
|
||||||
|
* Mostly for internal use.
|
||||||
*
|
*
|
||||||
* @return this element handler
|
* @return this element handler
|
||||||
*/
|
*/
|
||||||
|
@ApiStatus.Internal
|
||||||
public @NotNull Handler getHandler() {
|
public @NotNull Handler getHandler() {
|
||||||
return handler;
|
return handler;
|
||||||
}
|
}
|
||||||
|
@ -12,8 +12,6 @@ import java.util.function.Consumer;
|
|||||||
|
|
||||||
public final class Acquisition {
|
public final class Acquisition {
|
||||||
|
|
||||||
private static final ThreadLocal<ScheduledAcquisition> SCHEDULED_ACQUISITION = ThreadLocal.withInitial(ScheduledAcquisition::new);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Global lock used for synchronization.
|
* Global lock used for synchronization.
|
||||||
*/
|
*/
|
||||||
@ -32,11 +30,11 @@ public final class Acquisition {
|
|||||||
public static void acquireForEach(@NotNull Collection<AcquirableEntity> collection,
|
public static void acquireForEach(@NotNull Collection<AcquirableEntity> collection,
|
||||||
@NotNull Consumer<Entity> consumer) {
|
@NotNull Consumer<Entity> consumer) {
|
||||||
final Thread currentThread = Thread.currentThread();
|
final Thread currentThread = Thread.currentThread();
|
||||||
Map<TickThread, List<Entity>> threadCacheMap = retrieveOptionalThreadMap(collection, currentThread, consumer);
|
var threadEntitiesMap = retrieveOptionalThreadMap(collection, currentThread, consumer);
|
||||||
|
|
||||||
// Acquire all the threads one by one
|
// Acquire all the threads one by one
|
||||||
{
|
{
|
||||||
for (Map.Entry<TickThread, List<Entity>> entry : threadCacheMap.entrySet()) {
|
for (var entry : threadEntitiesMap.entrySet()) {
|
||||||
final TickThread tickThread = entry.getKey();
|
final TickThread tickThread = entry.getKey();
|
||||||
final List<Entity> entities = entry.getValue();
|
final List<Entity> entities = entry.getValue();
|
||||||
|
|
||||||
@ -49,30 +47,6 @@ public final class Acquisition {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Processes all scheduled acquisitions.
|
|
||||||
*/
|
|
||||||
public static void processThreadTick() {
|
|
||||||
ScheduledAcquisition scheduledAcquisition = SCHEDULED_ACQUISITION.get();
|
|
||||||
|
|
||||||
final List<AcquirableEntity> acquirableEntityElements = scheduledAcquisition.acquirableEntityElements;
|
|
||||||
|
|
||||||
if (!acquirableEntityElements.isEmpty()) {
|
|
||||||
final Map<Object, List<Consumer<Entity>>> callbacks = scheduledAcquisition.callbacks;
|
|
||||||
|
|
||||||
acquireForEach(acquirableEntityElements, element -> {
|
|
||||||
List<Consumer<Entity>> consumers = callbacks.get(element);
|
|
||||||
if (consumers == null || consumers.isEmpty())
|
|
||||||
return;
|
|
||||||
consumers.forEach(objectConsumer -> objectConsumer.accept(element));
|
|
||||||
});
|
|
||||||
|
|
||||||
// Clear collections..
|
|
||||||
acquirableEntityElements.clear();
|
|
||||||
callbacks.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensures that {@code callback} is safely executed inside the batch thread.
|
* Ensures that {@code callback} is safely executed inside the batch thread.
|
||||||
*/
|
*/
|
||||||
@ -118,10 +92,6 @@ public final class Acquisition {
|
|||||||
return !acquired ? lock : null;
|
return !acquired ? lock : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static ReentrantLock acquireEnter(TickThread elementThread) {
|
|
||||||
return acquireEnter(Thread.currentThread(), elementThread);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static void acquireLeave(ReentrantLock lock) {
|
protected static void acquireLeave(ReentrantLock lock) {
|
||||||
if (lock != null) {
|
if (lock != null) {
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
@ -129,14 +99,6 @@ public final class Acquisition {
|
|||||||
GLOBAL_LOCK.unlock();
|
GLOBAL_LOCK.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected synchronized static void scheduledAcquireRequest(@NotNull AcquirableEntity acquirableEntity, Consumer<Entity> consumer) {
|
|
||||||
ScheduledAcquisition scheduledAcquisition = SCHEDULED_ACQUISITION.get();
|
|
||||||
scheduledAcquisition.acquirableEntityElements.add(acquirableEntity);
|
|
||||||
scheduledAcquisition.callbacks
|
|
||||||
.computeIfAbsent(acquirableEntity.unwrap(), objectAcquirable -> new ArrayList<>())
|
|
||||||
.add(consumer);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates
|
* Creates
|
||||||
*
|
*
|
||||||
@ -169,21 +131,6 @@ public final class Acquisition {
|
|||||||
return threadCacheMap;
|
return threadCacheMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static Map<TickThread, List<Entity>> retrieveThreadMap(@NotNull Collection<AcquirableEntity> collection) {
|
|
||||||
// Separate a collection of acquirable elements into a map of thread->elements
|
|
||||||
// Useful to reduce the number of acquisition
|
|
||||||
Map<TickThread, List<Entity>> threadCacheMap = new HashMap<>();
|
|
||||||
for (AcquirableEntity acquirableEntity : collection) {
|
|
||||||
final Entity entity = acquirableEntity.unwrap();
|
|
||||||
final TickThread elementThread = acquirableEntity.getHandler().getTickThread();
|
|
||||||
|
|
||||||
List<Entity> threadCacheList = threadCacheMap.computeIfAbsent(elementThread, tickThread -> new ArrayList<>());
|
|
||||||
threadCacheList.add(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
return threadCacheMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static long getCurrentWaitMonitoring() {
|
public static long getCurrentWaitMonitoring() {
|
||||||
return WAIT_COUNTER_NANO.get();
|
return WAIT_COUNTER_NANO.get();
|
||||||
}
|
}
|
||||||
@ -191,9 +138,4 @@ public final class Acquisition {
|
|||||||
public static void resetWaitMonitoring() {
|
public static void resetWaitMonitoring() {
|
||||||
WAIT_COUNTER_NANO.set(0);
|
WAIT_COUNTER_NANO.set(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ScheduledAcquisition {
|
|
||||||
private final List<AcquirableEntity> acquirableEntityElements = new ArrayList<>();
|
|
||||||
private final Map<Object, List<Consumer<Entity>>> callbacks = new HashMap<>();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user