Independent mutex to check and remove timer from scheduled timer#1168
Independent mutex to check and remove timer from scheduled timer#1168
Conversation
8743230 to
40fdb8e
Compare
fujitatomoya
left a comment
There was a problem hiding this comment.
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.
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.
I changed it to test_time_call_count.cpp
|
btw, i confirmed the following difference between before and after,
|
fujitatomoya
left a comment
There was a problem hiding this comment.
lgtm, but there seems to be a confliction.
61e37df to
f0a59be
Compare
fujitatomoya
left a comment
There was a problem hiding this comment.
lgtm, I'd like someone else to check.
This was only a performance problem, right? |
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 |
|
|
||
| executor.add_node(node); | ||
| executor.spin(); | ||
| } No newline at end of file |
There was a problem hiding this comment.
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. |
wjwwood
left a comment
There was a problem hiding this comment.
lgtm! sorry for the delay in reviewing.
fujitatomoya
left a comment
There was a problem hiding this comment.
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.
- context switch here with already scheduled timer by Thread-1. (Thread-2)
|
|
||
| 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.
- 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.
- 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: