-
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
Changes from all commits
d77d349
3e9a5be
51f5549
948b973
eac8710
a40cf1f
f7abf76
2b9db0d
702dec0
360087a
48abc00
230ad21
402662d
4624a04
079f10d
bacbc44
3bc0be0
dc708f7
545b891
41f8ccc
fc353f8
67e7b94
d92cd8f
be7c2ad
f4d44ec
c28f634
c62f434
7ad4971
fadce0a
6d6947b
1caa503
4df058b
cf95926
d4fa9ff
0df0650
86c21c9
fff8dc9
72efaa4
8ca3633
82ce44f
c05f2cd
f9b3b1b
0fa4c90
68584ce
6fc21a6
022efb5
dbcd0d6
b4918e0
48997e1
080f38e
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
#include "envoy/thread/thread.h" | ||
|
||
#include "common/common/non_copyable.h" | ||
#include "common/singleton/threadsafe_singleton.h" | ||
|
||
#include "absl/synchronization/mutex.h" | ||
|
||
|
@@ -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(); } | ||
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 commentThe 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 commentThe 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 commentThe 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. |
||
MainThreadSingleton::clear(); | ||
} | ||
static bool isMainThread() { return MainThreadSingleton::get().inMainThread(); } | ||
|
||
private: | ||
std::thread::id main_thread_id_{std::this_thread::get_id()}; | ||
}; | ||
|
||
} // namespace Thread | ||
} // namespace Envoy |
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!