LCOV - code coverage report
Current view: top level - clang/tools/extra/clangd - Threading.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 0 2 0.0 %
Date: 2018-02-22 16:16:46 Functions: 0 0 -
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===--- ThreadPool.h --------------------------------------------*- C++-*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : 
      10             : #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_THREADING_H
      11             : #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_THREADING_H
      12             : 
      13             : #include "Context.h"
      14             : #include "Function.h"
      15             : #include "llvm/ADT/Twine.h"
      16             : #include <atomic>
      17             : #include <cassert>
      18             : #include <condition_variable>
      19             : #include <memory>
      20             : #include <mutex>
      21             : #include <vector>
      22             : 
      23             : namespace clang {
      24             : namespace clangd {
      25             : 
      26             : /// A shared boolean flag indicating if the computation was cancelled.
      27             : /// Once cancelled, cannot be returned to the previous state.
      28             : class CancellationFlag {
      29             : public:
      30             :   CancellationFlag();
      31             : 
      32             :   void cancel() {
      33             :     assert(WasCancelled && "the object was moved");
      34             :     WasCancelled->store(true);
      35             :   }
      36             : 
      37             :   bool isCancelled() const {
      38             :     assert(WasCancelled && "the object was moved");
      39             :     return WasCancelled->load();
      40             :   }
      41             : 
      42             : private:
      43             :   std::shared_ptr<std::atomic<bool>> WasCancelled;
      44             : };
      45             : 
      46             : /// Limits the number of threads that can acquire the lock at the same time.
      47             : class Semaphore {
      48             : public:
      49             :   Semaphore(std::size_t MaxLocks);
      50             : 
      51             :   void lock();
      52             :   void unlock();
      53             : 
      54             : private:
      55             :   std::mutex Mutex;
      56             :   std::condition_variable SlotsChanged;
      57             :   std::size_t FreeSlots;
      58             : };
      59             : 
      60             : /// A point in time we may wait for, or None to wait forever.
      61             : /// (Not time_point::max(), because many std::chrono implementations overflow).
      62             : using Deadline = llvm::Optional<std::chrono::steady_clock::time_point>;
      63             : /// Makes a deadline from a timeout in seconds.
      64             : Deadline timeoutSeconds(llvm::Optional<double> Seconds);
      65             : /// Waits on a condition variable until F() is true or D expires.
      66             : template <typename Func>
      67             : LLVM_NODISCARD bool wait(std::unique_lock<std::mutex> &Lock,
      68             :                          std::condition_variable &CV, Deadline D, Func F) {
      69             :   if (D)
      70             :     return CV.wait_until(Lock, *D, F);
      71             :   CV.wait(Lock, F);
      72             :   return true;
      73             : }
      74             : 
      75             : /// Runs tasks on separate (detached) threads and wait for all tasks to finish.
      76             : /// Objects that need to spawn threads can own an AsyncTaskRunner to ensure they
      77             : /// all complete on destruction.
      78           0 : class AsyncTaskRunner {
      79             : public:
      80             :   /// Destructor waits for all pending tasks to finish.
      81             :   ~AsyncTaskRunner();
      82             : 
      83           0 :   void wait() const { (void) wait(llvm::None); }
      84             :   LLVM_NODISCARD bool wait(Deadline D) const;
      85             :   // The name is used for tracing and debugging (e.g. to name a spawned thread).
      86             :   void runAsync(llvm::Twine Name, UniqueFunction<void()> Action);
      87             : 
      88             : private:
      89             :   mutable std::mutex Mutex;
      90             :   mutable std::condition_variable TasksReachedZero;
      91             :   std::size_t InFlightTasks = 0;
      92             : };
      93             : } // namespace clangd
      94             : } // namespace clang
      95             : #endif

Generated by: LCOV version 1.13