[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

@@ -74,6 +74,7 @@
#ifndef LLVM_SOURCEKIT_LIB_SWIFTLANG_SWIFTASTMANAGER_H
#define LLVM_SOURCEKIT_LIB_SWIFTLANG_SWIFTASTMANAGER_H
#include "SourceKit/Core/Context.h"
#include "SourceKit/Core/LLVM.h"
#include "SourceKit/Support/CancellationToken.h"
#include "SwiftInvocation.h"
@@ -223,6 +224,7 @@ public:
explicit SwiftASTManager(std::shared_ptr<SwiftEditorDocumentFileMap>,
std::shared_ptr<GlobalConfig> Config,
std::shared_ptr<SwiftStatistics> Stats,
std::shared_ptr<RequestTracker> ReqTracker,
StringRef RuntimeResourcePath,
StringRef DiagnosticDocumentationPath);
~SwiftASTManager();
@@ -250,12 +252,6 @@ public:
ArrayRef<ImmutableTextSnapshotRef> Snapshots =
ArrayRef<ImmutableTextSnapshotRef>());
/// Request the \c SwiftASTConsumer with the given \p CancellationToken to be
/// cancelled. If \p CancellationToken is \c nullptr or no consumer with the
/// given cancellation token exists (e.g. because the consumer already
/// finished), this is a no-op.
void cancelASTConsumer(SourceKitCancellationToken CancellationToken);
std::unique_ptr<llvm::MemoryBuffer> getMemoryBuffer(StringRef Filename,
std::string &Error);