summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShawn Pearce <spearce@spearce.org>2016-11-14 13:25:36 -0500
committerGerrit Code Review @ Eclipse.org <gerrit@eclipse.org>2016-11-14 13:25:38 -0500
commita0bac65233127c646011417234caf2a42952f0b0 (patch)
tree9d413a9209ea368f73384ed6897dac64cf835448
parent013a4b84fd6d92a529c570cf10f0b233baf4f9bb (diff)
parentdf6f2d68607accc290b10b68c83abb26f8c9c8ab (diff)
downloadjgit-a0bac65233127c646011417234caf2a42952f0b0.tar.gz
jgit-a0bac65233127c646011417234caf2a42952f0b0.zip
Merge "Reduce synchronized scope around ConcurrentHashMap"
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsBlockCache.java30
1 files changed, 15 insertions, 15 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsBlockCache.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsBlockCache.java
index 05627ed86b..f7decf1324 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsBlockCache.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsBlockCache.java
@@ -263,20 +263,22 @@ public final class DfsBlockCache {
// TODO This table grows without bound. It needs to clean up
// entries that aren't in cache anymore, and aren't being used
// by a live DfsObjDatabase reference.
- synchronized (packCache) {
- DfsPackFile pack = packCache.get(dsc);
- if (pack != null && pack.invalid()) {
- packCache.remove(dsc);
- pack = null;
- }
- if (pack == null) {
- if (key == null)
- key = new DfsPackKey();
- pack = new DfsPackFile(this, dsc, key);
- packCache.put(dsc, pack);
- }
+
+ DfsPackFile pack = packCache.get(dsc);
+ if (pack != null && !pack.invalid()) {
return pack;
}
+
+ // 'pack' either didn't exist or was invalid. Compute a new
+ // entry atomically (guaranteed by ConcurrentHashMap).
+ return packCache.compute(dsc, (k, v) -> {
+ if (v != null && !v.invalid()) { // valid value added by
+ return v; // another thread
+ } else {
+ return new DfsPackFile(
+ this, dsc, key != null ? key : new DfsPackKey());
+ }
+ });
}
private int hash(int packHash, long off) {
@@ -504,9 +506,7 @@ public final class DfsBlockCache {
}
void remove(DfsPackFile pack) {
- synchronized (packCache) {
- packCache.remove(pack.getPackDescription());
- }
+ packCache.remove(pack.getPackDescription());
}
private int slot(DfsPackKey pack, long position) {