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
base: rolling
Are you sure you want to change the base?
Conversation
8743230
to
40fdb8e
Compare
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.
There are some minor comments.
rclcpp/test/test_wait_mutex.cpp
Outdated
|
||
using namespace std::chrono_literals; | ||
|
||
class TestWaitMutex : public ::testing::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 think that this test is to check wall_timer if the user callback is issued as expected times? if so, it would be better to change the file name and class name? like test_wall_timer_count.cpp or something?
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 changed it to test_time_call_count.cpp
btw, i confirmed the following difference between before and after,
|
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.
lgtm, but there seems to be a confliction.
61e37df
to
f0a59be
Compare
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.
lgtm, I'd like someone else to check.
This was only a performance problem, right? |
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.
Using two mutexes LGTM!
I was deleting a couple comments I did when reviewing (that were wrong), and by accident I deleted your comment @fujitatomoya 😁 . I don't know how to restore a comment, hope you remember what it was about. |
Yes, that is correct. After some number of iterations, the first thread is able to remove the timer from the |
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.
LGTM
|
||
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.
nit: missing new line before EOF
Thanks for clarifying!! |
f0a59be
to
fedf242
Compare
@peterpena is this ready? (edit) there seems to be a bunch of liner failures |
@ivanpauno I still have to check why I am getting errors that were not there before. |
0a40fcf
to
4345721
Compare
Signed-off-by: Pedro Pena <peter.a.pena@gmail.com>
Signed-off-by: Pedro Pena <peter.a.pena@gmail.com>
Signed-off-by: Pedro Pena <peter.a.pena@gmail.com>
Signed-off-by: Pedro Pena <peter.a.pena@gmail.com>
Signed-off-by: Pedro Pena <peter.a.pena@gmail.com>
Signed-off-by: Pedro Pena <peter.a.pena@gmail.com>
4345721
to
a4fe5a5
Compare
Its good to go now. I believe @wjwwood wanted to take a look before I merge it though. |
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.
lgtm! sorry for the delay in reviewing.
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 think get_next_executable
and scheduled_timers_
should be protected by the same mutex, unless there will be possibility that the same timer is executed multiple times.
} | ||
continue; | ||
} | ||
|
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.
- context switch here with already scheduled timer by Thread-1. (Thread-2)
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 comment
The reason will be displayed to describe this comment to others. Learn more.
- execute timer and remove it from
scheduled_timers_mutex_
(Thread-1)
} | ||
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
- the same timer will be scheduled again and executed. (Thread-2)
This PR tries to fix issue #1148. An independent mutex is added to protect the
scheduled_timer_
set. This enables one thread to remove a timer fromscheduled_timer_
while the other thread asynchronously tries to find the next executable. This removes the issue where one thread cannot remove the timer while the other thread keeps waking up and tries to get the timer but cannot execute it because the other thread is locked by the mutex. I ran tests to verify single and multi threaded executors can run the timer callbacks without any issues: