diff options
-rw-r--r-- | base/thread.cc | 7 | ||||
-rw-r--r-- | base/thread_unittest.cc | 23 |
2 files changed, 27 insertions, 3 deletions
diff --git a/base/thread.cc b/base/thread.cc index 9bc81a4..387b1c5 100644 --- a/base/thread.cc +++ b/base/thread.cc @@ -143,13 +143,14 @@ void Thread::ThreadMain() { message_loop.set_thread_name(name_); message_loop_ = &message_loop; + // Let the thread do extra initialization. + // Let's do this before signaling we are started. + Init(); + startup_data_->event.Signal(); // startup_data_ can't be touched anymore since the starting thread is now // unlocked. - // Let the thread do extra initialization. - Init(); - message_loop.Run(); // Let the thread do extra cleanup. diff --git a/base/thread_unittest.cc b/base/thread_unittest.cc index 085df71..083456d 100644 --- a/base/thread_unittest.cc +++ b/base/thread_unittest.cc @@ -37,6 +37,20 @@ class SleepSome : public Task { int msec_; }; +class SleepInsideInitThread : public Thread { + public: + SleepInsideInitThread() : Thread("none") { init_called_ = false; } + virtual ~SleepInsideInitThread() { } + + virtual void Init() { + PlatformThread::Sleep(1000); + init_called_ = true; + } + bool InitCalled() { return init_called_; } + private: + bool init_called_; +}; + } // namespace TEST_F(ThreadTest, Restart) { @@ -107,3 +121,12 @@ TEST_F(ThreadTest, ThreadName) { EXPECT_TRUE(a.Start()); EXPECT_EQ("ThreadName", a.thread_name()); } + +// Make sure we can't use a thread between Start() and Init(). +TEST_F(ThreadTest, SleepInsideInit) { + SleepInsideInitThread t; + EXPECT_FALSE(t.InitCalled()); + t.Start(); + EXPECT_TRUE(t.InitCalled()); +} + |