Skip to content

Commit 9b13b77

Browse files
author
Mauro Passerino
committed
Add get_single_event API
Signed-off-by: Mauro Passerino <[email protected]>
1 parent ecfdaca commit 9b13b77

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

rclcpp/include/rclcpp/experimental/buffers/events_queue.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ class EventsQueue
110110
virtual
111111
std::queue<rclcpp::executors::ExecutorEvent>
112112
pop_all_events() = 0;
113+
114+
/**
115+
* @brief gets a single entity event from the queue
116+
* @return a single entity event
117+
*/
118+
// TODO: find an alternative to pop_all_events and
119+
// get_single_event to use standard queue interfaces
120+
RCLCPP_PUBLIC
121+
virtual
122+
rclcpp::executors::ExecutorEvent
123+
get_single_event() = 0;
113124
};
114125

115126
} // namespace buffers

rclcpp/include/rclcpp/experimental/buffers/simple_events_queue.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,34 @@ class SimpleEventsQueue : public EventsQueue
126126
return local_queue;
127127
}
128128

129+
/**
130+
* @brief gets a single entity event from the queue
131+
* and decrements the event counter.
132+
* @return a single event
133+
*/
134+
RCLCPP_PUBLIC
135+
virtual
136+
rclcpp::executors::ExecutorEvent
137+
get_single_event()
138+
{
139+
rclcpp::executors::ExecutorEvent & front_event = event_queue_.front();
140+
141+
if (front_event.num_events > 1) {
142+
// We have more than a single event for the entity.
143+
// Decrement the counter by one, keeping the event in the front.
144+
front_event.num_events--;
145+
} else {
146+
// We have a single event, pop it from queue.
147+
event_queue_.pop();
148+
}
149+
150+
// Make sure we return a single event for the entity.
151+
rclcpp::executors::ExecutorEvent single_event = front_event;
152+
single_event.num_events = 1;
153+
154+
return single_event;
155+
}
156+
129157
private:
130158
std::queue<rclcpp::executors::ExecutorEvent> event_queue_;
131159
};

rclcpp/src/rclcpp/executors/events_executor.cpp

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ EventsExecutor::spin_once_impl(std::chrono::nanoseconds timeout)
183183
// When condition variable is notified, check this predicate to proceed
184184
auto has_event_predicate = [this]() {return !events_queue_->empty();};
185185

186-
ExecutorEvent single_event;
186+
ExecutorEvent event;
187187
bool has_event = false;
188188

189189
{
@@ -194,39 +194,14 @@ EventsExecutor::spin_once_impl(std::chrono::nanoseconds timeout)
194194
// Grab first event from queue if it exists
195195
has_event = !events_queue_->empty();
196196
if (has_event) {
197-
// If the event has num_events > 1, we have to only execute a single event and
198-
// decrement the event counter, leaving the event in the queue for future processing.
199-
// But we can't modify an element from a temporary object (the queue is a shared_ptr)
200-
// so, we need a local copy to manipulate and then update the original queue.
201-
std::queue<ExecutorEvent> local_events_queue = events_queue_->pop_all_events();
202-
203-
ExecutorEvent & front_event = local_events_queue.front();
204-
205-
if (front_event.num_events > 1) {
206-
// Decrement the counter by one, keeping the event in the front.
207-
front_event.num_events--;
208-
} else {
209-
// We have a single event, pop it from queue.
210-
local_events_queue.pop();
211-
}
212-
213-
// Make sure we only execute a single event
214-
single_event = front_event;
215-
single_event.num_events = 1;
216-
217-
// Update the global queue
218-
while (!local_events_queue.empty()) {
219-
ExecutorEvent event = local_events_queue.front();
220-
local_events_queue.pop();
221-
events_queue_->push(event);
222-
}
197+
event = events_queue_->get_single_event();
223198
}
224199
}
225200

226201
// If we wake up from the wait with an event, it means that it
227202
// arrived before any of the timers expired.
228203
if (has_event) {
229-
this->execute_event(single_event);
204+
this->execute_event(event);
230205
} else {
231206
timers_manager_->execute_head_timer();
232207
}

0 commit comments

Comments
 (0)