GlobalExecutor.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include <memory>
  17. #include <thread>
  18. #include <folly/Function.h>
  19. #include <folly/SharedMutex.h>
  20. #include <folly/Singleton.h>
  21. #include <folly/executors/IOExecutor.h>
  22. #include <folly/executors/IOThreadPoolExecutor.h>
  23. #include <folly/executors/InlineExecutor.h>
  24. using namespace folly;
  25. namespace {
  26. template <class ExecutorBase>
  27. class GlobalExecutor {
  28. public:
  29. explicit GlobalExecutor(
  30. Function<std::unique_ptr<ExecutorBase>()> constructDefault)
  31. : constructDefault_(std::move(constructDefault)) {}
  32. std::shared_ptr<ExecutorBase> get() {
  33. {
  34. SharedMutex::ReadHolder guard(mutex_);
  35. if (auto executor = executor_.lock()) {
  36. return executor; // Fast path.
  37. }
  38. }
  39. SharedMutex::WriteHolder guard(mutex_);
  40. if (auto executor = executor_.lock()) {
  41. return executor;
  42. }
  43. if (!defaultExecutor_) {
  44. defaultExecutor_ = constructDefault_();
  45. }
  46. return defaultExecutor_;
  47. }
  48. void set(std::weak_ptr<ExecutorBase> executor) {
  49. SharedMutex::WriteHolder guard(mutex_);
  50. executor_.swap(executor);
  51. }
  52. private:
  53. SharedMutex mutex_;
  54. std::weak_ptr<ExecutorBase> executor_;
  55. std::shared_ptr<ExecutorBase> defaultExecutor_;
  56. Function<std::unique_ptr<ExecutorBase>()> constructDefault_;
  57. };
  58. Singleton<GlobalExecutor<Executor>> gGlobalCPUExecutor([] {
  59. return new GlobalExecutor<Executor>(
  60. // Default global CPU executor is an InlineExecutor.
  61. [] { return std::make_unique<InlineExecutor>(); });
  62. });
  63. Singleton<GlobalExecutor<IOExecutor>> gGlobalIOExecutor([] {
  64. return new GlobalExecutor<IOExecutor>(
  65. // Default global IO executor is an IOThreadPoolExecutor.
  66. [] {
  67. return std::make_unique<IOThreadPoolExecutor>(
  68. std::thread::hardware_concurrency(),
  69. std::make_shared<NamedThreadFactory>("GlobalIOThreadPool"));
  70. });
  71. });
  72. } // namespace
  73. namespace folly {
  74. std::shared_ptr<Executor> getCPUExecutor() {
  75. if (auto singleton = gGlobalCPUExecutor.try_get()) {
  76. return singleton->get();
  77. }
  78. return nullptr;
  79. }
  80. void setCPUExecutor(std::weak_ptr<Executor> executor) {
  81. if (auto singleton = gGlobalCPUExecutor.try_get()) {
  82. singleton->set(std::move(executor));
  83. }
  84. }
  85. std::shared_ptr<IOExecutor> getIOExecutor() {
  86. if (auto singleton = gGlobalIOExecutor.try_get()) {
  87. return singleton->get();
  88. }
  89. return nullptr;
  90. }
  91. void setIOExecutor(std::weak_ptr<IOExecutor> executor) {
  92. if (auto singleton = gGlobalIOExecutor.try_get()) {
  93. singleton->set(std::move(executor));
  94. }
  95. }
  96. EventBase* getEventBase() {
  97. return getIOExecutor()->getEventBase();
  98. }
  99. } // namespace folly