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
Independent mutex to check and remove timer from scheduled timer #1168
Open
peterpena
wants to merge
6
commits into
rolling
Choose a base branch
from
peterpena/timer-mutex
base: rolling
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
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
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -83,27 +83,30 @@ MultiThreadedExecutor::run(size_t) | |
if (!get_next_executable(any_exec, next_exec_timeout_)) { | ||
continue; | ||
} | ||
if (any_exec.timer) { | ||
// Guard against multiple threads getting the same timer. | ||
if (scheduled_timers_.count(any_exec.timer) != 0) { | ||
// Make sure that any_exec's callback group is reset before | ||
// the lock is released. | ||
if (any_exec.callback_group) { | ||
any_exec.callback_group->can_be_taken_from().store(true); | ||
} | ||
continue; | ||
} | ||
|
||
if (any_exec.timer) { | ||
std::lock_guard<std::mutex> wait_lock(scheduled_timers_mutex_); | ||
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.
|
||
// Guard against multiple threads getting the same timer. | ||
if (scheduled_timers_.count(any_exec.timer) != 0) { | ||
// Make sure that any_exec's callback group is reset before | ||
// the lock is released. | ||
if (any_exec.callback_group) { | ||
any_exec.callback_group->can_be_taken_from().store(true); | ||
} | ||
scheduled_timers_.insert(any_exec.timer); | ||
continue; | ||
} | ||
scheduled_timers_.insert(any_exec.timer); | ||
} | ||
|
||
if (yield_before_execute_) { | ||
std::this_thread::yield(); | ||
} | ||
|
||
execute_any_executable(any_exec); | ||
|
||
if (any_exec.timer) { | ||
std::lock_guard<std::mutex> wait_lock(wait_mutex_); | ||
std::lock_guard<std::mutex> wait_lock(scheduled_timers_mutex_); | ||
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.
|
||
auto it = scheduled_timers_.find(any_exec.timer); | ||
if (it != scheduled_timers_.end()) { | ||
scheduled_timers_.erase(it); | ||
|
This file contains 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 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,116 @@ | ||
// Copyright 2020 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <chrono> | ||
#include <string> | ||
#include <memory> | ||
|
||
#include "rclcpp/rclcpp.hpp" | ||
#include "rclcpp/exceptions.hpp" | ||
#include "rclcpp/node.hpp" | ||
#include "rclcpp/executors.hpp" | ||
#include "rclcpp/executor.hpp" | ||
|
||
using namespace std::chrono_literals; | ||
|
||
class TestTimerCount : public ::testing::Test | ||
{ | ||
protected: | ||
static void SetUpTestCase() | ||
{ | ||
rclcpp::init(0, nullptr); | ||
} | ||
}; | ||
|
||
constexpr int EXECUTION_COUNT = 5; | ||
constexpr rcl_duration_value_t TIME_ELAPSED = 12; | ||
/* | ||
Test timer wait mutex with multithreaded executor. | ||
After 5 call | ||
*/ | ||
|
||
TEST_F(TestTimerCount, timer_call_count_multi_threaded) { | ||
ivanpauno marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rclcpp::executors::MultiThreadedExecutor executor; | ||
|
||
std::shared_ptr<rclcpp::Node> node = | ||
std::make_shared<rclcpp::Node>("timer_call_count_multi_threaded"); | ||
|
||
auto cbg = node->create_callback_group(rclcpp::CallbackGroupType::Reentrant); | ||
|
||
rclcpp::Clock system_clock(RCL_STEADY_TIME); | ||
std::mutex last_mutex; | ||
rclcpp::Time initial = system_clock.now(); | ||
|
||
std::atomic_int timer_count {0}; | ||
|
||
auto timer_callback = [&timer_count, &executor, &system_clock, &last_mutex, &initial]() { | ||
rclcpp::Time now = system_clock.now(); | ||
timer_count++; | ||
|
||
{ | ||
std::lock_guard<std::mutex> lock(last_mutex); | ||
rcl_duration_value_t diff = (now - initial).nanoseconds() / (rcl_duration_value_t)1.0e9; | ||
|
||
if (diff > TIME_ELAPSED) { | ||
executor.cancel(); | ||
ASSERT_GT(timer_count, EXECUTION_COUNT); | ||
} | ||
} | ||
}; | ||
|
||
auto timer_ = node->create_wall_timer( | ||
2s, timer_callback, cbg); | ||
|
||
executor.add_node(node); | ||
executor.spin(); | ||
} | ||
|
||
/* | ||
Test timer wait mutex with singlethreaded executor | ||
*/ | ||
TEST_F(TestTimerCount, timer_call_count_single_threaded) { | ||
rclcpp::executors::SingleThreadedExecutor executor; | ||
|
||
std::shared_ptr<rclcpp::Node> node = | ||
std::make_shared<rclcpp::Node>("timer_call_count_single_threaded"); | ||
|
||
rclcpp::Clock system_clock(RCL_STEADY_TIME); | ||
std::mutex last_mutex; | ||
rclcpp::Time initial = system_clock.now(); | ||
|
||
std::atomic_int timer_count {0}; | ||
|
||
auto timer_callback = [&timer_count, &executor, &system_clock, &last_mutex, &initial]() { | ||
rclcpp::Time now = system_clock.now(); | ||
timer_count++; | ||
|
||
{ | ||
std::lock_guard<std::mutex> lock(last_mutex); | ||
rcl_duration_value_t diff = (now - initial).nanoseconds() / (rcl_duration_value_t)1.0e9; | ||
|
||
if (diff > TIME_ELAPSED) { | ||
executor.cancel(); | ||
ASSERT_GT(timer_count, EXECUTION_COUNT); | ||
} | ||
} | ||
}; | ||
|
||
auto timer_ = node->create_wall_timer( | ||
2s, timer_callback); | ||
|
||
executor.add_node(node); | ||
executor.spin(); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.