Spot all Pools access different from the main thread

This commit is contained in:
fgnm
2024-04-30 17:53:21 +02:00
parent ad9b06d036
commit 21b455d4d9
8 changed files with 11 additions and 86 deletions
Binary file not shown.
@@ -1,74 +0,0 @@
package com.badlogic.gdx.utils;
public class Pools {
static private final ThreadLocal<ObjectMap<Class, Pool>> typePools = new ThreadLocal<ObjectMap<Class, Pool>>() {
protected ObjectMap<Class, Pool> initialValue () {
return new ObjectMap<Class, Pool>();
};
};
/** Returns a new or existing pool for the specified type, stored in a Class to {@link Pool} map. Note the max size is ignored
* if this is not the first time this pool has been requested. */
static public <T> Pool<T> get (Class<T> type, int max) {
Pool pool = typePools.get().get(type);
if (pool == null) {
pool = new ReflectionPool(type, 4, max);
typePools.get().put(type, pool);
}
return pool;
}
/** Returns a new or existing pool for the specified type, stored in a Class to {@link Pool} map. The max size of the pool used
* is 100. */
static public <T> Pool<T> get (Class<T> type) {
return get(type, 100);
}
/** Sets an existing pool for the specified type, stored in a Class to {@link Pool} map. */
static public <T> void set (Class<T> type, Pool<T> pool) {
typePools.get().put(type, pool);
}
/** Obtains an object from the {@link #get(Class) pool}. */
static public <T> T obtain (Class<T> type) {
return get(type).obtain();
}
/** Frees an object from the {@link #get(Class) pool}. */
static public void free (Object object) {
if (object == null) throw new IllegalArgumentException("object cannot be null.");
Pool pool = typePools.get().get(object.getClass());
if (pool == null) return; // Ignore freeing an object that was never retained.
pool.free(object);
}
/** Frees the specified objects from the {@link #get(Class) pool}. Null objects within the array are silently ignored. Objects
* don't need to be from the same pool. */
static public void freeAll (Array objects) {
freeAll(objects, false);
}
/** Frees the specified objects from the {@link #get(Class) pool}. Null objects within the array are silently ignored.
* @param samePool If true, objects don't need to be from the same pool but the pool must be looked up for each object. */
static public void freeAll (Array objects, boolean samePool) {
if (objects == null) throw new IllegalArgumentException("objects cannot be null.");
Pool pool = null;
for (int i = 0, n = objects.size; i < n; i++) {
Object object = objects.get(i);
if (object == null) continue;
if (pool == null) {
pool = typePools.get().get(object.getClass());
if (pool == null) continue; // Ignore freeing an object that was never retained.
}
pool.free(object);
if (!samePool) pool = null;
}
}
public static String name() {
return "ThreadSafe Pools";
}
private Pools () {
}
}
@@ -2,7 +2,6 @@ package games.rednblack.editor.controller;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.utils.Pools;
import games.rednblack.editor.utils.AppConfig;
import games.rednblack.h2d.common.HyperLog;
import games.rednblack.puremvc.commands.SimpleCommand;
@@ -22,7 +21,5 @@ public class BootstrapInfoCommand extends SimpleCommand {
HyperLog.info("Shaders version " + gl20.glGetString(GL20.GL_SHADING_LANGUAGE_VERSION));
HyperLog.info("JVM Version: " + System.getProperty("java.version") + " (" + System.getProperty("java.vendor") + ")");
HyperLog.info(Pools.name());
}
}
@@ -250,17 +250,17 @@ public class ProjectManager extends Proxy {
FileAlterationListener listener = new FileAlterationListenerAdaptor() {
@Override
public void onFileCreate(File file) {
facade.sendNotification(MsgAPI.PROJECT_FILE_CREATED, file);
Gdx.app.postRunnable(() -> facade.sendNotification(MsgAPI.PROJECT_FILE_CREATED, file));
}
@Override
public void onFileDelete(File file) {
facade.sendNotification(MsgAPI.PROJECT_FILE_DELETED, file);
Gdx.app.postRunnable(() -> facade.sendNotification(MsgAPI.PROJECT_FILE_DELETED, file));
}
@Override
public void onFileChange(File file) {
facade.sendNotification(MsgAPI.PROJECT_FILE_MODIFIED, file);
Gdx.app.postRunnable(() -> facade.sendNotification(MsgAPI.PROJECT_FILE_MODIFIED, file));
}
};
observer.addListener(listener);
@@ -327,10 +327,10 @@ public class ResolutionManager extends Proxy {
public void rePackProjectImagesForAllResolutions(boolean reloadProjectData, RepackCallback callback) {
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> {
facade.sendNotification(MsgAPI.SHOW_LOADING_DIALOG);
Gdx.app.postRunnable(() -> facade.sendNotification(MsgAPI.SHOW_LOADING_DIALOG));
try {
rePackProjectImagesForAllResolutionsSync();
facade.sendNotification(MsgAPI.HIDE_LOADING_DIALOG);
Gdx.app.postRunnable(() -> facade.sendNotification(MsgAPI.HIDE_LOADING_DIALOG));
if (callback != null)
callback.onRepack(true);
} catch (Exception e) {
@@ -76,8 +76,10 @@ public class SplashMediator extends Mediator<Object> {
e.printStackTrace();
}
splash.loadedData();
Gdx.app.postRunnable(() -> HyperLap2DApp.getInstance().mainWindow.setVisible(true));
Gdx.app.postRunnable(() -> {
splash.loadedData();
HyperLap2DApp.getInstance().mainWindow.setVisible(true);
});
});
executor.shutdown();
}
@@ -130,7 +130,7 @@ public class ImportPanelMediator extends Mediator<ImportPanel> {
@Override
public void progressChanged(float value) {
viewComponent.getProgressBar().setValue(value);
Gdx.app.postRunnable(() -> viewComponent.getProgressBar().setValue(value));
}
@Override