Skip to content

Commit 4d78d42

Browse files
author
iRobot ROS
authored
Merge pull request #40 from mauropasse/mauro/add-events-queue-class-2
Add events queue abstract class and implem
2 parents 02c2ef1 + c4ac6f9 commit 4d78d42

File tree

6 files changed

+359
-24
lines changed

6 files changed

+359
-24
lines changed

rclcpp/include/rclcpp/executors/events_executor.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "rclcpp/executors/events_executor_entities_collector.hpp"
2525
#include "rclcpp/executors/events_executor_notify_waitable.hpp"
2626
#include "rclcpp/executors/timers_manager.hpp"
27+
#include "rclcpp/experimental/buffers/events_queue.hpp"
28+
#include "rclcpp/experimental/buffers/simple_events_queue.hpp"
2729
#include "rclcpp/node.hpp"
2830

2931
#include "rmw/listener_event_types.h"
@@ -55,10 +57,13 @@ class EventsExecutor : public rclcpp::Executor
5557

5658
/// Default constructor. See the default constructor for Executor.
5759
/**
60+
* \param[in] events_queue The queue used to store events.
5861
* \param[in] options Options used to configure the executor.
5962
*/
6063
RCLCPP_PUBLIC
6164
explicit EventsExecutor(
65+
rclcpp::experimental::buffers::EventsQueue::UniquePtr events_queue = std::make_unique<
66+
rclcpp::experimental::buffers::SimpleEventsQueue>(),
6267
const rclcpp::ExecutorOptions & options = rclcpp::ExecutorOptions());
6368

6469
/// Default destrcutor.
@@ -190,10 +195,10 @@ class EventsExecutor : public rclcpp::Executor
190195
{
191196
std::unique_lock<std::mutex> lock(this_executor->push_mutex_);
192197

193-
this_executor->event_queue_.push(event);
198+
this_executor->events_queue_->push(event);
194199
}
195200
// Notify that the event queue has some events in it.
196-
this_executor->event_queue_cv_.notify_one();
201+
this_executor->events_queue_cv_.notify_one();
197202
}
198203

199204
/// Extract and execute events from the queue until it is empty
@@ -207,15 +212,15 @@ class EventsExecutor : public rclcpp::Executor
207212
execute_event(const rmw_listener_event_t & event);
208213

209214
// Queue where entities can push events
210-
EventQueue event_queue_;
215+
rclcpp::experimental::buffers::EventsQueue::SharedPtr events_queue_;
211216

212217
EventsExecutorEntitiesCollector::SharedPtr entities_collector_;
213218
EventsExecutorNotifyWaitable::SharedPtr executor_notifier_;
214219

215220
// Mutex to protect the insertion of events in the queue
216221
std::mutex push_mutex_;
217222
// Variable used to notify when an event is added to the queue
218-
std::condition_variable event_queue_cv_;
223+
std::condition_variable events_queue_cv_;
219224
// Timers manager
220225
std::shared_ptr<TimersManager> timers_manager_;
221226
};
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright 2021 Open Source Robotics Foundation, Inc.
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+
#ifndef RCLCPP__EXPERIMENTAL__BUFFERS__EVENTS_QUEUE_HPP_
16+
#define RCLCPP__EXPERIMENTAL__BUFFERS__EVENTS_QUEUE_HPP_
17+
18+
#include <queue>
19+
20+
#include "rclcpp/executors/events_executor_entities_collector.hpp"
21+
#include "rclcpp/macros.hpp"
22+
23+
#include "rmw/listener_event_types.h"
24+
25+
namespace rclcpp
26+
{
27+
namespace experimental
28+
{
29+
namespace buffers
30+
{
31+
32+
/**
33+
* @brief This abstract class is intended to be used as
34+
* a wrapper around a queue. The derived classes should chose
35+
* which container to use and the strategies for push and prune
36+
* events from the queue.
37+
*/
38+
class EventsQueue
39+
{
40+
public:
41+
RCLCPP_SMART_PTR_DEFINITIONS(EventsQueue)
42+
43+
/**
44+
* @brief Destruct the object.
45+
*/
46+
RCLCPP_PUBLIC
47+
virtual ~EventsQueue() = default;
48+
49+
/**
50+
* @brief push event into the queue
51+
* @param event The event to push into the queue
52+
*/
53+
RCLCPP_PUBLIC
54+
virtual
55+
void
56+
push(const rmw_listener_event_t & event) = 0;
57+
58+
/**
59+
* @brief removes front element from the queue
60+
* The element removed is the "oldest" element in the queue whose
61+
* value can be retrieved by calling member front().
62+
*/
63+
RCLCPP_PUBLIC
64+
virtual
65+
void
66+
pop() = 0;
67+
68+
/**
69+
* @brief gets the front event from the queue
70+
* @return the front event
71+
*/
72+
RCLCPP_PUBLIC
73+
virtual
74+
rmw_listener_event_t
75+
front() const = 0;
76+
77+
/**
78+
* @brief Test whether queue is empty
79+
* @return true if the queue's size is 0, false otherwise.
80+
*/
81+
RCLCPP_PUBLIC
82+
virtual
83+
bool
84+
empty() const = 0;
85+
86+
/**
87+
* @brief Initializes the queue
88+
*/
89+
RCLCPP_PUBLIC
90+
virtual
91+
void
92+
init() = 0;
93+
94+
/**
95+
* @brief gets a queue with all events accumulated on it since
96+
* the last call. The member queue is empty when the call returns.
97+
* @return queue with events
98+
*/
99+
RCLCPP_PUBLIC
100+
virtual
101+
std::queue<rmw_listener_event_t>
102+
get_all_events() = 0;
103+
};
104+
105+
} // namespace buffers
106+
} // namespace experimental
107+
} // namespace rclcpp
108+
109+
#endif // RCLCPP__EXPERIMENTAL__BUFFERS__EVENTS_QUEUE_HPP_
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright 2021 Open Source Robotics Foundation, Inc.
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+
#ifndef RCLCPP__EXPERIMENTAL__BUFFERS__SIMPLE_EVENTS_QUEUE_HPP_
16+
#define RCLCPP__EXPERIMENTAL__BUFFERS__SIMPLE_EVENTS_QUEUE_HPP_
17+
18+
#include <queue>
19+
#include <utility>
20+
21+
#include "rclcpp/experimental/buffers/events_queue.hpp"
22+
23+
namespace rclcpp
24+
{
25+
namespace experimental
26+
{
27+
namespace buffers
28+
{
29+
30+
/**
31+
* @brief This class provides a simple queue implementation
32+
* based on a std::queue. As the objective is having a CPU peformant
33+
* queue, it does not performs any checks about the size of
34+
* the queue, so the queue size could grow unbounded.
35+
* It does not implement any pruning mechanisms.
36+
*/
37+
class SimpleEventsQueue : public EventsQueue
38+
{
39+
public:
40+
RCLCPP_PUBLIC
41+
~SimpleEventsQueue() = default;
42+
43+
/**
44+
* @brief push event into the queue
45+
* @param event The event to push into the queue
46+
*/
47+
RCLCPP_PUBLIC
48+
virtual
49+
void
50+
push(const rmw_listener_event_t & event)
51+
{
52+
event_queue_.push(event);
53+
}
54+
55+
/**
56+
* @brief removes front element from the queue
57+
* The element removed is the "oldest" element in the queue whose
58+
* value can be retrieved by calling member front().
59+
*/
60+
RCLCPP_PUBLIC
61+
virtual
62+
void
63+
pop()
64+
{
65+
event_queue_.pop();
66+
}
67+
68+
/**
69+
* @brief gets the front event from the queue
70+
* @return the front event
71+
*/
72+
RCLCPP_PUBLIC
73+
virtual
74+
rmw_listener_event_t
75+
front() const
76+
{
77+
return event_queue_.front();
78+
}
79+
80+
/**
81+
* @brief Test whether queue is empty
82+
* @return true if the queue's size is 0, false otherwise.
83+
*/
84+
RCLCPP_PUBLIC
85+
virtual
86+
bool
87+
empty() const
88+
{
89+
return event_queue_.empty();
90+
}
91+
92+
/**
93+
* @brief Initializes the queue
94+
*/
95+
RCLCPP_PUBLIC
96+
virtual
97+
void
98+
init()
99+
{
100+
// Make sure the queue is empty when we start
101+
std::queue<rmw_listener_event_t> local_queue;
102+
std::swap(event_queue_, local_queue);
103+
}
104+
105+
106+
/**
107+
* @brief gets a queue with all events accumulated on it since
108+
* the last call. The member queue is empty when the call returns.
109+
* @return std::queue with events
110+
*/
111+
RCLCPP_PUBLIC
112+
virtual
113+
std::queue<rmw_listener_event_t>
114+
get_all_events()
115+
{
116+
std::queue<rmw_listener_event_t> local_queue;
117+
std::swap(event_queue_, local_queue);
118+
return local_queue;
119+
}
120+
121+
private:
122+
std::queue<rmw_listener_event_t> event_queue_;
123+
};
124+
125+
} // namespace buffers
126+
} // namespace experimental
127+
} // namespace rclcpp
128+
129+
130+
#endif // RCLCPP__EXPERIMENTAL__BUFFERS__SIMPLE_EVENTS_QUEUE_HPP_

0 commit comments

Comments
 (0)