9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SUPPORT_THREADING_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SUPPORT_THREADING_H
13 #include "llvm/ADT/FunctionExtras.h"
14 #include "llvm/ADT/Twine.h"
17 #include <condition_variable>
39 std::condition_variable SlotsChanged;
40 std::size_t FreeSlots;
48 Deadline(std::chrono::steady_clock::time_point Time)
49 : Type(Finite), Time(Time) {}
53 std::chrono::steady_clock::time_point
time()
const {
54 assert(Type == Finite);
58 return (Type == Zero) ||
59 (Type == Finite && Time < std::chrono::steady_clock::now());
62 return (Type == Other.Type) && (Type != Finite || Time == Other.Time);
66 enum Type { Zero, Infinite, Finite };
70 std::chrono::steady_clock::time_point Time;
76 void wait(std::unique_lock<std::mutex> &Lock, std::condition_variable &CV,
79 template <
typename Func>
80 [[nodiscard]]
bool wait(std::unique_lock<std::mutex> &Lock,
81 std::condition_variable &CV,
Deadline D, Func F) {
100 bool Notified =
false;
101 mutable std::condition_variable CV;
102 mutable std::mutex Mu;
119 mutable std::mutex Mutex;
120 mutable std::condition_variable TasksReachedZero;
121 std::size_t InFlightTasks = 0;
126 template <
typename T>
149 std::unique_ptr<std::mutex> Mu;
152 Memoize() : Mu(std::make_unique<std::mutex>()) {}
154 template <
typename T,
typename Func>
155 typename Container::mapped_type
get(T &&
Key, Func Compute)
const {
157 std::lock_guard<std::mutex> Lock(*Mu);
158 auto It = Cache.find(
Key);
159 if (It != Cache.end())
165 std::lock_guard<std::mutex> Lock(*Mu);
166 auto R = Cache.try_emplace(std::forward<T>(
Key), V);
169 return R.first->second;
187 using Stopwatch = std::chrono::steady_clock;
188 using Rep = Stopwatch::duration::rep;
191 std::atomic<Rep> Next;
196 : Period(Period.count()),
197 Next((Stopwatch::now() + Delay).time_since_epoch().count()) {}