Skip to content

Commit 9099635

Browse files
committed
Add base executor objects that can be used by implementors
Signed-off-by: Michael Carroll <[email protected]>
1 parent 2bf88de commit 9099635

File tree

9 files changed

+1263
-0
lines changed

9 files changed

+1263
-0
lines changed

rclcpp/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ set(${PROJECT_NAME}_SRCS
5454
src/rclcpp/executable_list.cpp
5555
src/rclcpp/executor.cpp
5656
src/rclcpp/executors.cpp
57+
src/rclcpp/executors/executor_notify_waitable.cpp
58+
src/rclcpp/executors/executor_entities_collector.cpp
59+
src/rclcpp/executors/executor_entities_collection.cpp
5760
src/rclcpp/executors/multi_threaded_executor.cpp
5861
src/rclcpp/executors/single_threaded_executor.cpp
5962
src/rclcpp/executors/static_executor_entities_collector.cpp
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Copyright 2023 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__EXECUTORS__EXECUTOR_ENTITIES_COLLECTION_HPP_
16+
#define RCLCPP__EXECUTORS__EXECUTOR_ENTITIES_COLLECTION_HPP_
17+
18+
#include <deque>
19+
#include <unordered_map>
20+
#include <vector>
21+
22+
#include "rcpputils/thread_safety_annotations.hpp"
23+
24+
#include <rclcpp/any_executable.hpp>
25+
#include <rclcpp/node_interfaces/node_base.hpp>
26+
#include <rclcpp/callback_group.hpp>
27+
#include <rclcpp/executors/executor_notify_waitable.hpp>
28+
#include <rclcpp/visibility_control.hpp>
29+
#include <rclcpp/wait_result.hpp>
30+
#include <rclcpp/wait_set.hpp>
31+
32+
namespace rclcpp
33+
{
34+
namespace executors
35+
{
36+
37+
struct ExecutorEntitiesCollection
38+
{
39+
struct TimerEntry
40+
{
41+
rclcpp::TimerBase::WeakPtr timer;
42+
rclcpp::CallbackGroup::WeakPtr callback_group;
43+
};
44+
using TimerCollection = std::unordered_map<const rcl_timer_t *, TimerEntry>;
45+
46+
struct SubscriptionEntry
47+
{
48+
rclcpp::SubscriptionBase::WeakPtr subscription;
49+
rclcpp::CallbackGroup::WeakPtr callback_group;
50+
};
51+
using SubscriptionCollection = std::unordered_map<const rcl_subscription_t *, SubscriptionEntry>;
52+
53+
struct ClientEntry
54+
{
55+
rclcpp::ClientBase::WeakPtr client;
56+
rclcpp::CallbackGroup::WeakPtr callback_group;
57+
};
58+
using ClientCollection = std::unordered_map<const rcl_client_t *, ClientEntry>;
59+
60+
struct ServiceEntry
61+
{
62+
rclcpp::ServiceBase::WeakPtr service;
63+
rclcpp::CallbackGroup::WeakPtr callback_group;
64+
};
65+
using ServiceCollection = std::unordered_map<const rcl_service_t *, ServiceEntry>;
66+
67+
struct WaitableEntry
68+
{
69+
rclcpp::Waitable::WeakPtr waitable;
70+
rclcpp::CallbackGroup::WeakPtr callback_group;
71+
};
72+
using WaitableCollection = std::unordered_map<const rclcpp::Waitable *, WaitableEntry>;
73+
74+
using GuardConditionCollection = std::unordered_map<const rcl_guard_condition_t *,
75+
rclcpp::GuardCondition::WeakPtr>;
76+
77+
TimerCollection timers;
78+
SubscriptionCollection subscriptions;
79+
ClientCollection clients;
80+
ServiceCollection services;
81+
GuardConditionCollection guard_conditions;
82+
WaitableCollection waitables;
83+
84+
void clear();
85+
86+
using TimerUpdatedFunc = std::function<void (rclcpp::TimerBase::SharedPtr)>;
87+
void update_timers(
88+
const ExecutorEntitiesCollection::TimerCollection & other,
89+
TimerUpdatedFunc timer_added,
90+
TimerUpdatedFunc timer_removed);
91+
92+
using SubscriptionUpdatedFunc = std::function<void (rclcpp::SubscriptionBase::SharedPtr)>;
93+
void update_subscriptions(
94+
const ExecutorEntitiesCollection::SubscriptionCollection & other,
95+
SubscriptionUpdatedFunc subscription_added,
96+
SubscriptionUpdatedFunc subscription_removed);
97+
98+
using ClientUpdatedFunc = std::function<void (rclcpp::ClientBase::SharedPtr)>;
99+
void update_clients(
100+
const ExecutorEntitiesCollection::ClientCollection & other,
101+
ClientUpdatedFunc client_added,
102+
ClientUpdatedFunc client_removed);
103+
104+
using ServiceUpdatedFunc = std::function<void (rclcpp::ServiceBase::SharedPtr)>;
105+
void update_services(
106+
const ExecutorEntitiesCollection::ServiceCollection & other,
107+
ServiceUpdatedFunc service_added,
108+
ServiceUpdatedFunc service_removed);
109+
110+
using GuardConditionUpdatedFunc = std::function<void (rclcpp::GuardCondition::SharedPtr)>;
111+
void update_guard_conditions(
112+
const ExecutorEntitiesCollection::GuardConditionCollection & other,
113+
GuardConditionUpdatedFunc guard_condition_added,
114+
GuardConditionUpdatedFunc guard_condition_removed);
115+
116+
using WaitableUpdatedFunc = std::function<void (rclcpp::Waitable::SharedPtr)>;
117+
void update_waitables(
118+
const ExecutorEntitiesCollection::WaitableCollection & other,
119+
WaitableUpdatedFunc waitable_added,
120+
WaitableUpdatedFunc waitable_removed);
121+
};
122+
123+
void
124+
build_entities_collection(
125+
const std::vector<rclcpp::CallbackGroup::WeakPtr> & callback_groups,
126+
ExecutorEntitiesCollection & collection);
127+
128+
std::deque<rclcpp::AnyExecutable>
129+
ready_executables(
130+
const ExecutorEntitiesCollection & collection,
131+
rclcpp::WaitResult<rclcpp::WaitSet> & wait_result
132+
);
133+
134+
} // namespace executors
135+
} // namespace rclcpp
136+
137+
#endif // RCLCPP__EXECUTORS__EXECUTOR_ENTITIES_COLLECTION_HPP_
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
// Copyright 2023 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__EXECUTORS__EXECUTOR_ENTITIES_COLLECTOR_HPP_
16+
#define RCLCPP__EXECUTORS__EXECUTOR_ENTITIES_COLLECTOR_HPP_
17+
18+
#include <list>
19+
#include <memory>
20+
#include <set>
21+
#include <vector>
22+
23+
#include "rcpputils/thread_safety_annotations.hpp"
24+
25+
#include <rclcpp/any_executable.hpp>
26+
#include <rclcpp/node_interfaces/node_base.hpp>
27+
#include <rclcpp/callback_group.hpp>
28+
#include <rclcpp/executors/executor_notify_waitable.hpp>
29+
#include <rclcpp/visibility_control.hpp>
30+
#include <rclcpp/wait_set.hpp>
31+
#include <rclcpp/wait_result.hpp>
32+
33+
namespace rclcpp
34+
{
35+
namespace executors
36+
{
37+
38+
class ExecutorEntitiesCollector
39+
{
40+
public:
41+
RCLCPP_PUBLIC
42+
ExecutorEntitiesCollector();
43+
44+
RCLCPP_PUBLIC
45+
~ExecutorEntitiesCollector();
46+
47+
/// Add a node to the entity collector
48+
/**
49+
* \param[in] node_ptr a shared pointer that points to a node base interface
50+
* \throw std::runtime_error if the node is associated with an executor
51+
*/
52+
RCLCPP_PUBLIC
53+
void
54+
add_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr);
55+
56+
/// Remove a node from the entity collector
57+
/**
58+
* \param[in] node_ptr a shared pointer that points to a node base interface
59+
* \throw std::runtime_error if the node is associated with an executor
60+
* \throw std::runtime_error if the node is associated with this executor
61+
*/
62+
RCLCPP_PUBLIC
63+
void
64+
remove_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr);
65+
66+
RCLCPP_PUBLIC
67+
bool
68+
has_node(const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr);
69+
70+
/// Add a callback group to the entity collector
71+
/**
72+
* \param[in] group_ptr a shared pointer that points to a callback group
73+
* \throw std::runtime_error if the callback_group is associated with an executor
74+
*/
75+
RCLCPP_PUBLIC
76+
void
77+
add_callback_group(rclcpp::CallbackGroup::SharedPtr group_ptr);
78+
79+
/// Remove a callback group from the entity collector
80+
/**
81+
* \param[in] group_ptr a shared pointer that points to a callback group
82+
* \throw std::runtime_error if the callback_group is not associated with an executor
83+
* \throw std::runtime_error if the callback_group is not associated with this executor
84+
*/
85+
RCLCPP_PUBLIC
86+
void
87+
remove_callback_group(rclcpp::CallbackGroup::SharedPtr group_ptr);
88+
89+
RCLCPP_PUBLIC
90+
std::vector<rclcpp::CallbackGroup::WeakPtr>
91+
get_all_callback_groups();
92+
93+
RCLCPP_PUBLIC
94+
std::vector<rclcpp::CallbackGroup::WeakPtr>
95+
get_manually_added_callback_groups();
96+
97+
RCLCPP_PUBLIC
98+
std::vector<rclcpp::CallbackGroup::WeakPtr>
99+
get_automatically_added_callback_groups();
100+
101+
RCLCPP_PUBLIC
102+
void
103+
update_collections();
104+
105+
RCLCPP_PUBLIC
106+
std::shared_ptr<ExecutorNotifyWaitable>
107+
get_executor_notify_waitable();
108+
109+
protected:
110+
using CallbackGroupCollection = std::set<
111+
rclcpp::CallbackGroup::WeakPtr,
112+
std::owner_less<rclcpp::CallbackGroup::WeakPtr>>;
113+
114+
RCLCPP_PUBLIC
115+
void
116+
add_callback_group_to_collection(
117+
rclcpp::CallbackGroup::SharedPtr group_ptr,
118+
CallbackGroupCollection & collection) RCPPUTILS_TSA_REQUIRES(mutex_);
119+
120+
RCLCPP_PUBLIC
121+
void
122+
remove_callback_group_from_collection(
123+
rclcpp::CallbackGroup::SharedPtr group_ptr,
124+
CallbackGroupCollection & collection) RCPPUTILS_TSA_REQUIRES(mutex_);
125+
126+
RCLCPP_PUBLIC
127+
void
128+
add_automatically_associated_callback_groups() RCPPUTILS_TSA_REQUIRES(mutex_);
129+
130+
RCLCPP_PUBLIC
131+
void
132+
prune_invalid_nodes_and_groups() RCPPUTILS_TSA_REQUIRES(mutex_);
133+
134+
mutable std::mutex mutex_;
135+
136+
CallbackGroupCollection
137+
manually_added_groups_ RCPPUTILS_TSA_GUARDED_BY(mutex_);
138+
139+
CallbackGroupCollection
140+
automatically_added_groups_ RCPPUTILS_TSA_GUARDED_BY(mutex_);
141+
142+
/// nodes that are associated with the executor
143+
std::list<rclcpp::node_interfaces::NodeBaseInterface::WeakPtr>
144+
weak_nodes_ RCPPUTILS_TSA_GUARDED_BY(mutex_);
145+
146+
std::shared_ptr<ExecutorNotifyWaitable> notify_waitable_ RCPPUTILS_TSA_GUARDED_BY(mutex_);
147+
};
148+
} // namespace executors
149+
} // namespace rclcpp
150+
151+
#endif // RCLCPP__EXECUTORS__EXECUTOR_ENTITIES_COLLECTOR_HPP_

0 commit comments

Comments
 (0)