| 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" |
| 11 #include "base/threading/thread_restrictions.h" |
| 11 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
| 12 | 13 |
| 13 namespace base { | 14 namespace base { |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 // We use this thread-local variable to record whether or not a thread exited | 18 // We use this thread-local variable to record whether or not a thread exited |
| 18 // because its Stop method was called. This allows us to catch cases where | 19 // because its Stop method was called. This allows us to catch cases where |
| 19 // MessageLoop::Quit() is called directly, which is unexpected when using a | 20 // MessageLoop::Quit() is called directly, which is unexpected when using a |
| 20 // Thread to setup and run a MessageLoop. | 21 // Thread to setup and run a MessageLoop. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 StartupData startup_data(options); | 70 StartupData startup_data(options); |
| 70 startup_data_ = &startup_data; | 71 startup_data_ = &startup_data; |
| 71 | 72 |
| 72 if (!PlatformThread::Create(options.stack_size, this, &thread_)) { | 73 if (!PlatformThread::Create(options.stack_size, this, &thread_)) { |
| 73 DLOG(ERROR) << "failed to create thread"; | 74 DLOG(ERROR) << "failed to create thread"; |
| 74 startup_data_ = NULL; | 75 startup_data_ = NULL; |
| 75 return false; | 76 return false; |
| 76 } | 77 } |
| 77 | 78 |
| 78 // Wait for the thread to start and initialize message_loop_ | 79 // Wait for the thread to start and initialize message_loop_ |
| 80 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 79 startup_data.event.Wait(); | 81 startup_data.event.Wait(); |
| 80 | 82 |
| 81 // set it to NULL so we don't keep a pointer to some object on the stack. | 83 // set it to NULL so we don't keep a pointer to some object on the stack. |
| 82 startup_data_ = NULL; | 84 startup_data_ = NULL; |
| 83 started_ = true; | 85 started_ = true; |
| 84 | 86 |
| 85 DCHECK(message_loop_); | 87 DCHECK(message_loop_); |
| 86 return true; | 88 return true; |
| 87 } | 89 } |
| 88 | 90 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. | 168 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. |
| 167 DCHECK(GetThreadWasQuitProperly()); | 169 DCHECK(GetThreadWasQuitProperly()); |
| 168 | 170 |
| 169 // We can't receive messages anymore. | 171 // We can't receive messages anymore. |
| 170 message_loop_ = NULL; | 172 message_loop_ = NULL; |
| 171 } | 173 } |
| 172 thread_id_ = kInvalidThreadId; | 174 thread_id_ = kInvalidThreadId; |
| 173 } | 175 } |
| 174 | 176 |
| 175 } // namespace base | 177 } // namespace base |
| OLD | NEW |