-
Notifications
You must be signed in to change notification settings - Fork 411
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
executor could take more than once incorrectly #383
Changes from 4 commits
7d80613
6853019
a2c7e9e
38c48d1
d51bf80
fdcfd01
1b5d3fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
#include <chrono> | ||
#include <functional> | ||
#include <memory> | ||
#include <vector> | ||
|
||
#include "rclcpp/utilities.hpp" | ||
|
@@ -25,8 +26,9 @@ using rclcpp::executors::MultiThreadedExecutor; | |
|
||
MultiThreadedExecutor::MultiThreadedExecutor( | ||
const rclcpp::executor::ExecutorArgs & args, | ||
size_t number_of_threads) | ||
: executor::Executor(args) | ||
size_t number_of_threads, | ||
bool yield_before_execute) | ||
: executor::Executor(args), yield_before_execute_(yield_before_execute) | ||
{ | ||
number_of_threads_ = number_of_threads ? number_of_threads : std::thread::hardware_concurrency(); | ||
if (number_of_threads_ == 0) { | ||
|
@@ -69,16 +71,33 @@ void | |
MultiThreadedExecutor::run(size_t) | ||
{ | ||
while (rclcpp::ok() && spinning.load()) { | ||
executor::AnyExecutable any_exec; | ||
auto any_exec = std::make_shared<executor::AnyExecutable>(); | ||
{ | ||
std::lock_guard<std::mutex> wait_lock(wait_mutex_); | ||
if (!rclcpp::ok() || !spinning.load()) { | ||
return; | ||
} | ||
if (!get_next_executable(any_exec)) { | ||
if (!get_next_executable(*any_exec)) { | ||
continue; | ||
} | ||
{ | ||
std::lock_guard<std::mutex> lock(scheduled_mutex_); | ||
if (scheduled_.count(any_exec) != 0) { | ||
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. I'm a bit confused by this line. AFAIK |
||
continue; | ||
} | ||
scheduled_.insert(any_exec); | ||
} | ||
} | ||
if (yield_before_execute_) { | ||
std::this_thread::yield(); | ||
} | ||
execute_any_executable(*any_exec); | ||
{ | ||
std::lock_guard<std::mutex> lock(scheduled_mutex_); | ||
auto it = scheduled_.find(any_exec); | ||
if (it != scheduled_.end()) { | ||
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. When is an executable not found in 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. It should never happen, afaict. |
||
scheduled_.erase(it); | ||
} | ||
} | ||
execute_any_executable(any_exec); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2017 Open Source Robotics Foundation, Inc. | ||
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. 2018 |
||
// | ||
// 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/exceptions.hpp" | ||
#include "rclcpp/node.hpp" | ||
#include "rclcpp/rclcpp.hpp" | ||
#include "rclcpp/executors.hpp" | ||
|
||
#include "rcl_interfaces/msg/intra_process_message.hpp" | ||
|
||
using namespace std::chrono_literals; | ||
|
||
using rcl_interfaces::msg::IntraProcessMessage; | ||
|
||
class TestMultiThreadedExecutor : public ::testing::Test | ||
{ | ||
protected: | ||
static void SetUpTestCase() | ||
{ | ||
rclcpp::init(0, nullptr); | ||
} | ||
}; | ||
|
||
/* | ||
Test that timers are not taken multiple times when using reentrant callback groups. | ||
*/ | ||
TEST_F(TestMultiThreadedExecutor, timer_over_take) { | ||
struct sched_param param; | ||
param.sched_priority = 0; | ||
if (sched_setscheduler(0, SCHED_BATCH, ¶m) != 0) { | ||
perror("sched_setscheduler"); | ||
} | ||
|
||
rclcpp::executors::MultiThreadedExecutor executor( | ||
rclcpp::executor::create_default_executor_arguments(), 0, true); | ||
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. Personal preference is |
||
|
||
ASSERT_GT(executor.get_number_of_threads(), 1u); | ||
|
||
std::shared_ptr<rclcpp::Node> node = | ||
std::make_shared<rclcpp::Node>("test_multi_threaded_executor_timer_over_take"); | ||
|
||
auto cbg = node->create_callback_group(rclcpp::callback_group::CallbackGroupType::Reentrant); | ||
|
||
rclcpp::Clock system_clock(RCL_STEADY_TIME); | ||
std::mutex last_mutex; | ||
auto last = system_clock.now(); | ||
|
||
std::atomic_int timer_count {0}; | ||
|
||
auto timer_callback = [&timer_count, &executor, &system_clock, &last_mutex, &last]() { | ||
rclcpp::Time now = system_clock.now(); | ||
timer_count++; | ||
|
||
if (timer_count > 20) { | ||
executor.cancel(); | ||
} | ||
|
||
{ | ||
std::lock_guard<std::mutex> lock(last_mutex); | ||
double diff = labs((now - last).nanoseconds()) / 1.0e9; | ||
last = now; | ||
|
||
if (diff < 0.009 || diff > 0.011) { | ||
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. Recommend replacing numbers with something like |
||
executor.cancel(); | ||
ASSERT_TRUE(diff > 0.009); | ||
ASSERT_TRUE(diff < 0.011); | ||
} | ||
} | ||
}; | ||
|
||
auto timer = node->create_wall_timer(10ms, timer_callback, cbg); | ||
executor.add_node(node); | ||
executor.spin(); | ||
} |
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.
Does this need to be wrapped in
if(UNIX)
due tosched_setscheduler()
in the test?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.
I added an
#ifdef
around the corresponding sched_setscheduler(), because the bug originally manifested on OSX, so we wanted to test against that. That trick additionally isn't necessary on OSX because the bug shows up with the default scheduler.