From cefce84f5eb95884344c3f97fc710d4ac0626359 Mon Sep 17 00:00:00 2001 From: Alexander Neonxp Kiryukhin Date: Fri, 16 May 2025 23:32:08 +0300 Subject: =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=BA=D0=BE=D0=BD=D1=84=D0=B8=D0=B3=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zsh/theme/gitstatus/src/thread_pool.h | 74 +++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 zsh/theme/gitstatus/src/thread_pool.h (limited to 'zsh/theme/gitstatus/src/thread_pool.h') diff --git a/zsh/theme/gitstatus/src/thread_pool.h b/zsh/theme/gitstatus/src/thread_pool.h new file mode 100644 index 0000000..1e39b91 --- /dev/null +++ b/zsh/theme/gitstatus/src/thread_pool.h @@ -0,0 +1,74 @@ +#ifndef ROMKATV_GITSTATUS_THREAD_POOL_H_ +#define ROMKATV_GITSTATUS_THREAD_POOL_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "time.h" + +namespace gitstatus { + +class ThreadPool { + public: + explicit ThreadPool(size_t num_threads); + ThreadPool(ThreadPool&&) = delete; + + // Waits for the currently running functions to finish. + // Does NOT wait for the queue of functions to drain. + // If you want the latter, call Wait() manually. + ~ThreadPool(); + + // Runs `f` on one of the threads at or after time `t`. Can be called + // from any thread. Can be called concurrently. + // + // Does not block. + void Schedule(Time t, std::function f); + + void Schedule(std::function f) { Schedule(Clock::now(), std::move(f)); } + + // Blocks until the work queue is empty and there are no currently + // running functions. + void Wait(); + + size_t num_threads() const { return threads_.size(); } + + private: + struct Work { + bool operator<(const Work& w) const { return std::tie(w.t, w.idx) < std::tie(t, idx); } + Time t; + int64_t idx; + mutable std::function f; + }; + + void Loop(size_t tid); + + int64_t last_idx_ = 0; + int64_t num_inflight_; + bool exit_ = false; + // Do we have a thread waiting on sleeper_cv_? + bool have_sleeper_ = false; + std::mutex mutex_; + // Any number of threads can wait on this condvar. Always without a timeout. + std::condition_variable cv_; + // At most one thread can wait on this condvar at a time. Always with a timeout. + std::condition_variable sleeper_cv_; + // Signalled when the work queue is empty and there is nothing inflight. + std::condition_variable idle_cv_; + std::priority_queue work_; + std::vector threads_; +}; + +void InitGlobalThreadPool(size_t num_threads); + +ThreadPool* GlobalThreadPool(); + +} // namespace gitstatus + +#endif // ROMKATV_GITSTATUS_THREAD_POOL_H_ -- cgit v1.2.3