dirtyfix.

This commit is contained in:
Ivan Rakov
2021-06-16 18:16:04 +03:00
parent 8be733eb06
commit c8117b9502
@@ -90,6 +90,46 @@ public final class FileLockUtil {
printWarningTheFirstTime();
}
/**
* Executes a closure under exclusive file lock.
* If USE_LOCKING is false, provides synchronization only within local JVM.
*
* @param fileIOAction Closure to run, can throw {@link IOException}s.
*/
public static boolean tryRunExclusively(@NotNull final File canonicalFile,
@NotNull final FileChannel fileChannel,
@NotNull final FileIOAction fileIOAction) {
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);
continue;
}
fileIOAction.fileIOAction();
break;
}
} while (true);
} else {
fileIOAction.fileIOAction();
}
return null;
} catch (Exception e) {
throw Jvm.rethrow(e);
}
}
);
}
/**
* Executes a closure under exclusive file lock.
* If USE_LOCKING is false, provides synchronization only within local JVM.