Index: base/threading/thread.h |
diff --git a/base/threading/thread.h b/base/threading/thread.h |
index 45064240d76f8436ceb7ab51ff205be4646a8485..3699c954f463b66d8a10450c3a9db7c0ac5074cf 100644 |
--- a/base/threading/thread.h |
+++ b/base/threading/thread.h |
@@ -17,6 +17,7 @@ |
#include "base/message_loop/timer_slack.h" |
#include "base/sequence_checker.h" |
#include "base/single_thread_task_runner.h" |
+#include "base/synchronization/atomic_flag.h" |
#include "base/synchronization/lock.h" |
#include "base/synchronization/waitable_event.h" |
#include "base/threading/platform_thread.h" |
@@ -74,6 +75,16 @@ class BASE_EXPORT Thread : PlatformThread::Delegate { |
// Specifies the initial thread priority. |
ThreadPriority priority = ThreadPriority::NORMAL; |
+ |
+ // If false, the thread will not be joined on destruction. This is intended |
+ // for threads that want TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN |
+ // semantics. Stop() will not be synchronous and will instead merely have |
+ // StopSoon() semantics on such threads. Thread instances can only be |
+ // destroyed after their ThreadMain() returns (which typically means that |
+ // non-joinable instances must be leaked). |
+ // TODO(gab): allow non-joinable instances to be deleted without causing |
+ // user-after-frees (proposal @ https://crbug.com/629139#c14) |
+ bool joinable = true; |
}; |
// Constructor. |
@@ -131,12 +142,14 @@ class BASE_EXPORT Thread : PlatformThread::Delegate { |
// carefully for production code. |
bool WaitUntilThreadStarted() const; |
- // 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). |
+ // Signals the thread to exit and returns once the thread has exited (or right |
+ // away if the thread is non-joinable). For joinable threads only: 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) -- non-joinable |
+ // threads are not re-usable. |
// |
// Stop may be called multiple times and is simply ignored if the thread is |
- // already stopped. |
+ // already stopped or currently stopping. |
// |
// NOTE: If you are a consumer of Thread, it is not necessary to call this |
// before deleting your Thread objects, as the destructor will do it. |
@@ -228,10 +241,8 @@ class BASE_EXPORT Thread : PlatformThread::Delegate { |
static void SetThreadWasQuitProperly(bool flag); |
static bool GetThreadWasQuitProperly(); |
- void set_message_loop(MessageLoop* message_loop) { |
- DCHECK(owning_sequence_checker_.CalledOnValidSequencedThread()); |
- message_loop_ = message_loop; |
- } |
+ // Bind this Thread to an existing MessageLoop instead of starting a new one. |
+ void SetMessageLoop(MessageLoop* message_loop); |
private: |
#if defined(OS_WIN) |
@@ -275,6 +286,11 @@ class BASE_EXPORT Thread : PlatformThread::Delegate { |
MessageLoop* message_loop_ = nullptr; |
RunLoop* run_loop_ = nullptr; |
+ // True only if |message_loop_| was externally provided by |SetMessageLoop()| |
+ // in which case this Thread has no underlying |thread_| and should merely |
+ // drop |message_loop_| on Stop(). |
+ bool using_external_message_loop_ = false; |
+ |
// Stores Options::timer_slack_ until the message loop has been bound to |
// a thread. |
TimerSlack message_loop_timer_slack_ = TIMER_SLACK_NONE; |
@@ -289,6 +305,12 @@ class BASE_EXPORT Thread : PlatformThread::Delegate { |
// sequence of the Thread. |
SequenceChecker owning_sequence_checker_; |
+ // Set when ThreadMain() returns. The pointer itself is null until the thread |
+ // is started for the first time. so a Thread()/Start()/Stop()/Start()/... |
+ // sequence will result in null/false/true/false/... state for |
+ // |thread_main_exited_|. |
+ std::unique_ptr<AtomicFlag> thread_main_exited_; |
+ |
DISALLOW_COPY_AND_ASSIGN(Thread); |
}; |