DrivableExecutor.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright 2015-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/Executor.h>
  18. namespace folly {
  19. /*
  20. * A DrivableExecutor can be driven via its drive() method
  21. * Examples include EventBase (via loopOnce()) and ManualExecutor
  22. * (via makeProgress()).
  23. *
  24. * This interface is most handy in conjunction with
  25. * Future<T>::getVia(DrivableExecutor*) and
  26. * Future<T>::waitVia(DrivableExecutor*)
  27. *
  28. * These call drive() * repeatedly until the Future is fulfilled.
  29. * getVia() returns the value (or throws the exception) and waitVia() returns
  30. * the same Future for chainability.
  31. *
  32. * These will be most helpful in tests, for instance if you need to pump a mock
  33. * EventBase until Futures complete.
  34. */
  35. class DrivableExecutor : public virtual Executor {
  36. public:
  37. ~DrivableExecutor() override = default;
  38. // Make progress on this Executor's work.
  39. //
  40. // Drive *must not* busy wait if there is no work to do. Instead,
  41. // sleep (using a semaphore or similar) until at least one event is
  42. // processed.
  43. // I.e. make_future().via(foo).then(...).getVia(DrivableExecutor)
  44. // must not spin, even though nothing happens on the drivable
  45. // executor.
  46. virtual void drive() = 0;
  47. };
  48. } // namespace folly