2 * Copyright 2015 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.
19 #include <folly/io/async/EventBaseManager.h>
20 #include <folly/wangle/concurrent/IOExecutor.h>
21 #include <folly/wangle/concurrent/ThreadPoolExecutor.h>
23 namespace folly { namespace wangle {
25 // N.B. For this thread pool, stop() behaves like join() because outstanding
26 // tasks belong to the event base and will be executed upon its destruction.
27 class IOThreadPoolExecutor : public ThreadPoolExecutor, public IOExecutor {
29 explicit IOThreadPoolExecutor(
31 std::shared_ptr<ThreadFactory> threadFactory =
32 std::make_shared<NamedThreadFactory>("IOThreadPool"),
33 EventBaseManager* ebm = folly::EventBaseManager::get());
35 ~IOThreadPoolExecutor();
37 void add(Func func) override;
40 std::chrono::milliseconds expiration,
41 Func expireCallback = nullptr) override;
43 EventBase* getEventBase() override;
45 static EventBase* getEventBase(ThreadPoolExecutor::ThreadHandle*);
48 struct FOLLY_ALIGN_TO_AVOID_FALSE_SHARING IOThread : public Thread {
49 IOThread(IOThreadPoolExecutor* pool)
53 std::atomic<bool> shouldRun;
54 std::atomic<size_t> pendingTasks;
58 ThreadPtr makeThread() override;
59 std::shared_ptr<IOThread> pickThread();
60 void threadRun(ThreadPtr thread) override;
61 void stopThreads(size_t n) override;
62 uint64_t getPendingTaskCount() override;
65 ThreadLocal<std::shared_ptr<IOThread>> thisThread_;
66 EventBaseManager* eventBaseManager_;