/src/Fast-DDS/include/fastdds/rtps/resources/ResourceEvent.h
Line | Count | Source (jump to first uncovered line) |
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 | | /** |
16 | | * @file ResourceEvent.h |
17 | | * |
18 | | */ |
19 | | |
20 | | #ifndef _FASTDDS_RTPS_RESOURCES_RESOURCEEVENT_H_ |
21 | | #define _FASTDDS_RTPS_RESOURCES_RESOURCEEVENT_H_ |
22 | | |
23 | | #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC |
24 | | |
25 | | #include <fastrtps/utils/TimedMutex.hpp> |
26 | | #include <fastrtps/utils/TimedConditionVariable.hpp> |
27 | | |
28 | | #include <thread> |
29 | | #include <atomic> |
30 | | #include <vector> |
31 | | |
32 | | namespace eprosima { |
33 | | namespace fastrtps { |
34 | | namespace rtps { |
35 | | |
36 | | class TimedEventImpl; |
37 | | |
38 | | /** |
39 | | * This class centralizes all operations over timed events in the same thread. |
40 | | * @ingroup MANAGEMENT_MODULE |
41 | | */ |
42 | | class ResourceEvent |
43 | | { |
44 | | public: |
45 | | |
46 | 0 | ResourceEvent() = default; |
47 | | |
48 | | ~ResourceEvent(); |
49 | | |
50 | | /*! |
51 | | * @brief Method to initialize the internal thread. |
52 | | */ |
53 | | void init_thread(); |
54 | | |
55 | | void stop_thread(); |
56 | | |
57 | | /*! |
58 | | * @brief This method informs that a TimedEventImpl has been created. |
59 | | * |
60 | | * This method has to be called when creating a TimedEventImpl object. |
61 | | * @param event TimedEventImpl object that has been created. |
62 | | */ |
63 | | void register_timer( |
64 | | TimedEventImpl* event); |
65 | | |
66 | | /*! |
67 | | * @brief This method removes a TimedEventImpl object in case it is waiting to be processed by ResourceEvent's |
68 | | * internal thread. |
69 | | * |
70 | | * This method has to be called before deleting the TimedEventImpl object. |
71 | | * This method cancels any operation of the timer. |
72 | | * Then it avoids the situation of the execution thread calling the event handler when it was previously removed. |
73 | | * @param event TimedEventImpl object that will be deleted and we have to be sure all its operations are cancelled. |
74 | | */ |
75 | | void unregister_timer( |
76 | | TimedEventImpl* event); |
77 | | |
78 | | /*! |
79 | | * @brief This method notifies to ResourceEvent that the TimedEventImpl object has operations to be scheduled. |
80 | | * |
81 | | * These operations can be the cancellation of the timer or starting another async_wait. |
82 | | * @param event TimedEventImpl object that has operations to be scheduled. |
83 | | */ |
84 | | void notify( |
85 | | TimedEventImpl* event); |
86 | | |
87 | | /*! |
88 | | * @brief This method notifies to ResourceEvent that the TimedEventImpl object has operations to be scheduled. |
89 | | * |
90 | | * These operations can be the cancellation of the timer or starting another async_wait. |
91 | | * @note Non-blocking call version of the method. |
92 | | * @param event TimedEventImpl object that has operations to be scheduled. |
93 | | * @param timeout Maximum blocking time of the method. |
94 | | */ |
95 | | void notify( |
96 | | TimedEventImpl* event, |
97 | | const std::chrono::steady_clock::time_point& timeout); |
98 | | |
99 | | private: |
100 | | |
101 | | //! Warns the internal thread can stop. |
102 | | std::atomic<bool> stop_{ false }; |
103 | | |
104 | | //! Protects internal data. |
105 | | TimedMutex mutex_; |
106 | | |
107 | | //! Used to warn about changes on allow_vector_manipulation_. |
108 | | TimedConditionVariable cv_manipulation_; |
109 | | |
110 | | //! Flag used to allow a thread to manipulate the timer collections when the execution thread is not using them. |
111 | | bool allow_vector_manipulation_ = true; |
112 | | |
113 | | //! Used to warn there are new TimedEventImpl objects to be processed. |
114 | | TimedConditionVariable cv_; |
115 | | |
116 | | //! The total number of created timers. |
117 | | size_t timers_count_ = 0; |
118 | | |
119 | | //! Collection of events pending update action. |
120 | | std::vector<TimedEventImpl*> pending_timers_; |
121 | | |
122 | | //! Collection of registered events waiting completion. |
123 | | std::vector<TimedEventImpl*> active_timers_; |
124 | | |
125 | | //! Current time as seen by the execution thread. |
126 | | std::chrono::steady_clock::time_point current_time_; |
127 | | |
128 | | //! Execution thread. |
129 | | std::thread thread_; |
130 | | |
131 | | /*! |
132 | | * @brief Registers a new TimedEventImpl object in the internal queue to be processed. |
133 | | * Non thread safe. |
134 | | * @param event Event to be added in the queue. |
135 | | * @return True value if the insertion was successful. In other case, it return False. |
136 | | */ |
137 | | bool register_timer_nts( |
138 | | TimedEventImpl* event); |
139 | | |
140 | | //! Method called by the internal thread. |
141 | | void event_service(); |
142 | | |
143 | | //! Sorts waiting timers in ascending order of trigger time. |
144 | | void sort_timers(); |
145 | | |
146 | | //! Updates internal register of current time. |
147 | | void update_current_time(); |
148 | | |
149 | | //! Method called by the internal thread to process due actions. |
150 | | void do_timer_actions(); |
151 | | |
152 | | //! Ensures internal collections can accommodate current total number of timers. |
153 | | void resize_collections() |
154 | 0 | { |
155 | 0 | pending_timers_.reserve(timers_count_); |
156 | 0 | active_timers_.reserve(timers_count_); |
157 | 0 | } |
158 | | |
159 | | }; |
160 | | |
161 | | } /* namespace rtps */ |
162 | | } /* namespace fastrtps */ |
163 | | } /* namespace eprosima */ |
164 | | |
165 | | #endif //DOXYGEN_SHOULD_SKIP_THIS_PUBLIC |
166 | | |
167 | | #endif //_FASTDDS_RTPS_RESOURCES_RESOURCEEVENT_H_ |