-
Notifications
You must be signed in to change notification settings - Fork 3
feat: reentrant callback group #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sykwer
wants to merge
8
commits into
main
Choose a base branch
from
reentrant-parallelism
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f6b4866
deal with reentrant
sykwer f747b3c
fix
sykwer 43e2207
fix
sykwer 16eca5a
fix
sykwer 240849c
Update cie_sample_application/include/cie_sample_application/reentran…
sykwer 49609c6
Update cie_sample_application/include/cie_sample_application/reentran…
sykwer 0a710b9
fix
sykwer eb0e5ee
Merge branch 'main' into reentrant-parallelism
atsushi421 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...isolated_executor/include/callback_isolated_executor/multi_threaded_executor_internal.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #pragma once | ||
|
|
||
| #include "rclcpp/rclcpp.hpp" | ||
|
|
||
| // To implement parallelism for reentrant callback groups in | ||
| // CallbackIsolatedExecutor | ||
| class MultiThreadedExecutorInternal : public rclcpp::Executor { | ||
| RCLCPP_DISABLE_COPY(MultiThreadedExecutorInternal) | ||
|
|
||
| // Configuration | ||
| size_t number_of_threads_; | ||
| bool yield_before_execute_; | ||
| std::chrono::nanoseconds next_exec_timeout_; | ||
|
|
||
| // Thread management | ||
| std::vector<std::thread> threads_; | ||
| std::vector<pid_t> tids_; // guarded by mtx_ | ||
| size_t ready_count_{0}; // guarded by mtx_ (# of threads that saved TID) | ||
| bool start_allowed_{false}; // guarded by mtx_ (run() allowed to proceed) | ||
|
|
||
| // Synchronization in start phase | ||
| std::mutex mtx_; | ||
| std::condition_variable cv_all_ready_; | ||
| std::condition_variable cv_start_; | ||
|
|
||
| std::mutex wait_mutex_; // to guard get_next_executable in run() | ||
| std::atomic_bool pre_spinning_{false}; | ||
|
|
||
| void run(); | ||
|
|
||
| public: | ||
| explicit MultiThreadedExecutorInternal(size_t number_of_threads) | ||
| : rclcpp::Executor(rclcpp::ExecutorOptions()), | ||
| number_of_threads_(number_of_threads) { | ||
| // hardcode for now | ||
| yield_before_execute_ = false; | ||
| next_exec_timeout_ = std::chrono::nanoseconds(-1); | ||
| } | ||
|
|
||
| void pre_spin(); | ||
|
|
||
| void spin() override; | ||
|
|
||
| std::vector<pid_t> get_thread_ids(); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
callback_isolated_executor/src/multi_threaded_executor_internal.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,115 @@ | ||||||
| #include <sys/syscall.h> | ||||||
|
|
||||||
| #include "callback_isolated_executor/multi_threaded_executor_internal.hpp" | ||||||
|
|
||||||
| void MultiThreadedExecutorInternal::pre_spin() { | ||||||
| if (pre_spinning_.exchange(true)) { | ||||||
| throw std::runtime_error("pre_spin() called while already pre-spinning"); | ||||||
| } | ||||||
|
|
||||||
| { | ||||||
| std::lock_guard<std::mutex> lock{mtx_}; | ||||||
| assert(threads_.empty() && tids_.empty()); | ||||||
| ready_count_ = 0; | ||||||
| start_allowed_ = false; | ||||||
| } | ||||||
|
|
||||||
| threads_.reserve(number_of_threads_); | ||||||
| for (size_t i = 0; i < number_of_threads_; i++) { | ||||||
| threads_.emplace_back(&MultiThreadedExecutorInternal::run, this); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| void MultiThreadedExecutorInternal::run() { | ||||||
| auto tid = static_cast<pid_t>(syscall(SYS_gettid)); | ||||||
| bool will_notify = false; | ||||||
|
|
||||||
| { | ||||||
| std::lock_guard<std::mutex> lock{wait_mutex_}; | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| tids_.push_back(tid); | ||||||
| ready_count_++; | ||||||
| if (ready_count_ == number_of_threads_) { | ||||||
| will_notify = true; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if (will_notify) { | ||||||
| cv_all_ready_.notify_one(); | ||||||
| } | ||||||
|
|
||||||
| { | ||||||
| std::unique_lock<std::mutex> lock{mtx_}; | ||||||
| cv_start_.wait(lock, [this]() { return start_allowed_; }); | ||||||
| } | ||||||
|
|
||||||
| while (rclcpp::ok(this->context_) && spinning.load()) { | ||||||
| rclcpp::AnyExecutable any_exec; | ||||||
|
|
||||||
| { | ||||||
| std::lock_guard wait_lock{wait_mutex_}; | ||||||
|
|
||||||
| if (!rclcpp::ok(this->context_) || !spinning.load()) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| if (!get_next_executable(any_exec, next_exec_timeout_)) { | ||||||
| continue; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if (yield_before_execute_) { | ||||||
| std::this_thread::yield(); | ||||||
| } | ||||||
|
|
||||||
| execute_any_executable(any_exec); | ||||||
|
|
||||||
| // Clear the callback_group to prevent the AnyExecutable destructor from | ||||||
| // resetting the callback group `can_be_taken_from` | ||||||
| any_exec.callback_group.reset(); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| void MultiThreadedExecutorInternal::spin() { | ||||||
| if (!pre_spinning_.load()) { | ||||||
| throw std::runtime_error("spin() called without pre_spin()"); | ||||||
| } | ||||||
|
|
||||||
| if (spinning.exchange(true)) { | ||||||
| throw std::runtime_error("spin() called while already spinning"); | ||||||
| } | ||||||
|
|
||||||
| RCPPUTILS_SCOPE_EXIT(pre_spinning_.store(false);); | ||||||
| RCPPUTILS_SCOPE_EXIT(this->spinning.store(false);); | ||||||
|
|
||||||
| { | ||||||
| std::unique_lock<std::mutex> lock{mtx_}; | ||||||
| cv_all_ready_.wait(lock, | ||||||
| [this]() { return ready_count_ == number_of_threads_; }); | ||||||
| } | ||||||
|
|
||||||
| { | ||||||
| std::lock_guard<std::mutex> lock{mtx_}; | ||||||
| start_allowed_ = true; | ||||||
| } | ||||||
|
|
||||||
| cv_start_.notify_all(); | ||||||
|
|
||||||
| for (auto &thread : threads_) { | ||||||
| thread.join(); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| std::vector<pid_t> MultiThreadedExecutorInternal::get_thread_ids() { | ||||||
| if (!pre_spinning_.load()) { | ||||||
| throw std::runtime_error("get_thread_ids() called without pre_spin()"); | ||||||
| } | ||||||
|
|
||||||
| { | ||||||
| std::unique_lock<std::mutex> lock{mtx_}; | ||||||
| cv_all_ready_.wait(lock, | ||||||
| [this]() { return ready_count_ == number_of_threads_; }); | ||||||
| } | ||||||
|
|
||||||
| std::lock_guard<std::mutex> lock{mtx_}; | ||||||
| return tids_; | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
cie_sample_application/include/cie_sample_application/reentrant_node.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #pragma once | ||
|
|
||
| #include "rclcpp/rclcpp.hpp" | ||
| #include "std_msgs/msg/int32.hpp" | ||
|
|
||
| class ReentrantNode : public rclcpp::Node { | ||
| public: | ||
| explicit ReentrantNode( | ||
| const rclcpp::NodeOptions &options = rclcpp::NodeOptions()); | ||
|
|
||
| private: | ||
| void timer_callback_1(); | ||
| void timer_callback_2(); | ||
|
|
||
| rclcpp::TimerBase::SharedPtr timer1_; | ||
| rclcpp::TimerBase::SharedPtr timer2_; | ||
|
|
||
| // Single reentrant group to allow parallel callbacks | ||
| rclcpp::CallbackGroup::SharedPtr reentrant_group_; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #include <sys/syscall.h> | ||
| #include <thread> | ||
| #include <utility> | ||
|
|
||
| #include "cie_sample_application/reentrant_node.hpp" | ||
| #include "rclcpp_components/register_node_macro.hpp" | ||
|
|
||
| using namespace std::chrono_literals; | ||
|
|
||
| ReentrantNode::ReentrantNode(const rclcpp::NodeOptions &options) | ||
| : Node("reentrant_node", options) { | ||
| // Create a single reentrant callback group | ||
| reentrant_group_ = | ||
| create_callback_group(rclcpp::CallbackGroupType::Reentrant); | ||
|
|
||
| timer1_ = this->create_wall_timer( | ||
| 100ms, std::bind(&ReentrantNode::timer_callback_1, this), | ||
| reentrant_group_); | ||
| timer2_ = this->create_wall_timer( | ||
| 1000ms, std::bind(&ReentrantNode::timer_callback_2, this), | ||
| reentrant_group_); | ||
| } | ||
|
|
||
| void ReentrantNode::timer_callback_1() { | ||
| long tid = syscall(SYS_gettid); | ||
| RCLCPP_INFO(this->get_logger(), "Timer1 (T=100ms) (tid=%ld)", tid); | ||
| } | ||
|
|
||
| void ReentrantNode::timer_callback_2() { | ||
| std::this_thread::sleep_for(900ms); | ||
| long tid = syscall(SYS_gettid); | ||
| RCLCPP_INFO(this->get_logger(), "Timer2 (T=1000ms) (tid=%ld)", tid); | ||
| } | ||
|
|
||
| RCLCPP_COMPONENTS_REGISTER_NODE(ReentrantNode) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.