Replace singleton names with type tags
[folly.git] / folly / wangle / concurrent / CPUThreadPoolExecutor.h
1 /*
2  * Copyright 2014 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   explicit 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
35   explicit CPUThreadPoolExecutor(
36       size_t numThreads,
37       std::shared_ptr<ThreadFactory> threadFactory);
38
39   explicit CPUThreadPoolExecutor(
40       size_t numThreads,
41       uint32_t numPriorities,
42       std::shared_ptr<ThreadFactory> threadFactory =
43           std::make_shared<NamedThreadFactory>("CPUThreadPool"));
44
45   ~CPUThreadPoolExecutor();
46
47   void add(Func func) override;
48   void add(
49       Func func,
50       std::chrono::milliseconds expiration,
51       Func expireCallback = nullptr) override;
52
53   void add(Func func, uint32_t priority);
54   void add(
55       Func func,
56       uint32_t priority,
57       std::chrono::milliseconds expiration,
58       Func expireCallback = nullptr);
59
60   uint32_t getNumPriorities() const;
61
62   struct CPUTask : public ThreadPoolExecutor::Task {
63     // Must be noexcept move constructible so it can be used in MPMCQueue
64     explicit CPUTask(
65         Func&& f,
66         std::chrono::milliseconds expiration,
67         Func&& expireCallback)
68       : Task(std::move(f), expiration, std::move(expireCallback)),
69         poison(false) {}
70     CPUTask()
71       : Task(nullptr, std::chrono::milliseconds(0), nullptr),
72         poison(true) {}
73     CPUTask(CPUTask&& o) noexcept : Task(std::move(o)), poison(o.poison) {}
74     CPUTask(const CPUTask&) = default;
75     CPUTask& operator=(const CPUTask&) = default;
76     bool poison;
77   };
78
79   static const size_t kDefaultMaxQueueSize;
80   static const size_t kDefaultNumPriorities;
81
82  protected:
83   BlockingQueue<CPUTask>* getTaskQueue();
84
85  private:
86   void threadRun(ThreadPtr thread) override;
87   void stopThreads(size_t n) override;
88   uint64_t getPendingTaskCount() override;
89
90   std::unique_ptr<BlockingQueue<CPUTask>> taskQueue_;
91   std::atomic<ssize_t> threadsToStop_{0};
92 };
93
94 }} // folly::wangle