Skip to content

[Frontend] Use new LockFileManager APIs #80743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: release/6.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 31 additions & 36 deletions lib/Frontend/ModuleInterfaceBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,53 +391,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;
return build();
}
case llvm::LockFileManager::LFS_Owned: {
if (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();
// 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;
}
}
}
Expand Down
1 change: 0 additions & 1 deletion lib/FrontendTool/LoadedModuleTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include "llvm/Support/Path.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/LockFileManager.h"

#if !defined(_MSC_VER) && !defined(__MINGW32__)
#include <unistd.h>
Expand Down