-
Notifications
You must be signed in to change notification settings - Fork 27
RSDK-10643: condition var refresh thread #428
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
Changes from 2 commits
05b21d8
a2c8bf2
a78291f
7198e15
4fd7e13
1a81c32
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 |
|---|---|---|
|
|
@@ -143,7 +143,11 @@ RobotClient::~RobotClient() { | |
| } | ||
|
|
||
| void RobotClient::close() { | ||
| should_refresh_.store(false); | ||
| if (should_refresh_) { | ||
| std::unique_lock<std::mutex> lk{refresh_lock_}; | ||
| should_refresh_ = false; | ||
| refresh_cv_.notify_one(); | ||
| } | ||
|
|
||
| if (refresh_thread_.joinable()) { | ||
| refresh_thread_.join(); | ||
|
|
@@ -220,16 +224,21 @@ void RobotClient::refresh() { | |
| } | ||
|
|
||
| void RobotClient::refresh_every() { | ||
| while (should_refresh_.load()) { | ||
| try { | ||
| std::this_thread::sleep_for(refresh_interval_); | ||
| refresh(); | ||
| std::unique_lock<std::mutex> lk{refresh_lock_}; | ||
|
|
||
| } catch (std::exception&) { | ||
| while (true) { | ||
|
||
| if (refresh_cv_.wait_for(lk, refresh_interval_) == std::cv_status::timeout) { | ||
|
||
| try { | ||
| refresh(); | ||
| } catch (const std::exception& e) { | ||
| VIAM_SDK_LOG(warn) << "Refresh thread terminated with exception: " << e.what(); | ||
| break; | ||
|
||
| } | ||
| } else if (should_refresh_ == false) { | ||
|
||
| break; | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| RobotClient::RobotClient(ViamChannel channel) | ||
| : viam_channel_(std::move(channel)), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
| /// @brief gRPC client implementation for a `robot`. | ||
| #pragma once | ||
|
|
||
| #include <atomic> | ||
| #include <condition_variable> | ||
| #include <string> | ||
| #include <thread> | ||
|
|
||
|
|
@@ -186,7 +186,9 @@ class RobotClient { | |
| void refresh_every(); | ||
|
|
||
| std::thread refresh_thread_; | ||
| std::atomic<bool> should_refresh_; | ||
| std::mutex refresh_lock_; | ||
| std::condition_variable refresh_cv_; | ||
| bool should_refresh_; | ||
|
Member
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. Might be possible to eliminate the bool by making
Collaborator
Author
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 think you're right, we're storing a (we could just have
Member
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 think it is probably worth doing. There's just less state to manage, which means less risk of things decohering. It'd also simplify From To |
||
| std::chrono::seconds refresh_interval_; | ||
|
|
||
| ViamChannel viam_channel_; | ||
|
|
||
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.
If
should_refresh_is changing state, it should do so under the lock I think.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 it is not changing state--
should_refresh_is only mutated in this method and in the named constructor which sets it in the first place, so there may be a concurrent read happening here but there's no way for it to be read while being mutatedThere 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'll argue that this is one of those cases where even if it is safe in practice, it is still better to do the check under the lock. It'll be annoying if someday we stand up TSAN and it whines about a leak here, and this isn't a hot path, so trying to eliminate the lock doesn't buy much of anything.
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 mean race of course, not leak, since I'm talking about TSAN not ASAN.
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.
done!
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'd even move the read of
should_referesh_under the lock, or even eliminate the check entirely. It should be fine incloseto just unconditionally setshould_refresh_ = false(under the lock), and then notify. If there is no refresh thread, it is just a big no-op.