IOThreadPoolExecutor.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #pragma once
  17. #include <atomic>
  18. #include <folly/executors/IOExecutor.h>
  19. #include <folly/executors/ThreadPoolExecutor.h>
  20. #include <folly/io/async/EventBaseManager.h>
  21. namespace folly {
  22. /**
  23. * A Thread Pool for IO bound tasks
  24. *
  25. * @note Uses event_fd for notification, and waking an epoll loop.
  26. * There is one queue (NotificationQueue specifically) per thread/epoll.
  27. * If the thread is already running and not waiting on epoll,
  28. * we don't make any additional syscalls to wake up the loop,
  29. * just put the new task in the queue.
  30. * If any thread has been waiting for more than a few seconds,
  31. * its stack is madvised away. Currently however tasks are scheduled round
  32. * robin on the queues, so unless there is no work going on,
  33. * this isn't very effective.
  34. * Since there is one queue per thread, there is hardly any contention
  35. * on the queues - so a simple spinlock around an std::deque is used for
  36. * the tasks. There is no max queue size.
  37. * By default, there is one thread per core - it usually doesn't make sense to
  38. * have more IO threads than this, assuming they don't block.
  39. *
  40. * @note ::getEventBase() will return an EventBase you can schedule IO work on
  41. * directly, chosen round-robin.
  42. *
  43. * @note N.B. For this thread pool, stop() behaves like join() because
  44. * outstanding tasks belong to the event base and will be executed upon its
  45. * destruction.
  46. */
  47. class IOThreadPoolExecutor : public ThreadPoolExecutor, public IOExecutor {
  48. public:
  49. explicit IOThreadPoolExecutor(
  50. size_t numThreads,
  51. std::shared_ptr<ThreadFactory> threadFactory =
  52. std::make_shared<NamedThreadFactory>("IOThreadPool"),
  53. folly::EventBaseManager* ebm = folly::EventBaseManager::get(),
  54. bool waitForAll = false);
  55. ~IOThreadPoolExecutor() override;
  56. void add(Func func) override;
  57. void add(
  58. Func func,
  59. std::chrono::milliseconds expiration,
  60. Func expireCallback = nullptr) override;
  61. folly::EventBase* getEventBase() override;
  62. static folly::EventBase* getEventBase(ThreadPoolExecutor::ThreadHandle*);
  63. folly::EventBaseManager* getEventBaseManager();
  64. private:
  65. struct alignas(hardware_destructive_interference_size) IOThread
  66. : public Thread {
  67. IOThread(IOThreadPoolExecutor* pool)
  68. : Thread(pool), shouldRun(true), pendingTasks(0) {}
  69. std::atomic<bool> shouldRun;
  70. std::atomic<size_t> pendingTasks;
  71. folly::EventBase* eventBase;
  72. std::mutex eventBaseShutdownMutex_;
  73. };
  74. ThreadPtr makeThread() override;
  75. std::shared_ptr<IOThread> pickThread();
  76. void threadRun(ThreadPtr thread) override;
  77. void stopThreads(size_t n) override;
  78. size_t getPendingTaskCountImpl() override;
  79. std::atomic<size_t> nextThread_;
  80. folly::ThreadLocal<std::shared_ptr<IOThread>> thisThread_;
  81. folly::EventBaseManager* eventBaseManager_;
  82. };
  83. } // namespace folly