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> FirstInFirstOutScheduler::
149 get_handle_for_callback_group(
const rclcpp::CallbackGroup::SharedPtr &)
151 return std::make_unique<FirstInFirstOutCallbackGroupHandle>(*
this);
154 CBGScheduler::ExecutableEntityWithInfo FirstInFirstOutScheduler::get_next_ready_entity_intern()
156 std::lock_guard l(ready_callback_groups_mutex);
158 while(!ready_callback_groups.empty()) {
159 FirstInFirstOutCallbackGroupHandle *ready_cbg =
160 static_cast<FirstInFirstOutCallbackGroupHandle *
>(ready_callback_groups.front());
161 ready_callback_groups.pop_front();
163 std::optional<FirstInFirstOutScheduler::ExecutableEntity> ret =
164 ready_cbg->get_next_ready_entity();
166 return CBGScheduler::ExecutableEntityWithInfo{std::move(ret),
167 !ready_callback_groups.empty()};
171 return CBGScheduler::ExecutableEntityWithInfo{std::nullopt,
175 CBGScheduler::ExecutableEntityWithInfo FirstInFirstOutScheduler::get_next_ready_entity_intern(
176 GlobalEventIdProvider::MonotonicId max_id)
178 std::lock_guard l(ready_callback_groups_mutex);
183 for(
auto it = ready_callback_groups.begin(); it != ready_callback_groups.end(); it++) {
184 FirstInFirstOutCallbackGroupHandle *ready_cbg(
185 static_cast<FirstInFirstOutCallbackGroupHandle *
>(*it));
186 std::optional<FirstInFirstOutScheduler::ExecutableEntity> ret =
187 ready_cbg->get_next_ready_entity(max_id);
189 ready_callback_groups.erase(it);
190 return CBGScheduler::ExecutableEntityWithInfo{std::move(ret),
191 !ready_callback_groups.empty()};
195 return CBGScheduler::ExecutableEntityWithInfo{std::nullopt,
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
void add_ready_entity(const add_fun &fun)