2 * Copyright 2015 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/wangle/concurrent/CPUThreadPoolExecutor.h>
18 #include <folly/wangle/concurrent/PriorityLifoSemMPMCQueue.h>
20 namespace folly { namespace wangle {
22 const size_t CPUThreadPoolExecutor::kDefaultMaxQueueSize = 1 << 14;
24 CPUThreadPoolExecutor::CPUThreadPoolExecutor(
26 std::unique_ptr<BlockingQueue<CPUTask>> taskQueue,
27 std::shared_ptr<ThreadFactory> threadFactory)
28 : ThreadPoolExecutor(numThreads, std::move(threadFactory)),
29 taskQueue_(std::move(taskQueue)) {
30 addThreads(numThreads);
31 CHECK(threadList_.get().size() == numThreads);
34 CPUThreadPoolExecutor::CPUThreadPoolExecutor(
36 std::shared_ptr<ThreadFactory> threadFactory)
37 : CPUThreadPoolExecutor(
39 folly::make_unique<LifoSemMPMCQueue<CPUTask>>(
40 CPUThreadPoolExecutor::kDefaultMaxQueueSize),
41 std::move(threadFactory)) {}
43 CPUThreadPoolExecutor::CPUThreadPoolExecutor(size_t numThreads)
44 : CPUThreadPoolExecutor(
46 std::make_shared<NamedThreadFactory>("CPUThreadPool")) {}
48 CPUThreadPoolExecutor::CPUThreadPoolExecutor(
51 std::shared_ptr<ThreadFactory> threadFactory)
52 : CPUThreadPoolExecutor(
54 folly::make_unique<PriorityLifoSemMPMCQueue<CPUTask>>(
56 CPUThreadPoolExecutor::kDefaultMaxQueueSize),
57 std::move(threadFactory)) {}
59 CPUThreadPoolExecutor::CPUThreadPoolExecutor(
63 std::shared_ptr<ThreadFactory> threadFactory)
64 : CPUThreadPoolExecutor(
66 folly::make_unique<PriorityLifoSemMPMCQueue<CPUTask>>(
69 std::move(threadFactory)) {}
71 CPUThreadPoolExecutor::~CPUThreadPoolExecutor() {
73 CHECK(threadsToStop_ == 0);
76 void CPUThreadPoolExecutor::add(Func func) {
77 add(std::move(func), std::chrono::milliseconds(0));
80 void CPUThreadPoolExecutor::add(
82 std::chrono::milliseconds expiration,
83 Func expireCallback) {
84 // TODO handle enqueue failure, here and in other add() callsites
86 CPUTask(std::move(func), expiration, std::move(expireCallback)));
89 void CPUThreadPoolExecutor::addWithPriority(Func func, int8_t priority) {
90 add(std::move(func), priority, std::chrono::milliseconds(0));
93 void CPUThreadPoolExecutor::add(
96 std::chrono::milliseconds expiration,
97 Func expireCallback) {
98 CHECK(getNumPriorities() > 0);
99 taskQueue_->addWithPriority(
100 CPUTask(std::move(func), expiration, std::move(expireCallback)),
104 uint8_t CPUThreadPoolExecutor::getNumPriorities() const {
105 return taskQueue_->getNumPriorities();
108 BlockingQueue<CPUThreadPoolExecutor::CPUTask>*
109 CPUThreadPoolExecutor::getTaskQueue() {
110 return taskQueue_.get();
113 void CPUThreadPoolExecutor::threadRun(std::shared_ptr<Thread> thread) {
114 thread->startupBaton.post();
116 auto task = taskQueue_->take();
117 if (UNLIKELY(task.poison)) {
118 CHECK(threadsToStop_-- > 0);
119 for (auto& o : observers_) {
120 o->threadStopped(thread.get());
123 stoppedThreads_.add(thread);
126 runTask(thread, std::move(task));
129 if (UNLIKELY(threadsToStop_ > 0 && !isJoin_)) {
130 if (--threadsToStop_ >= 0) {
131 stoppedThreads_.add(thread);
140 void CPUThreadPoolExecutor::stopThreads(size_t n) {
141 CHECK(stoppedThreads_.size() == 0);
143 for (size_t i = 0; i < n; i++) {
144 taskQueue_->addWithPriority(CPUTask(), Executor::LO_PRI);
148 uint64_t CPUThreadPoolExecutor::getPendingTaskCount() {
149 return taskQueue_->size();