[Frontend] Use new LockFileManager APIs (#80123)

This commit is contained in:
Jan Svoboda
2025-03-24 10:14:24 -07:00
committed by GitHub
parent 8f4220c6fa
commit 0aeba05790
2 changed files with 32 additions and 38 deletions

View File

@@ -392,53 +392,48 @@ bool ImplicitModuleInterfaceBuilder::buildSwiftModule(StringRef OutPath,
// processes are doing the same.
// FIXME: We should surface the module building step to the build system so
// we don't need to synchronize here.
llvm::LockFileManager Locked(OutPath);
switch (Locked) {
case llvm::LockFileManager::LFS_Error:{
llvm::LockFileManager Lock(OutPath);
bool Owned;
if (llvm::Error Err = Lock.tryLock().moveInto(Owned)) {
llvm::consumeError(std::move(Err));
// ModuleInterfaceBuilder takes care of correctness and locks are only
// necessary for performance. Fallback to building the module in case of any lock
// related errors.
if (RemarkRebuild) {
diagnose(diag::interface_file_lock_failure);
}
// Clear out any potential leftover.
Locked.unsafeRemoveLockFile();
LLVM_FALLTHROUGH;
}
case llvm::LockFileManager::LFS_Owned: {
return build();
}
case llvm::LockFileManager::LFS_Shared: {
// Someone else is responsible for building the module. Wait for them to
// finish.
switch (Locked.waitForUnlock(256)) {
case llvm::LockFileManager::Res_Success: {
// This process may have a different module output path. If the other
// process doesn't build the interface to this output path, we should try
// building ourselves.
auto bufferOrError = llvm::MemoryBuffer::getFile(OutPath);
if (!bufferOrError)
continue;
if (ModuleBuffer)
*ModuleBuffer = std::move(bufferOrError.get());
return false;
}
case llvm::LockFileManager::Res_OwnerDied: {
continue; // try again to get the lock.
}
case llvm::LockFileManager::Res_Timeout: {
// Since ModuleInterfaceBuilder takes care of correctness, we try waiting for
// another process to complete the build so swift does not do it done
// twice. If case of timeout, build it ourselves.
if (RemarkRebuild) {
diagnose(diag::interface_file_lock_timed_out, interfacePath);
}
// Clear the lock file so that future invocations can make progress.
Locked.unsafeRemoveLockFile();
if (Owned) {
return build();
}
// Someone else is responsible for building the module. Wait for them to
// finish.
switch (Lock.waitForUnlockFor(std::chrono::seconds(256))) {
case llvm::WaitForUnlockResult::Success: {
// This process may have a different module output path. If the other
// process doesn't build the interface to this output path, we should try
// building ourselves.
auto bufferOrError = llvm::MemoryBuffer::getFile(OutPath);
if (!bufferOrError)
continue;
if (ModuleBuffer)
*ModuleBuffer = std::move(bufferOrError.get());
return false;
}
case llvm::WaitForUnlockResult::OwnerDied: {
continue; // try again to get the lock.
}
case llvm::WaitForUnlockResult::Timeout: {
// Since ModuleInterfaceBuilder takes care of correctness, we try waiting for
// another process to complete the build so swift does not do it done
// twice. If case of timeout, build it ourselves.
if (RemarkRebuild) {
diagnose(diag::interface_file_lock_timed_out, interfacePath);
}
}
break;
// Clear the lock file so that future invocations can make progress.
Lock.unsafeMaybeUnlock();
continue;
}
}
}