Skip to content

Commit 4534383

Browse files
[utility]: Change behavior of main thread verification utility (envoyproxy#14660)
Currently, the isMainThread function can only be called during the lifetime of thread local instance because the singleton that store main thread id is initialized in the constructor of tls instance and cleared in the destructor of tls instance. Change the utility so that outside the lifetime of tls instance, the function return true by default because everything is in main thread when threading is off. Risk Level: low Testing: change unit to reflect change of behavior. Signed-off-by: chaoqin-li1123 <[email protected]>
1 parent 7b5cf87 commit 4534383

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

source/common/common/thread.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,14 @@ struct MainThread {
177177
delete MainThreadSingleton::getExisting();
178178
MainThreadSingleton::clear();
179179
}
180-
static bool isMainThread() { return MainThreadSingleton::get().inMainThread(); }
180+
static bool isMainThread() {
181+
// If threading is off, only main thread is running.
182+
if (MainThreadSingleton::getExisting() == nullptr) {
183+
return true;
184+
}
185+
// When threading is on, compare thread id with main thread id.
186+
return MainThreadSingleton::get().inMainThread();
187+
}
181188

182189
private:
183190
std::thread::id main_thread_id_{std::this_thread::get_id()};

test/common/thread_local/thread_local_impl_test.cc

+9-12
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,18 @@ namespace Envoy {
1616
namespace ThreadLocal {
1717

1818
TEST(MainThreadVerificationTest, All) {
19-
// Main thread singleton is initialized in the constructor of tls instance. Call to main thread
20-
// verification will fail before that.
21-
EXPECT_DEATH(Thread::MainThread::isMainThread(),
22-
"InjectableSingleton used prior to initialization");
19+
// Before threading is on, assertion on main thread should be true.
20+
EXPECT_TRUE(Thread::MainThread::isMainThread());
2321
{
24-
EXPECT_DEATH(Thread::MainThread::isMainThread(),
25-
"InjectableSingleton used prior to initialization");
2622
InstanceImpl tls;
27-
// Call to main thread verification should succeed after tls instance has been initialized.
28-
ASSERT(Thread::MainThread::isMainThread());
23+
// Tls instance has been initialized.
24+
// Call to main thread verification should succeed in main thread.
25+
EXPECT_TRUE(Thread::MainThread::isMainThread());
2926
tls.shutdownGlobalThreading();
3027
tls.shutdownThread();
3128
}
32-
// Main thread singleton is cleared in the destructor of tls instance. Call to main thread
33-
// verification will fail after that.
34-
EXPECT_DEATH(Thread::MainThread::isMainThread(),
35-
"InjectableSingleton used prior to initialization");
29+
// After threading is off, assertion on main thread should be true.
30+
EXPECT_TRUE(Thread::MainThread::isMainThread());
3631
}
3732

3833
class TestThreadLocalObject : public ThreadLocalObject {
@@ -305,6 +300,8 @@ TEST(ThreadLocalInstanceImplDispatcherTest, Dispatcher) {
305300
thread_dispatcher->run(Event::Dispatcher::RunType::NonBlock);
306301
// Verify we have the expected dispatcher for the new thread thread.
307302
EXPECT_EQ(thread_dispatcher.get(), &tls.dispatcher());
303+
// Verify that it is inside the worker thread.
304+
EXPECT_FALSE(Thread::MainThread::isMainThread());
308305
});
309306
thread->join();
310307

0 commit comments

Comments
 (0)