-
Notifications
You must be signed in to change notification settings - Fork 25
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
base: main
Are you sure you want to change the base?
Changes from all 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_) { | ||
const 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,22 @@ 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&) { | ||
break; | ||
refresh_cv_.wait_for(lk, refresh_interval_, [this] { | ||
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 this is going to terminate after one timeout, but tbh I always get this stuff wrong, so don't let me mislead you if you have thought it through and this actually does what is intended. 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. you were right, classic case of a solution that is simple, elegant, and wrong 🙂 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 generally skeptical of the Here is how I think I would write this loop unrolled:
But it is possible that the predicate version is more or less equivalent to that, in which case it should be fine. |
||
if (should_refresh_) { | ||
try { | ||
refresh(); | ||
} catch (const std::exception& e) { | ||
VIAM_SDK_LOG(warn) << "Refresh thread got exception " << e.what(); | ||
// TODO: maybe recoverable | ||
return true; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
return !should_refresh_; | ||
}); | ||
} | ||
|
||
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_; | ||
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 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 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 inclose
to just unconditionally setshould_refresh_ = false
(under the lock), and then notify. If there is no refresh thread, it is just a big no-op.