copy wangle back into folly
[folly.git] / folly / wangle / concurrent / CPUThreadPoolExecutor.h
1 /*
2  * Copyright 2015 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #pragma once
18
19 #include <folly/wangle/concurrent/ThreadPoolExecutor.h>
20
21 namespace folly { namespace wangle {
22
23 class CPUThreadPoolExecutor : public ThreadPoolExecutor {
24  public:
25   struct CPUTask;
26
27   CPUThreadPoolExecutor(
28       size_t numThreads,
29       std::unique_ptr<BlockingQueue<CPUTask>> taskQueue,
30       std::shared_ptr<ThreadFactory> threadFactory =
31           std::make_shared<NamedThreadFactory>("CPUThreadPool"));
32
33   explicit CPUThreadPoolExecutor(size_t numThreads);
34 CPUThreadPoolExecutor(
35       size_t numThreads,
36       std::shared_ptr<ThreadFactory> threadFactory);
37
38   CPUThreadPoolExecutor(
39       size_t numThreads,
40       int8_t numPriorities,
41       std::shared_ptr<ThreadFactory> threadFactory =
42           std::make_shared<NamedThreadFactory>("CPUThreadPool"));
43
44   CPUThreadPoolExecutor(
45       size_t numThreads,
46       int8_t numPriorities,
47       size_t maxQueueSize,
48       std::shared_ptr<ThreadFactory> threadFactory =
49           std::make_shared<NamedThreadFactory>("CPUThreadPool"));
50
51   ~CPUThreadPoolExecutor();
52
53   void add(Func func) override;
54   void add(
55       Func func,
56       std::chrono::milliseconds expiration,
57       Func expireCallback = nullptr) override;
58
59   void addWithPriority(Func func, int8_t priority) override;
60   void add(
61       Func func,
62       int8_t priority,
63       std::chrono::milliseconds expiration,
64       Func expireCallback = nullptr);
65
66   uint8_t getNumPriorities() const override;
67
68   struct CPUTask : public ThreadPoolExecutor::Task {
69     // Must be noexcept move constructible so it can be used in MPMCQueue
70     explicit CPUTask(
71         Func&& f,
72         std::chrono::milliseconds expiration,
73         Func&& expireCallback)
74       : Task(std::move(f), expiration, std::move(expireCallback)),
75         poison(false) {}
76     CPUTask()
77       : Task(nullptr, std::chrono::milliseconds(0), nullptr),
78         poison(true) {}
79     CPUTask(CPUTask&& o) noexcept : Task(std::move(o)), poison(o.poison) {}
80     CPUTask(const CPUTask&) = default;
81     CPUTask& operator=(const CPUTask&) = default;
82     bool poison;
83   };
84
85   static const size_t kDefaultMaxQueueSize;
86
87  protected:
88   BlockingQueue<CPUTask>* getTaskQueue();
89
90  private:
91   void threadRun(ThreadPtr thread) override;
92   void stopThreads(size_t n) override;
93   uint64_t getPendingTaskCount() override;
94
95   std::unique_ptr<BlockingQueue<CPUTask>> taskQueue_;
96   std::atomic<ssize_t> threadsToStop_{0};
97 };
98
99 }} // folly::wangle