Add //cancel

This commit is contained in:
Jesse Boyd 2019-11-02 12:51:24 +01:00
parent df9e9e510a
commit 6ed7923a1e
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F
7 changed files with 129 additions and 57 deletions

View File

@ -105,6 +105,9 @@ public enum FaweCache implements Trimable {
@Override @Override
public synchronized boolean trim(boolean aggressive) { public synchronized boolean trim(boolean aggressive) {
if (aggressive) {
CleanableThreadLocal.cleanAll();
} else {
CHUNK_FLAG.clean(); CHUNK_FLAG.clean();
BYTE_BUFFER_8192.clean(); BYTE_BUFFER_8192.clean();
BLOCK_TO_PALETTE.clean(); BLOCK_TO_PALETTE.clean();
@ -121,6 +124,7 @@ public enum FaweCache implements Trimable {
for (Map.Entry<Class, CleanableThreadLocal> entry : REGISTERED_SINGLETONS.entrySet()) { for (Map.Entry<Class, CleanableThreadLocal> entry : REGISTERED_SINGLETONS.entrySet()) {
entry.getValue().clean(); entry.getValue().clean();
} }
}
for (Map.Entry<Class, Pool> entry : REGISTERED_POOLS.entrySet()) { for (Map.Entry<Class, Pool> entry : REGISTERED_POOLS.entrySet()) {
Pool pool = entry.getValue(); Pool pool = entry.getValue();
pool.clear(); pool.clear();

View File

@ -5,7 +5,10 @@ import com.boydti.fawe.util.MainUtil;
import java.lang.ref.Reference; import java.lang.ref.Reference;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.LongAdder; import java.util.concurrent.atomic.LongAdder;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
@ -51,16 +54,61 @@ public class CleanableThreadLocal<T> extends ThreadLocal<T> {
} }
} }
public static void clean(ThreadLocal instance) { public List<T> getAll() {
List<T> list = new ArrayList<>();
iterate(this, new Consumer<Object>() {
Method methodGetEntry;
Field fieldValue;
@Override
public void accept(Object tlm) {
try {
if (methodGetEntry == null) {
methodGetEntry = tlm.getClass().getDeclaredMethod("getEntry", ThreadLocal.class);
methodGetEntry.setAccessible(true);
}
Object entry = methodGetEntry.invoke(tlm, CleanableThreadLocal.this);
if (entry != null) {
if (fieldValue == null) {
fieldValue = entry.getClass().getDeclaredField("value");
fieldValue.setAccessible(true);
}
Object value = fieldValue.get(entry);
if (value != null) {
list.add((T) value);
}
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | NoSuchFieldException e) {
throw new RuntimeException(e);
}
}
});
return list;
}
public static void iterate(ThreadLocal instance, Consumer<Object> withMap) {
try { try {
Thread[] threads = MainUtil.getThreads(); Thread[] threads = MainUtil.getThreads();
Field tl = Thread.class.getDeclaredField("threadLocals"); Field tl = Thread.class.getDeclaredField("threadLocals");
tl.setAccessible(true); tl.setAccessible(true);
Method methodRemove = null;
for (Thread thread : threads) { for (Thread thread : threads) {
if (thread != null) { if (thread != null) {
Object tlm = tl.get(thread); Object tlm = tl.get(thread);
if (tlm != null) { if (tlm != null) {
withMap.accept(tlm);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void clean(ThreadLocal instance) {
iterate(instance, new Consumer<Object>() {
Method methodRemove;
@Override
public void accept(Object tlm) {
try {
if (methodRemove == null) { if (methodRemove == null) {
methodRemove = tlm.getClass().getDeclaredMethod("remove", ThreadLocal.class); methodRemove = tlm.getClass().getDeclaredMethod("remove", ThreadLocal.class);
methodRemove.setAccessible(true); methodRemove.setAccessible(true);
@ -68,14 +116,14 @@ public class CleanableThreadLocal<T> extends ThreadLocal<T> {
if (methodRemove != null) { if (methodRemove != null) {
try { try {
methodRemove.invoke(tlm, instance); methodRemove.invoke(tlm, instance);
} catch (Throwable ignore) {} } catch (Throwable ignore) {
} }
} }
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} }
} }
} catch (Exception e) { });
e.printStackTrace();
}
} }
public static void cleanAll() { public static void cleanAll() {

View File

@ -1,6 +1,7 @@
package com.boydti.fawe.object.extent; package com.boydti.fawe.object.extent;
import com.boydti.fawe.FaweCache; import com.boydti.fawe.FaweCache;
import com.boydti.fawe.beta.IBatchProcessor;
import com.boydti.fawe.beta.IChunk; import com.boydti.fawe.beta.IChunk;
import com.boydti.fawe.beta.IChunkGet; import com.boydti.fawe.beta.IChunkGet;
import com.boydti.fawe.beta.IChunkSet; import com.boydti.fawe.beta.IChunkSet;
@ -34,7 +35,7 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class NullExtent extends FaweRegionExtent { public class NullExtent extends FaweRegionExtent implements IBatchProcessor {
private final FaweException reason; private final FaweException reason;
@ -328,11 +329,16 @@ public class NullExtent extends FaweRegionExtent {
@Override @Override
public IChunkSet processSet(IChunk chunk, IChunkGet get, IChunkSet set) { public IChunkSet processSet(IChunk chunk, IChunkGet get, IChunkSet set) {
return null; throw reason;
} }
@Override @Override
public boolean processGet(int chunkX, int chunkZ) { public boolean processGet(int chunkX, int chunkZ) {
return false; throw reason;
}
@Override
public Extent construct(Extent child) {
throw reason;
} }
} }

View File

@ -332,6 +332,7 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
if (get instanceof AbstractDelegateExtent && !(get instanceof NullExtent)) { if (get instanceof AbstractDelegateExtent && !(get instanceof NullExtent)) {
traverser.setNext(nullExtent); traverser.setNext(nullExtent);
} }
get.addProcessor(nullExtent);
traverser = next; traverser = next;
} }
return super.cancel(); return super.cancel();

View File

@ -25,6 +25,7 @@ import com.boydti.fawe.object.brush.visualization.VirtualWorld;
import com.boydti.fawe.object.clipboard.DiskOptimizedClipboard; import com.boydti.fawe.object.clipboard.DiskOptimizedClipboard;
import com.boydti.fawe.regions.FaweMaskManager; import com.boydti.fawe.regions.FaweMaskManager;
import com.boydti.fawe.util.MainUtil; import com.boydti.fawe.util.MainUtil;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.EmptyClipboardException; import com.sk89q.worldedit.EmptyClipboardException;
import com.sk89q.worldedit.IncompleteRegionException; import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.LocalSession;
@ -40,6 +41,7 @@ import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.RegionSelector; import com.sk89q.worldedit.regions.RegionSelector;
import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.session.ClipboardHolder;
import com.sk89q.worldedit.session.request.Request;
import com.sk89q.worldedit.util.Direction; import com.sk89q.worldedit.util.Direction;
import com.sk89q.worldedit.util.HandSide; import com.sk89q.worldedit.util.HandSide;
import com.sk89q.worldedit.util.Location; import com.sk89q.worldedit.util.Location;
@ -50,6 +52,7 @@ import com.sk89q.worldedit.world.gamemode.GameMode;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.io.File; import java.io.File;
import java.io.IOException;
/** /**
* Represents a player * Represents a player
@ -381,32 +384,36 @@ public interface Player extends Entity, Actor {
} }
default int cancel(boolean close) { default int cancel(boolean close) {
// Collection<IQueueExtent> queues = SetQueue.IMP.getAllQueues(); TODO NOT IMPLEMENTED int cancelled = 0;
// int cancelled = 0;
// clearActions(); for (Request request : Request.getAll()) {
// for (IQueueExtent queue : queues) { EditSession editSession = request.getEditSession();
// Collection<EditSession> sessions = queue.getEditSessions(); if (editSession != null) {
// for (EditSession session : sessions) { Player player = editSession.getPlayer();
// FawePlayer currentPlayer = session.getPlayer(); if (equals(player)) {
// if (currentPlayer == this) { editSession.cancel();
// if (session.cancel()) { cancelled++;
// cancelled++; }
// } }
// } }
// } VirtualWorld world = getSession().getVirtualWorld();
// } if (world != null) {
// VirtualWorld world = getSession().getVirtualWorld(); if (close) {
// if (world != null) { try {
// if (close) { world.close(false);
// try { } catch (IOException e) {
// world.close(false); e.printStackTrace();
// } catch (IOException e) { }
// e.printStackTrace(); }
// } else {
// } try {
// else world.clear(); world.close(false);
// } } catch (IOException e) {
return 0; e.printStackTrace();
}
}
}
return cancelled;
} }
void sendTitle(String title, String sub); void sendTitle(String title, String sub);

View File

@ -630,6 +630,7 @@ public final class PlatformCommandManager {
Command cmd = optional.get(); Command cmd = optional.get();
CommandQueuedCondition queued = cmd.getCondition().as(CommandQueuedCondition.class).orElse(null); CommandQueuedCondition queued = cmd.getCondition().as(CommandQueuedCondition.class).orElse(null);
if (queued != null && !queued.isQueued()) { if (queued != null && !queued.isQueued()) {
System.out.println("Not queued");
handleCommandOnCurrentThread(event); handleCommandOnCurrentThread(event);
return; return;
} }
@ -728,8 +729,7 @@ public final class PlatformCommandManager {
} else { } else {
System.out.println("Invalid context " + context); System.out.println("Invalid context " + context);
} }
Optional<EditSession> editSessionOpt = Optional<EditSession> editSessionOpt = context.injectedValue(Key.of(EditSession.class));
context.injectedValue(Key.of(EditSession.class));
if (editSessionOpt.isPresent()) { if (editSessionOpt.isPresent()) {
EditSession editSession = editSessionOpt.get(); EditSession editSession = editSessionOpt.get();

View File

@ -19,6 +19,7 @@
package com.sk89q.worldedit.session.request; package com.sk89q.worldedit.session.request;
import com.boydti.fawe.object.collection.CleanableThreadLocal;
import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extension.platform.Actor;
@ -26,13 +27,14 @@ import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.World;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.List;
/** /**
* Describes the current request using a {@link ThreadLocal}. * Describes the current request using a {@link ThreadLocal}.
*/ */
public final class Request { public final class Request {
private static final ThreadLocal<Request> threadLocal = ThreadLocal.withInitial(Request::new); private static final CleanableThreadLocal<Request> threadLocal = new CleanableThreadLocal<>(Request::new);
private @Nullable World world; private @Nullable World world;
private @Nullable Actor actor; private @Nullable Actor actor;
@ -44,6 +46,10 @@ public final class Request {
private Request() { private Request() {
} }
public static List<Request> getAll() {
return threadLocal.getAll();
}
/** /**
* Get the request world. * Get the request world.
* *