replace synchronisation with Striped access semaphore locks

Dieser Commit ist enthalten in:
dordsor21 2020-05-18 16:52:33 +01:00
Ursprung 31cc5e0b60
Commit fd3604beba
3 geänderte Dateien mit 51 neuen und 11 gelöschten Zeilen

Datei anzeigen

@ -9,6 +9,7 @@ import com.boydti.fawe.object.collection.BitArray;
import com.boydti.fawe.util.MathMan; import com.boydti.fawe.util.MathMan;
import com.boydti.fawe.util.ReflectionUtils; import com.boydti.fawe.util.ReflectionUtils;
import com.boydti.fawe.util.TaskManager; import com.boydti.fawe.util.TaskManager;
import com.google.common.util.concurrent.Striped;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockTypesCache; import com.sk89q.worldedit.world.block.BlockTypesCache;
@ -18,6 +19,7 @@ import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import net.jpountz.util.UnsafeUtils; import net.jpountz.util.UnsafeUtils;
import net.minecraft.server.v1_14_R1.Block; import net.minecraft.server.v1_14_R1.Block;
@ -32,6 +34,7 @@ import net.minecraft.server.v1_14_R1.GameProfileSerializer;
import net.minecraft.server.v1_14_R1.IBlockData; import net.minecraft.server.v1_14_R1.IBlockData;
import net.minecraft.server.v1_14_R1.PlayerChunk; import net.minecraft.server.v1_14_R1.PlayerChunk;
import net.minecraft.server.v1_14_R1.PlayerChunkMap; import net.minecraft.server.v1_14_R1.PlayerChunkMap;
import net.minecraft.server.v1_14_R1.World;
import org.bukkit.craftbukkit.v1_14_R1.CraftChunk; import org.bukkit.craftbukkit.v1_14_R1.CraftChunk;
import org.bukkit.craftbukkit.v1_14_R1.CraftWorld; import org.bukkit.craftbukkit.v1_14_R1.CraftWorld;
import sun.misc.Unsafe; import sun.misc.Unsafe;
@ -63,6 +66,8 @@ public final class BukkitAdapter_1_14 extends NMSAdapter {
private static final Field fieldLock; private static final Field fieldLock;
private static final Striped<Semaphore> stripe = Striped.lazyWeakSemaphore(5, 1);
static { static {
try { try {
fieldSize = DataPaletteBlock.class.getDeclaredField("i"); fieldSize = DataPaletteBlock.class.getDeclaredField("i");
@ -135,10 +140,15 @@ public final class BukkitAdapter_1_14 extends NMSAdapter {
} }
} }
public static Chunk ensureLoaded(net.minecraft.server.v1_14_R1.World nmsWorld, int X, int Z) { public static Chunk ensureLoaded(World nmsWorld, int X, int Z) {
Chunk nmsChunk; Semaphore lock = stripe.get(nmsWorld.hashCode());
synchronized (nmsWorld) { Chunk nmsChunk = null;
try {
lock.acquire();
nmsChunk = nmsWorld.getChunkIfLoaded(X, Z); nmsChunk = nmsWorld.getChunkIfLoaded(X, Z);
} catch (InterruptedException ignored) {
} finally {
lock.release();
} }
if (nmsChunk != null) { if (nmsChunk != null) {
return nmsChunk; return nmsChunk;
@ -147,7 +157,8 @@ public final class BukkitAdapter_1_14 extends NMSAdapter {
return nmsWorld.getChunkAt(X, Z); return nmsWorld.getChunkAt(X, Z);
} }
if (PaperLib.isPaper()) { if (PaperLib.isPaper()) {
synchronized (nmsWorld) { try {
lock.acquire();
CraftWorld craftWorld = nmsWorld.getWorld(); CraftWorld craftWorld = nmsWorld.getWorld();
CompletableFuture<org.bukkit.Chunk> future = craftWorld.getChunkAtAsync(X, Z, true); CompletableFuture<org.bukkit.Chunk> future = craftWorld.getChunkAtAsync(X, Z, true);
try { try {
@ -156,6 +167,9 @@ public final class BukkitAdapter_1_14 extends NMSAdapter {
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
} }
} catch (InterruptedException ignored) {
} finally {
lock.release();
} }
} }
// TODO optimize // TODO optimize

Datei anzeigen

@ -9,6 +9,7 @@ import com.boydti.fawe.object.collection.BitArray;
import com.boydti.fawe.util.MathMan; import com.boydti.fawe.util.MathMan;
import com.boydti.fawe.util.ReflectionUtils; import com.boydti.fawe.util.ReflectionUtils;
import com.boydti.fawe.util.TaskManager; import com.boydti.fawe.util.TaskManager;
import com.google.common.util.concurrent.Striped;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockTypesCache; import com.sk89q.worldedit.world.block.BlockTypesCache;
@ -18,6 +19,7 @@ import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import net.jpountz.util.UnsafeUtils; import net.jpountz.util.UnsafeUtils;
import net.minecraft.server.v1_15_R1.*; import net.minecraft.server.v1_15_R1.*;
@ -52,6 +54,8 @@ public final class BukkitAdapter_1_15 extends NMSAdapter {
private static final Field fieldLock; private static final Field fieldLock;
private static final Striped<Semaphore> stripe = Striped.lazyWeakSemaphore(5, 1);
static { static {
try { try {
fieldSize = DataPaletteBlock.class.getDeclaredField("i"); fieldSize = DataPaletteBlock.class.getDeclaredField("i");
@ -122,10 +126,15 @@ public final class BukkitAdapter_1_15 extends NMSAdapter {
} }
} }
public static Chunk ensureLoaded(net.minecraft.server.v1_15_R1.World nmsWorld, int X, int Z) { public static Chunk ensureLoaded(World nmsWorld, int X, int Z) {
Chunk nmsChunk; Semaphore lock = stripe.get(nmsWorld.hashCode());
synchronized (nmsWorld) { Chunk nmsChunk = null;
try {
lock.acquire();
nmsChunk = nmsWorld.getChunkIfLoaded(X, Z); nmsChunk = nmsWorld.getChunkIfLoaded(X, Z);
} catch (InterruptedException ignored) {
} finally {
lock.release();
} }
if (nmsChunk != null) { if (nmsChunk != null) {
return nmsChunk; return nmsChunk;
@ -134,7 +143,8 @@ public final class BukkitAdapter_1_15 extends NMSAdapter {
return nmsWorld.getChunkAt(X, Z); return nmsWorld.getChunkAt(X, Z);
} }
if (PaperLib.isPaper()) { if (PaperLib.isPaper()) {
synchronized (nmsWorld) { try {
lock.acquire();
CraftWorld craftWorld = nmsWorld.getWorld(); CraftWorld craftWorld = nmsWorld.getWorld();
CompletableFuture<org.bukkit.Chunk> future = craftWorld.getChunkAtAsync(X, Z, true); CompletableFuture<org.bukkit.Chunk> future = craftWorld.getChunkAtAsync(X, Z, true);
try { try {
@ -143,6 +153,9 @@ public final class BukkitAdapter_1_15 extends NMSAdapter {
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
} }
} catch (InterruptedException ignored) {
} finally {
lock.release();
} }
} }
// TODO optimize // TODO optimize

Datei anzeigen

@ -9,6 +9,7 @@ import com.boydti.fawe.object.collection.BitArray;
import com.boydti.fawe.util.MathMan; import com.boydti.fawe.util.MathMan;
import com.boydti.fawe.util.ReflectionUtils; import com.boydti.fawe.util.ReflectionUtils;
import com.boydti.fawe.util.TaskManager; import com.boydti.fawe.util.TaskManager;
import com.google.common.util.concurrent.Striped;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockTypesCache; import com.sk89q.worldedit.world.block.BlockTypesCache;
@ -18,6 +19,7 @@ import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import net.jpountz.util.UnsafeUtils; import net.jpountz.util.UnsafeUtils;
import net.minecraft.server.v1_15_R1.*; import net.minecraft.server.v1_15_R1.*;
@ -52,6 +54,8 @@ public final class BukkitAdapter_1_15_2 extends NMSAdapter {
private static final Field fieldLock; private static final Field fieldLock;
private static final Striped<Semaphore> stripe = Striped.lazyWeakSemaphore(5, 1);
static { static {
try { try {
fieldSize = DataPaletteBlock.class.getDeclaredField("i"); fieldSize = DataPaletteBlock.class.getDeclaredField("i");
@ -124,9 +128,14 @@ public final class BukkitAdapter_1_15_2 extends NMSAdapter {
} }
public static Chunk ensureLoaded(World nmsWorld, int X, int Z) { public static Chunk ensureLoaded(World nmsWorld, int X, int Z) {
Chunk nmsChunk; Semaphore lock = stripe.get(nmsWorld.hashCode());
synchronized (nmsWorld) { Chunk nmsChunk = null;
try {
lock.acquire();
nmsChunk = nmsWorld.getChunkIfLoaded(X, Z); nmsChunk = nmsWorld.getChunkIfLoaded(X, Z);
} catch (InterruptedException ignored) {
} finally {
lock.release();
} }
if (nmsChunk != null) { if (nmsChunk != null) {
return nmsChunk; return nmsChunk;
@ -135,7 +144,8 @@ public final class BukkitAdapter_1_15_2 extends NMSAdapter {
return nmsWorld.getChunkAt(X, Z); return nmsWorld.getChunkAt(X, Z);
} }
if (PaperLib.isPaper()) { if (PaperLib.isPaper()) {
synchronized (nmsWorld) { try {
lock.acquire();
CraftWorld craftWorld = nmsWorld.getWorld(); CraftWorld craftWorld = nmsWorld.getWorld();
CompletableFuture<org.bukkit.Chunk> future = craftWorld.getChunkAtAsync(X, Z, true); CompletableFuture<org.bukkit.Chunk> future = craftWorld.getChunkAtAsync(X, Z, true);
try { try {
@ -144,6 +154,9 @@ public final class BukkitAdapter_1_15_2 extends NMSAdapter {
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
} }
} catch (InterruptedException ignored) {
} finally {
lock.release();
} }
} }
// TODO optimize // TODO optimize