AsyncWriterThread.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 AsyncWriterThread.h
  16. *
  17. */
  18. #ifndef _FASTDDS_RTPS_RESOURCES_ASYNCWRITERTHREAD_H_
  19. #define _FASTDDS_RTPS_RESOURCES_ASYNCWRITERTHREAD_H_
  20. #include <thread>
  21. #include <atomic>
  22. #include <list>
  23. #include <fastdds/rtps/resources/AsyncInterestTree.h>
  24. #include <fastrtps/utils/TimedMutex.hpp>
  25. #include <fastrtps/utils/TimedConditionVariable.hpp>
  26. namespace eprosima {
  27. namespace fastrtps {
  28. namespace rtps {
  29. class RTPSWriter;
  30. /**
  31. * @brief This static class owns a thread that manages asynchronous writes.
  32. * Asynchronous writes happen directly (when using an async writer) and
  33. * indirectly (when responding to a NACK).
  34. * @ingroup COMMON_MODULE
  35. */
  36. class AsyncWriterThread
  37. {
  38. public:
  39. AsyncWriterThread() = default;
  40. ~AsyncWriterThread();
  41. /*!
  42. * @brief Unregister a writer if it is waiting to be processed.
  43. * @param writer Asynchronous writer to be removed.
  44. * @return Result of the operation.
  45. * @note Always call this function from writer's destructor.
  46. */
  47. void unregister_writer(
  48. RTPSWriter* writer);
  49. /*!
  50. * Wakes the thread up and starts processing async writers.
  51. * @param interested_writer The writer interested in an async write.
  52. */
  53. void wake_up(
  54. RTPSWriter* interested_writer);
  55. /*!
  56. * Wakes the thread up and starts processing async writers.
  57. * @param interested_writer The writer interested in an async write.
  58. * @param max_blocking_time Time point until the function must be blocked.
  59. * @note This method is blocked for a period of time.
  60. */
  61. void wake_up(
  62. RTPSWriter* interested_writer,
  63. const std::chrono::time_point<std::chrono::steady_clock>& max_blocking_time);
  64. private:
  65. AsyncWriterThread(const AsyncWriterThread&) = delete;
  66. const AsyncWriterThread& operator=(const AsyncWriterThread&) = delete;
  67. //! @brief runs main method
  68. void run();
  69. std::thread* thread_ = nullptr;
  70. RecursiveTimedMutex condition_variable_mutex_;
  71. //! List of asynchronous writers.
  72. AsyncInterestTree interestTree_;
  73. bool running_ = false;
  74. bool run_scheduled_ = false;
  75. TimedConditionVariable cv_;
  76. };
  77. } // namespace rtps
  78. } // namespace fastrtps
  79. } // namespace eprosima
  80. #endif // _FASTDDS_RTPS_RESOURCES_ASYNCWRITERTHREAD_H_