Skip to content

Commit e03f777

Browse files
authored
Restore threadpool num threads query behavior (#20474)
Differential Revision: D109521669 Pull Request resolved: #20474
1 parent 80b6c34 commit e03f777

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

extension/threadpool/threadpool.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ void child_atfork() {
5252
#endif
5353

5454
ThreadPool::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

5759
size_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

6465
bool 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

7881
void 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

8488
void ThreadPool::run(

extension/threadpool/threadpool.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff 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
/**

0 commit comments

Comments
 (0)