Index: base/threading/simple_thread.cc |
diff --git a/base/threading/simple_thread.cc b/base/threading/simple_thread.cc |
index 9eb443afab1b5d5dcb3dcd7427f8c4a1faacbb1b..0e7d9e6eed2ee7d0571d3f6e13642ef1811be2a5 100644 |
--- a/base/threading/simple_thread.cc |
+++ b/base/threading/simple_thread.cc |
@@ -18,17 +18,25 @@ SimpleThread::SimpleThread(const std::string& name_prefix, |
const Options& options) |
: name_prefix_(name_prefix), |
options_(options), |
- event_(WaitableEvent::ResetPolicy::MANUAL, |
- WaitableEvent::InitialState::NOT_SIGNALED) {} |
+ id_event_(WaitableEvent::ResetPolicy::MANUAL, |
+ WaitableEvent::InitialState::NOT_SIGNALED) {} |
SimpleThread::~SimpleThread() { |
- DCHECK(HasBeenStarted()) << "SimpleThread was never started."; |
- DCHECK(!options_.joinable || HasBeenJoined()) |
+#if DCHECK_IS_ON() |
+ DCHECK(has_been_started_) << "SimpleThread was never started."; |
+ DCHECK(!options_.joinable || has_been_joined_) |
<< "Joinable SimpleThread destroyed without being Join()ed."; |
+#endif |
} |
void SimpleThread::Start() { |
- DCHECK(!HasBeenStarted()) << "Tried to Start a thread multiple times."; |
+#if DCHECK_IS_ON() |
+ DCHECK(!has_been_started_) << "Tried to Start a thread multiple times."; |
+ |
+ // Set |has_been_started_| before creating the thread so that Run() sees it. |
+ // Otherwise, Run() could hit a DCHECK when deleting |this|. |
gab
2017/02/06 21:19:28
// Set |has_been_started_| before creating the thr
fdoray
2017/02/07 12:57:16
Done.
|
+ has_been_started_ = true; |
+#endif |
bool success = |
options_.joinable |
? PlatformThread::CreateWithPriority(options_.stack_size, this, |
@@ -36,35 +44,39 @@ void SimpleThread::Start() { |
: PlatformThread::CreateNonJoinableWithPriority( |
options_.stack_size, this, options_.priority); |
DCHECK(success); |
- ThreadRestrictions::ScopedAllowWait allow_wait; |
- event_.Wait(); // Wait for the thread to complete initialization. |
+ |
+ // No member access after creating the thread, |this| can be deleted at any |
+ // point after invoking Run() on non-joinable threads. |
} |
void SimpleThread::Join() { |
+#if DCHECK_IS_ON() |
DCHECK(options_.joinable) << "A non-joinable thread can't be joined."; |
- DCHECK(HasBeenStarted()) << "Tried to Join a never-started thread."; |
- DCHECK(!HasBeenJoined()) << "Tried to Join a thread multiple times."; |
+ DCHECK(has_been_started_) << "Tried to Join a never-started thread."; |
+ DCHECK(!has_been_joined_) << "Tried to Join a thread multiple times."; |
+#endif |
PlatformThread::Join(thread_); |
thread_ = PlatformThreadHandle(); |
- joined_ = true; |
+#if DCHECK_IS_ON() |
+ has_been_joined_ = true; |
+#endif |
} |
-bool SimpleThread::HasBeenStarted() { |
- ThreadRestrictions::ScopedAllowWait allow_wait; |
- return event_.IsSignaled(); |
+PlatformThreadId SimpleThread::GetTid() const { |
+ id_event_.Wait(); |
+ return tid_; |
} |
void SimpleThread::ThreadMain() { |
tid_ = PlatformThread::CurrentId(); |
+ id_event_.Signal(); |
+ |
// Construct our full name of the form "name_prefix_/TID". |
std::string name(name_prefix_); |
name.push_back('/'); |
name.append(IntToString(tid_)); |
PlatformThread::SetName(name); |
- // We've initialized our new thread, signal that we're done to Start(). |
- event_.Signal(); |
- |
Run(); |
} |