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.
This commit is contained in:
JosJuice
2022-09-27 19:05:02 +02:00
parent 51debaeb47
commit 45901f64b5
2 changed files with 25 additions and 34 deletions

View File

@ -109,11 +109,11 @@ public class GameFileCache
public static native String[] getAllGamePaths(String[] folderPaths, boolean recursiveScan); 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. * Sets the list of games to cache.
@ -123,7 +123,7 @@ public class GameFileCache
* *
* @return true if the cache was modified * @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 * 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 * @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();
} }

View File

@ -158,7 +158,7 @@ public final class GameFileCacheManager
{ {
// Common case: The game is in the cache, so just grab it from there. // 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 // (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(); GameFile[] allGames = sGameFiles.getValue();
for (GameFile game : allGames) for (GameFile game : allGames)
{ {
@ -171,11 +171,8 @@ public final class GameFileCacheManager
// Unusual case: The game wasn't found in the cache. // 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. // Scan the game and add it to the cache so that we can return it.
createGameFileCacheIfNeeded(); createGameFileCacheIfNeeded();
synchronized (sGameFileCache)
{
return sGameFileCache.addOrGet(gamePath); return sGameFileCache.addOrGet(gamePath);
} }
}
/** /**
* Loads the game file cache from disk, without checking if the * Loads the game file cache from disk, without checking if the
@ -185,8 +182,6 @@ public final class GameFileCacheManager
private static void load() private static void load()
{ {
if (!sFirstLoadDone) if (!sFirstLoadDone)
{
synchronized (sGameFileCache)
{ {
sFirstLoadDone = true; sFirstLoadDone = true;
sGameFileCache.load(); sGameFileCache.load();
@ -195,7 +190,6 @@ public final class GameFileCacheManager
updateGameFileArray(); updateGameFileArray();
} }
} }
}
if (sRunRescanAfterLoad) if (sRunRescanAfterLoad)
{ {
@ -227,8 +221,6 @@ public final class GameFileCacheManager
{ {
String[] gamePaths = GameFileCache.getAllGamePaths(); String[] gamePaths = GameFileCache.getAllGamePaths();
synchronized (sGameFileCache)
{
boolean changed = sGameFileCache.update(gamePaths); boolean changed = sGameFileCache.update(gamePaths);
if (changed) if (changed)
{ {
@ -246,7 +238,6 @@ public final class GameFileCacheManager
sGameFileCache.save(); sGameFileCache.save();
} }
} }
}
sRescanInProgress.postValue(false); sRescanInProgress.postValue(false);
} }