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 "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" |
11 #include "base/message_loop_proxy.h" | 11 #include "base/message_loop_proxy.h" |
12 #include "base/threading/sequenced_worker_pool.h" | |
13 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
14 | 13 |
15 namespace content { | 14 namespace content { |
16 | 15 |
17 namespace { | 16 namespace { |
18 | 17 |
19 // Friendly names for the well-known threads. | 18 // Friendly names for the well-known threads. |
20 static const char* g_browser_thread_names[BrowserThread::ID_COUNT] = { | 19 static const char* g_browser_thread_names[BrowserThread::ID_COUNT] = { |
21 "", // UI (name assembled in browser_main.cc). | 20 "", // UI (name assembled in browser_main.cc). |
22 "Chrome_DBThread", // DB | 21 "Chrome_DBThread", // DB |
23 "Chrome_WebKitThread", // WEBKIT_DEPRECATED | 22 "Chrome_WebKitThread", // WEBKIT_DEPRECATED |
24 "Chrome_FileThread", // FILE | 23 "Chrome_FileThread", // FILE |
25 "Chrome_FileUserBlockingThread", // FILE_USER_BLOCKING | 24 "Chrome_FileUserBlockingThread", // FILE_USER_BLOCKING |
26 "Chrome_ProcessLauncherThread", // PROCESS_LAUNCHER | 25 "Chrome_ProcessLauncherThread", // PROCESS_LAUNCHER |
27 "Chrome_CacheThread", // CACHE | 26 "Chrome_CacheThread", // CACHE |
28 "Chrome_IOThread", // IO | 27 "Chrome_IOThread", // IO |
29 #if defined(OS_CHROMEOS) | 28 #if defined(OS_CHROMEOS) |
30 "Chrome_WebSocketproxyThread", // WEB_SOCKET_PROXY | 29 "Chrome_WebSocketproxyThread", // WEB_SOCKET_PROXY |
31 #endif | 30 #endif |
32 }; | 31 }; |
33 | 32 |
34 struct BrowserThreadGlobals { | 33 // This lock protects |g_browser_threads|. Do not read or modify that |
35 BrowserThreadGlobals() | 34 // array without holding this lock. Do not block while holding this |
36 : blocking_pool(new base::SequencedWorkerPool(3, "BrowserBlocking")) { | 35 // lock. |
37 memset(threads, 0, | 36 base::LazyInstance<base::Lock, |
38 BrowserThread::ID_COUNT * sizeof(BrowserThreadImpl*)); | 37 base::LeakyLazyInstanceTraits<base::Lock> > |
39 memset(thread_delegates, 0, | 38 g_lock = LAZY_INSTANCE_INITIALIZER; |
40 BrowserThread::ID_COUNT * sizeof(BrowserThreadDelegate*)); | |
41 } | |
42 | 39 |
43 // This lock protects |threads|. Do not read or modify that array | 40 // This array is protected by |g_lock|. The threads are not owned by this |
44 // without holding this lock. Do not block while holding this lock. | 41 // array. Typically, the threads are owned on the UI thread by |
45 base::Lock lock; | 42 // content::BrowserMainLoop. BrowserThreadImpl objects remove |
| 43 // themselves from this array upon destruction. |
| 44 static BrowserThreadImpl* g_browser_threads[BrowserThread::ID_COUNT]; |
46 | 45 |
47 // This array is protected by |lock|. The threads are not owned by this | 46 // Only atomic operations are used on this array. The delegates are |
48 // array. Typically, the threads are owned on the UI thread by | 47 // not owned by this array, rather by whoever calls |
49 // content::BrowserMainLoop. BrowserThreadImpl objects remove themselves from | 48 // BrowserThread::SetDelegate. |
50 // this array upon destruction. | 49 static BrowserThreadDelegate* g_browser_thread_delegates[ |
51 BrowserThreadImpl* threads[BrowserThread::ID_COUNT]; | 50 BrowserThread::ID_COUNT]; |
52 | |
53 // Only atomic operations are used on this array. The delegates are not owned | |
54 // by this array, rather by whoever calls BrowserThread::SetDelegate. | |
55 BrowserThreadDelegate* thread_delegates[BrowserThread::ID_COUNT]; | |
56 | |
57 // This pointer is deliberately leaked on shutdown. This allows the pool to | |
58 // implement "continue on shutdown" semantics. | |
59 base::SequencedWorkerPool* blocking_pool; | |
60 }; | |
61 | |
62 base::LazyInstance<BrowserThreadGlobals, | |
63 base::LeakyLazyInstanceTraits<BrowserThreadGlobals> > | |
64 g_globals = LAZY_INSTANCE_INITIALIZER; | |
65 | 51 |
66 } // namespace | 52 } // namespace |
67 | 53 |
68 BrowserThreadImpl::BrowserThreadImpl(ID identifier) | 54 BrowserThreadImpl::BrowserThreadImpl(ID identifier) |
69 : Thread(g_browser_thread_names[identifier]), | 55 : Thread(g_browser_thread_names[identifier]), |
70 identifier_(identifier) { | 56 identifier_(identifier) { |
71 Initialize(); | 57 Initialize(); |
72 } | 58 } |
73 | 59 |
74 BrowserThreadImpl::BrowserThreadImpl(ID identifier, | 60 BrowserThreadImpl::BrowserThreadImpl(ID identifier, |
75 MessageLoop* message_loop) | 61 MessageLoop* message_loop) |
76 : Thread(message_loop->thread_name().c_str()), | 62 : Thread(message_loop->thread_name().c_str()), |
77 identifier_(identifier) { | 63 identifier_(identifier) { |
78 set_message_loop(message_loop); | 64 set_message_loop(message_loop); |
79 Initialize(); | 65 Initialize(); |
80 } | 66 } |
81 | 67 |
82 // static | |
83 void BrowserThreadImpl::ShutdownThreadPool() { | |
84 BrowserThreadGlobals& globals = g_globals.Get(); | |
85 globals.blocking_pool->Shutdown(); | |
86 } | |
87 | |
88 void BrowserThreadImpl::Init() { | 68 void BrowserThreadImpl::Init() { |
89 BrowserThreadGlobals& globals = g_globals.Get(); | |
90 | |
91 using base::subtle::AtomicWord; | 69 using base::subtle::AtomicWord; |
92 AtomicWord* storage = | 70 AtomicWord* storage = |
93 reinterpret_cast<AtomicWord*>(&globals.thread_delegates[identifier_]); | 71 reinterpret_cast<AtomicWord*>(&g_browser_thread_delegates[identifier_]); |
94 AtomicWord stored_pointer = base::subtle::NoBarrier_Load(storage); | 72 AtomicWord stored_pointer = base::subtle::NoBarrier_Load(storage); |
95 BrowserThreadDelegate* delegate = | 73 BrowserThreadDelegate* delegate = |
96 reinterpret_cast<BrowserThreadDelegate*>(stored_pointer); | 74 reinterpret_cast<BrowserThreadDelegate*>(stored_pointer); |
97 if (delegate) | 75 if (delegate) |
98 delegate->Init(); | 76 delegate->Init(); |
99 } | 77 } |
100 | 78 |
101 void BrowserThreadImpl::CleanUp() { | 79 void BrowserThreadImpl::CleanUp() { |
102 BrowserThreadGlobals& globals = g_globals.Get(); | |
103 | |
104 using base::subtle::AtomicWord; | 80 using base::subtle::AtomicWord; |
105 AtomicWord* storage = | 81 AtomicWord* storage = |
106 reinterpret_cast<AtomicWord*>(&globals.thread_delegates[identifier_]); | 82 reinterpret_cast<AtomicWord*>(&g_browser_thread_delegates[identifier_]); |
107 AtomicWord stored_pointer = base::subtle::NoBarrier_Load(storage); | 83 AtomicWord stored_pointer = base::subtle::NoBarrier_Load(storage); |
108 BrowserThreadDelegate* delegate = | 84 BrowserThreadDelegate* delegate = |
109 reinterpret_cast<BrowserThreadDelegate*>(stored_pointer); | 85 reinterpret_cast<BrowserThreadDelegate*>(stored_pointer); |
110 | 86 |
111 if (delegate) | 87 if (delegate) |
112 delegate->CleanUp(); | 88 delegate->CleanUp(); |
113 } | 89 } |
114 | 90 |
115 void BrowserThreadImpl::Initialize() { | 91 void BrowserThreadImpl::Initialize() { |
116 BrowserThreadGlobals& globals = g_globals.Get(); | 92 base::AutoLock lock(g_lock.Get()); |
117 | |
118 base::AutoLock lock(globals.lock); | |
119 DCHECK(identifier_ >= 0 && identifier_ < ID_COUNT); | 93 DCHECK(identifier_ >= 0 && identifier_ < ID_COUNT); |
120 DCHECK(globals.threads[identifier_] == NULL); | 94 DCHECK(g_browser_threads[identifier_] == NULL); |
121 globals.threads[identifier_] = this; | 95 g_browser_threads[identifier_] = this; |
122 } | 96 } |
123 | 97 |
124 BrowserThreadImpl::~BrowserThreadImpl() { | 98 BrowserThreadImpl::~BrowserThreadImpl() { |
125 // All Thread subclasses must call Stop() in the destructor. This is | 99 // All Thread subclasses must call Stop() in the destructor. This is |
126 // doubly important here as various bits of code check they are on | 100 // doubly important here as various bits of code check they are on |
127 // the right BrowserThread. | 101 // the right BrowserThread. |
128 Stop(); | 102 Stop(); |
129 | 103 |
130 BrowserThreadGlobals& globals = g_globals.Get(); | 104 base::AutoLock lock(g_lock.Get()); |
131 base::AutoLock lock(globals.lock); | 105 g_browser_threads[identifier_] = NULL; |
132 globals.threads[identifier_] = NULL; | |
133 #ifndef NDEBUG | 106 #ifndef NDEBUG |
134 // Double check that the threads are ordered correctly in the enumeration. | 107 // Double check that the threads are ordered correctly in the enumeration. |
135 for (int i = identifier_ + 1; i < ID_COUNT; ++i) { | 108 for (int i = identifier_ + 1; i < ID_COUNT; ++i) { |
136 DCHECK(!globals.threads[i]) << | 109 DCHECK(!g_browser_threads[i]) << |
137 "Threads must be listed in the reverse order that they die"; | 110 "Threads must be listed in the reverse order that they die"; |
138 } | 111 } |
139 #endif | 112 #endif |
140 } | 113 } |
141 | 114 |
142 // static | 115 // static |
143 bool BrowserThreadImpl::PostTaskHelper( | 116 bool BrowserThreadImpl::PostTaskHelper( |
144 BrowserThread::ID identifier, | 117 BrowserThread::ID identifier, |
145 const tracked_objects::Location& from_here, | 118 const tracked_objects::Location& from_here, |
146 const base::Closure& task, | 119 const base::Closure& task, |
147 int64 delay_ms, | 120 int64 delay_ms, |
148 bool nestable) { | 121 bool nestable) { |
149 DCHECK(identifier >= 0 && identifier < ID_COUNT); | 122 DCHECK(identifier >= 0 && identifier < ID_COUNT); |
150 // Optimization: to avoid unnecessary locks, we listed the ID enumeration in | 123 // Optimization: to avoid unnecessary locks, we listed the ID enumeration in |
151 // 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 |
152 // outlives this one. | 125 // outlives this one. |
153 // 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, |
154 // 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 |
155 // the whole purpose of this optimization. | 128 // the whole purpose of this optimization. |
156 BrowserThread::ID current_thread; | 129 BrowserThread::ID current_thread; |
157 bool guaranteed_to_outlive_target_thread = | 130 bool guaranteed_to_outlive_target_thread = |
158 GetCurrentThreadIdentifier(¤t_thread) && | 131 GetCurrentThreadIdentifier(¤t_thread) && |
159 current_thread <= identifier; | 132 current_thread <= identifier; |
160 | 133 |
161 BrowserThreadGlobals& globals = g_globals.Get(); | |
162 if (!guaranteed_to_outlive_target_thread) | 134 if (!guaranteed_to_outlive_target_thread) |
163 globals.lock.Acquire(); | 135 g_lock.Get().Acquire(); |
164 | 136 |
165 MessageLoop* message_loop = globals.threads[identifier] ? | 137 MessageLoop* message_loop = g_browser_threads[identifier] ? |
166 globals.threads[identifier]->message_loop() : NULL; | 138 g_browser_threads[identifier]->message_loop() : NULL; |
167 if (message_loop) { | 139 if (message_loop) { |
168 if (nestable) { | 140 if (nestable) { |
169 message_loop->PostDelayedTask(from_here, task, delay_ms); | 141 message_loop->PostDelayedTask(from_here, task, delay_ms); |
170 } else { | 142 } else { |
171 message_loop->PostNonNestableDelayedTask(from_here, task, delay_ms); | 143 message_loop->PostNonNestableDelayedTask(from_here, task, delay_ms); |
172 } | 144 } |
173 } | 145 } |
174 | 146 |
175 if (!guaranteed_to_outlive_target_thread) | 147 if (!guaranteed_to_outlive_target_thread) |
176 globals.lock.Release(); | 148 g_lock.Get().Release(); |
177 | 149 |
178 return !!message_loop; | 150 return !!message_loop; |
179 } | 151 } |
180 | 152 |
181 // An implementation of MessageLoopProxy to be used in conjunction | 153 // An implementation of MessageLoopProxy to be used in conjunction |
182 // with BrowserThread. | 154 // with BrowserThread. |
183 class BrowserThreadMessageLoopProxy : public base::MessageLoopProxy { | 155 class BrowserThreadMessageLoopProxy : public base::MessageLoopProxy { |
184 public: | 156 public: |
185 explicit BrowserThreadMessageLoopProxy(BrowserThread::ID identifier) | 157 explicit BrowserThreadMessageLoopProxy(BrowserThread::ID identifier) |
186 : id_(identifier) { | 158 : id_(identifier) { |
(...skipping 26 matching lines...) Expand all Loading... |
213 virtual bool BelongsToCurrentThread() { | 185 virtual bool BelongsToCurrentThread() { |
214 return BrowserThread::CurrentlyOn(id_); | 186 return BrowserThread::CurrentlyOn(id_); |
215 } | 187 } |
216 | 188 |
217 private: | 189 private: |
218 BrowserThread::ID id_; | 190 BrowserThread::ID id_; |
219 DISALLOW_COPY_AND_ASSIGN(BrowserThreadMessageLoopProxy); | 191 DISALLOW_COPY_AND_ASSIGN(BrowserThreadMessageLoopProxy); |
220 }; | 192 }; |
221 | 193 |
222 // static | 194 // static |
223 bool BrowserThread::PostBlockingPoolTask( | 195 bool BrowserThread::IsWellKnownThread(ID identifier) { |
224 const tracked_objects::Location& from_here, | 196 base::AutoLock lock(g_lock.Get()); |
225 const base::Closure& task) { | 197 return (identifier >= 0 && identifier < ID_COUNT && |
226 return g_globals.Get().blocking_pool->PostWorkerTask(from_here, task); | 198 g_browser_threads[identifier]); |
227 } | 199 } |
228 | 200 |
229 // static | 201 // static |
230 bool BrowserThread::PostBlockingPoolSequencedTask( | |
231 const std::string& sequence_token_name, | |
232 const tracked_objects::Location& from_here, | |
233 const base::Closure& task) { | |
234 return g_globals.Get().blocking_pool->PostNamedSequencedWorkerTask( | |
235 sequence_token_name, from_here, task); | |
236 } | |
237 | |
238 // static | |
239 base::SequencedWorkerPool* BrowserThread::GetBlockingPool() { | |
240 return g_globals.Get().blocking_pool; | |
241 } | |
242 | |
243 // static | |
244 bool BrowserThread::IsWellKnownThread(ID identifier) { | |
245 BrowserThreadGlobals& globals = g_globals.Get(); | |
246 base::AutoLock lock(globals.lock); | |
247 return (identifier >= 0 && identifier < ID_COUNT && | |
248 globals.threads[identifier]); | |
249 } | |
250 | |
251 // static | |
252 bool BrowserThread::CurrentlyOn(ID identifier) { | 202 bool BrowserThread::CurrentlyOn(ID identifier) { |
253 // We shouldn't use MessageLoop::current() since it uses LazyInstance which | 203 // We shouldn't use MessageLoop::current() since it uses LazyInstance which |
254 // may be deleted by ~AtExitManager when a WorkerPool thread calls this | 204 // may be deleted by ~AtExitManager when a WorkerPool thread calls this |
255 // function. | 205 // function. |
256 // http://crbug.com/63678 | 206 // http://crbug.com/63678 |
257 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; | 207 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; |
258 BrowserThreadGlobals& globals = g_globals.Get(); | 208 base::AutoLock lock(g_lock.Get()); |
259 base::AutoLock lock(globals.lock); | |
260 DCHECK(identifier >= 0 && identifier < ID_COUNT); | 209 DCHECK(identifier >= 0 && identifier < ID_COUNT); |
261 return globals.threads[identifier] && | 210 return g_browser_threads[identifier] && |
262 globals.threads[identifier]->message_loop() == | 211 g_browser_threads[identifier]->message_loop() == |
263 MessageLoop::current(); | 212 MessageLoop::current(); |
264 } | 213 } |
265 | 214 |
266 // static | 215 // static |
267 bool BrowserThread::IsMessageLoopValid(ID identifier) { | 216 bool BrowserThread::IsMessageLoopValid(ID identifier) { |
268 BrowserThreadGlobals& globals = g_globals.Get(); | 217 base::AutoLock lock(g_lock.Get()); |
269 base::AutoLock lock(globals.lock); | |
270 DCHECK(identifier >= 0 && identifier < ID_COUNT); | 218 DCHECK(identifier >= 0 && identifier < ID_COUNT); |
271 return globals.threads[identifier] && | 219 return g_browser_threads[identifier] && |
272 globals.threads[identifier]->message_loop(); | 220 g_browser_threads[identifier]->message_loop(); |
273 } | 221 } |
274 | 222 |
275 // static | 223 // static |
276 bool BrowserThread::PostTask(ID identifier, | 224 bool BrowserThread::PostTask(ID identifier, |
277 const tracked_objects::Location& from_here, | 225 const tracked_objects::Location& from_here, |
278 const base::Closure& task) { | 226 const base::Closure& task) { |
279 return BrowserThreadImpl::PostTaskHelper( | 227 return BrowserThreadImpl::PostTaskHelper( |
280 identifier, from_here, task, 0, true); | 228 identifier, from_here, task, 0, true); |
281 } | 229 } |
282 | 230 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 } | 268 } |
321 | 269 |
322 // static | 270 // static |
323 bool BrowserThread::GetCurrentThreadIdentifier(ID* identifier) { | 271 bool BrowserThread::GetCurrentThreadIdentifier(ID* identifier) { |
324 // We shouldn't use MessageLoop::current() since it uses LazyInstance which | 272 // We shouldn't use MessageLoop::current() since it uses LazyInstance which |
325 // may be deleted by ~AtExitManager when a WorkerPool thread calls this | 273 // may be deleted by ~AtExitManager when a WorkerPool thread calls this |
326 // function. | 274 // function. |
327 // http://crbug.com/63678 | 275 // http://crbug.com/63678 |
328 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; | 276 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; |
329 MessageLoop* cur_message_loop = MessageLoop::current(); | 277 MessageLoop* cur_message_loop = MessageLoop::current(); |
330 BrowserThreadGlobals& globals = g_globals.Get(); | |
331 for (int i = 0; i < ID_COUNT; ++i) { | 278 for (int i = 0; i < ID_COUNT; ++i) { |
332 if (globals.threads[i] && | 279 if (g_browser_threads[i] && |
333 globals.threads[i]->message_loop() == cur_message_loop) { | 280 g_browser_threads[i]->message_loop() == cur_message_loop) { |
334 *identifier = globals.threads[i]->identifier_; | 281 *identifier = g_browser_threads[i]->identifier_; |
335 return true; | 282 return true; |
336 } | 283 } |
337 } | 284 } |
338 | 285 |
339 return false; | 286 return false; |
340 } | 287 } |
341 | 288 |
342 // static | 289 // static |
343 scoped_refptr<base::MessageLoopProxy> | 290 scoped_refptr<base::MessageLoopProxy> |
344 BrowserThread::GetMessageLoopProxyForThread(ID identifier) { | 291 BrowserThread::GetMessageLoopProxyForThread( |
| 292 ID identifier) { |
345 scoped_refptr<base::MessageLoopProxy> proxy( | 293 scoped_refptr<base::MessageLoopProxy> proxy( |
346 new BrowserThreadMessageLoopProxy(identifier)); | 294 new BrowserThreadMessageLoopProxy(identifier)); |
347 return proxy; | 295 return proxy; |
348 } | 296 } |
349 | 297 |
350 // static | 298 // static |
351 MessageLoop* BrowserThread::UnsafeGetMessageLoopForThread(ID identifier) { | 299 MessageLoop* BrowserThread::UnsafeGetMessageLoopForThread(ID identifier) { |
352 BrowserThreadGlobals& globals = g_globals.Get(); | 300 base::AutoLock lock(g_lock.Get()); |
353 base::AutoLock lock(globals.lock); | 301 base::Thread* thread = g_browser_threads[identifier]; |
354 base::Thread* thread = globals.threads[identifier]; | |
355 DCHECK(thread); | 302 DCHECK(thread); |
356 MessageLoop* loop = thread->message_loop(); | 303 MessageLoop* loop = thread->message_loop(); |
357 return loop; | 304 return loop; |
358 } | 305 } |
359 | 306 |
360 // static | 307 // static |
361 void BrowserThread::SetDelegate(ID identifier, | 308 void BrowserThread::SetDelegate(ID identifier, |
362 BrowserThreadDelegate* delegate) { | 309 BrowserThreadDelegate* delegate) { |
363 using base::subtle::AtomicWord; | 310 using base::subtle::AtomicWord; |
364 BrowserThreadGlobals& globals = g_globals.Get(); | |
365 AtomicWord* storage = reinterpret_cast<AtomicWord*>( | 311 AtomicWord* storage = reinterpret_cast<AtomicWord*>( |
366 &globals.thread_delegates[identifier]); | 312 &g_browser_thread_delegates[identifier]); |
367 AtomicWord old_pointer = base::subtle::NoBarrier_AtomicExchange( | 313 AtomicWord old_pointer = base::subtle::NoBarrier_AtomicExchange( |
368 storage, reinterpret_cast<AtomicWord>(delegate)); | 314 storage, reinterpret_cast<AtomicWord>(delegate)); |
369 | 315 |
370 // This catches registration when previously registered. | 316 // This catches registration when previously registered. |
371 DCHECK(!delegate || !old_pointer); | 317 DCHECK(!delegate || !old_pointer); |
372 } | 318 } |
373 | 319 |
374 } // namespace content | 320 } // namespace content |
OLD | NEW |