-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Add utility to check whether the execution is in main thread. #14457
Conversation
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
…_macros Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
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.
Thanks for adding this utility! One thought from me regarding testing/corner cases.
@@ -168,5 +169,19 @@ class AtomicPtr : private AtomicPtrArray<T, 1, alloc_mode> { | |||
T* get(const MakeObject& make_object) { return BaseClass::get(0, make_object); } | |||
}; | |||
|
|||
struct MainThread { | |||
using MainThreadSingleton = InjectableSingleton<MainThread>; | |||
bool inMainThread() const { return main_thread_id_ == std::this_thread::get_id(); } |
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.
What happens if inMainThread is called before Init? should we check for that?
I'd be great to have unit tests, for corner cases like this.
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.
RELEASE_ASSERT(loader_ != nullptr, "InjectableSingleton used prior to initialization"); The assert in get() method in InjectableSingleton will fail in this case.
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 will add some unit test to verify this condition.
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 am not sure how to implement the unit test. Can we expect a RELEASE_ASSERT in gtest?
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 EXPECT_DEATH should work there?
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.
Thanks, it work!
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
…_macros Signed-off-by: chaoqin-li1123 <chaoqin@uchicago.edu>
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.
Looks great! In future do you mind filling out the template when you submit PRs? All of the
Additional Description:
sections.
Thanks! Will fill out all the description next time. |
bool inMainThread() const { return main_thread_id_ == std::this_thread::get_id(); } | ||
static void init() { MainThreadSingleton::initialize(new MainThread()); } | ||
static void clear() { | ||
free(MainThreadSingleton::getExisting()); |
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.
Post review drive by: I think you have a mismatched new/free here? Should this be delete? cc @chaoqin-li1123
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.
Sorry, it is my mistake. i will fix it in a new PR.
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 guess free doesn't leak memory here because the struct doesn't have a destructor. In the fixing PR should be changed to new/delete.
* master: (48 commits) Resolve 14506, avoid libidn2 for our curl dependency (envoyproxy#14601) fix new/free mismatch in Mainthread utility (envoyproxy#14596) opencensus: deprecate Zipkin configuration. (envoyproxy#14576) upstream: clean up code location (envoyproxy#14580) configuration impl: add cast for ios compilation (envoyproxy#14590) buffer impl: add cast for android compilation (envoyproxy#14589) ratelimit: add dynamic metadata to ratelimit response (envoyproxy#14508) tcp_proxy: wait for CONNECT response before start streaming data (envoyproxy#14317) stream info: cleanup address handling (envoyproxy#14432) [deps] update upb to latest commit (envoyproxy#14582) Add utility to check whether the execution is in main thread. (envoyproxy#14457) listener: undeprecate bind_to_port (envoyproxy#14480) Fix data race in overload integration test (envoyproxy#14586) deps: update PGV (envoyproxy#14571) dependencies: update cve_scan.py for some libcurl 7.74.0 false positives. (envoyproxy#14572) Network::Connection: Add L4 crash dumping support (envoyproxy#14509) ssl: remember stat names for configured ciphers. (envoyproxy#14534) formatter: add custom date formatting to downstream cert start and end dates (envoyproxy#14502) feat(lua): allow setting response body when the upstream response body is empty (envoyproxy#14486) Generalize the gRPC access logger base classes (envoyproxy#14469) ... Signed-off-by: Michael Puncel <mpuncel@squareup.com>
For an explanation of how to fill out the fields, please see the relevant section
in PULL_REQUESTS.md
Commit Message: Use a singleton to store the main thread id and use it for assertion that some function should be executed in main thread. Prepare for [#issue 14320] (#14320) to eliminate data plane exceptions.
Additional Description:
Risk Level:
Testing:
Docs Changes:
Release Notes:
Platform Specific Features:
[Optional Runtime guard:]
[Optional Fixes #Issue]
[Optional Deprecated:]