AsyncInterestTree.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #ifndef _FASTDDS_RTPS_RESOURCES_ASYNC_INTEREST_TREE_H_
  15. #define _FASTDDS_RTPS_RESOURCES_ASYNC_INTEREST_TREE_H_
  16. #include <fastrtps/rtps/writer/RTPSWriter.h>
  17. #include <mutex>
  18. #include <set>
  19. namespace eprosima {
  20. namespace fastrtps {
  21. namespace rtps {
  22. /*!
  23. * Used by AsyncWriterThread to manage a double queue.
  24. * One queue is being processed by AsyncWriterThread's internal thread while in the other one other threads can register
  25. * RTPSWriter pointers that need to send samples asynchronously.
  26. */
  27. class AsyncInterestTree
  28. {
  29. friend class AsyncWriterThread;
  30. public:
  31. /*!
  32. * @brief Registers a writer in a hidden queue.
  33. * @param writer Pointer to the writer.
  34. * @return true if the writer was queued or false if it already is queued.
  35. */
  36. bool register_interest(
  37. RTPSWriter* writer);
  38. /*!
  39. * @brief Registers a writer in a hidden queue.
  40. * @param writer Pointer to the writer.
  41. * @param max_blocking_time Time point until the function must be blocked.
  42. * @return true if the writer was queued or false if it already is queued.
  43. * @note This method is blocked for a period of time.
  44. */
  45. bool register_interest(
  46. RTPSWriter* writer,
  47. const std::chrono::time_point<std::chrono::steady_clock>& max_blocking_time);
  48. /*!
  49. * @brief Unregister a writer from both queues.
  50. * @param writer Pointer to the writer.
  51. * @return true if both queues remain empty.
  52. */
  53. bool unregister_interest(
  54. RTPSWriter* writer);
  55. /*!
  56. * @brief Clears the visible queue and swaps with the hidden set.
  57. */
  58. void swap();
  59. /*!
  60. * @brief Remove next writer from visible queue and returns it.
  61. * @return Next writer.
  62. */
  63. RTPSWriter* next_active_nts();
  64. private:
  65. bool register_interest_nts(
  66. RTPSWriter* writer);
  67. mutable std::timed_mutex mMutexActive, mMutexHidden;
  68. RTPSWriter* active_front_ = nullptr;
  69. RTPSWriter* hidden_front_ = nullptr;
  70. int active_pos_ = 0;
  71. int hidden_pos_ = 1;
  72. };
  73. } /* namespace rtps */
  74. } /* namespace fastrtps */
  75. } /* namespace eprosima */
  76. #endif // _FASTDDS_RTPS_RESOURCES_ASYNC_INTEREST_TREE_H_