dirtyfix.

This commit is contained in:
Ivan Rakov
2021-06-16 18:26:38 +03:00
parent c8117b9502
commit d442aca7a8
2 changed files with 30 additions and 18 deletions
@@ -1703,15 +1703,26 @@ public final class ChronicleMapBuilder<K, V> implements
final AtomicBoolean newFile = new AtomicBoolean();
final FileChannel fileChannel = raf.getChannel();
FileLockUtil.runExclusively(canonicalFile, fileChannel, () -> {
while (true) {
if (raf.length() == 0) {
map.set(newMap());
headerBuffer.set(writeHeader(fileChannel, map.get()));
newFile.set(true);
} else {
newFile.set(false);
final boolean locked = FileLockUtil.tryRunExclusively(canonicalFile, fileChannel, () -> {
if (raf.length() == 0) {
map.set(newMap());
headerBuffer.set(writeHeader(fileChannel, map.get()));
newFile.set(true);
} else {
newFile.set(false);
}
});
if (locked)
break;
else
Jvm.pause(10);
}
});
else
break;
}
if (newFile.get()) {
final int headerSize = headerBuffer.get().remaining();
@@ -99,27 +99,26 @@ public final class FileLockUtil {
public static boolean tryRunExclusively(@NotNull final File canonicalFile,
@NotNull final FileChannel fileChannel,
@NotNull final FileIOAction fileIOAction) {
AtomicBoolean locked = new AtomicBoolean(false);
FILE_LOCKS.compute(canonicalFile, (f, flr) -> {
if (flr != null)
throw new ChronicleFileLockException("A file lock instance already exists for the file " + canonicalFile);
try {
if (USE_LOCKING) {
do {
try (FileLock ignored = fileChannel.tryLock()) {
if (ignored == null) {
Jvm.pause(10);
try (FileLock ignored = fileChannel.tryLock()) {
if (ignored == null)
return null;
continue;
}
fileIOAction.fileIOAction();
fileIOAction.fileIOAction();
break;
}
} while (true);
locked.set(true);
}
} else {
fileIOAction.fileIOAction();
locked.set(true);
}
return null;
@@ -128,6 +127,8 @@ public final class FileLockUtil {
}
}
);
return locked.get();
}
/**