[clangd] Use UniqueFunction for deferred computations.

Previsouly, `std::future` that were results of
`std::async(std::launch::deferred, ...` were used.

llvm-svn: 315325
This commit is contained in:
Ilya Biryukov 2017-10-10 16:12:54 +00:00
parent eab499d31b
commit 98a1fd7f96
3 changed files with 24 additions and 23 deletions

View File

@ -413,8 +413,9 @@ std::future<void> ClangdServer::scheduleReparseAndDiags(
Tagged<IntrusiveRefCntPtr<vfs::FileSystem>> TaggedFS) {
assert(Contents.Draft && "Draft must have contents");
std::future<llvm::Optional<std::vector<DiagWithFixIts>>> DeferredRebuild =
Resources->deferRebuild(*Contents.Draft, TaggedFS.Value);
UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>()>
DeferredRebuild =
Resources->deferRebuild(*Contents.Draft, TaggedFS.Value);
std::promise<void> DonePromise;
std::future<void> DoneFuture = DonePromise.get_future();
@ -423,7 +424,7 @@ std::future<void> ClangdServer::scheduleReparseAndDiags(
VFSTag Tag = TaggedFS.Tag;
auto ReparseAndPublishDiags =
[this, FileStr, Version,
Tag](std::future<llvm::Optional<std::vector<DiagWithFixIts>>>
Tag](UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>()>
DeferredRebuild,
std::promise<void> DonePromise) -> void {
FulfillPromiseGuard Guard(DonePromise);
@ -432,7 +433,7 @@ std::future<void> ClangdServer::scheduleReparseAndDiags(
if (CurrentVersion != Version)
return; // This request is outdated
auto Diags = DeferredRebuild.get();
auto Diags = DeferredRebuild();
if (!Diags)
return; // A new reparse was requested before this one completed.
@ -467,11 +468,11 @@ ClangdServer::scheduleCancelRebuild(std::shared_ptr<CppFile> Resources) {
return DoneFuture;
}
std::future<void> DeferredCancel = Resources->deferCancelRebuild();
UniqueFunction<void()> DeferredCancel = Resources->deferCancelRebuild();
auto CancelReparses = [Resources](std::promise<void> DonePromise,
std::future<void> DeferredCancel) {
UniqueFunction<void()> DeferredCancel) {
FulfillPromiseGuard Guard(DonePromise);
DeferredCancel.get();
DeferredCancel();
};
WorkScheduler.addToFront(std::move(CancelReparses), std::move(DonePromise),
std::move(DeferredCancel));

View File

@ -1121,9 +1121,9 @@ CppFile::CppFile(PathRef FileName, tooling::CompileCommand Command,
ASTFuture = ASTPromise.get_future();
}
void CppFile::cancelRebuild() { deferCancelRebuild().get(); }
void CppFile::cancelRebuild() { deferCancelRebuild()(); }
std::future<void> CppFile::deferCancelRebuild() {
UniqueFunction<void()> CppFile::deferCancelRebuild() {
std::unique_lock<std::mutex> Lock(Mutex);
// Cancel an ongoing rebuild, if any, and wait for it to finish.
unsigned RequestRebuildCounter = ++this->RebuildCounter;
@ -1143,7 +1143,7 @@ std::future<void> CppFile::deferCancelRebuild() {
RebuildCond.notify_all();
std::shared_ptr<CppFile> That = shared_from_this();
return std::async(std::launch::deferred, [That, RequestRebuildCounter]() {
return [That, RequestRebuildCounter]() {
std::unique_lock<std::mutex> Lock(That->Mutex);
CppFile *This = &*That;
This->RebuildCond.wait(Lock, [This, RequestRebuildCounter]() {
@ -1158,16 +1158,16 @@ std::future<void> CppFile::deferCancelRebuild() {
// Set empty results for Promises.
That->PreamblePromise.set_value(nullptr);
That->ASTPromise.set_value(std::make_shared<ParsedASTWrapper>(llvm::None));
});
};
}
llvm::Optional<std::vector<DiagWithFixIts>>
CppFile::rebuild(StringRef NewContents,
IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
return deferRebuild(NewContents, std::move(VFS)).get();
return deferRebuild(NewContents, std::move(VFS))();
}
std::future<llvm::Optional<std::vector<DiagWithFixIts>>>
UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>()>
CppFile::deferRebuild(StringRef NewContents,
IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
std::shared_ptr<const PreambleData> OldPreamble;
@ -1315,7 +1315,7 @@ CppFile::deferRebuild(StringRef NewContents,
return Diagnostics;
};
return std::async(std::launch::deferred, FinishRebuild, NewContents.str());
return BindWithForward(FinishRebuild, NewContents.str());
}
std::shared_future<std::shared_ptr<const PreambleData>>

View File

@ -10,6 +10,7 @@
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDUNIT_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDUNIT_H
#include "Function.h"
#include "Path.h"
#include "Protocol.h"
#include "clang/Frontend/FrontendAction.h"
@ -157,12 +158,11 @@ public:
void cancelRebuild();
/// Similar to deferRebuild, but sets both Preamble and AST to nulls instead
/// of doing an actual parsing. Returned future is a deferred computation that
/// will wait for any ongoing rebuilds to finish and actually set the AST and
/// Preamble to nulls. It can be run on a different thread.
/// This function is useful to cancel ongoing rebuilds, if any, before
/// removing CppFile.
std::future<void> deferCancelRebuild();
/// of doing an actual parsing. Returned function is a deferred computation
/// that will wait for any ongoing rebuilds to finish and actually set the AST
/// and Preamble to nulls. It can be run on a different thread. This function
/// is useful to cancel ongoing rebuilds, if any, before removing CppFile.
UniqueFunction<void()> deferCancelRebuild();
/// Rebuild AST and Preamble synchronously on the calling thread.
/// Returns a list of diagnostics or a llvm::None, if another rebuild was
@ -175,8 +175,8 @@ public:
/// rebuild, that can be called on a different thread.
/// After calling this method, resources, available via futures returned by
/// getPreamble() and getAST(), will be waiting for rebuild to finish. A
/// future fininshing rebuild, returned by this function, must be
/// computed(i.e. get() should be called on it) in order to make those
/// continuation fininshing rebuild, returned by this function, must be
/// computed(i.e., operator() must be called on it) in order to make those
/// resources ready. If deferRebuild is called again before the rebuild is
/// finished (either because returned future had not been called or because it
/// had not returned yet), the previous rebuild request is cancelled and the
@ -185,7 +185,7 @@ public:
/// The future to finish rebuild returns a list of diagnostics built during
/// reparse, or None, if another deferRebuild was called before this
/// rebuild was finished.
std::future<llvm::Optional<std::vector<DiagWithFixIts>>>
UniqueFunction<llvm::Optional<std::vector<DiagWithFixIts>>()>
deferRebuild(StringRef NewContents, IntrusiveRefCntPtr<vfs::FileSystem> VFS);
/// Returns a future to get the most fresh PreambleData for a file. The