diff options
| author | JosJuice <josjuice@gmail.com> | 2022-09-27 19:05:02 +0200 |
|---|---|---|
| committer | JosJuice <josjuice@gmail.com> | 2022-09-27 19:06:10 +0200 |
| commit | 45901f64b5816213e774a7e89e70af5372527765 (patch) | |
| tree | 1e089bb934cc85e6abab5f3b94546ad3ae4a5f78 /Source/Android/app/src/main/java/org | |
| parent | 51debaeb47de93edec1ba10161df74a2f7f49209 (diff) | |
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.
Diffstat (limited to 'Source/Android/app/src/main/java/org')
2 files changed, 27 insertions, 36 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) + { + updateGameFileArray(); + } + + boolean additionalMetadataChanged = sGameFileCache.updateAdditionalMetadata(); + if (additionalMetadataChanged) + { + updateGameFileArray(); + } + + if (changed || additionalMetadataChanged) { - boolean changed = sGameFileCache.update(gamePaths); - if (changed) - { - updateGameFileArray(); - } - - boolean additionalMetadataChanged = sGameFileCache.updateAdditionalMetadata(); - if (additionalMetadataChanged) - { - updateGameFileArray(); - } - - if (changed || additionalMetadataChanged) - { - sGameFileCache.save(); - } + sGameFileCache.save(); } } |
