From 45901f64b5816213e774a7e89e70af5372527765 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Tue, 27 Sep 2022 19:05:02 +0200 Subject: [PATCH] Android: Use synchronized methods for GameFileCache Compared to the previous solution of using big `synchronized` blocks, this makes GameFileCacheManager's executor thread release and re-lock the lock when possible, giving the GUI thread a chance to do a (comparatively) quick getOrAdd call if it needs to. --- .../dolphinemu/model/GameFileCache.java | 14 +++--- .../services/GameFileCacheManager.java | 45 ++++++++----------- 2 files changed, 25 insertions(+), 34 deletions(-) diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameFileCache.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameFileCache.java index 8ea8d82757..7b29370c53 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameFileCache.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/model/GameFileCache.java @@ -109,11 +109,11 @@ public class GameFileCache public static native String[] getAllGamePaths(String[] folderPaths, boolean recursiveScan); - public native int getSize(); + public synchronized native int getSize(); - public native GameFile[] getAllGames(); + public synchronized native GameFile[] getAllGames(); - public native GameFile addOrGet(String gamePath); + public synchronized native GameFile addOrGet(String gamePath); /** * Sets the list of games to cache. @@ -123,7 +123,7 @@ public class GameFileCache * * @return true if the cache was modified */ - public native boolean update(String[] gamePaths); + public synchronized native boolean update(String[] gamePaths); /** * For each game that already is in the cache, scans the folder that contains the game @@ -131,9 +131,9 @@ public class GameFileCache * * @return true if the cache was modified */ - public native boolean updateAdditionalMetadata(); + public synchronized native boolean updateAdditionalMetadata(); - public native boolean load(); + public synchronized native boolean load(); - public native boolean save(); + public synchronized native boolean save(); } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/services/GameFileCacheManager.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/services/GameFileCacheManager.java index 4e883413b4..2b89640051 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/services/GameFileCacheManager.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/services/GameFileCacheManager.java @@ -158,7 +158,7 @@ public final class GameFileCacheManager { // Common case: The game is in the cache, so just grab it from there. // (Actually, addOrGet already checks for this case, but we want to avoid calling it if possible - // because onHandleIntent may hold a lock on sGameFileCache for extended periods of time.) + // because the executor thread may hold a lock on sGameFileCache for extended periods of time.) GameFile[] allGames = sGameFiles.getValue(); for (GameFile game : allGames) { @@ -171,10 +171,7 @@ public final class GameFileCacheManager // Unusual case: The game wasn't found in the cache. // Scan the game and add it to the cache so that we can return it. createGameFileCacheIfNeeded(); - synchronized (sGameFileCache) - { - return sGameFileCache.addOrGet(gamePath); - } + return sGameFileCache.addOrGet(gamePath); } /** @@ -186,14 +183,11 @@ public final class GameFileCacheManager { if (!sFirstLoadDone) { - synchronized (sGameFileCache) + sFirstLoadDone = true; + sGameFileCache.load(); + if (sGameFileCache.getSize() != 0) { - sFirstLoadDone = true; - sGameFileCache.load(); - if (sGameFileCache.getSize() != 0) - { - updateGameFileArray(); - } + updateGameFileArray(); } } @@ -227,24 +221,21 @@ public final class GameFileCacheManager { String[] gamePaths = GameFileCache.getAllGamePaths(); - synchronized (sGameFileCache) + boolean changed = sGameFileCache.update(gamePaths); + if (changed) { - boolean changed = sGameFileCache.update(gamePaths); - if (changed) - { - updateGameFileArray(); - } + updateGameFileArray(); + } - boolean additionalMetadataChanged = sGameFileCache.updateAdditionalMetadata(); - if (additionalMetadataChanged) - { - updateGameFileArray(); - } + boolean additionalMetadataChanged = sGameFileCache.updateAdditionalMetadata(); + if (additionalMetadataChanged) + { + updateGameFileArray(); + } - if (changed || additionalMetadataChanged) - { - sGameFileCache.save(); - } + if (changed || additionalMetadataChanged) + { + sGameFileCache.save(); } }