VirtualExecutor.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2018-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/DefaultKeepAliveExecutor.h>
  18. namespace folly {
  19. /**
  20. * VirtualExecutor implements a light-weight view onto existing Executor.
  21. *
  22. * Multiple VirtualExecutors can be backed by a single Executor.
  23. *
  24. * VirtualExecutor's destructor blocks until all tasks scheduled through it are
  25. * complete. Executor's destructor also blocks until all VirtualExecutors
  26. * backed by it are released.
  27. */
  28. class VirtualExecutor : public DefaultKeepAliveExecutor {
  29. auto wrapFunc(Func f) {
  30. class FuncAndKeepAlive {
  31. public:
  32. FuncAndKeepAlive(Func&& f, VirtualExecutor* executor)
  33. : keepAlive_(getKeepAliveToken(executor)), f_(std::move(f)) {}
  34. void operator()() {
  35. f_();
  36. }
  37. private:
  38. Executor::KeepAlive<VirtualExecutor> keepAlive_;
  39. Func f_;
  40. };
  41. return FuncAndKeepAlive(std::move(f), this);
  42. }
  43. public:
  44. explicit VirtualExecutor(KeepAlive<> executor)
  45. : executor_(std::move(executor)) {
  46. assert(!isKeepAliveDummy(executor_));
  47. }
  48. explicit VirtualExecutor(Executor* executor)
  49. : VirtualExecutor(getKeepAliveToken(executor)) {}
  50. explicit VirtualExecutor(Executor& executor)
  51. : VirtualExecutor(getKeepAliveToken(executor)) {}
  52. VirtualExecutor(const VirtualExecutor&) = delete;
  53. VirtualExecutor& operator=(const VirtualExecutor&) = delete;
  54. uint8_t getNumPriorities() const override {
  55. return executor_->getNumPriorities();
  56. }
  57. void add(Func f) override {
  58. executor_->add(wrapFunc(std::move(f)));
  59. }
  60. void addWithPriority(Func f, int8_t priority) override {
  61. executor_->addWithPriority(wrapFunc(std::move(f)), priority);
  62. }
  63. ~VirtualExecutor() override {
  64. joinKeepAlive();
  65. }
  66. private:
  67. const KeepAlive<> executor_;
  68. };
  69. } // namespace folly