removing non-existing file from the build
[folly.git] / folly / wangle / concurrent / IOThreadPoolExecutor.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/io/async/EventBaseManager.h>
20 #include <folly/wangle/concurrent/IOExecutor.h>
21 #include <folly/wangle/concurrent/ThreadPoolExecutor.h>
22
23 namespace folly { namespace wangle {
24
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 {
28  public:
29   explicit IOThreadPoolExecutor(
30       size_t numThreads,
31       std::shared_ptr<ThreadFactory> threadFactory =
32           std::make_shared<NamedThreadFactory>("IOThreadPool"),
33       EventBaseManager* ebm = folly::EventBaseManager::get());
34
35   ~IOThreadPoolExecutor();
36
37   void add(Func func) override;
38   void add(
39       Func func,
40       std::chrono::milliseconds expiration,
41       Func expireCallback = nullptr) override;
42
43   EventBase* getEventBase() override;
44
45   static EventBase* getEventBase(ThreadPoolExecutor::ThreadHandle*);
46
47   EventBaseManager* getEventBaseManager();
48
49  private:
50   struct FOLLY_ALIGN_TO_AVOID_FALSE_SHARING IOThread : public Thread {
51     IOThread(IOThreadPoolExecutor* pool)
52       : Thread(pool),
53         shouldRun(true),
54         pendingTasks(0) {};
55     std::atomic<bool> shouldRun;
56     std::atomic<size_t> pendingTasks;
57     EventBase* eventBase;
58   };
59
60   ThreadPtr makeThread() override;
61   std::shared_ptr<IOThread> pickThread();
62   void threadRun(ThreadPtr thread) override;
63   void stopThreads(size_t n) override;
64   uint64_t getPendingTaskCount() override;
65
66   size_t nextThread_;
67   ThreadLocal<std::shared_ptr<IOThread>> thisThread_;
68   EventBaseManager* eventBaseManager_;
69 };
70
71 }} // folly::wangle