clang-tools 8.0.0

Threading.h
Go to the documentation of this file.
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 <cassert>
17 #include <condition_variable>
18 #include <memory>
19 #include <mutex>
20 #include <thread>
21 #include <vector>
22
23 namespace clang {
24 namespace clangd {
25
26 /// A threadsafe flag that is initially clear.
27 class Notification {
28 public:
29 // Sets the flag. No-op if already set.
30 void notify();
31 // Blocks until flag is set.
32 void wait() const;
33
34 private:
35 bool Notified = false;
36 mutable std::condition_variable CV;
37 mutable std::mutex Mu;
38 };
39
40 /// Limits the number of threads that can acquire the lock at the same time.
41 class Semaphore {
42 public:
43 Semaphore(std::size_t MaxLocks);
44
45 bool try_lock();
46 void lock();
47 void unlock();
48
49 private:
50 std::mutex Mutex;
51 std::condition_variable SlotsChanged;
52 std::size_t FreeSlots;
53 };
54
55 /// A point in time we can wait for.
56 /// Can be zero (don't wait) or infinity (wait forever).
57 /// (Not time_point::max(), because many std::chrono implementations overflow).
58 class Deadline {
59 public:
60 Deadline(std::chrono::steady_clock::time_point Time)
61 : Type(Finite), Time(Time) {}
62 static Deadline zero() { return Deadline(Zero); }
63 static Deadline infinity() { return Deadline(Infinite); }
64
65 std::chrono::steady_clock::time_point time() const {
66 assert(Type == Finite);
67 return Time;
68 }
69 bool expired() const {
70 return (Type == Zero) ||
71 (Type == Finite && Time < std::chrono::steady_clock::now());
72 }
73 bool operator==(const Deadline &Other) const {
74 return (Type == Other.Type) && (Type != Finite || Time == Other.Time);
75 }
76
77 private:
78 enum Type { Zero, Infinite, Finite };
79
80 Deadline(enum Type Type) : Type(Type) {}
81 enum Type Type;
82 std::chrono::steady_clock::time_point Time;
83 };
84
85 /// Makes a deadline from a timeout in seconds. None means wait forever.
86 Deadline timeoutSeconds(llvm::Optional<double> Seconds);
87 /// Wait once on CV for the specified duration.
88 void wait(std::unique_lock<std::mutex> &Lock, std::condition_variable &CV,
89 Deadline D);
90 /// Waits on a condition variable until F() is true or D expires.
91 template <typename Func>
92 LLVM_NODISCARD bool wait(std::unique_lock<std::mutex> &Lock,
93 std::condition_variable &CV, Deadline D, Func F) {
94 while (!F()) {
95 if (D.expired())
96 return false;
97 wait(Lock, CV, D);
98 }
99 return true;
100 }
101
102 /// Runs tasks on separate (detached) threads and wait for all tasks to finish.
103 /// Objects that need to spawn threads can own an AsyncTaskRunner to ensure they
104 /// all complete on destruction.
105 class AsyncTaskRunner {
106 public:
107 /// Destructor waits for all pending tasks to finish.
108 ~AsyncTaskRunner();
109
110 void wait() const { (void)wait(Deadline::infinity()); }
111 LLVM_NODISCARD bool wait(Deadline D) const;
112 // The name is used for tracing and debugging (e.g. to name a spawned thread).
113 void runAsync(const llvm::Twine &Name, llvm::unique_function<void()> Action);
114
115 private:
116 mutable std::mutex Mutex;
117 mutable std::condition_variable TasksReachedZero;
118 std::size_t InFlightTasks = 0;
119 };
120
121 enum class ThreadPriority {
122 Low = 0,
123 Normal = 1,
124 };
125 void setCurrentThreadPriority(ThreadPriority Priority);
126 // Avoid the use of scheduler policies that may starve low-priority threads.
127 // This prevents tests from timing out on loaded systems.
128 // Affects subsequent setThreadPriority() calls.
129 void preventThreadStarvationInTests();
130
131 } // namespace clangd
132 } // namespace clang
133 #endif
clang::clangd::Notification
A threadsafe flag that is initially clear.
Definition: Threading.h:27
clang::clangd::preventThreadStarvationInTests
void preventThreadStarvationInTests()
Definition: Threading.cpp:142
clang::clangd::Deadline::operator==
bool operator==(const Deadline &Other) const
Definition: Threading.h:73
clang::clangd::ThreadPriority
ThreadPriority
Definition: Threading.h:121
Context.h
clang::clangd::Semaphore
Limits the number of threads that can acquire the lock at the same time.
Definition: Threading.h:41
clang::clangd::runAsync
std::future< T > runAsync(llvm::unique_function< T()> Action)
Runs Action asynchronously with a new std::thread.
Definition: TUScheduler.h:228
clang::clangd::AsyncTaskRunner::wait
void wait() const
Definition: Threading.h:110
clang::clangd::ThreadPriority::Normal
clang::clangd::Deadline::infinity
static Deadline infinity()
Definition: Threading.h:63
Action
llvm::unique_function< void()> Action
Definition: TUScheduler.cpp:221
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
D
const Decl * D
Definition: XRefs.cpp:79
clang::clangd::Notification::notify
void notify()
Definition: Threading.cpp:19
clang::clangd::AsyncTaskRunner
Runs tasks on separate (detached) threads and wait for all tasks to finish.
Definition: Threading.h:105
clang::clangd::setCurrentThreadPriority
void setCurrentThreadPriority(ThreadPriority Priority)
Definition: Threading.cpp:118
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::clangd::Deadline::zero
static Deadline zero()
Definition: Threading.h:62
clang::clangd::timeoutSeconds
Deadline timeoutSeconds(llvm::Optional< double > Seconds)
Makes a deadline from a timeout in seconds. None means wait forever.
Definition: Threading.cpp:99
clang::clangd::Deadline::Deadline
Deadline(std::chrono::steady_clock::time_point Time)
Definition: Threading.h:60
Function.h
clang::clangd::Deadline
A point in time we can wait for.
Definition: Threading.h:58
clang::clangd::Deadline::expired
bool expired() const
Definition: Threading.h:69
clang::clangd::Notification::wait
void wait() const
Definition: Threading.cpp:27
clang::clangd::Deadline::time
std::chrono::steady_clock::time_point time() const
Definition: Threading.h:65
clang::clangd::ThreadPriority::Low