mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-01 17:01:56 +01:00
902942d9e4
Also check class loader cache before locking to speed up cached hits to avoid the lock wasn't gonna make a unique build just for that but can lump it in here.
60 lines
No EOL
2.6 KiB
Diff
60 lines
No EOL
2.6 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Trigary <trigary0@gmail.com>
|
|
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 8ff228ce..ba2c5c6e 100644
|
|
--- a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
|
|
+++ b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
|
|
@@ -0,0 +0,0 @@ public final class JavaPluginLoader implements PluginLoader {
|
|
final Server server;
|
|
private final Pattern[] fileFilters = new Pattern[]{Pattern.compile("\\.jar$")};
|
|
private final Map<String, Class<?>> classes = new ConcurrentHashMap<String, Class<?>>();
|
|
+ private final Map<String, java.util.concurrent.locks.ReentrantReadWriteLock> classLoadLock = new java.util.HashMap<String, java.util.concurrent.locks.ReentrantReadWriteLock>(); // Paper
|
|
+ private final Map<String, Integer> classLoadLockCount = new java.util.HashMap<String, Integer>(); // Paper
|
|
private final List<PluginClassLoader> loaders = new CopyOnWriteArrayList<PluginClassLoader>();
|
|
|
|
/**
|
|
@@ -0,0 +0,0 @@ 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;
|
|
@@ -0,0 +0,0 @@ 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;
|
|
}
|
|
|
|
--
|