Simplify locking of FileRepository's index snapshot

synchronize on simple Object monitor instead of using ReentrantLock

Change-Id: I897020ab35786336b51b0fef76ea6071aff8aefa
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
Matthias Sohn 2018-06-08 09:50:39 +02:00
parent b782518cae
commit 1cb8c5d7fe
1 changed files with 3 additions and 10 deletions

View File

@ -56,7 +56,6 @@
import java.util.Locale; import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
import org.eclipse.jgit.annotations.Nullable; import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.api.errors.JGitInternalException;
@ -125,7 +124,7 @@ public class FileRepository extends Repository {
private final RefDatabase refs; private final RefDatabase refs;
private final ObjectDirectory objectDatabase; private final ObjectDirectory objectDatabase;
private final ReentrantLock snapshotLock = new ReentrantLock(); private final Object snapshotLock = new Object();
// protected by snapshotLock // protected by snapshotLock
private FileSnapshot snapshot; private FileSnapshot snapshot;
@ -553,8 +552,7 @@ private void detectIndexChanges() {
} }
File indexFile = getIndexFile(); File indexFile = getIndexFile();
snapshotLock.lock(); synchronized (snapshotLock) {
try {
if (snapshot == null) { if (snapshot == null) {
snapshot = FileSnapshot.save(indexFile); snapshot = FileSnapshot.save(indexFile);
return; return;
@ -562,8 +560,6 @@ private void detectIndexChanges() {
if (!snapshot.isModified(indexFile)) { if (!snapshot.isModified(indexFile)) {
return; return;
} }
} finally {
snapshotLock.unlock();
} }
notifyIndexChanged(false); notifyIndexChanged(false);
} }
@ -571,11 +567,8 @@ private void detectIndexChanges() {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public void notifyIndexChanged(boolean internal) { public void notifyIndexChanged(boolean internal) {
snapshotLock.lock(); synchronized (snapshotLock) {
try {
snapshot = FileSnapshot.save(getIndexFile()); snapshot = FileSnapshot.save(getIndexFile());
} finally {
snapshotLock.unlock();
} }
fireEvent(new IndexChangedEvent(internal)); fireEvent(new IndexChangedEvent(internal));
} }