| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/location.h" | 9 #include "base/location.h" |
| 10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 (options.message_loop_type == MessageLoop::TYPE_UI)); | 87 (options.message_loop_type == MessageLoop::TYPE_UI)); |
| 88 #endif | 88 #endif |
| 89 | 89 |
| 90 SetThreadWasQuitProperly(false); | 90 SetThreadWasQuitProperly(false); |
| 91 | 91 |
| 92 MessageLoop::Type type = options.message_loop_type; | 92 MessageLoop::Type type = options.message_loop_type; |
| 93 if (!options.message_pump_factory.is_null()) | 93 if (!options.message_pump_factory.is_null()) |
| 94 type = MessageLoop::TYPE_CUSTOM; | 94 type = MessageLoop::TYPE_CUSTOM; |
| 95 | 95 |
| 96 message_loop_timer_slack_ = options.timer_slack; | 96 message_loop_timer_slack_ = options.timer_slack; |
| 97 message_loop_ = new MessageLoop(type, options.message_pump_factory); | 97 scoped_ptr<MessageLoop> message_loop = MessageLoop::CreateUnbound( |
| 98 | 98 type, options.message_pump_factory); |
| 99 message_loop_ = message_loop.get(); |
| 99 start_event_.reset(new WaitableEvent(false, false)); | 100 start_event_.reset(new WaitableEvent(false, false)); |
| 100 | 101 |
| 101 // Hold the thread_lock_ while starting a new thread, so that we can make sure | 102 // Hold the thread_lock_ while starting a new thread, so that we can make sure |
| 102 // that thread_ is populated before the newly created thread accesses it. | 103 // that thread_ is populated before the newly created thread accesses it. |
| 103 { | 104 { |
| 104 AutoLock lock(thread_lock_); | 105 AutoLock lock(thread_lock_); |
| 105 bool created; | 106 bool created; |
| 106 if (options.priority == ThreadPriority::NORMAL) { | 107 if (options.priority == ThreadPriority::NORMAL) { |
| 107 created = PlatformThread::Create(options.stack_size, this, &thread_); | 108 created = PlatformThread::Create(options.stack_size, this, &thread_); |
| 108 } else { | 109 } else { |
| 109 created = PlatformThread::CreateWithPriority(options.stack_size, this, | 110 created = PlatformThread::CreateWithPriority(options.stack_size, this, |
| 110 &thread_, options.priority); | 111 &thread_, options.priority); |
| 111 } | 112 } |
| 112 if (!created) { | 113 if (!created) { |
| 113 DLOG(ERROR) << "failed to create thread"; | 114 DLOG(ERROR) << "failed to create thread"; |
| 114 delete message_loop_; | |
| 115 message_loop_ = nullptr; | 115 message_loop_ = nullptr; |
| 116 start_event_.reset(); | 116 start_event_.reset(); |
| 117 return false; | 117 return false; |
| 118 } | 118 } |
| 119 } | 119 } |
| 120 | 120 |
| 121 // The ownership of message_loop is managemed by the newly created thread |
| 122 // within the ThreadMain. |
| 123 ignore_result(message_loop.release()); |
| 124 |
| 121 DCHECK(message_loop_); | 125 DCHECK(message_loop_); |
| 122 return true; | 126 return true; |
| 123 } | 127 } |
| 124 | 128 |
| 125 bool Thread::StartAndWaitForTesting() { | 129 bool Thread::StartAndWaitForTesting() { |
| 126 bool result = Start(); | 130 bool result = Start(); |
| 127 if (!result) | 131 if (!result) |
| 128 return false; | 132 return false; |
| 129 WaitUntilThreadStarted(); | 133 WaitUntilThreadStarted(); |
| 130 return true; | 134 return true; |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 | 261 |
| 258 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. | 262 // Assert that MessageLoop::Quit was called by ThreadQuitHelper. |
| 259 DCHECK(GetThreadWasQuitProperly()); | 263 DCHECK(GetThreadWasQuitProperly()); |
| 260 | 264 |
| 261 // We can't receive messages anymore. | 265 // We can't receive messages anymore. |
| 262 // (The message loop is destructed at the end of this block) | 266 // (The message loop is destructed at the end of this block) |
| 263 message_loop_ = NULL; | 267 message_loop_ = NULL; |
| 264 } | 268 } |
| 265 | 269 |
| 266 } // namespace base | 270 } // namespace base |
| OLD | NEW |