Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 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 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 13 | #include "Context.h" |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 14 | #include "Function.h" |
Sam McCall | c901c5d | 2018-02-19 09:56:28 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Twine.h" |
Ilya Biryukov | 7e5ee26 | 2018-02-08 07:37:35 +0000 | [diff] [blame] | 16 | #include <cassert> |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 17 | #include <condition_variable> |
Ilya Biryukov | 7e5ee26 | 2018-02-08 07:37:35 +0000 | [diff] [blame] | 18 | #include <memory> |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 19 | #include <mutex> |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
| 22 | namespace clang { |
| 23 | namespace clangd { |
Ilya Biryukov | 7e5ee26 | 2018-02-08 07:37:35 +0000 | [diff] [blame] | 24 | |
Sam McCall | 568e17f | 2018-02-22 13:11:12 +0000 | [diff] [blame] | 25 | /// A threadsafe flag that is initially clear. |
| 26 | class Notification { |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 27 | public: |
Sam McCall | 568e17f | 2018-02-22 13:11:12 +0000 | [diff] [blame] | 28 | // Sets the flag. No-op if already set. |
| 29 | void notify(); |
| 30 | // Blocks until flag is set. |
| 31 | void wait() const; |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 32 | |
| 33 | private: |
Sam McCall | 568e17f | 2018-02-22 13:11:12 +0000 | [diff] [blame] | 34 | bool Notified = false; |
| 35 | mutable std::condition_variable CV; |
| 36 | mutable std::mutex Mu; |
Ilya Biryukov | 7e5ee26 | 2018-02-08 07:37:35 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | /// Limits the number of threads that can acquire the lock at the same time. |
| 40 | class Semaphore { |
| 41 | public: |
| 42 | Semaphore(std::size_t MaxLocks); |
| 43 | |
| 44 | void lock(); |
| 45 | void unlock(); |
| 46 | |
| 47 | private: |
| 48 | std::mutex Mutex; |
| 49 | std::condition_variable SlotsChanged; |
| 50 | std::size_t FreeSlots; |
| 51 | }; |
| 52 | |
Sam McCall | d1e0deb | 2018-03-02 08:56:37 +0000 | [diff] [blame] | 53 | /// A point in time we can wait for. |
| 54 | /// Can be zero (don't wait) or infinity (wait forever). |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 55 | /// (Not time_point::max(), because many std::chrono implementations overflow). |
Sam McCall | d1e0deb | 2018-03-02 08:56:37 +0000 | [diff] [blame] | 56 | class Deadline { |
| 57 | public: |
| 58 | Deadline(std::chrono::steady_clock::time_point Time) |
| 59 | : Type(Finite), Time(Time) {} |
| 60 | static Deadline zero() { return Deadline(Zero); } |
| 61 | static Deadline infinity() { return Deadline(Infinite); } |
| 62 | |
| 63 | std::chrono::steady_clock::time_point time() const { |
| 64 | assert(Type == Finite); |
| 65 | return Time; |
| 66 | } |
| 67 | bool expired() const { |
| 68 | return (Type == Zero) || |
| 69 | (Type == Finite && Time < std::chrono::steady_clock::now()); |
| 70 | } |
| 71 | bool operator==(const Deadline &Other) const { |
| 72 | return (Type == Other.Type) && (Type != Finite || Time == Other.Time); |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | enum Type { Zero, Infinite, Finite }; |
| 77 | |
| 78 | Deadline(enum Type Type) : Type(Type) {} |
| 79 | enum Type Type; |
| 80 | std::chrono::steady_clock::time_point Time; |
| 81 | }; |
| 82 | |
| 83 | /// Makes a deadline from a timeout in seconds. None means wait forever. |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 84 | Deadline timeoutSeconds(llvm::Optional<double> Seconds); |
Sam McCall | d1e0deb | 2018-03-02 08:56:37 +0000 | [diff] [blame] | 85 | /// Wait once on CV for the specified duration. |
| 86 | void wait(std::unique_lock<std::mutex> &Lock, std::condition_variable &CV, |
| 87 | Deadline D); |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 88 | /// Waits on a condition variable until F() is true or D expires. |
| 89 | template <typename Func> |
| 90 | LLVM_NODISCARD bool wait(std::unique_lock<std::mutex> &Lock, |
| 91 | std::condition_variable &CV, Deadline D, Func F) { |
Sam McCall | d1e0deb | 2018-03-02 08:56:37 +0000 | [diff] [blame] | 92 | while (!F()) { |
| 93 | if (D.expired()) |
| 94 | return false; |
| 95 | wait(Lock, CV, D); |
| 96 | } |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 97 | return true; |
| 98 | } |
| 99 | |
Ilya Biryukov | 7e5ee26 | 2018-02-08 07:37:35 +0000 | [diff] [blame] | 100 | /// Runs tasks on separate (detached) threads and wait for all tasks to finish. |
| 101 | /// Objects that need to spawn threads can own an AsyncTaskRunner to ensure they |
| 102 | /// all complete on destruction. |
| 103 | class AsyncTaskRunner { |
| 104 | public: |
| 105 | /// Destructor waits for all pending tasks to finish. |
| 106 | ~AsyncTaskRunner(); |
| 107 | |
Sam McCall | d1e0deb | 2018-03-02 08:56:37 +0000 | [diff] [blame] | 108 | void wait() const { (void)wait(Deadline::infinity()); } |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 109 | LLVM_NODISCARD bool wait(Deadline D) const; |
Sam McCall | c901c5d | 2018-02-19 09:56:28 +0000 | [diff] [blame] | 110 | // The name is used for tracing and debugging (e.g. to name a spawned thread). |
Ilya Biryukov | 1d5e640 | 2018-07-26 16:13:52 +0000 | [diff] [blame] | 111 | void runAsync(const llvm::Twine &Name, llvm::unique_function<void()> Action); |
Ilya Biryukov | 7e5ee26 | 2018-02-08 07:37:35 +0000 | [diff] [blame] | 112 | |
| 113 | private: |
Sam McCall | 0bb24cd | 2018-02-13 08:59:23 +0000 | [diff] [blame] | 114 | mutable std::mutex Mutex; |
| 115 | mutable std::condition_variable TasksReachedZero; |
Ilya Biryukov | 7e5ee26 | 2018-02-08 07:37:35 +0000 | [diff] [blame] | 116 | std::size_t InFlightTasks = 0; |
Ilya Biryukov | 75f1dd9 | 2018-01-31 08:51:16 +0000 | [diff] [blame] | 117 | }; |
| 118 | } // namespace clangd |
| 119 | } // namespace clang |
| 120 | #endif |