[SourceKit] Add a request tracker that manages cancellaiton

Previously, `SwiftASTManager` and `SlowRequestSimulator` maintained their own list of in-progress cancellation tokens. With code completion cancellation coming up, there would need to be yet another place to track in-progress requests, so let’s centralize it.

While at it, also support cancelling requests before they are scheduled, eliminating the need for a `sleep` in a test case.

The current implementaiton leaks tiny amounts of memory if a request is cancelled after if finishes. I think this is fine because it is a pretty nieche case and the leaked memory is pretty small (a `std::map` entry pointing to a `std::function` + `bool`). Alternatively, we could require the client to always dispose of the cancellation token manually.
This commit is contained in:
Alex Hoppen
2021-11-10 22:11:02 +01:00
parent 86a1bfd340
commit 772485def3
11 changed files with 143 additions and 73 deletions

View File

@@ -273,7 +273,7 @@ configureCompletionInstance(std::shared_ptr<CompletionInstance> CompletionInst,
SwiftLangSupport::SwiftLangSupport(SourceKit::Context &SKCtx)
: NotificationCtr(SKCtx.getNotificationCenter()),
CCCache(new SwiftCompletionCache) {
ReqTracker(SKCtx.getRequestTracker()), CCCache(new SwiftCompletionCache) {
llvm::SmallString<128> LibPath(SKCtx.getRuntimeLibPath());
llvm::sys::path::append(LibPath, "swift");
RuntimeResourcePath = std::string(LibPath.str());
@@ -282,7 +282,7 @@ SwiftLangSupport::SwiftLangSupport(SourceKit::Context &SKCtx)
Stats = std::make_shared<SwiftStatistics>();
EditorDocuments = std::make_shared<SwiftEditorDocumentFileMap>();
ASTMgr = std::make_shared<SwiftASTManager>(
EditorDocuments, SKCtx.getGlobalConfiguration(), Stats,
EditorDocuments, SKCtx.getGlobalConfiguration(), Stats, ReqTracker,
RuntimeResourcePath, DiagnosticDocumentationPath);
CompletionInst = std::make_shared<CompletionInstance>();
@@ -307,11 +307,6 @@ void SwiftLangSupport::dependencyUpdated() {
CompletionInst->markCachedCompilerInstanceShouldBeInvalidated();
}
void SwiftLangSupport::cancelRequest(
SourceKitCancellationToken CancellationToken) {
getASTManager()->cancelASTConsumer(CancellationToken);
}
UIdent SwiftLangSupport::getUIDForDeclLanguage(const swift::Decl *D) {
if (D->hasClangNode())
return KindObjC;