14 #include "first_in_first_out_scheduler.hpp"
21 namespace cbg_executor
23 std::function<void(
size_t)> FirstInFirstOutCallbackGroupHandle::get_ready_callback_for_entity(
24 const rclcpp::SubscriptionBase::WeakPtr & entity)
26 return [weak_ptr = entity,
this](
size_t nr_msg) {
28 for (
size_t i = 0; i < nr_msg; i++) {
29 ready_entities.emplace_back(weak_ptr);
35 std::function<void(std::function<
void()> executed_callback)> FirstInFirstOutCallbackGroupHandle::
36 get_ready_callback_for_entity(
const rclcpp::TimerBase::WeakPtr & entity)
38 return [weak_ptr = entity,
this](std::function<void()> executed_callback) {
40 ready_entities.emplace_back(ReadyEntity::ReadyTimerWithExecutedCallback{weak_ptr,
46 std::function<void(
size_t)> FirstInFirstOutCallbackGroupHandle::get_ready_callback_for_entity(
47 const rclcpp::ClientBase::WeakPtr & entity)
49 return [weak_ptr = entity,
this](
size_t nr_msg) {
51 for (
size_t i = 0; i < nr_msg; i++) {
52 ready_entities.emplace_back(weak_ptr);
58 std::function<void(
size_t)> FirstInFirstOutCallbackGroupHandle::get_ready_callback_for_entity(
59 const rclcpp::ServiceBase::WeakPtr & entity)
61 return [weak_ptr = entity,
this](
size_t nr_msg) {
63 for (
size_t i = 0; i < nr_msg; i++) {
64 ready_entities.emplace_back(weak_ptr);
70 std::function<void(
size_t,
71 int)> FirstInFirstOutCallbackGroupHandle::get_ready_callback_for_entity(
72 const rclcpp::Waitable::WeakPtr & entity)
74 return [weak_ptr = entity,
this](
size_t nr_msg,
int event_type) {
76 for (
size_t i = 0; i < nr_msg; i++) {
77 ready_entities.emplace_back(CBGScheduler::WaitableWithEventType({weak_ptr,
83 std::function<void(
size_t)> FirstInFirstOutCallbackGroupHandle::get_ready_callback_for_entity(
84 const CBGScheduler::CallbackEventType & entity)
86 return [weak_ptr = entity,
this](
size_t nr_msg) {
88 for (
size_t i = 0; i < nr_msg; i++) {
89 ready_entities.emplace_back(weak_ptr);
95 std::optional<CBGScheduler::ExecutableEntity> FirstInFirstOutCallbackGroupHandle::
96 get_next_ready_entity()
98 std::lock_guard l(ready_mutex);
100 while(!ready_entities.empty()) {
101 auto & first = ready_entities.front();
103 std::function<void()> exec_fun = first.get_execute_function();
104 ready_entities.pop_front();
112 return CBGScheduler::ExecutableEntity{exec_fun,
this};
120 std::optional<CBGScheduler::ExecutableEntity> FirstInFirstOutCallbackGroupHandle::
121 get_next_ready_entity(GlobalEventIdProvider::MonotonicId max_id)
123 std::lock_guard l(ready_mutex);
125 while(!ready_entities.empty()) {
126 auto & first = ready_entities.front();
127 if(first.id > max_id) {
131 std::function<void()> exec_fun = first.get_execute_function();
132 ready_entities.pop_front();
140 return CBGScheduler::ExecutableEntity{exec_fun,
this};
148 std::unique_ptr<FirstInFirstOutScheduler::CallbackGroupHandle>
149 FirstInFirstOutScheduler::get_handle_for_callback_group(
150 const rclcpp::CallbackGroup::SharedPtr & callback_group)
152 return std::make_unique<FirstInFirstOutCallbackGroupHandle>(*
this, callback_group->type());
155 CBGScheduler::ExecutableEntityWithInfo FirstInFirstOutScheduler::get_next_ready_entity_intern()
157 std::lock_guard l(ready_callback_groups_mutex);
159 while(!ready_callback_groups.empty()) {
160 FirstInFirstOutCallbackGroupHandle *ready_cbg =
161 static_cast<FirstInFirstOutCallbackGroupHandle *
>(ready_callback_groups.front());
162 ready_callback_groups.pop_front();
163 ready_cbg->in_queue =
false;
165 std::optional<FirstInFirstOutScheduler::ExecutableEntity> ret =
166 ready_cbg->get_next_ready_entity();
168 if (ready_cbg->get_type() == CallbackGroupType::Reentrant && ready_cbg->has_ready_entities()) {
169 ready_callback_groups.push_back(ready_cbg);
170 ready_cbg->in_queue =
true;
174 return CBGScheduler::ExecutableEntityWithInfo{.entity = std::move(ret),
175 .moreEntitiesReady = !ready_callback_groups.empty()};
179 return CBGScheduler::ExecutableEntityWithInfo{.entity = std::nullopt,
180 .moreEntitiesReady =
false};
183 CBGScheduler::ExecutableEntityWithInfo FirstInFirstOutScheduler::get_next_ready_entity_intern(
184 GlobalEventIdProvider::MonotonicId max_id)
186 std::lock_guard l(ready_callback_groups_mutex);
191 for(
auto it = ready_callback_groups.begin(); it != ready_callback_groups.end(); it++) {
192 FirstInFirstOutCallbackGroupHandle *ready_cbg(
193 static_cast<FirstInFirstOutCallbackGroupHandle *
>(*it));
194 std::optional<FirstInFirstOutScheduler::ExecutableEntity> ret =
195 ready_cbg->get_next_ready_entity(max_id);
197 ready_callback_groups.erase(it);
198 ready_cbg->in_queue =
false;
201 ready_cbg->get_type() == CallbackGroupType::Reentrant && ready_cbg->has_ready_entities())
203 ready_callback_groups.push_back(ready_cbg);
204 ready_cbg->in_queue =
true;
206 return CBGScheduler::ExecutableEntityWithInfo{
207 .entity = std::move(ret), .moreEntitiesReady = !ready_callback_groups.empty()};
211 return CBGScheduler::ExecutableEntityWithInfo{.entity = std::nullopt,
212 .moreEntitiesReady =
false};
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
void add_ready_entity(const add_fun &fun)