File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -52,13 +52,14 @@ void child_atfork() {
5252#endif
5353
5454ThreadPool::ThreadPool (size_t thread_count)
55- : threadpool_(pthreadpool_create(thread_count), pthreadpool_destroy) {}
55+ : threadpool_(pthreadpool_create(thread_count), pthreadpool_destroy),
56+ thread_count_ (
57+ threadpool_ ? pthreadpool_get_threads_count(threadpool_.get()) : 0) {}
5658
5759size_t ThreadPool::get_thread_count () const {
5860 std::lock_guard<std::mutex> lock{mutex_};
5961
60- ET_CHECK_MSG (threadpool_.get (), " Invalid threadpool!" );
61- return pthreadpool_get_threads_count (threadpool_.get ());
62+ return thread_count_;
6263}
6364
6465bool ThreadPool::_unsafe_reset_threadpool (uint32_t new_thread_count) {
@@ -72,13 +73,16 @@ bool ThreadPool::_unsafe_reset_threadpool(uint32_t new_thread_count) {
7273 std::lock_guard<std::mutex> lock{mutex_};
7374
7475 threadpool_.reset (pthreadpool_create (new_thread_count));
76+ thread_count_ =
77+ threadpool_ ? pthreadpool_get_threads_count (threadpool_.get ()) : 0 ;
7578 return true ;
7679}
7780
7881void ThreadPool::_unsafe_destroy_threadpool () {
7982 std::lock_guard<std::mutex> lock{mutex_};
8083 ET_LOG (Info, " Destroying threadpool." );
8184 threadpool_.reset ();
85+ thread_count_ = 0 ;
8286}
8387
8488void ThreadPool::run (
Original file line number Diff line number Diff line change @@ -50,6 +50,10 @@ class ThreadPool final {
5050 ThreadPool (ThreadPool&&) = delete ;
5151 ThreadPool& operator =(ThreadPool&&) = delete ;
5252
53+ /* *
54+ * Returns the number of threads in the threadpool. This number does not
55+ * reflect temporary lowering via UseNThreadsThreadPoolGuard.
56+ */
5357 size_t get_thread_count () const ;
5458
5559 /* *
@@ -97,6 +101,8 @@ class ThreadPool final {
97101 // which case this mutex will be useful. Otherwise remove it.
98102 mutable std::mutex mutex_;
99103 std::unique_ptr<pthreadpool, decltype (&pthreadpool_destroy)> threadpool_;
104+ // The number of threads in the threadpool.
105+ size_t thread_count_;
100106};
101107
102108/* *
You can’t perform that action at this time.
0 commit comments