Skip to content

Commit

Permalink
Alter detection of UUID thread
Browse files Browse the repository at this point in the history
  • Loading branch information
dordsor21 committed Sep 29, 2024
1 parent a780efc commit e6481ee
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import com.fastasyncworldedit.bukkit.BukkitPermissionAttachmentManager;
import com.fastasyncworldedit.bukkit.FaweBukkit;
import com.fastasyncworldedit.core.util.TaskManager;
import com.fastasyncworldedit.core.util.UpdateNotification;
import com.fastasyncworldedit.core.Fawe;
import com.fastasyncworldedit.core.util.WEManager;
Expand Down Expand Up @@ -92,7 +91,6 @@
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.function.Supplier;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.sk89q.worldedit.internal.anvil.ChunkDeleter.DELCHUNKS_FILE_NAME;
Expand Down Expand Up @@ -566,18 +564,17 @@ public BukkitPermissionAttachmentManager getPermissionAttachmentManager() {
public BukkitPlayer wrapPlayer(Player player) {
//FAWE start - Use cache over returning a direct BukkitPlayer
BukkitPlayer wePlayer = getCachedPlayer(player);
if (wePlayer == null) {
Supplier<BukkitPlayer> task = () -> {
BukkitPlayer bukkitPlayer = getCachedPlayer(player);
if (bukkitPlayer == null) {
bukkitPlayer = new BukkitPlayer(this, player);
player.setMetadata("WE", new FixedMetadataValue(this, bukkitPlayer));
}
return bukkitPlayer;
};
TaskManager.taskManager().sync(task);
if (wePlayer != null) {
return wePlayer;
}
synchronized (player) {
BukkitPlayer bukkitPlayer = getCachedPlayer(player);
if (bukkitPlayer == null) {
bukkitPlayer = new BukkitPlayer(this, player);
player.setMetadata("WE", new FixedMetadataValue(this, bukkitPlayer));
}
return bukkitPlayer;
}
return wePlayer;
//FAWE end
}

Expand All @@ -591,12 +588,11 @@ BukkitPlayer getCachedPlayer(Player player) {
}

BukkitPlayer reCachePlayer(Player player) {
Supplier<BukkitPlayer> task = () -> {
synchronized (player) {
BukkitPlayer wePlayer = new BukkitPlayer(this, player);
player.setMetadata("WE", new FixedMetadataValue(this, wePlayer));
return wePlayer;
};
return TaskManager.taskManager().sync(task);
}
}
//FAWE end

Expand Down
28 changes: 5 additions & 23 deletions worldedit-core/src/main/java/com/fastasyncworldedit/core/Fawe.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
import com.fastasyncworldedit.core.util.TextureUtil;
import com.fastasyncworldedit.core.util.WEManager;
import com.fastasyncworldedit.core.util.task.KeyQueuedExecutorService;
import com.fastasyncworldedit.core.util.task.UUIDKeyQueuedThreadFactory;
import com.github.luben.zstd.Zstd;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.internal.util.LogManagerCompat;
import net.jpountz.lz4.LZ4Factory;
import org.apache.logging.log4j.Logger;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.management.InstanceAlreadyExistsException;
import javax.management.NotificationEmitter;
Expand All @@ -40,10 +39,8 @@
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -87,7 +84,6 @@
public class Fawe {

private static final Logger LOGGER = LogManagerCompat.getLogger();
private static final ThreadLocal<Boolean> IS_UUID_KEYED_EXECUTOR_THREAD = ThreadLocal.withInitial(() -> false);

private static Fawe instance;

Expand Down Expand Up @@ -153,21 +149,7 @@ private Fawe(final IFawe implementation) {
0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(),
new ThreadFactoryBuilder().setNameFormat("FAWE UUID-key-queued - %d").setThreadFactory(new ThreadFactory() {
private final ThreadFactory defaultFactory = Executors.defaultThreadFactory();

@Override
public Thread newThread(@Nonnull Runnable r) {
return defaultFactory.newThread(() -> {
IS_UUID_KEYED_EXECUTOR_THREAD.set(true);
try {
r.run();
} finally {
IS_UUID_KEYED_EXECUTOR_THREAD.set(false); // Clear the mark after execution
}
});
}
}).build()
new UUIDKeyQueuedThreadFactory()
));
}

Expand Down Expand Up @@ -499,7 +481,7 @@ public KeyQueuedExecutorService<UUID> getClipboardExecutor() {
* @since TODO
*/
public Future<?> submitUUIDKeyQueuedTask(UUID uuid, Runnable runnable) {
if (IS_UUID_KEYED_EXECUTOR_THREAD.get()) {
if (Thread.currentThread() instanceof UUIDKeyQueuedThreadFactory.UUIDKeyQueuedThread) {
runnable.run();
return CompletableFuture.completedFuture(null);
}
Expand All @@ -513,7 +495,7 @@ public Future<?> submitUUIDKeyQueuedTask(UUID uuid, Runnable runnable) {
* @since TODO
*/
public <T> Future<T> submitUUIDKeyQueuedTask(UUID uuid, Runnable runnable, T result) {
if (IS_UUID_KEYED_EXECUTOR_THREAD.get()) {
if (Thread.currentThread() instanceof UUIDKeyQueuedThreadFactory.UUIDKeyQueuedThread) {
runnable.run();
return CompletableFuture.completedFuture(result);
}
Expand All @@ -527,7 +509,7 @@ public <T> Future<T> submitUUIDKeyQueuedTask(UUID uuid, Runnable runnable, T res
* @since TODO
*/
public <T> Future<T> submitUUIDKeyQueuedTask(UUID uuid, Callable<T> callable) {
if (IS_UUID_KEYED_EXECUTOR_THREAD.get()) {
if (Thread.currentThread() instanceof UUIDKeyQueuedThreadFactory.UUIDKeyQueuedThread) {
try {
CompletableFuture.completedFuture(callable.call());
} catch (Throwable t) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.fastasyncworldedit.core.util.task;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class UUIDKeyQueuedThreadFactory implements ThreadFactory {

private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;

public UUIDKeyQueuedThreadFactory() {
group = new ThreadGroup("UUIDKeyQueuedThreadGroup");
namePrefix = "FAWE UUID-key-queued - ";
}

public Thread newThread(Runnable r) {
Thread t = new UUIDKeyQueuedThread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
if (t.isDaemon()) {
t.setDaemon(false);
}
if (t.getPriority() != Thread.NORM_PRIORITY) {
t.setPriority(Thread.NORM_PRIORITY);
}
return t;
}

public static final class UUIDKeyQueuedThread extends Thread {

public UUIDKeyQueuedThread(@Nullable ThreadGroup group, Runnable task, @Nonnull String name, long stackSize) {
super(group, task, name, stackSize);
}

}

}

0 comments on commit e6481ee

Please sign in to comment.