Index: base/threading/thread.h |
diff --git a/base/threading/thread.h b/base/threading/thread.h |
index 5010f0ec428bdcc69e54a9fdfe723862cacd75aa..8ec02e6c566eca374e07041b769ed699caabca65 100644 |
--- a/base/threading/thread.h |
+++ b/base/threading/thread.h |
@@ -13,11 +13,13 @@ |
#include "base/message_loop/message_loop.h" |
#include "base/message_loop/message_loop_proxy.h" |
#include "base/message_loop/timer_slack.h" |
+#include "base/synchronization/lock.h" |
#include "base/threading/platform_thread.h" |
namespace base { |
class MessagePump; |
+class WaitableEvent; |
// A simple thread abstraction that establishes a MessageLoop on a new thread. |
// The consumer uses the MessageLoop of the thread to cause code to execute on |
@@ -45,7 +47,7 @@ class BASE_EXPORT Thread : PlatformThread::Delegate { |
// This is ignored if message_pump_factory.is_null() is false. |
MessageLoop::Type message_loop_type; |
- // Specify timer slack for thread message loop. |
+ // Specifies timer slack for thread message loop. |
TimerSlack timer_slack; |
// Used to create the MessagePump for the MessageLoop. The callback is Run() |
@@ -81,7 +83,7 @@ class BASE_EXPORT Thread : PlatformThread::Delegate { |
// init_com_with_mta(false) and then StartWithOptions() with any message loop |
// type other than TYPE_UI. |
void init_com_with_mta(bool use_mta) { |
- DCHECK(!started_); |
+ DCHECK(!start_event_); |
com_status_ = use_mta ? MTA : STA; |
} |
#endif |
@@ -103,6 +105,17 @@ class BASE_EXPORT Thread : PlatformThread::Delegate { |
// callback. |
bool StartWithOptions(const Options& options); |
+ // Starts the thread and wait for the thread to start and run initialization |
+ // before returning. It's same as calling Start() and then |
+ // WaitUntilThreadStarted(). |
+ // Note that using this (instead of Start() or StartWithOptions() causes |
+ // jank on the calling thread, and must be used carefully for production code. |
+ bool StartAndWait(); |
+ |
+ // Blocks until the thread starts running. Called within StartAndWait(), |
+ // otherwise supposed to be used only for testing. |
+ bool WaitUntilThreadStarted(); |
+ |
// Signals the thread to exit and returns once the thread has exited. After |
// this method returns, the Thread object is completely reset and may be used |
// as if it were newly constructed (i.e., Start may be called again). |
@@ -128,27 +141,13 @@ class BASE_EXPORT Thread : PlatformThread::Delegate { |
// the thread object once it is known that the thread has quit. |
void StopSoon(); |
- // Returns the message loop for this thread. Use the MessageLoop's |
- // PostTask methods to execute code on the thread. This only returns |
- // non-null after a successful call to Start. After Stop has been called, |
- // this will return NULL. |
- // |
- // NOTE: You must not call this MessageLoop's Quit method directly. Use |
- // the Thread's Stop method instead. |
- // |
+ // TODO(kinuko): These are racy now, need to be removed |
+ /* |
MessageLoop* message_loop() const { return message_loop_; } |
- |
- // Returns a MessageLoopProxy for this thread. Use the MessageLoopProxy's |
- // PostTask methods to execute code on the thread. Returns NULL if the thread |
- // is not running (e.g. before Start or after Stop have been called). Callers |
- // can hold on to this even after the thread is gone; in this situation, |
- // attempts to PostTask() will fail. |
- // |
- // Note: This method is deprecated. Callers should call task_runner() instead |
- // and use the TaskRunner interfaces for safely interfacing with the Thread. |
scoped_refptr<MessageLoopProxy> message_loop_proxy() const { |
return message_loop_ ? message_loop_->message_loop_proxy() : NULL; |
} |
+ */ |
// Returns a TaskRunner for this thread. Use the TaskRunner's PostTask |
// methods to execute code on the thread. Returns NULL if the thread is not |
@@ -156,7 +155,7 @@ class BASE_EXPORT Thread : PlatformThread::Delegate { |
// hold on to this even after the thread is gone; in this situation, attempts |
// to PostTask() will fail. |
scoped_refptr<SingleThreadTaskRunner> task_runner() const { |
- return message_loop_proxy(); |
+ return task_runner_; |
} |
// Returns the name of this thread (for display in debugger too). |
@@ -166,7 +165,7 @@ class BASE_EXPORT Thread : PlatformThread::Delegate { |
PlatformThreadHandle thread_handle() { return thread_; } |
// The thread ID. |
- PlatformThreadId thread_id() const { return thread_id_; } |
+ PlatformThreadId thread_id() const { return thread_.id(); } |
// Returns true if the thread has been started, and not yet stopped. |
bool IsRunning() const; |
@@ -200,6 +199,8 @@ class BASE_EXPORT Thread : PlatformThread::Delegate { |
}; |
#endif |
+ class IncomingTaskQueueRunner; |
+ |
// PlatformThread::Delegate methods: |
void ThreadMain() override; |
@@ -208,19 +209,16 @@ class BASE_EXPORT Thread : PlatformThread::Delegate { |
ComStatus com_status_; |
#endif |
- // Whether we successfully started the thread. |
- bool started_; |
- |
// If true, we're in the middle of stopping, and shouldn't access |
// |message_loop_|. It may non-NULL and invalid. |
bool stopping_; |
// True while inside of Run(). |
+ mutable base::Lock lock_; |
bool running_; |
// Used to pass data to ThreadMain. |
- struct StartupData; |
- StartupData* startup_data_; |
+ scoped_ptr<Options> startup_data_; |
// The thread's handle. |
PlatformThreadHandle thread_; |
@@ -229,12 +227,14 @@ class BASE_EXPORT Thread : PlatformThread::Delegate { |
// by the created thread. |
MessageLoop* message_loop_; |
- // Our thread's ID. |
- PlatformThreadId thread_id_; |
- |
// The name of the thread. Used for debugging purposes. |
std::string name_; |
+ // Non-null if the thread has successfully started. |
+ scoped_ptr<WaitableEvent> start_event_; |
+ |
+ scoped_refptr<SingleThreadTaskRunner> task_runner_; |
+ |
friend void ThreadQuitHelper(); |
DISALLOW_COPY_AND_ASSIGN(Thread); |