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 if (delegate) |
| 87 delegate->CleanUp(); |
| 88 } |
| 89 |
| 90 void BrowserThreadImpl::Initialize() { |
| 91 base::AutoLock lock(g_lock.Get()); |
| 92 DCHECK(identifier_ >= 0 && identifier_ < ID_COUNT); |
| 93 DCHECK(g_browser_threads[identifier_] == NULL); |
| 94 g_browser_threads[identifier_] = this; |
57 } | 95 } |
58 | 96 |
59 BrowserThreadImpl::~BrowserThreadImpl() { | 97 BrowserThreadImpl::~BrowserThreadImpl() { |
| 98 // All Thread subclasses must call Stop() in the destructor. This is |
| 99 // doubly important here as various bits of code check they are on |
| 100 // the right BrowserThread. |
60 Stop(); | 101 Stop(); |
| 102 |
| 103 base::AutoLock lock(g_lock.Get()); |
| 104 g_browser_threads[identifier_] = NULL; |
| 105 #ifndef NDEBUG |
| 106 // Double check that the threads are ordered correctly in the enumeration. |
| 107 for (int i = identifier_ + 1; i < ID_COUNT; ++i) { |
| 108 DCHECK(!g_browser_threads[i]) << |
| 109 "Threads must be listed in the reverse order that they die"; |
| 110 } |
| 111 #endif |
61 } | 112 } |
62 | 113 |
63 // static | 114 // static |
64 bool BrowserThreadImpl::PostTaskHelper( | 115 bool BrowserThreadImpl::PostTaskHelper( |
65 BrowserThread::ID identifier, | 116 BrowserThread::ID identifier, |
66 const tracked_objects::Location& from_here, | 117 const tracked_objects::Location& from_here, |
67 Task* task, | 118 Task* task, |
68 int64 delay_ms, | 119 int64 delay_ms, |
69 bool nestable) { | 120 bool nestable) { |
70 DCHECK(identifier >= 0 && identifier < ID_COUNT); | 121 DCHECK(identifier >= 0 && identifier < ID_COUNT); |
71 // Optimization: to avoid unnecessary locks, we listed the ID enumeration in | 122 // 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 | 123 // order of lifetime. So no need to lock if we know that the other thread |
73 // outlives this one. | 124 // outlives this one. |
74 // Note: since the array is so small, ok to loop instead of creating a map, | 125 // 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 | 126 // which would require a lock because std::map isn't thread safe, defeating |
76 // the whole purpose of this optimization. | 127 // the whole purpose of this optimization. |
77 BrowserThread::ID current_thread; | 128 BrowserThread::ID current_thread; |
78 bool guaranteed_to_outlive_target_thread = | 129 bool guaranteed_to_outlive_target_thread = |
79 GetCurrentThreadIdentifier(¤t_thread) && | 130 GetCurrentThreadIdentifier(¤t_thread) && |
80 current_thread >= identifier; | 131 current_thread <= identifier; |
81 | 132 |
82 if (!guaranteed_to_outlive_target_thread) | 133 if (!guaranteed_to_outlive_target_thread) |
83 g_lock.Get().Acquire(); | 134 g_lock.Get().Acquire(); |
84 | 135 |
85 MessageLoop* message_loop = g_browser_threads[identifier] ? | 136 MessageLoop* message_loop = g_browser_threads[identifier] ? |
86 g_browser_threads[identifier]->message_loop() : NULL; | 137 g_browser_threads[identifier]->message_loop() : NULL; |
87 if (message_loop) { | 138 if (message_loop) { |
88 if (nestable) { | 139 if (nestable) { |
89 message_loop->PostDelayedTask(from_here, task, delay_ms); | 140 message_loop->PostDelayedTask(from_here, task, delay_ms); |
90 } else { | 141 } else { |
(...skipping 20 matching lines...) Expand all Loading... |
111 DCHECK(identifier >= 0 && identifier < ID_COUNT); | 162 DCHECK(identifier >= 0 && identifier < ID_COUNT); |
112 // Optimization: to avoid unnecessary locks, we listed the ID enumeration in | 163 // 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 | 164 // order of lifetime. So no need to lock if we know that the other thread |
114 // outlives this one. | 165 // outlives this one. |
115 // Note: since the array is so small, ok to loop instead of creating a map, | 166 // 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 | 167 // which would require a lock because std::map isn't thread safe, defeating |
117 // the whole purpose of this optimization. | 168 // the whole purpose of this optimization. |
118 BrowserThread::ID current_thread; | 169 BrowserThread::ID current_thread; |
119 bool guaranteed_to_outlive_target_thread = | 170 bool guaranteed_to_outlive_target_thread = |
120 GetCurrentThreadIdentifier(¤t_thread) && | 171 GetCurrentThreadIdentifier(¤t_thread) && |
121 current_thread >= identifier; | 172 current_thread <= identifier; |
122 | 173 |
123 if (!guaranteed_to_outlive_target_thread) | 174 if (!guaranteed_to_outlive_target_thread) |
124 g_lock.Get().Acquire(); | 175 g_lock.Get().Acquire(); |
125 | 176 |
126 MessageLoop* message_loop = g_browser_threads[identifier] ? | 177 MessageLoop* message_loop = g_browser_threads[identifier] ? |
127 g_browser_threads[identifier]->message_loop() : NULL; | 178 g_browser_threads[identifier]->message_loop() : NULL; |
128 if (message_loop) { | 179 if (message_loop) { |
129 if (nestable) { | 180 if (nestable) { |
130 message_loop->PostDelayedTask(from_here, task, delay_ms); | 181 message_loop->PostDelayedTask(from_here, task, delay_ms); |
131 } else { | 182 } else { |
132 message_loop->PostNonNestableDelayedTask(from_here, task, delay_ms); | 183 message_loop->PostNonNestableDelayedTask(from_here, task, delay_ms); |
133 } | 184 } |
134 } | 185 } |
135 | 186 |
136 if (!guaranteed_to_outlive_target_thread) | 187 if (!guaranteed_to_outlive_target_thread) |
137 g_lock.Get().Release(); | 188 g_lock.Get().Release(); |
138 | 189 |
139 return !!message_loop; | 190 return !!message_loop; |
140 } | 191 } |
141 | 192 |
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 | 193 // An implementation of MessageLoopProxy to be used in conjunction |
155 // with BrowserThread. | 194 // with BrowserThread. |
156 class BrowserThreadMessageLoopProxy : public base::MessageLoopProxy { | 195 class BrowserThreadMessageLoopProxy : public base::MessageLoopProxy { |
157 public: | 196 public: |
158 explicit BrowserThreadMessageLoopProxy(BrowserThread::ID identifier) | 197 explicit BrowserThreadMessageLoopProxy(BrowserThread::ID identifier) |
159 : id_(identifier) { | 198 : id_(identifier) { |
160 } | 199 } |
161 | 200 |
162 // MessageLoopProxy implementation. | 201 // MessageLoopProxy implementation. |
163 virtual bool PostTask(const tracked_objects::Location& from_here, | 202 virtual bool PostTask(const tracked_objects::Location& from_here, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 | 247 |
209 virtual bool BelongsToCurrentThread() { | 248 virtual bool BelongsToCurrentThread() { |
210 return BrowserThread::CurrentlyOn(id_); | 249 return BrowserThread::CurrentlyOn(id_); |
211 } | 250 } |
212 | 251 |
213 private: | 252 private: |
214 BrowserThread::ID id_; | 253 BrowserThread::ID id_; |
215 DISALLOW_COPY_AND_ASSIGN(BrowserThreadMessageLoopProxy); | 254 DISALLOW_COPY_AND_ASSIGN(BrowserThreadMessageLoopProxy); |
216 }; | 255 }; |
217 | 256 |
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 | 257 // static |
257 bool BrowserThread::IsWellKnownThread(ID identifier) { | 258 bool BrowserThread::IsWellKnownThread(ID identifier) { |
258 base::AutoLock lock(g_lock.Get()); | 259 base::AutoLock lock(g_lock.Get()); |
259 return (identifier >= 0 && identifier < ID_COUNT && | 260 return (identifier >= 0 && identifier < ID_COUNT && |
260 g_browser_threads[identifier]); | 261 g_browser_threads[identifier]); |
261 } | 262 } |
262 | 263 |
263 // static | 264 // static |
264 bool BrowserThread::CurrentlyOn(ID identifier) { | 265 bool BrowserThread::CurrentlyOn(ID identifier) { |
265 // We shouldn't use MessageLoop::current() since it uses LazyInstance which | 266 // 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 | 387 |
387 // static | 388 // static |
388 scoped_refptr<base::MessageLoopProxy> | 389 scoped_refptr<base::MessageLoopProxy> |
389 BrowserThread::GetMessageLoopProxyForThread( | 390 BrowserThread::GetMessageLoopProxyForThread( |
390 ID identifier) { | 391 ID identifier) { |
391 scoped_refptr<base::MessageLoopProxy> proxy( | 392 scoped_refptr<base::MessageLoopProxy> proxy( |
392 new BrowserThreadMessageLoopProxy(identifier)); | 393 new BrowserThreadMessageLoopProxy(identifier)); |
393 return proxy; | 394 return proxy; |
394 } | 395 } |
395 | 396 |
| 397 base::Thread* BrowserThread::UnsafeGetBrowserThread(ID identifier) { |
| 398 base::AutoLock lock(g_lock.Get()); |
| 399 base::Thread* thread = g_browser_threads[identifier]; |
| 400 DCHECK(thread); |
| 401 return thread; |
| 402 } |
| 403 |
| 404 void BrowserThread::SetDelegate(ID identifier, |
| 405 BrowserThreadDelegate* delegate) { |
| 406 using base::subtle::AtomicWord; |
| 407 AtomicWord* storage = reinterpret_cast<AtomicWord*>( |
| 408 &g_browser_thread_delegates[identifier]); |
| 409 AtomicWord old_pointer = base::subtle::NoBarrier_AtomicExchange( |
| 410 storage, reinterpret_cast<AtomicWord>(delegate)); |
| 411 |
| 412 // This catches registration when previously registered. |
| 413 DCHECK(!delegate || !old_pointer); |
| 414 } |
| 415 |
396 } // namespace content | 416 } // namespace content |
OLD | NEW |