| 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 "content/browser/browser_thread_impl.h" | 5 #include "content/browser/browser_thread_impl.h" |
| 6 | 6 |
| 7 #include "base/atomicops.h" |
| 7 #include "base/bind.h" | 8 #include "base/bind.h" |
| 8 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 10 #include "base/message_loop_proxy.h" | 11 #include "base/message_loop_proxy.h" |
| 11 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
| 12 | 13 |
| 14 namespace content { |
| 15 |
| 13 namespace { | 16 namespace { |
| 14 | 17 |
| 15 // Friendly names for the well-known threads. | 18 // Friendly names for the well-known threads. |
| 16 static const char* browser_thread_names[content::BrowserThread::ID_COUNT] = { | 19 static const char* g_browser_thread_names[BrowserThread::ID_COUNT] = { |
| 17 "", // UI (name assembled in browser_main.cc). | 20 "", // UI (name assembled in browser_main.cc). |
| 18 "Chrome_DBThread", // DB | 21 "Chrome_DBThread", // DB |
| 19 "Chrome_WebKitThread", // WEBKIT | 22 "Chrome_WebKitThread", // WEBKIT |
| 20 "Chrome_FileThread", // FILE | 23 "Chrome_FileThread", // FILE |
| 21 "Chrome_ProcessLauncherThread", // PROCESS_LAUNCHER | 24 "Chrome_ProcessLauncherThread", // PROCESS_LAUNCHER |
| 22 "Chrome_CacheThread", // CACHE | 25 "Chrome_CacheThread", // CACHE |
| 23 "Chrome_IOThread", // IO | 26 "Chrome_IOThread", // IO |
| 24 #if defined(OS_CHROMEOS) | 27 #if defined(OS_CHROMEOS) |
| 25 "Chrome_WebSocketproxyThread", // WEB_SOCKET_PROXY | 28 "Chrome_WebSocketproxyThread", // WEB_SOCKET_PROXY |
| 26 #endif | 29 #endif |
| 27 }; | 30 }; |
| 28 | 31 |
| 29 } // namespace | 32 // This lock protects |g_browser_threads|. Do not read or modify that |
| 30 | 33 // array without holding this lock. Do not block while holding this |
| 31 namespace content { | 34 // lock. |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 // This lock protects |g_browser_threads|. Do not read or modify that array | |
| 36 // without holding this lock. Do not block while holding this lock. | |
| 37 base::LazyInstance<base::Lock, | 35 base::LazyInstance<base::Lock, |
| 38 base::LeakyLazyInstanceTraits<base::Lock> > | 36 base::LeakyLazyInstanceTraits<base::Lock> > |
| 39 g_lock = LAZY_INSTANCE_INITIALIZER; | 37 g_lock = LAZY_INSTANCE_INITIALIZER; |
| 40 | 38 |
| 39 // This array is protected by |g_lock|. The threads are not owned by this |
| 40 // array. Typically, the threads are owned on the UI thread by |
| 41 // content::BrowserMainLoop. BrowserThreadImpl objects remove |
| 42 // themselves from this array upon destruction. |
| 43 static BrowserThreadImpl* g_browser_threads[BrowserThread::ID_COUNT]; |
| 41 | 44 |
| 42 // An array of the BrowserThread objects. This array is protected by |g_lock|. | 45 // Only atomic operations are used on this array. The delegates are |
| 43 // The threads are not owned by this array. Typically, the threads are owned | 46 // not owned by this array, rather by whoever calls |
| 44 // on the UI thread by the g_browser_process object. BrowserThreads remove | 47 // BrowserThread::SetDelegate. |
| 45 // themselves from this array upon destruction. | 48 static BrowserThreadDelegate* g_browser_thread_delegates[ |
| 46 BrowserThread* g_browser_threads[BrowserThread::ID_COUNT]; | 49 BrowserThread::ID_COUNT]; |
| 47 | 50 |
| 48 } // namespace | 51 } // namespace |
| 49 | 52 |
| 50 BrowserThreadImpl::BrowserThreadImpl(BrowserThread::ID identifier) | 53 BrowserThreadImpl::BrowserThreadImpl(ID identifier) |
| 51 : BrowserThread(identifier) { | 54 : Thread(g_browser_thread_names[identifier]), |
| 55 identifier_(identifier) { |
| 56 Initialize(); |
| 52 } | 57 } |
| 53 | 58 |
| 54 BrowserThreadImpl::BrowserThreadImpl(BrowserThread::ID identifier, | 59 BrowserThreadImpl::BrowserThreadImpl(ID identifier, |
| 55 MessageLoop* message_loop) | 60 MessageLoop* message_loop) |
| 56 : BrowserThread(identifier, message_loop) { | 61 : Thread(message_loop->thread_name().c_str()), |
| 62 identifier_(identifier) { |
| 63 set_message_loop(message_loop); |
| 64 Initialize(); |
| 65 } |
| 66 |
| 67 void BrowserThreadImpl::Init() { |
| 68 using base::subtle::AtomicWord; |
| 69 AtomicWord* storage = |
| 70 reinterpret_cast<AtomicWord*>(&g_browser_thread_delegates[identifier_]); |
| 71 AtomicWord stored_pointer = base::subtle::NoBarrier_Load(storage); |
| 72 BrowserThreadDelegate* delegate = |
| 73 reinterpret_cast<BrowserThreadDelegate*>(stored_pointer); |
| 74 if (delegate) |
| 75 delegate->Init(); |
| 76 } |
| 77 |
| 78 void BrowserThreadImpl::CleanUp() { |
| 79 using base::subtle::AtomicWord; |
| 80 AtomicWord* storage = |
| 81 reinterpret_cast<AtomicWord*>(&g_browser_thread_delegates[identifier_]); |
| 82 AtomicWord stored_pointer = base::subtle::NoBarrier_Load(storage); |
| 83 BrowserThreadDelegate* delegate = |
| 84 reinterpret_cast<BrowserThreadDelegate*>(stored_pointer); |
| 85 |
| 86 // Note that we could remove the delegate pointer from |
| 87 // g_browser_thread_delegates after calling CleanUp on it, but this |
| 88 // would defeat the DCHECK in SetDelegate below that checks if a |
| 89 // delegate was previously registered for the thread. |
| 90 if (delegate) |
| 91 delegate->CleanUp(); |
| 92 } |
| 93 |
| 94 void BrowserThreadImpl::Initialize() { |
| 95 base::AutoLock lock(g_lock.Get()); |
| 96 DCHECK(identifier_ >= 0 && identifier_ < ID_COUNT); |
| 97 DCHECK(g_browser_threads[identifier_] == NULL); |
| 98 g_browser_threads[identifier_] = this; |
| 57 } | 99 } |
| 58 | 100 |
| 59 BrowserThreadImpl::~BrowserThreadImpl() { | 101 BrowserThreadImpl::~BrowserThreadImpl() { |
| 102 // All Thread subclasses must call Stop() in the destructor. This is |
| 103 // doubly important here as various bits of code check they are on |
| 104 // the right BrowserThread. |
| 60 Stop(); | 105 Stop(); |
| 106 |
| 107 base::AutoLock lock(g_lock.Get()); |
| 108 g_browser_threads[identifier_] = NULL; |
| 109 #ifndef NDEBUG |
| 110 // Double check that the threads are ordered correctly in the enumeration. |
| 111 for (int i = identifier_ + 1; i < ID_COUNT; ++i) { |
| 112 DCHECK(!g_browser_threads[i]) << |
| 113 "Threads must be listed in the reverse order that they die"; |
| 114 } |
| 115 #endif |
| 61 } | 116 } |
| 62 | 117 |
| 63 // static | 118 // static |
| 64 bool BrowserThreadImpl::PostTaskHelper( | 119 bool BrowserThreadImpl::PostTaskHelper( |
| 65 BrowserThread::ID identifier, | 120 BrowserThread::ID identifier, |
| 66 const tracked_objects::Location& from_here, | 121 const tracked_objects::Location& from_here, |
| 67 Task* task, | 122 Task* task, |
| 68 int64 delay_ms, | 123 int64 delay_ms, |
| 69 bool nestable) { | 124 bool nestable) { |
| 70 DCHECK(identifier >= 0 && identifier < ID_COUNT); | 125 DCHECK(identifier >= 0 && identifier < ID_COUNT); |
| 71 // Optimization: to avoid unnecessary locks, we listed the ID enumeration in | 126 // Optimization: to avoid unnecessary locks, we listed the ID enumeration in |
| 72 // order of lifetime. So no need to lock if we know that the other thread | 127 // order of lifetime. So no need to lock if we know that the other thread |
| 73 // outlives this one. | 128 // outlives this one. |
| 74 // Note: since the array is so small, ok to loop instead of creating a map, | 129 // Note: since the array is so small, ok to loop instead of creating a map, |
| 75 // which would require a lock because std::map isn't thread safe, defeating | 130 // which would require a lock because std::map isn't thread safe, defeating |
| 76 // the whole purpose of this optimization. | 131 // the whole purpose of this optimization. |
| 77 BrowserThread::ID current_thread; | 132 BrowserThread::ID current_thread; |
| 78 bool guaranteed_to_outlive_target_thread = | 133 bool guaranteed_to_outlive_target_thread = |
| 79 GetCurrentThreadIdentifier(¤t_thread) && | 134 GetCurrentThreadIdentifier(¤t_thread) && |
| 80 current_thread >= identifier; | 135 current_thread <= identifier; |
| 81 | 136 |
| 82 if (!guaranteed_to_outlive_target_thread) | 137 if (!guaranteed_to_outlive_target_thread) |
| 83 g_lock.Get().Acquire(); | 138 g_lock.Get().Acquire(); |
| 84 | 139 |
| 85 MessageLoop* message_loop = g_browser_threads[identifier] ? | 140 MessageLoop* message_loop = g_browser_threads[identifier] ? |
| 86 g_browser_threads[identifier]->message_loop() : NULL; | 141 g_browser_threads[identifier]->message_loop() : NULL; |
| 87 if (message_loop) { | 142 if (message_loop) { |
| 88 if (nestable) { | 143 if (nestable) { |
| 89 message_loop->PostDelayedTask(from_here, task, delay_ms); | 144 message_loop->PostDelayedTask(from_here, task, delay_ms); |
| 90 } else { | 145 } else { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 111 DCHECK(identifier >= 0 && identifier < ID_COUNT); | 166 DCHECK(identifier >= 0 && identifier < ID_COUNT); |
| 112 // Optimization: to avoid unnecessary locks, we listed the ID enumeration in | 167 // Optimization: to avoid unnecessary locks, we listed the ID enumeration in |
| 113 // order of lifetime. So no need to lock if we know that the other thread | 168 // order of lifetime. So no need to lock if we know that the other thread |
| 114 // outlives this one. | 169 // outlives this one. |
| 115 // Note: since the array is so small, ok to loop instead of creating a map, | 170 // Note: since the array is so small, ok to loop instead of creating a map, |
| 116 // which would require a lock because std::map isn't thread safe, defeating | 171 // which would require a lock because std::map isn't thread safe, defeating |
| 117 // the whole purpose of this optimization. | 172 // the whole purpose of this optimization. |
| 118 BrowserThread::ID current_thread; | 173 BrowserThread::ID current_thread; |
| 119 bool guaranteed_to_outlive_target_thread = | 174 bool guaranteed_to_outlive_target_thread = |
| 120 GetCurrentThreadIdentifier(¤t_thread) && | 175 GetCurrentThreadIdentifier(¤t_thread) && |
| 121 current_thread >= identifier; | 176 current_thread <= identifier; |
| 122 | 177 |
| 123 if (!guaranteed_to_outlive_target_thread) | 178 if (!guaranteed_to_outlive_target_thread) |
| 124 g_lock.Get().Acquire(); | 179 g_lock.Get().Acquire(); |
| 125 | 180 |
| 126 MessageLoop* message_loop = g_browser_threads[identifier] ? | 181 MessageLoop* message_loop = g_browser_threads[identifier] ? |
| 127 g_browser_threads[identifier]->message_loop() : NULL; | 182 g_browser_threads[identifier]->message_loop() : NULL; |
| 128 if (message_loop) { | 183 if (message_loop) { |
| 129 if (nestable) { | 184 if (nestable) { |
| 130 message_loop->PostDelayedTask(from_here, task, delay_ms); | 185 message_loop->PostDelayedTask(from_here, task, delay_ms); |
| 131 } else { | 186 } else { |
| 132 message_loop->PostNonNestableDelayedTask(from_here, task, delay_ms); | 187 message_loop->PostNonNestableDelayedTask(from_here, task, delay_ms); |
| 133 } | 188 } |
| 134 } | 189 } |
| 135 | 190 |
| 136 if (!guaranteed_to_outlive_target_thread) | 191 if (!guaranteed_to_outlive_target_thread) |
| 137 g_lock.Get().Release(); | 192 g_lock.Get().Release(); |
| 138 | 193 |
| 139 return !!message_loop; | 194 return !!message_loop; |
| 140 } | 195 } |
| 141 | 196 |
| 142 // TODO(joi): Remove | |
| 143 DeprecatedBrowserThread::DeprecatedBrowserThread(BrowserThread::ID identifier) | |
| 144 : BrowserThread(identifier) { | |
| 145 } | |
| 146 DeprecatedBrowserThread::DeprecatedBrowserThread(BrowserThread::ID identifier, | |
| 147 MessageLoop* message_loop) | |
| 148 : BrowserThread(identifier, message_loop) { | |
| 149 } | |
| 150 DeprecatedBrowserThread::~DeprecatedBrowserThread() { | |
| 151 Stop(); | |
| 152 } | |
| 153 | |
| 154 // An implementation of MessageLoopProxy to be used in conjunction | 197 // An implementation of MessageLoopProxy to be used in conjunction |
| 155 // with BrowserThread. | 198 // with BrowserThread. |
| 156 class BrowserThreadMessageLoopProxy : public base::MessageLoopProxy { | 199 class BrowserThreadMessageLoopProxy : public base::MessageLoopProxy { |
| 157 public: | 200 public: |
| 158 explicit BrowserThreadMessageLoopProxy(BrowserThread::ID identifier) | 201 explicit BrowserThreadMessageLoopProxy(BrowserThread::ID identifier) |
| 159 : id_(identifier) { | 202 : id_(identifier) { |
| 160 } | 203 } |
| 161 | 204 |
| 162 // MessageLoopProxy implementation. | 205 // MessageLoopProxy implementation. |
| 163 virtual bool PostTask(const tracked_objects::Location& from_here, | 206 virtual bool PostTask(const tracked_objects::Location& from_here, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 | 251 |
| 209 virtual bool BelongsToCurrentThread() { | 252 virtual bool BelongsToCurrentThread() { |
| 210 return BrowserThread::CurrentlyOn(id_); | 253 return BrowserThread::CurrentlyOn(id_); |
| 211 } | 254 } |
| 212 | 255 |
| 213 private: | 256 private: |
| 214 BrowserThread::ID id_; | 257 BrowserThread::ID id_; |
| 215 DISALLOW_COPY_AND_ASSIGN(BrowserThreadMessageLoopProxy); | 258 DISALLOW_COPY_AND_ASSIGN(BrowserThreadMessageLoopProxy); |
| 216 }; | 259 }; |
| 217 | 260 |
| 218 BrowserThread::BrowserThread(ID identifier) | |
| 219 : Thread(browser_thread_names[identifier]), | |
| 220 identifier_(identifier) { | |
| 221 Initialize(); | |
| 222 } | |
| 223 | |
| 224 BrowserThread::BrowserThread(ID identifier, | |
| 225 MessageLoop* message_loop) | |
| 226 : Thread(message_loop->thread_name().c_str()), | |
| 227 identifier_(identifier) { | |
| 228 set_message_loop(message_loop); | |
| 229 Initialize(); | |
| 230 } | |
| 231 | |
| 232 void BrowserThread::Initialize() { | |
| 233 base::AutoLock lock(g_lock.Get()); | |
| 234 DCHECK(identifier_ >= 0 && identifier_ < ID_COUNT); | |
| 235 DCHECK(g_browser_threads[identifier_] == NULL); | |
| 236 g_browser_threads[identifier_] = this; | |
| 237 } | |
| 238 | |
| 239 BrowserThread::~BrowserThread() { | |
| 240 // Stop the thread here, instead of the parent's class destructor. This is so | |
| 241 // that if there are pending tasks that run, code that checks that it's on the | |
| 242 // correct BrowserThread succeeds. | |
| 243 Stop(); | |
| 244 | |
| 245 base::AutoLock lock(g_lock.Get()); | |
| 246 g_browser_threads[identifier_] = NULL; | |
| 247 #ifndef NDEBUG | |
| 248 // Double check that the threads are ordered correctly in the enumeration. | |
| 249 for (int i = identifier_ + 1; i < ID_COUNT; ++i) { | |
| 250 DCHECK(!g_browser_threads[i]) << | |
| 251 "Threads must be listed in the reverse order that they die"; | |
| 252 } | |
| 253 #endif | |
| 254 } | |
| 255 | |
| 256 // static | 261 // static |
| 257 bool BrowserThread::IsWellKnownThread(ID identifier) { | 262 bool BrowserThread::IsWellKnownThread(ID identifier) { |
| 258 base::AutoLock lock(g_lock.Get()); | 263 base::AutoLock lock(g_lock.Get()); |
| 259 return (identifier >= 0 && identifier < ID_COUNT && | 264 return (identifier >= 0 && identifier < ID_COUNT && |
| 260 g_browser_threads[identifier]); | 265 g_browser_threads[identifier]); |
| 261 } | 266 } |
| 262 | 267 |
| 263 // static | 268 // static |
| 264 bool BrowserThread::CurrentlyOn(ID identifier) { | 269 bool BrowserThread::CurrentlyOn(ID identifier) { |
| 265 // We shouldn't use MessageLoop::current() since it uses LazyInstance which | 270 // We shouldn't use MessageLoop::current() since it uses LazyInstance which |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 | 391 |
| 387 // static | 392 // static |
| 388 scoped_refptr<base::MessageLoopProxy> | 393 scoped_refptr<base::MessageLoopProxy> |
| 389 BrowserThread::GetMessageLoopProxyForThread( | 394 BrowserThread::GetMessageLoopProxyForThread( |
| 390 ID identifier) { | 395 ID identifier) { |
| 391 scoped_refptr<base::MessageLoopProxy> proxy( | 396 scoped_refptr<base::MessageLoopProxy> proxy( |
| 392 new BrowserThreadMessageLoopProxy(identifier)); | 397 new BrowserThreadMessageLoopProxy(identifier)); |
| 393 return proxy; | 398 return proxy; |
| 394 } | 399 } |
| 395 | 400 |
| 401 base::Thread* BrowserThread::UnsafeGetBrowserThread(ID identifier) { |
| 402 base::AutoLock lock(g_lock.Get()); |
| 403 base::Thread* thread = g_browser_threads[identifier]; |
| 404 DCHECK(thread); |
| 405 return thread; |
| 406 } |
| 407 |
| 408 void BrowserThread::SetDelegate(ID identifier, |
| 409 BrowserThreadDelegate* delegate) { |
| 410 DCHECK(delegate); |
| 411 |
| 412 using base::subtle::AtomicWord; |
| 413 AtomicWord* storage = reinterpret_cast<AtomicWord*>( |
| 414 &g_browser_thread_delegates[identifier]); |
| 415 AtomicWord old_pointer = base::subtle::NoBarrier_AtomicExchange( |
| 416 storage, reinterpret_cast<AtomicWord>(delegate)); |
| 417 |
| 418 // This catches registration when previously registered. |
| 419 DCHECK(!old_pointer); |
| 420 } |
| 421 |
| 396 } // namespace content | 422 } // namespace content |
| OLD | NEW |