ResourceEvent.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @file ResourceEvent.h
  16. *
  17. */
  18. #ifndef _FASTDDS_RTPS_RESOURCES_RESOURCEEVENT_H_
  19. #define _FASTDDS_RTPS_RESOURCES_RESOURCEEVENT_H_
  20. #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
  21. #include <fastrtps/utils/TimedMutex.hpp>
  22. #include <fastrtps/utils/TimedConditionVariable.hpp>
  23. #include <thread>
  24. #include <atomic>
  25. #include <vector>
  26. namespace eprosima {
  27. namespace fastrtps {
  28. namespace rtps {
  29. class TimedEventImpl;
  30. /**
  31. * This class centralizes all operations over timed events in the same thread.
  32. * @ingroup MANAGEMENT_MODULE
  33. */
  34. class ResourceEvent
  35. {
  36. public:
  37. ResourceEvent() = default;
  38. ~ResourceEvent();
  39. /*!
  40. * @brief Method to initialize the internal thread.
  41. */
  42. void init_thread();
  43. /*!
  44. * @brief This method informs that a TimedEventImpl has been created.
  45. *
  46. * This method has to be called when creating a TimedEventImpl object.
  47. * @param event TimedEventImpl object that has been created.
  48. */
  49. void register_timer(
  50. TimedEventImpl* event);
  51. /*!
  52. * @brief This method removes a TimedEventImpl object in case it is waiting to be processed by ResourceEvent's
  53. * internal thread.
  54. *
  55. * This method has to be called before deleting the TimedEventImpl object.
  56. * This method cancels any operation of the timer.
  57. * Then it avoids the situation of the execution thread calling the event handler when it was previously removed.
  58. * @param event TimedEventImpl object that will be deleted and we have to be sure all its operations are cancelled.
  59. */
  60. void unregister_timer(
  61. TimedEventImpl* event);
  62. /*!
  63. * @brief This method notifies to ResourceEvent that the TimedEventImpl object has operations to be scheduled.
  64. *
  65. * These operations can be the cancellation of the timer or starting another async_wait.
  66. * @param event TimedEventImpl object that has operations to be scheduled.
  67. */
  68. void notify(
  69. TimedEventImpl* event);
  70. /*!
  71. * @brief This method notifies to ResourceEvent that the TimedEventImpl object has operations to be scheduled.
  72. *
  73. * These operations can be the cancellation of the timer or starting another async_wait.
  74. * @note Non-blocking call version of the method.
  75. * @param event TimedEventImpl object that has operations to be scheduled.
  76. * @param timeout Maximum blocking time of the method.
  77. */
  78. void notify(
  79. TimedEventImpl* event,
  80. const std::chrono::steady_clock::time_point& timeout);
  81. private:
  82. //! Warns the internal thread can stop.
  83. std::atomic<bool> stop_{ false };
  84. //! Protects internal data.
  85. TimedMutex mutex_;
  86. //! Used to warn about changes on allow_vector_manipulation_.
  87. TimedConditionVariable cv_manipulation_;
  88. //! Flag used to allow a thread to manipulate the timer collections when the execution thread is not using them.
  89. bool allow_vector_manipulation_ = true;
  90. //! Used to warn there are new TimedEventImpl objects to be processed.
  91. TimedConditionVariable cv_;
  92. //! The total number of created timers.
  93. size_t timers_count_ = 0;
  94. //! Collection of events pending update action.
  95. std::vector<TimedEventImpl*> pending_timers_;
  96. //! Collection of registered events waiting completion.
  97. std::vector<TimedEventImpl*> active_timers_;
  98. //! Current time as seen by the execution thread.
  99. std::chrono::steady_clock::time_point current_time_;
  100. //! Execution thread.
  101. std::thread thread_;
  102. /*!
  103. * @brief Registers a new TimedEventImpl object in the internal queue to be processed.
  104. * Non thread safe.
  105. * @param event Event to be added in the queue.
  106. * @return True value if the insertion was successful. In other case, it return False.
  107. */
  108. bool register_timer_nts(
  109. TimedEventImpl* event);
  110. //! Method called by the internal thread.
  111. void event_service();
  112. //! Sorts waiting timers in ascending order of trigger time.
  113. void sort_timers();
  114. //! Updates internal register of current time.
  115. void update_current_time();
  116. //! Method called by the internal thread to process due actions.
  117. void do_timer_actions();
  118. //! Ensures internal collections can accommodate current total number of timers.
  119. void resize_collections()
  120. {
  121. pending_timers_.reserve(timers_count_);
  122. active_timers_.reserve(timers_count_);
  123. }
  124. };
  125. } /* namespace rtps */
  126. } /* namespace fastrtps */
  127. } /* namespace eprosima */
  128. #endif //DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
  129. #endif //_FASTDDS_RTPS_RESOURCES_RESOURCEEVENT_H_