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"
16 #include <condition_variable>
35 bool Notified =
false;
36 mutable std::condition_variable CV;
37 mutable std::mutex Mu;
51 std::condition_variable SlotsChanged;
52 std::size_t FreeSlots;
60 Deadline(std::chrono::steady_clock::time_point Time)
61 : Type(Finite), Time(Time) {}
65 std::chrono::steady_clock::time_point
time()
const {
66 assert(Type == Finite);
70 return (Type == Zero) ||
71 (Type == Finite && Time < std::chrono::steady_clock::now());
74 return (Type == Other.Type) && (Type != Finite || Time == Other.Time);
78 enum Type { Zero, Infinite, Finite };
82 std::chrono::steady_clock::time_point Time;
88 void wait(std::unique_lock<std::mutex> &Lock, std::condition_variable &CV,
91 template <
typename Func>
92 LLVM_NODISCARD
bool wait(std::unique_lock<std::mutex> &Lock,
93 std::condition_variable &CV,
Deadline D, Func F) {
116 mutable std::mutex Mutex;
117 mutable std::condition_variable TasksReachedZero;
118 std::size_t InFlightTasks = 0;
123 template <
typename T>
145 mutable Container Cache;
146 std::unique_ptr<std::mutex> Mu;
149 Memoize() : Mu(std::make_unique<std::mutex>()) {}
151 template <
typename T,
typename Func>
152 typename Container::mapped_type
get(T &&
Key, Func Compute)
const {
154 std::lock_guard<std::mutex> Lock(*Mu);
155 auto It = Cache.find(
Key);
156 if (It != Cache.end())
162 std::lock_guard<std::mutex> Lock(*Mu);
163 auto R = Cache.try_emplace(std::forward<T>(
Key), V);
166 return R.first->second;