| 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 "content/browser/browser_thread_impl.h" | 5 #include "content/browser/browser_thread_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/atomicops.h" | 9 #include "base/atomicops.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 | 152 |
| 153 void BrowserThreadImpl::Init() { | 153 void BrowserThreadImpl::Init() { |
| 154 BrowserThreadGlobals& globals = g_globals.Get(); | 154 BrowserThreadGlobals& globals = g_globals.Get(); |
| 155 | 155 |
| 156 using base::subtle::AtomicWord; | 156 using base::subtle::AtomicWord; |
| 157 AtomicWord* storage = | 157 AtomicWord* storage = |
| 158 reinterpret_cast<AtomicWord*>(&globals.thread_delegates[identifier_]); | 158 reinterpret_cast<AtomicWord*>(&globals.thread_delegates[identifier_]); |
| 159 AtomicWord stored_pointer = base::subtle::NoBarrier_Load(storage); | 159 AtomicWord stored_pointer = base::subtle::NoBarrier_Load(storage); |
| 160 BrowserThreadDelegate* delegate = | 160 BrowserThreadDelegate* delegate = |
| 161 reinterpret_cast<BrowserThreadDelegate*>(stored_pointer); | 161 reinterpret_cast<BrowserThreadDelegate*>(stored_pointer); |
| 162 if (delegate) { | 162 if (delegate) |
| 163 delegate->Init(); | 163 delegate->Init(); |
| 164 message_loop()->PostTask(FROM_HERE, | |
| 165 base::Bind(&BrowserThreadDelegate::InitAsync, | |
| 166 // Delegate is expected to exist for the | |
| 167 // duration of the thread's lifetime | |
| 168 base::Unretained(delegate))); | |
| 169 } | |
| 170 } | 164 } |
| 171 | 165 |
| 172 // We disable optimizations for this block of functions so the compiler doesn't | 166 // We disable optimizations for this block of functions so the compiler doesn't |
| 173 // merge them all together. | 167 // merge them all together. |
| 174 MSVC_DISABLE_OPTIMIZE() | 168 MSVC_DISABLE_OPTIMIZE() |
| 175 MSVC_PUSH_DISABLE_WARNING(4748) | 169 MSVC_PUSH_DISABLE_WARNING(4748) |
| 176 | 170 |
| 177 NOINLINE void BrowserThreadImpl::UIThreadRun(base::MessageLoop* message_loop) { | 171 NOINLINE void BrowserThreadImpl::UIThreadRun(base::MessageLoop* message_loop) { |
| 178 volatile int line_number = __LINE__; | 172 volatile int line_number = __LINE__; |
| 179 Thread::Run(message_loop); | 173 Thread::Run(message_loop); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 globals.threads[identifier_] = NULL; | 287 globals.threads[identifier_] = NULL; |
| 294 #ifndef NDEBUG | 288 #ifndef NDEBUG |
| 295 // Double check that the threads are ordered correctly in the enumeration. | 289 // Double check that the threads are ordered correctly in the enumeration. |
| 296 for (int i = identifier_ + 1; i < ID_COUNT; ++i) { | 290 for (int i = identifier_ + 1; i < ID_COUNT; ++i) { |
| 297 DCHECK(!globals.threads[i]) << | 291 DCHECK(!globals.threads[i]) << |
| 298 "Threads must be listed in the reverse order that they die"; | 292 "Threads must be listed in the reverse order that they die"; |
| 299 } | 293 } |
| 300 #endif | 294 #endif |
| 301 } | 295 } |
| 302 | 296 |
| 297 bool BrowserThreadImpl::StartWithOptions(const Options& options) { |
| 298 // The global thread table needs to be locked while a new thread is |
| 299 // starting, as the new thread can asynchronously start touching the |
| 300 // table (and other thread's message_loop). |
| 301 BrowserThreadGlobals& globals = g_globals.Get(); |
| 302 base::AutoLock lock(globals.lock); |
| 303 return Thread::StartWithOptions(options); |
| 304 } |
| 305 |
| 303 // static | 306 // static |
| 304 bool BrowserThreadImpl::PostTaskHelper( | 307 bool BrowserThreadImpl::PostTaskHelper( |
| 305 BrowserThread::ID identifier, | 308 BrowserThread::ID identifier, |
| 306 const tracked_objects::Location& from_here, | 309 const tracked_objects::Location& from_here, |
| 307 const base::Closure& task, | 310 const base::Closure& task, |
| 308 base::TimeDelta delay, | 311 base::TimeDelta delay, |
| 309 bool nestable) { | 312 bool nestable) { |
| 310 DCHECK(identifier >= 0 && identifier < ID_COUNT); | 313 DCHECK(identifier >= 0 && identifier < ID_COUNT); |
| 311 // Optimization: to avoid unnecessary locks, we listed the ID enumeration in | 314 // Optimization: to avoid unnecessary locks, we listed the ID enumeration in |
| 312 // order of lifetime. So no need to lock if we know that the target thread | 315 // order of lifetime. So no need to lock if we know that the target thread |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 540 AtomicWord* storage = reinterpret_cast<AtomicWord*>( | 543 AtomicWord* storage = reinterpret_cast<AtomicWord*>( |
| 541 &globals.thread_delegates[identifier]); | 544 &globals.thread_delegates[identifier]); |
| 542 AtomicWord old_pointer = base::subtle::NoBarrier_AtomicExchange( | 545 AtomicWord old_pointer = base::subtle::NoBarrier_AtomicExchange( |
| 543 storage, reinterpret_cast<AtomicWord>(delegate)); | 546 storage, reinterpret_cast<AtomicWord>(delegate)); |
| 544 | 547 |
| 545 // This catches registration when previously registered. | 548 // This catches registration when previously registered. |
| 546 DCHECK(!delegate || !old_pointer); | 549 DCHECK(!delegate || !old_pointer); |
| 547 } | 550 } |
| 548 | 551 |
| 549 } // namespace content | 552 } // namespace content |
| OLD | NEW |