From 49584133dc28884b641698f3b8996b0b1d60642c Mon Sep 17 00:00:00 2001 From: Trigary Date: Wed, 15 Apr 2020 01:24:55 -0400 Subject: [PATCH] Make JavaPluginLoader thread-safe diff --git a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java index 8ff228ced..ba2c5c6ee 100644 --- a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java +++ b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java @@ -52,6 +52,8 @@ public final class JavaPluginLoader implements PluginLoader { final Server server; private final Pattern[] fileFilters = new Pattern[]{Pattern.compile("\\.jar$")}; private final Map> classes = new ConcurrentHashMap>(); + private final Map classLoadLock = new java.util.HashMap(); // Paper + private final Map classLoadLockCount = new java.util.HashMap(); // Paper private final List loaders = new CopyOnWriteArrayList(); /** @@ -191,7 +193,19 @@ public final class JavaPluginLoader implements PluginLoader { @Nullable Class getClassByName(final String name) { + // Paper start - make MT safe Class cachedClass = classes.get(name); + if (cachedClass != null) { + return cachedClass; + } + java.util.concurrent.locks.ReentrantReadWriteLock lock; + synchronized (classLoadLock) { + lock = classLoadLock.computeIfAbsent(name, (x) -> new java.util.concurrent.locks.ReentrantReadWriteLock()); + classLoadLockCount.compute(name, (x, prev) -> prev != null ? prev + 1 : 1); + } + lock.writeLock().lock();try { + cachedClass = classes.get(name); + // Paper end if (cachedClass != null) { return cachedClass; @@ -205,6 +219,19 @@ public final class JavaPluginLoader implements PluginLoader { } } } + // Paper start - make MT safe + } finally { + synchronized (classLoadLock) { + lock.writeLock().unlock(); + if (classLoadLockCount.get(name) == 1) { + classLoadLock.remove(name); + classLoadLockCount.remove(name); + } else { + classLoadLockCount.compute(name, (x, prev) -> prev - 1); + } + } + } + // Paper end return null; } -- 2.26.2