proxygen
IOThreadPoolExecutor.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 
19 #include <atomic>
20 
24 
25 namespace folly {
26 
53  public:
54  explicit IOThreadPoolExecutor(
55  size_t numThreads,
56  std::shared_ptr<ThreadFactory> threadFactory =
57  std::make_shared<NamedThreadFactory>("IOThreadPool"),
59  bool waitForAll = false);
60 
61  ~IOThreadPoolExecutor() override;
62 
63  void add(Func func) override;
64  void add(
65  Func func,
66  std::chrono::milliseconds expiration,
67  Func expireCallback = nullptr) override;
68 
69  folly::EventBase* getEventBase() override;
70 
72 
74 
75  private:
76  struct alignas(hardware_destructive_interference_size) IOThread
77  : public Thread {
79  : Thread(pool), shouldRun(true), pendingTasks(0) {}
80  std::atomic<bool> shouldRun;
81  std::atomic<size_t> pendingTasks;
84  };
85 
86  ThreadPtr makeThread() override;
87  std::shared_ptr<IOThread> pickThread();
88  void threadRun(ThreadPtr thread) override;
89  void stopThreads(size_t n) override;
90  size_t getPendingTaskCountImpl() override;
91 
92  std::atomic<size_t> nextThread_;
95 };
96 
97 } // namespace folly
std::atomic< size_t > nextThread_
folly::EventBaseManager * getEventBaseManager()
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
std::shared_ptr< Thread > ThreadPtr
static void expiration()
static EventBaseManager * get()
void threadRun(ThreadPtr thread) override
void add(Func func) override
folly::EventBaseManager * eventBaseManager_
folly::EventBase * getEventBase() override
std::mutex mutex
folly::ThreadLocal< std::shared_ptr< IOThread > > thisThread_
IOThreadPoolExecutor(size_t numThreads, std::shared_ptr< ThreadFactory > threadFactory=std::make_shared< NamedThreadFactory >("IOThreadPool"), folly::EventBaseManager *ebm=folly::EventBaseManager::get(), bool waitForAll=false)
IOThread(IOThreadPoolExecutor *pool)
void stopThreads(size_t n) override
std::shared_ptr< IOThread > pickThread()