OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/threading/thread.h" | 5 #include "base/threading/thread.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
10 #include "base/threading/thread_local.h" | 10 #include "base/threading/thread_local.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 WaitableEvent event; | 40 WaitableEvent event; |
41 | 41 |
42 explicit StartupData(const Options& opt) | 42 explicit StartupData(const Options& opt) |
43 : options(opt), | 43 : options(opt), |
44 event(false, false) {} | 44 event(false, false) {} |
45 }; | 45 }; |
46 | 46 |
47 Thread::Thread(const char* name) | 47 Thread::Thread(const char* name) |
48 : started_(false), | 48 : started_(false), |
49 stopping_(false), | 49 stopping_(false), |
| 50 running_(false), |
50 startup_data_(NULL), | 51 startup_data_(NULL), |
51 thread_(0), | 52 thread_(0), |
52 message_loop_(NULL), | 53 message_loop_(NULL), |
53 thread_id_(kInvalidThreadId), | 54 thread_id_(kInvalidThreadId), |
54 name_(name) { | 55 name_(name) { |
55 } | 56 } |
56 | 57 |
57 Thread::~Thread() { | 58 Thread::~Thread() { |
58 Stop(); | 59 Stop(); |
59 } | 60 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 // with ThreadMain, so we annotate it to stay silent under ThreadSanitizer. | 118 // with ThreadMain, so we annotate it to stay silent under ThreadSanitizer. |
118 DCHECK_NE(ANNOTATE_UNPROTECTED_READ(thread_id_), PlatformThread::CurrentId()); | 119 DCHECK_NE(ANNOTATE_UNPROTECTED_READ(thread_id_), PlatformThread::CurrentId()); |
119 | 120 |
120 if (stopping_ || !message_loop_) | 121 if (stopping_ || !message_loop_) |
121 return; | 122 return; |
122 | 123 |
123 stopping_ = true; | 124 stopping_ = true; |
124 message_loop_->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper)); | 125 message_loop_->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper)); |
125 } | 126 } |
126 | 127 |
| 128 bool Thread::IsRunning() const { |
| 129 return running_; |
| 130 } |
| 131 |
127 void Thread::Run(MessageLoop* message_loop) { | 132 void Thread::Run(MessageLoop* message_loop) { |
128 message_loop->Run(); | 133 message_loop->Run(); |
129 } | 134 } |
130 | 135 |
131 void Thread::SetThreadWasQuitProperly(bool flag) { | 136 void Thread::SetThreadWasQuitProperly(bool flag) { |
132 lazy_tls_bool.Pointer()->Set(flag); | 137 lazy_tls_bool.Pointer()->Set(flag); |
133 } | 138 } |
134 | 139 |
135 bool Thread::GetThreadWasQuitProperly() { | 140 bool Thread::GetThreadWasQuitProperly() { |
136 bool quit_properly = true; | 141 bool quit_properly = true; |
(...skipping 16 matching lines...) Expand all Loading... |
153 message_loop_ = &message_loop; | 158 message_loop_ = &message_loop; |
154 | 159 |
155 // Let the thread do extra initialization. | 160 // Let the thread do extra initialization. |
156 // Let's do this before signaling we are started. | 161 // Let's do this before signaling we are started. |
157 Init(); | 162 Init(); |
158 | 163 |
159 startup_data_->event.Signal(); | 164 startup_data_->event.Signal(); |
160 // startup_data_ can't be touched anymore since the starting thread is now | 165 // startup_data_ can't be touched anymore since the starting thread is now |
161 // unlocked. | 166 // unlocked. |
162 | 167 |
| 168 running_ = true; |
163 Run(message_loop_); | 169 Run(message_loop_); |
| 170 running_ = false; |
164 | 171 |
165 // Let the thread do extra cleanup. | 172 // Let the thread do extra cleanup. |
166 CleanUp(); | 173 CleanUp(); |
167 | 174 |
168 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. | 175 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. |
169 DCHECK(GetThreadWasQuitProperly()); | 176 DCHECK(GetThreadWasQuitProperly()); |
170 | 177 |
171 // We can't receive messages anymore. | 178 // We can't receive messages anymore. |
172 message_loop_ = NULL; | 179 message_loop_ = NULL; |
173 } | 180 } |
174 thread_id_ = kInvalidThreadId; | |
175 } | 181 } |
176 | 182 |
177 } // namespace base | 183 } // namespace base |
OLD | NEW |