diff options
Diffstat (limited to 'base')
-rw-r--r-- | base/thread.cc | 7 | ||||
-rw-r--r-- | base/thread.h | 8 | ||||
-rw-r--r-- | base/thread_unittest.cc | 9 |
3 files changed, 20 insertions, 4 deletions
diff --git a/base/thread.cc b/base/thread.cc index 387b1c5..f447fbb 100644 --- a/base/thread.cc +++ b/base/thread.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -29,7 +29,9 @@ struct Thread::StartupData { // Used to synchronize thread startup. WaitableEvent event; - StartupData(const Options& opt) : options(opt), event(false, false) {} + explicit StartupData(const Options& opt) + : options(opt), + event(false, false) {} }; Thread::Thread(const char *name) @@ -161,6 +163,7 @@ void Thread::ThreadMain() { // We can't receive messages anymore. message_loop_ = NULL; + thread_id_ = 0; } } // namespace base diff --git a/base/thread.h b/base/thread.h index 13aa35b..5a51bec 100644 --- a/base/thread.h +++ b/base/thread.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -22,7 +22,7 @@ class Thread : PlatformThread::Delegate { struct Options { // Specifies the type of message loop that will be allocated on the thread. MessageLoop::Type message_loop_type; - + // Specifies the maximum stack size that the thread is allowed to use. // This does not necessarily correspond to the thread's initial stack size. // A value of 0 indicates that the default maximum should be used. @@ -104,6 +104,10 @@ class Thread : PlatformThread::Delegate { // The thread ID. PlatformThreadId thread_id() const { return thread_id_; } + // Returns true if the thread has been started, and not yet stopped. + // When a thread is running, the thread_id_ is non-zero. + bool IsRunning() const { return thread_id_ != 0; } + protected: // Called just prior to starting the message loop virtual void Init() {} diff --git a/base/thread_unittest.cc b/base/thread_unittest.cc index f741951..f72c9c4 100644 --- a/base/thread_unittest.cc +++ b/base/thread_unittest.cc @@ -57,16 +57,22 @@ TEST_F(ThreadTest, Restart) { Thread a("Restart"); a.Stop(); EXPECT_FALSE(a.message_loop()); + EXPECT_FALSE(a.IsRunning()); EXPECT_TRUE(a.Start()); EXPECT_TRUE(a.message_loop()); + EXPECT_TRUE(a.IsRunning()); a.Stop(); EXPECT_FALSE(a.message_loop()); + EXPECT_FALSE(a.IsRunning()); EXPECT_TRUE(a.Start()); EXPECT_TRUE(a.message_loop()); + EXPECT_TRUE(a.IsRunning()); a.Stop(); EXPECT_FALSE(a.message_loop()); + EXPECT_FALSE(a.IsRunning()); a.Stop(); EXPECT_FALSE(a.message_loop()); + EXPECT_FALSE(a.IsRunning()); } TEST_F(ThreadTest, StartWithOptions_StackSize) { @@ -77,6 +83,7 @@ TEST_F(ThreadTest, StartWithOptions_StackSize) { options.stack_size = 12*1024; EXPECT_TRUE(a.StartWithOptions(options)); EXPECT_TRUE(a.message_loop()); + EXPECT_TRUE(a.IsRunning()); bool was_invoked = false; a.message_loop()->PostTask(FROM_HERE, new ToggleValue(&was_invoked)); @@ -110,10 +117,12 @@ TEST_F(ThreadTest, StopSoon) { Thread a("StopSoon"); EXPECT_TRUE(a.Start()); EXPECT_TRUE(a.message_loop()); + EXPECT_TRUE(a.IsRunning()); a.StopSoon(); a.StopSoon(); a.Stop(); EXPECT_FALSE(a.message_loop()); + EXPECT_FALSE(a.IsRunning()); } TEST_F(ThreadTest, ThreadName) { |