OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "ios/web/web_thread_impl.h" | 5 #include "ios/web/web_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 15 matching lines...) Expand all Loading... |
26 // Friendly names for the well-known threads. | 26 // Friendly names for the well-known threads. |
27 const char* const g_web_thread_names[WebThread::ID_COUNT] = { | 27 const char* const g_web_thread_names[WebThread::ID_COUNT] = { |
28 "Web_UIThread", // UI | 28 "Web_UIThread", // UI |
29 "Web_DBThread", // DB | 29 "Web_DBThread", // DB |
30 "Web_FileThread", // FILE | 30 "Web_FileThread", // FILE |
31 "Web_FileUserBlockingThread", // FILE_USER_BLOCKING | 31 "Web_FileUserBlockingThread", // FILE_USER_BLOCKING |
32 "Web_CacheThread", // CACHE | 32 "Web_CacheThread", // CACHE |
33 "Web_IOThread", // IO | 33 "Web_IOThread", // IO |
34 }; | 34 }; |
35 | 35 |
| 36 static const char* GetThreadName(WebThread::ID thread) { |
| 37 if (WebThread::UI <= thread && thread < WebThread::ID_COUNT) |
| 38 return g_web_thread_names[thread]; |
| 39 return "Unknown Thread"; |
| 40 } |
| 41 |
36 // An implementation of SingleThreadTaskRunner to be used in conjunction | 42 // An implementation of SingleThreadTaskRunner to be used in conjunction |
37 // with WebThread. | 43 // with WebThread. |
38 class WebThreadTaskRunner : public base::SingleThreadTaskRunner { | 44 class WebThreadTaskRunner : public base::SingleThreadTaskRunner { |
39 public: | 45 public: |
40 explicit WebThreadTaskRunner(WebThread::ID identifier) : id_(identifier) {} | 46 explicit WebThreadTaskRunner(WebThread::ID identifier) : id_(identifier) {} |
41 | 47 |
42 // SingleThreadTaskRunner implementation. | 48 // SingleThreadTaskRunner implementation. |
43 bool PostDelayedTask(const tracked_objects::Location& from_here, | 49 bool PostDelayedTask(const tracked_objects::Location& from_here, |
44 const base::Closure& task, | 50 const base::Closure& task, |
45 base::TimeDelta delay) override { | 51 base::TimeDelta delay) override { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 109 |
104 const scoped_refptr<base::SequencedWorkerPool> blocking_pool; | 110 const scoped_refptr<base::SequencedWorkerPool> blocking_pool; |
105 }; | 111 }; |
106 | 112 |
107 base::LazyInstance<WebThreadGlobals>::Leaky g_globals = | 113 base::LazyInstance<WebThreadGlobals>::Leaky g_globals = |
108 LAZY_INSTANCE_INITIALIZER; | 114 LAZY_INSTANCE_INITIALIZER; |
109 | 115 |
110 } // namespace | 116 } // namespace |
111 | 117 |
112 WebThreadImpl::WebThreadImpl(ID identifier) | 118 WebThreadImpl::WebThreadImpl(ID identifier) |
113 : Thread(g_web_thread_names[identifier]), identifier_(identifier) { | 119 : Thread(GetThreadName(identifier)), identifier_(identifier) { |
114 Initialize(); | 120 Initialize(); |
115 } | 121 } |
116 | 122 |
117 WebThreadImpl::WebThreadImpl(ID identifier, base::MessageLoop* message_loop) | 123 WebThreadImpl::WebThreadImpl(ID identifier, base::MessageLoop* message_loop) |
118 : Thread(message_loop->thread_name()), identifier_(identifier) { | 124 : Thread(GetThreadName(identifier)), identifier_(identifier) { |
119 set_message_loop(message_loop); | 125 set_message_loop(message_loop); |
120 Initialize(); | 126 Initialize(); |
121 } | 127 } |
122 | 128 |
123 // static | 129 // static |
124 void WebThreadImpl::ShutdownThreadPool() { | 130 void WebThreadImpl::ShutdownThreadPool() { |
125 // The goal is to make it impossible to 'infinite loop' during shutdown, | 131 // The goal is to make it impossible to 'infinite loop' during shutdown, |
126 // but to reasonably expect that all BLOCKING_SHUTDOWN tasks queued during | 132 // but to reasonably expect that all BLOCKING_SHUTDOWN tasks queued during |
127 // shutdown get run. There's nothing particularly scientific about the | 133 // shutdown get run. There's nothing particularly scientific about the |
128 // number chosen. | 134 // number chosen. |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 // http://crbug.com/63678 | 370 // http://crbug.com/63678 |
365 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; | 371 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; |
366 WebThreadGlobals& globals = g_globals.Get(); | 372 WebThreadGlobals& globals = g_globals.Get(); |
367 base::AutoLock lock(globals.lock); | 373 base::AutoLock lock(globals.lock); |
368 DCHECK(identifier >= 0 && identifier < ID_COUNT); | 374 DCHECK(identifier >= 0 && identifier < ID_COUNT); |
369 return globals.threads[identifier] && | 375 return globals.threads[identifier] && |
370 globals.threads[identifier]->message_loop() == | 376 globals.threads[identifier]->message_loop() == |
371 base::MessageLoop::current(); | 377 base::MessageLoop::current(); |
372 } | 378 } |
373 | 379 |
374 static const char* GetThreadName(WebThread::ID thread) { | |
375 if (WebThread::UI <= thread && thread < WebThread::ID_COUNT) | |
376 return g_web_thread_names[thread]; | |
377 return "Unknown Thread"; | |
378 } | |
379 | |
380 // static | 380 // static |
381 std::string WebThread::GetDCheckCurrentlyOnErrorMessage(ID expected) { | 381 std::string WebThread::GetDCheckCurrentlyOnErrorMessage(ID expected) { |
382 const base::MessageLoop* message_loop = base::MessageLoop::current(); | 382 std::string actual_name = base::PlatformThread::GetName(); |
383 ID actual_web_thread; | 383 if (actual_name.empty()) |
384 const char* actual_name = "Unknown Thread"; | 384 actual_name = "Unknown Thread"; |
385 if (message_loop && !message_loop->thread_name().empty()) { | 385 |
386 actual_name = message_loop->thread_name().c_str(); | |
387 } else if (GetCurrentThreadIdentifier(&actual_web_thread)) { | |
388 actual_name = GetThreadName(actual_web_thread); | |
389 } | |
390 std::string result = "Must be called on "; | 386 std::string result = "Must be called on "; |
391 result += GetThreadName(expected); | 387 result += GetThreadName(expected); |
392 result += "; actually called on "; | 388 result += "; actually called on "; |
393 result += actual_name; | 389 result += actual_name; |
394 result += "."; | 390 result += "."; |
395 return result; | 391 return result; |
396 } | 392 } |
397 | 393 |
398 // static | 394 // static |
399 bool WebThread::IsMessageLoopValid(ID identifier) { | 395 bool WebThread::IsMessageLoopValid(ID identifier) { |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 AtomicWord* storage = | 496 AtomicWord* storage = |
501 reinterpret_cast<AtomicWord*>(&globals.thread_delegates[identifier]); | 497 reinterpret_cast<AtomicWord*>(&globals.thread_delegates[identifier]); |
502 AtomicWord old_pointer = base::subtle::NoBarrier_AtomicExchange( | 498 AtomicWord old_pointer = base::subtle::NoBarrier_AtomicExchange( |
503 storage, reinterpret_cast<AtomicWord>(delegate)); | 499 storage, reinterpret_cast<AtomicWord>(delegate)); |
504 | 500 |
505 // This catches registration when previously registered. | 501 // This catches registration when previously registered. |
506 DCHECK(!delegate || !old_pointer); | 502 DCHECK(!delegate || !old_pointer); |
507 } | 503 } |
508 | 504 |
509 } // namespace web | 505 } // namespace web |
OLD | NEW |