This commit is contained in:
TheMode 2021-04-24 15:15:23 +02:00
parent db2550f9ab
commit 0462c8f9a7
2 changed files with 8 additions and 75 deletions

View File

@ -9,7 +9,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
import java.util.function.Consumer;
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>
* 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.
*
* @param entities the new entity collection
* @param entities the new entity stream
*/
@ApiStatus.Internal
public static void refresh(@NotNull Stream<@NotNull Entity> entities) {
@ -97,17 +96,6 @@ public class AcquirableEntity {
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.
* <p>
@ -122,9 +110,12 @@ public class AcquirableEntity {
/**
* Gets the {@link Handler} of this acquirable element,
* containing the currently linked thread.
* <p>
* Mostly for internal use.
*
* @return this element handler
*/
@ApiStatus.Internal
public @NotNull Handler getHandler() {
return handler;
}

View File

@ -12,8 +12,6 @@ import java.util.function.Consumer;
public final class Acquisition {
private static final ThreadLocal<ScheduledAcquisition> SCHEDULED_ACQUISITION = ThreadLocal.withInitial(ScheduledAcquisition::new);
/**
* Global lock used for synchronization.
*/
@ -32,11 +30,11 @@ public final class Acquisition {
public static void acquireForEach(@NotNull Collection<AcquirableEntity> collection,
@NotNull Consumer<Entity> consumer) {
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
{
for (Map.Entry<TickThread, List<Entity>> entry : threadCacheMap.entrySet()) {
for (var entry : threadEntitiesMap.entrySet()) {
final TickThread tickThread = entry.getKey();
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.
*/
@ -118,10 +92,6 @@ public final class Acquisition {
return !acquired ? lock : null;
}
protected static ReentrantLock acquireEnter(TickThread elementThread) {
return acquireEnter(Thread.currentThread(), elementThread);
}
protected static void acquireLeave(ReentrantLock lock) {
if (lock != null) {
lock.unlock();
@ -129,14 +99,6 @@ public final class Acquisition {
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
*
@ -169,21 +131,6 @@ public final class Acquisition {
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() {
return WAIT_COUNTER_NANO.get();
}
@ -191,9 +138,4 @@ public final class Acquisition {
public static void resetWaitMonitoring() {
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<>();
}
}