Commit e8e6817c authored by Michael Park's avatar Michael Park Committed by Facebook Github Bot

Strengthened `const`-ness of `ThreadPoolExecutor`, and its derived classes.

Summary: It was observed in D14238146 that `ThreadPoolExecutor::numThreads()` is not marked `const`, and it seems that it really should be. This patch marks that function `const` as well as others in this class that seems they should be `const`.

Reviewed By: yfeldblum

Differential Revision: D14681960

fbshipit-source-id: f274b9788ae7e27d6bec22aec2818abeb6441531
parent 74f36602
......@@ -215,7 +215,7 @@ void CPUThreadPoolExecutor::stopThreads(size_t n) {
}
// threadListLock_ is read (or write) locked.
size_t CPUThreadPoolExecutor::getPendingTaskCountImpl() {
size_t CPUThreadPoolExecutor::getPendingTaskCountImpl() const {
return taskQueue_->size();
}
......
......@@ -141,7 +141,7 @@ class CPUThreadPoolExecutor : public ThreadPoolExecutor {
private:
void threadRun(ThreadPtr thread) override;
void stopThreads(size_t n) override;
size_t getPendingTaskCountImpl() override;
size_t getPendingTaskCountImpl() const override;
bool tryDecrToStop();
bool taskShouldStop(folly::Optional<CPUTask>&);
......
......@@ -219,7 +219,7 @@ void IOThreadPoolExecutor::stopThreads(size_t n) {
}
// threadListLock_ is readlocked
size_t IOThreadPoolExecutor::getPendingTaskCountImpl() {
size_t IOThreadPoolExecutor::getPendingTaskCountImpl() const {
size_t count = 0;
for (const auto& thread : threadList_.get()) {
auto ioThread = std::static_pointer_cast<IOThread>(thread);
......
......@@ -87,7 +87,7 @@ class IOThreadPoolExecutor : public ThreadPoolExecutor, public IOExecutor {
std::shared_ptr<IOThread> pickThread();
void threadRun(ThreadPtr thread) override;
void stopThreads(size_t n) override;
size_t getPendingTaskCountImpl() override;
size_t getPendingTaskCountImpl() const override;
std::atomic<size_t> nextThread_;
folly::ThreadLocal<std::shared_ptr<IOThread>> thisThread_;
......
......@@ -113,11 +113,11 @@ void ThreadPoolExecutor::runTask(const ThreadPtr& thread, Task&& task) {
});
}
size_t ThreadPoolExecutor::numThreads() {
size_t ThreadPoolExecutor::numThreads() const {
return maxThreads_.load(std::memory_order_relaxed);
}
size_t ThreadPoolExecutor::numActiveThreads() {
size_t ThreadPoolExecutor::numActiveThreads() const {
return activeThreads_.load(std::memory_order_relaxed);
}
......@@ -254,7 +254,7 @@ void ThreadPoolExecutor::withAll(FunctionRef<void(ThreadPoolExecutor&)> f) {
});
}
ThreadPoolExecutor::PoolStats ThreadPoolExecutor::getPoolStats() {
ThreadPoolExecutor::PoolStats ThreadPoolExecutor::getPoolStats() const {
const auto now = std::chrono::steady_clock::now();
SharedMutex::ReadHolder r{&threadListLock_};
ThreadPoolExecutor::PoolStats stats;
......@@ -279,12 +279,12 @@ ThreadPoolExecutor::PoolStats ThreadPoolExecutor::getPoolStats() {
return stats;
}
size_t ThreadPoolExecutor::getPendingTaskCount() {
size_t ThreadPoolExecutor::getPendingTaskCount() const {
SharedMutex::ReadHolder r{&threadListLock_};
return getPendingTaskCountImpl();
}
std::string ThreadPoolExecutor::getName() {
std::string ThreadPoolExecutor::getName() const {
auto ntf = dynamic_cast<NamedThreadFactory*>(threadFactory_.get());
if (ntf == nullptr) {
return folly::demangle(typeid(*this).name()).toStdString();
......
......@@ -71,16 +71,16 @@ class ThreadPoolExecutor : public DefaultKeepAliveExecutor {
threadFactory_ = std::move(threadFactory);
}
std::shared_ptr<ThreadFactory> getThreadFactory() {
std::shared_ptr<ThreadFactory> getThreadFactory() const {
return threadFactory_;
}
size_t numThreads();
size_t numThreads() const;
void setNumThreads(size_t numThreads);
// Return actual number of active threads -- this could be different from
// numThreads() due to ThreadPoolExecutor's dynamic behavior.
size_t numActiveThreads();
size_t numActiveThreads() const;
/*
* stop() is best effort - there is no guarantee that unexecuted tasks won't
......@@ -109,9 +109,9 @@ class ThreadPoolExecutor : public DefaultKeepAliveExecutor {
std::chrono::nanoseconds maxIdleTime;
};
PoolStats getPoolStats();
size_t getPendingTaskCount();
std::string getName();
PoolStats getPoolStats() const;
size_t getPendingTaskCount() const;
std::string getName() const;
struct TaskStats {
TaskStats() : expired(false), waitTime(0), runTime(0) {}
......@@ -224,7 +224,7 @@ class ThreadPoolExecutor : public DefaultKeepAliveExecutor {
}
// Prerequisite: threadListLock_ readlocked or writelocked
virtual size_t getPendingTaskCountImpl() = 0;
virtual size_t getPendingTaskCountImpl() const = 0;
class ThreadList {
public:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment