proxygen
CPUThreadPoolExecutor.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present 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 
20 
21 DECLARE_bool(dynamic_cputhreadpoolexecutor);
22 
23 namespace folly {
24 
64  public:
65  struct CPUTask;
66 
68  size_t numThreads,
69  std::unique_ptr<BlockingQueue<CPUTask>> taskQueue,
70  std::shared_ptr<ThreadFactory> threadFactory =
71  std::make_shared<NamedThreadFactory>("CPUThreadPool"));
72 
74  std::pair<size_t, size_t> numThreads,
75  std::unique_ptr<BlockingQueue<CPUTask>> taskQueue,
76  std::shared_ptr<ThreadFactory> threadFactory =
77  std::make_shared<NamedThreadFactory>("CPUThreadPool"));
78 
79  explicit CPUThreadPoolExecutor(size_t numThreads);
80 
82  size_t numThreads,
83  std::shared_ptr<ThreadFactory> threadFactory);
84 
86  std::pair<size_t, size_t> numThreads,
87  std::shared_ptr<ThreadFactory> threadFactory);
88 
90  size_t numThreads,
91  int8_t numPriorities,
92  std::shared_ptr<ThreadFactory> threadFactory =
93  std::make_shared<NamedThreadFactory>("CPUThreadPool"));
94 
96  size_t numThreads,
97  int8_t numPriorities,
98  size_t maxQueueSize,
99  std::shared_ptr<ThreadFactory> threadFactory =
100  std::make_shared<NamedThreadFactory>("CPUThreadPool"));
101 
102  ~CPUThreadPoolExecutor() override;
103 
104  void add(Func func) override;
105  void add(
106  Func func,
107  std::chrono::milliseconds expiration,
108  Func expireCallback = nullptr) override;
109 
110  void addWithPriority(Func func, int8_t priority) override;
111  void add(
112  Func func,
113  int8_t priority,
114  std::chrono::milliseconds expiration,
115  Func expireCallback = nullptr);
116 
117  size_t getTaskQueueSize() const;
118 
119  uint8_t getNumPriorities() const override;
120 
122  // Must be noexcept move constructible so it can be used in MPMCQueue
123 
124  explicit CPUTask(
125  Func&& f,
126  std::chrono::milliseconds expiration,
127  Func&& expireCallback)
128  : Task(std::move(f), expiration, std::move(expireCallback)),
129  poison(false) {}
131  : Task(nullptr, std::chrono::milliseconds(0), nullptr), poison(true) {}
132 
133  bool poison;
134  };
135 
136  static const size_t kDefaultMaxQueueSize;
137 
138  protected:
140 
141  private:
142  void threadRun(ThreadPtr thread) override;
143  void stopThreads(size_t n) override;
144  size_t getPendingTaskCountImpl() override;
145 
146  bool tryDecrToStop();
148 
149  std::unique_ptr<BlockingQueue<CPUTask>> taskQueue_;
150  std::atomic<ssize_t> threadsToStop_{0};
151 };
152 
153 } // namespace folly
chrono
Definition: CMakeCache.txt:563
CPUTask(Func &&f, std::chrono::milliseconds expiration, Func &&expireCallback)
DECLARE_bool(dynamic_cputhreadpoolexecutor)
auto f
void addWithPriority(Func func, int8_t priority) override
std::unique_ptr< BlockingQueue< CPUTask > > taskQueue_
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
void stopThreads(size_t n) override
uint8_t getNumPriorities() const override
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
std::shared_ptr< Thread > ThreadPtr
bool taskShouldStop(folly::Optional< CPUTask > &)
#define nullptr
Definition: http_parser.c:41
static void expiration()
BlockingQueue< CPUTask > * getTaskQueue()
void threadRun(ThreadPtr thread) override
CPUThreadPoolExecutor(size_t numThreads, std::unique_ptr< BlockingQueue< CPUTask >> taskQueue, std::shared_ptr< ThreadFactory > threadFactory=std::make_shared< NamedThreadFactory >("CPUThreadPool"))
static const size_t kDefaultMaxQueueSize
std::atomic< ssize_t > threadsToStop_