[clang-tools-extra] r315325 - [clangd] Use UniqueFunction for deferred computations.

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 10 09:12:54 PDT 2017


Author: ibiryukov
Date: Tue Oct 10 09:12:54 2017
New Revision: 315325

URL: http://llvm.org/viewvc/llvm-project?rev=315325&view=rev
Log:
[clangd] Use UniqueFunction for deferred computations.

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

Modified:
    clang-tools-extra/trunk/clangd/ClangdServer.cpp
    clang-tools-extra/trunk/clangd/ClangdUnit.cpp
    clang-tools-extra/trunk/clangd/ClangdUnit.h

Modified: clang-tools-extra/trunk/clangd/ClangdServer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdServer.cpp?rev=315325&r1=315324&r2=315325&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdServer.cpp (original)
+++ clang-tools-extra/trunk/clangd/ClangdServer.cpp Tue Oct 10 09:12:54 2017
@@ -413,8 +413,9 @@ std::future<void> ClangdServer::schedule
     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::schedule
   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::schedule
     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:
     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));

Modified: clang-tools-extra/trunk/clangd/ClangdUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdUnit.cpp?rev=315325&r1=315324&r2=315325&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdUnit.cpp (original)
+++ clang-tools-extra/trunk/clangd/ClangdUnit.cpp Tue Oct 10 09:12:54 2017
@@ -1121,9 +1121,9 @@ CppFile::CppFile(PathRef FileName, tooli
   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::deferCancelRe
   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::deferCancelRe
     // 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 NewConte
     return Diagnostics;
   };
 
-  return std::async(std::launch::deferred, FinishRebuild, NewContents.str());
+  return BindWithForward(FinishRebuild, NewContents.str());
 }
 
 std::shared_future<std::shared_ptr<const PreambleData>>

Modified: clang-tools-extra/trunk/clangd/ClangdUnit.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdUnit.h?rev=315325&r1=315324&r2=315325&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdUnit.h (original)
+++ clang-tools-extra/trunk/clangd/ClangdUnit.h Tue Oct 10 09:12:54 2017
@@ -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




More information about the cfe-commits mailing list