2 * Copyright 2014 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/experimental/wangle/concurrent/CPUThreadPoolExecutor.h>
19 namespace folly { namespace wangle {
21 const size_t CPUThreadPoolExecutor::kDefaultMaxQueueSize = 1 << 18;
23 CPUThreadPoolExecutor::CPUThreadPoolExecutor(
25 std::unique_ptr<BlockingQueue<CPUTask>> taskQueue,
26 std::unique_ptr<ThreadFactory> threadFactory)
27 : ThreadPoolExecutor(numThreads, std::move(threadFactory)),
28 taskQueue_(std::move(taskQueue)) {
29 addThreads(numThreads);
30 CHECK(threadList_.get().size() == numThreads);
33 CPUThreadPoolExecutor::~CPUThreadPoolExecutor() {
35 CHECK(threadsToStop_ == 0);
38 void CPUThreadPoolExecutor::add(Func func) {
39 // TODO handle enqueue failure, here and in other add() callsites
40 taskQueue_->add(CPUTask(std::move(func)));
43 void CPUThreadPoolExecutor::threadRun(std::shared_ptr<Thread> thread) {
45 // TODO expiration / codel
46 auto task = taskQueue_->take();
47 if (UNLIKELY(task.poison)) {
48 CHECK(threadsToStop_-- > 0);
49 stoppedThreads_.add(thread);
52 runTask(thread, std::move(task));
55 if (UNLIKELY(threadsToStop_ > 0 && !isJoin_)) {
56 if (--threadsToStop_ >= 0) {
57 stoppedThreads_.add(thread);
66 void CPUThreadPoolExecutor::stopThreads(size_t n) {
67 CHECK(stoppedThreads_.size() == 0);
69 for (int i = 0; i < n; i++) {
70 taskQueue_->add(CPUTask());
74 uint64_t CPUThreadPoolExecutor::getPendingTaskCount() {
75 return taskQueue_->size();