TimeoutManager.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2014-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 <chrono>
  18. #include <cstdint>
  19. #include <folly/Function.h>
  20. namespace folly {
  21. class AsyncTimeout;
  22. /**
  23. * Base interface to be implemented by all classes expecting to manage
  24. * timeouts. AsyncTimeout will use implementations of this interface
  25. * to schedule/cancel timeouts.
  26. */
  27. class TimeoutManager {
  28. public:
  29. typedef std::chrono::milliseconds timeout_type;
  30. using Func = folly::Function<void()>;
  31. enum class InternalEnum { INTERNAL, NORMAL };
  32. TimeoutManager();
  33. virtual ~TimeoutManager();
  34. /**
  35. * Attaches/detaches TimeoutManager to AsyncTimeout
  36. */
  37. virtual void attachTimeoutManager(
  38. AsyncTimeout* obj,
  39. InternalEnum internal) = 0;
  40. virtual void detachTimeoutManager(AsyncTimeout* obj) = 0;
  41. /**
  42. * Schedules AsyncTimeout to fire after `timeout` milliseconds
  43. */
  44. virtual bool scheduleTimeout(AsyncTimeout* obj, timeout_type timeout) = 0;
  45. /**
  46. * Cancels the AsyncTimeout, if scheduled
  47. */
  48. virtual void cancelTimeout(AsyncTimeout* obj) = 0;
  49. /**
  50. * This is used to mark the beginning of a new loop cycle by the
  51. * first handler fired within that cycle.
  52. */
  53. virtual void bumpHandlingTime() = 0;
  54. /**
  55. * Helper method to know whether we are running in the timeout manager
  56. * thread
  57. */
  58. virtual bool isInTimeoutManagerThread() = 0;
  59. /**
  60. * Runs the given Cob at some time after the specified number of
  61. * milliseconds. (No guarantees exactly when.)
  62. *
  63. * Throws a std::system_error if an error occurs.
  64. */
  65. void runAfterDelay(
  66. Func cob,
  67. uint32_t milliseconds,
  68. InternalEnum internal = InternalEnum::NORMAL);
  69. /**
  70. * @see tryRunAfterDelay for more details
  71. *
  72. * @return true iff the cob was successfully registered.
  73. */
  74. bool tryRunAfterDelay(
  75. Func cob,
  76. uint32_t milliseconds,
  77. InternalEnum internal = InternalEnum::NORMAL);
  78. protected:
  79. void clearCobTimeouts();
  80. private:
  81. struct CobTimeouts;
  82. std::unique_ptr<CobTimeouts> cobTimeouts_;
  83. };
  84. } // namespace folly