mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
dirtyfix.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user