| 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/atomicops.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 DCHECK(!g_browser_threads[i]) << | 109 DCHECK(!g_browser_threads[i]) << |
| 110 "Threads must be listed in the reverse order that they die"; | 110 "Threads must be listed in the reverse order that they die"; |
| 111 } | 111 } |
| 112 #endif | 112 #endif |
| 113 } | 113 } |
| 114 | 114 |
| 115 // static | 115 // static |
| 116 bool BrowserThreadImpl::PostTaskHelper( | 116 bool BrowserThreadImpl::PostTaskHelper( |
| 117 BrowserThread::ID identifier, | 117 BrowserThread::ID identifier, |
| 118 const tracked_objects::Location& from_here, | 118 const tracked_objects::Location& from_here, |
| 119 Task* task, | |
| 120 int64 delay_ms, | |
| 121 bool nestable) { | |
| 122 DCHECK(identifier >= 0 && identifier < ID_COUNT); | |
| 123 // Optimization: to avoid unnecessary locks, we listed the ID enumeration in | |
| 124 // order of lifetime. So no need to lock if we know that the other thread | |
| 125 // outlives this one. | |
| 126 // Note: since the array is so small, ok to loop instead of creating a map, | |
| 127 // which would require a lock because std::map isn't thread safe, defeating | |
| 128 // the whole purpose of this optimization. | |
| 129 BrowserThread::ID current_thread; | |
| 130 bool guaranteed_to_outlive_target_thread = | |
| 131 GetCurrentThreadIdentifier(¤t_thread) && | |
| 132 current_thread <= identifier; | |
| 133 | |
| 134 if (!guaranteed_to_outlive_target_thread) | |
| 135 g_lock.Get().Acquire(); | |
| 136 | |
| 137 MessageLoop* message_loop = g_browser_threads[identifier] ? | |
| 138 g_browser_threads[identifier]->message_loop() : NULL; | |
| 139 if (message_loop) { | |
| 140 if (nestable) { | |
| 141 message_loop->PostDelayedTask(from_here, task, delay_ms); | |
| 142 } else { | |
| 143 message_loop->PostNonNestableDelayedTask(from_here, task, delay_ms); | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 if (!guaranteed_to_outlive_target_thread) | |
| 148 g_lock.Get().Release(); | |
| 149 | |
| 150 if (!message_loop) | |
| 151 delete task; | |
| 152 | |
| 153 return !!message_loop; | |
| 154 } | |
| 155 | |
| 156 // static | |
| 157 bool BrowserThreadImpl::PostTaskHelper( | |
| 158 BrowserThread::ID identifier, | |
| 159 const tracked_objects::Location& from_here, | |
| 160 const base::Closure& task, | 119 const base::Closure& task, |
| 161 int64 delay_ms, | 120 int64 delay_ms, |
| 162 bool nestable) { | 121 bool nestable) { |
| 163 DCHECK(identifier >= 0 && identifier < ID_COUNT); | 122 DCHECK(identifier >= 0 && identifier < ID_COUNT); |
| 164 // Optimization: to avoid unnecessary locks, we listed the ID enumeration in | 123 // Optimization: to avoid unnecessary locks, we listed the ID enumeration in |
| 165 // order of lifetime. So no need to lock if we know that the other thread | 124 // order of lifetime. So no need to lock if we know that the other thread |
| 166 // outlives this one. | 125 // outlives this one. |
| 167 // Note: since the array is so small, ok to loop instead of creating a map, | 126 // Note: since the array is so small, ok to loop instead of creating a map, |
| 168 // which would require a lock because std::map isn't thread safe, defeating | 127 // which would require a lock because std::map isn't thread safe, defeating |
| 169 // the whole purpose of this optimization. | 128 // the whole purpose of this optimization. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 194 // An implementation of MessageLoopProxy to be used in conjunction | 153 // An implementation of MessageLoopProxy to be used in conjunction |
| 195 // with BrowserThread. | 154 // with BrowserThread. |
| 196 class BrowserThreadMessageLoopProxy : public base::MessageLoopProxy { | 155 class BrowserThreadMessageLoopProxy : public base::MessageLoopProxy { |
| 197 public: | 156 public: |
| 198 explicit BrowserThreadMessageLoopProxy(BrowserThread::ID identifier) | 157 explicit BrowserThreadMessageLoopProxy(BrowserThread::ID identifier) |
| 199 : id_(identifier) { | 158 : id_(identifier) { |
| 200 } | 159 } |
| 201 | 160 |
| 202 // MessageLoopProxy implementation. | 161 // MessageLoopProxy implementation. |
| 203 virtual bool PostTask(const tracked_objects::Location& from_here, | 162 virtual bool PostTask(const tracked_objects::Location& from_here, |
| 204 Task* task) { | |
| 205 return BrowserThread::PostTask(id_, from_here, task); | |
| 206 } | |
| 207 | |
| 208 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, | |
| 209 Task* task, int64 delay_ms) { | |
| 210 return BrowserThread::PostDelayedTask(id_, from_here, task, delay_ms); | |
| 211 } | |
| 212 | |
| 213 virtual bool PostNonNestableTask(const tracked_objects::Location& from_here, | |
| 214 Task* task) { | |
| 215 return BrowserThread::PostNonNestableTask(id_, from_here, task); | |
| 216 } | |
| 217 | |
| 218 virtual bool PostNonNestableDelayedTask( | |
| 219 const tracked_objects::Location& from_here, | |
| 220 Task* task, | |
| 221 int64 delay_ms) { | |
| 222 return BrowserThread::PostNonNestableDelayedTask(id_, from_here, task, | |
| 223 delay_ms); | |
| 224 } | |
| 225 | |
| 226 virtual bool PostTask(const tracked_objects::Location& from_here, | |
| 227 const base::Closure& task) { | 163 const base::Closure& task) { |
| 228 return BrowserThread::PostTask(id_, from_here, task); | 164 return BrowserThread::PostTask(id_, from_here, task); |
| 229 } | 165 } |
| 230 | 166 |
| 231 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, | 167 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 232 const base::Closure& task, int64 delay_ms) { | 168 const base::Closure& task, int64 delay_ms) { |
| 233 return BrowserThread::PostDelayedTask(id_, from_here, task, delay_ms); | 169 return BrowserThread::PostDelayedTask(id_, from_here, task, delay_ms); |
| 234 } | 170 } |
| 235 | 171 |
| 236 virtual bool PostNonNestableTask(const tracked_objects::Location& from_here, | 172 virtual bool PostNonNestableTask(const tracked_objects::Location& from_here, |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 bool BrowserThread::PostNonNestableDelayedTask( | 250 bool BrowserThread::PostNonNestableDelayedTask( |
| 315 ID identifier, | 251 ID identifier, |
| 316 const tracked_objects::Location& from_here, | 252 const tracked_objects::Location& from_here, |
| 317 const base::Closure& task, | 253 const base::Closure& task, |
| 318 int64 delay_ms) { | 254 int64 delay_ms) { |
| 319 return BrowserThreadImpl::PostTaskHelper( | 255 return BrowserThreadImpl::PostTaskHelper( |
| 320 identifier, from_here, task, delay_ms, false); | 256 identifier, from_here, task, delay_ms, false); |
| 321 } | 257 } |
| 322 | 258 |
| 323 // static | 259 // static |
| 324 bool BrowserThread::PostTask(ID identifier, | |
| 325 const tracked_objects::Location& from_here, | |
| 326 Task* task) { | |
| 327 return BrowserThreadImpl::PostTaskHelper( | |
| 328 identifier, from_here, task, 0, true); | |
| 329 } | |
| 330 | |
| 331 // static | |
| 332 bool BrowserThread::PostDelayedTask(ID identifier, | |
| 333 const tracked_objects::Location& from_here, | |
| 334 Task* task, | |
| 335 int64 delay_ms) { | |
| 336 return BrowserThreadImpl::PostTaskHelper( | |
| 337 identifier, from_here, task, delay_ms, true); | |
| 338 } | |
| 339 | |
| 340 // static | |
| 341 bool BrowserThread::PostNonNestableTask( | |
| 342 ID identifier, | |
| 343 const tracked_objects::Location& from_here, | |
| 344 Task* task) { | |
| 345 return BrowserThreadImpl::PostTaskHelper( | |
| 346 identifier, from_here, task, 0, false); | |
| 347 } | |
| 348 | |
| 349 // static | |
| 350 bool BrowserThread::PostNonNestableDelayedTask( | |
| 351 ID identifier, | |
| 352 const tracked_objects::Location& from_here, | |
| 353 Task* task, | |
| 354 int64 delay_ms) { | |
| 355 return BrowserThreadImpl::PostTaskHelper( | |
| 356 identifier, from_here, task, delay_ms, false); | |
| 357 } | |
| 358 | |
| 359 // static | |
| 360 bool BrowserThread::PostTaskAndReply( | 260 bool BrowserThread::PostTaskAndReply( |
| 361 ID identifier, | 261 ID identifier, |
| 362 const tracked_objects::Location& from_here, | 262 const tracked_objects::Location& from_here, |
| 363 const base::Closure& task, | 263 const base::Closure& task, |
| 364 const base::Closure& reply) { | 264 const base::Closure& reply) { |
| 365 return GetMessageLoopProxyForThread(identifier)->PostTaskAndReply(from_here, | 265 return GetMessageLoopProxyForThread(identifier)->PostTaskAndReply(from_here, |
| 366 task, | 266 task, |
| 367 reply); | 267 reply); |
| 368 } | 268 } |
| 369 | 269 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 AtomicWord* storage = reinterpret_cast<AtomicWord*>( | 311 AtomicWord* storage = reinterpret_cast<AtomicWord*>( |
| 412 &g_browser_thread_delegates[identifier]); | 312 &g_browser_thread_delegates[identifier]); |
| 413 AtomicWord old_pointer = base::subtle::NoBarrier_AtomicExchange( | 313 AtomicWord old_pointer = base::subtle::NoBarrier_AtomicExchange( |
| 414 storage, reinterpret_cast<AtomicWord>(delegate)); | 314 storage, reinterpret_cast<AtomicWord>(delegate)); |
| 415 | 315 |
| 416 // This catches registration when previously registered. | 316 // This catches registration when previously registered. |
| 417 DCHECK(!delegate || !old_pointer); | 317 DCHECK(!delegate || !old_pointer); |
| 418 } | 318 } |
| 419 | 319 |
| 420 } // namespace content | 320 } // namespace content |
| OLD | NEW |