FiberIOExecutor.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 <folly/executors/IOExecutor.h>
  18. #include <folly/fibers/FiberManagerMap.h>
  19. namespace folly {
  20. /**
  21. * @class FiberIOExecutor
  22. * @brief An IOExecutor that executes funcs under mapped fiber context
  23. *
  24. * A FiberIOExecutor wraps an IOExecutor, but executes funcs on the FiberManager
  25. * mapped to the underlying IOExector's event base.
  26. */
  27. class FiberIOExecutor : public IOExecutor {
  28. public:
  29. explicit FiberIOExecutor(
  30. const std::shared_ptr<IOExecutor>& ioExecutor,
  31. fibers::FiberManager::Options opts = fibers::FiberManager::Options())
  32. : ioExecutor_(ioExecutor), options_(std::move(opts)) {}
  33. virtual void add(folly::Function<void()> f) override {
  34. auto eventBase = ioExecutor_->getEventBase();
  35. folly::fibers::getFiberManager(*eventBase, options_).add(std::move(f));
  36. }
  37. virtual folly::EventBase* getEventBase() override {
  38. return ioExecutor_->getEventBase();
  39. }
  40. private:
  41. std::shared_ptr<IOExecutor> ioExecutor_;
  42. fibers::FiberManager::Options options_;
  43. };
  44. } // namespace folly