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" | |
8 #include "base/bind.h" | 7 #include "base/bind.h" |
9 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
10 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
11 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
12 #include "base/threading/thread_restrictions.h" | 11 #include "base/threading/thread_restrictions.h" |
13 | 12 |
14 namespace content { | |
15 | |
16 namespace { | 13 namespace { |
17 | 14 |
18 // Friendly names for the well-known threads. | 15 // Friendly names for the well-known threads. |
19 static const char* g_browser_thread_names[BrowserThread::ID_COUNT] = { | 16 static const char* browser_thread_names[content::BrowserThread::ID_COUNT] = { |
20 "", // UI (name assembled in browser_main.cc). | 17 "", // UI (name assembled in browser_main.cc). |
21 "Chrome_DBThread", // DB | 18 "Chrome_DBThread", // DB |
22 "Chrome_WebKitThread", // WEBKIT | 19 "Chrome_WebKitThread", // WEBKIT |
23 "Chrome_FileThread", // FILE | 20 "Chrome_FileThread", // FILE |
24 "Chrome_ProcessLauncherThread", // PROCESS_LAUNCHER | 21 "Chrome_ProcessLauncherThread", // PROCESS_LAUNCHER |
25 "Chrome_CacheThread", // CACHE | 22 "Chrome_CacheThread", // CACHE |
26 "Chrome_IOThread", // IO | 23 "Chrome_IOThread", // IO |
27 #if defined(OS_CHROMEOS) | 24 #if defined(OS_CHROMEOS) |
28 "Chrome_WebSocketproxyThread", // WEB_SOCKET_PROXY | 25 "Chrome_WebSocketproxyThread", // WEB_SOCKET_PROXY |
29 #endif | 26 #endif |
30 }; | 27 }; |
31 | 28 |
32 // This lock protects |g_browser_threads|. Do not read or modify that | 29 } // namespace |
33 // array without holding this lock. Do not block while holding this | 30 |
34 // lock. | 31 namespace content { |
| 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. |
35 base::LazyInstance<base::Lock, | 37 base::LazyInstance<base::Lock, |
36 base::LeakyLazyInstanceTraits<base::Lock> > | 38 base::LeakyLazyInstanceTraits<base::Lock> > |
37 g_lock = LAZY_INSTANCE_INITIALIZER; | 39 g_lock = LAZY_INSTANCE_INITIALIZER; |
38 | 40 |
39 // This array is protected by |g_lock|. The threads are not owned by this | 41 |
40 // array. Typically, the threads are owned on the UI thread by | 42 // An array of the BrowserThread objects. This array is protected by |g_lock|. |
41 // content::BrowserMainLoop. BrowserThreadImpl objects remove | 43 // The threads are not owned by this array. Typically, the threads are owned |
| 44 // on the UI thread by the g_browser_process object. BrowserThreads remove |
42 // themselves from this array upon destruction. | 45 // themselves from this array upon destruction. |
43 static BrowserThreadImpl* g_browser_threads[BrowserThread::ID_COUNT]; | 46 BrowserThread* g_browser_threads[BrowserThread::ID_COUNT]; |
44 | |
45 // Only atomic operations are used on this array. The delegates are | |
46 // not owned by this array, rather by whoever calls | |
47 // BrowserThread::SetDelegate. | |
48 static BrowserThreadDelegate* g_browser_thread_delegates[ | |
49 BrowserThread::ID_COUNT]; | |
50 | 47 |
51 } // namespace | 48 } // namespace |
52 | 49 |
53 BrowserThreadImpl::BrowserThreadImpl(ID identifier) | 50 BrowserThreadImpl::BrowserThreadImpl(BrowserThread::ID identifier) |
54 : Thread(g_browser_thread_names[identifier]), | 51 : BrowserThread(identifier) { |
55 identifier_(identifier) { | |
56 Initialize(); | |
57 } | 52 } |
58 | 53 |
59 BrowserThreadImpl::BrowserThreadImpl(ID identifier, | 54 BrowserThreadImpl::BrowserThreadImpl(BrowserThread::ID identifier, |
60 MessageLoop* message_loop) | 55 MessageLoop* message_loop) |
61 : Thread(message_loop->thread_name().c_str()), | 56 : BrowserThread(identifier, message_loop) { |
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; | |
95 } | 57 } |
96 | 58 |
97 BrowserThreadImpl::~BrowserThreadImpl() { | 59 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. | |
101 Stop(); | 60 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 | |
112 } | 61 } |
113 | 62 |
114 // static | 63 // static |
115 bool BrowserThreadImpl::PostTaskHelper( | 64 bool BrowserThreadImpl::PostTaskHelper( |
116 BrowserThread::ID identifier, | 65 BrowserThread::ID identifier, |
117 const tracked_objects::Location& from_here, | 66 const tracked_objects::Location& from_here, |
118 Task* task, | 67 Task* task, |
119 int64 delay_ms, | 68 int64 delay_ms, |
120 bool nestable) { | 69 bool nestable) { |
121 DCHECK(identifier >= 0 && identifier < ID_COUNT); | 70 DCHECK(identifier >= 0 && identifier < ID_COUNT); |
122 // Optimization: to avoid unnecessary locks, we listed the ID enumeration in | 71 // Optimization: to avoid unnecessary locks, we listed the ID enumeration in |
123 // order of lifetime. So no need to lock if we know that the other thread | 72 // order of lifetime. So no need to lock if we know that the other thread |
124 // outlives this one. | 73 // outlives this one. |
125 // Note: since the array is so small, ok to loop instead of creating a map, | 74 // Note: since the array is so small, ok to loop instead of creating a map, |
126 // which would require a lock because std::map isn't thread safe, defeating | 75 // which would require a lock because std::map isn't thread safe, defeating |
127 // the whole purpose of this optimization. | 76 // the whole purpose of this optimization. |
128 BrowserThread::ID current_thread; | 77 BrowserThread::ID current_thread; |
129 bool guaranteed_to_outlive_target_thread = | 78 bool guaranteed_to_outlive_target_thread = |
130 GetCurrentThreadIdentifier(¤t_thread) && | 79 GetCurrentThreadIdentifier(¤t_thread) && |
131 current_thread <= identifier; | 80 current_thread >= identifier; |
132 | 81 |
133 if (!guaranteed_to_outlive_target_thread) | 82 if (!guaranteed_to_outlive_target_thread) |
134 g_lock.Get().Acquire(); | 83 g_lock.Get().Acquire(); |
135 | 84 |
136 MessageLoop* message_loop = g_browser_threads[identifier] ? | 85 MessageLoop* message_loop = g_browser_threads[identifier] ? |
137 g_browser_threads[identifier]->message_loop() : NULL; | 86 g_browser_threads[identifier]->message_loop() : NULL; |
138 if (message_loop) { | 87 if (message_loop) { |
139 if (nestable) { | 88 if (nestable) { |
140 message_loop->PostDelayedTask(from_here, task, delay_ms); | 89 message_loop->PostDelayedTask(from_here, task, delay_ms); |
141 } else { | 90 } else { |
(...skipping 20 matching lines...) Expand all Loading... |
162 DCHECK(identifier >= 0 && identifier < ID_COUNT); | 111 DCHECK(identifier >= 0 && identifier < ID_COUNT); |
163 // Optimization: to avoid unnecessary locks, we listed the ID enumeration in | 112 // Optimization: to avoid unnecessary locks, we listed the ID enumeration in |
164 // order of lifetime. So no need to lock if we know that the other thread | 113 // order of lifetime. So no need to lock if we know that the other thread |
165 // outlives this one. | 114 // outlives this one. |
166 // Note: since the array is so small, ok to loop instead of creating a map, | 115 // Note: since the array is so small, ok to loop instead of creating a map, |
167 // which would require a lock because std::map isn't thread safe, defeating | 116 // which would require a lock because std::map isn't thread safe, defeating |
168 // the whole purpose of this optimization. | 117 // the whole purpose of this optimization. |
169 BrowserThread::ID current_thread; | 118 BrowserThread::ID current_thread; |
170 bool guaranteed_to_outlive_target_thread = | 119 bool guaranteed_to_outlive_target_thread = |
171 GetCurrentThreadIdentifier(¤t_thread) && | 120 GetCurrentThreadIdentifier(¤t_thread) && |
172 current_thread <= identifier; | 121 current_thread >= identifier; |
173 | 122 |
174 if (!guaranteed_to_outlive_target_thread) | 123 if (!guaranteed_to_outlive_target_thread) |
175 g_lock.Get().Acquire(); | 124 g_lock.Get().Acquire(); |
176 | 125 |
177 MessageLoop* message_loop = g_browser_threads[identifier] ? | 126 MessageLoop* message_loop = g_browser_threads[identifier] ? |
178 g_browser_threads[identifier]->message_loop() : NULL; | 127 g_browser_threads[identifier]->message_loop() : NULL; |
179 if (message_loop) { | 128 if (message_loop) { |
180 if (nestable) { | 129 if (nestable) { |
181 message_loop->PostDelayedTask(from_here, task, delay_ms); | 130 message_loop->PostDelayedTask(from_here, task, delay_ms); |
182 } else { | 131 } else { |
183 message_loop->PostNonNestableDelayedTask(from_here, task, delay_ms); | 132 message_loop->PostNonNestableDelayedTask(from_here, task, delay_ms); |
184 } | 133 } |
185 } | 134 } |
186 | 135 |
187 if (!guaranteed_to_outlive_target_thread) | 136 if (!guaranteed_to_outlive_target_thread) |
188 g_lock.Get().Release(); | 137 g_lock.Get().Release(); |
189 | 138 |
190 return !!message_loop; | 139 return !!message_loop; |
191 } | 140 } |
192 | 141 |
| 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 |
193 // An implementation of MessageLoopProxy to be used in conjunction | 154 // An implementation of MessageLoopProxy to be used in conjunction |
194 // with BrowserThread. | 155 // with BrowserThread. |
195 class BrowserThreadMessageLoopProxy : public base::MessageLoopProxy { | 156 class BrowserThreadMessageLoopProxy : public base::MessageLoopProxy { |
196 public: | 157 public: |
197 explicit BrowserThreadMessageLoopProxy(BrowserThread::ID identifier) | 158 explicit BrowserThreadMessageLoopProxy(BrowserThread::ID identifier) |
198 : id_(identifier) { | 159 : id_(identifier) { |
199 } | 160 } |
200 | 161 |
201 // MessageLoopProxy implementation. | 162 // MessageLoopProxy implementation. |
202 virtual bool PostTask(const tracked_objects::Location& from_here, | 163 virtual bool PostTask(const tracked_objects::Location& from_here, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 | 208 |
248 virtual bool BelongsToCurrentThread() { | 209 virtual bool BelongsToCurrentThread() { |
249 return BrowserThread::CurrentlyOn(id_); | 210 return BrowserThread::CurrentlyOn(id_); |
250 } | 211 } |
251 | 212 |
252 private: | 213 private: |
253 BrowserThread::ID id_; | 214 BrowserThread::ID id_; |
254 DISALLOW_COPY_AND_ASSIGN(BrowserThreadMessageLoopProxy); | 215 DISALLOW_COPY_AND_ASSIGN(BrowserThreadMessageLoopProxy); |
255 }; | 216 }; |
256 | 217 |
| 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 |
257 // static | 256 // static |
258 bool BrowserThread::IsWellKnownThread(ID identifier) { | 257 bool BrowserThread::IsWellKnownThread(ID identifier) { |
259 base::AutoLock lock(g_lock.Get()); | 258 base::AutoLock lock(g_lock.Get()); |
260 return (identifier >= 0 && identifier < ID_COUNT && | 259 return (identifier >= 0 && identifier < ID_COUNT && |
261 g_browser_threads[identifier]); | 260 g_browser_threads[identifier]); |
262 } | 261 } |
263 | 262 |
264 // static | 263 // static |
265 bool BrowserThread::CurrentlyOn(ID identifier) { | 264 bool BrowserThread::CurrentlyOn(ID identifier) { |
266 // We shouldn't use MessageLoop::current() since it uses LazyInstance which | 265 // We shouldn't use MessageLoop::current() since it uses LazyInstance which |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 | 386 |
388 // static | 387 // static |
389 scoped_refptr<base::MessageLoopProxy> | 388 scoped_refptr<base::MessageLoopProxy> |
390 BrowserThread::GetMessageLoopProxyForThread( | 389 BrowserThread::GetMessageLoopProxyForThread( |
391 ID identifier) { | 390 ID identifier) { |
392 scoped_refptr<base::MessageLoopProxy> proxy( | 391 scoped_refptr<base::MessageLoopProxy> proxy( |
393 new BrowserThreadMessageLoopProxy(identifier)); | 392 new BrowserThreadMessageLoopProxy(identifier)); |
394 return proxy; | 393 return proxy; |
395 } | 394 } |
396 | 395 |
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 | |
416 } // namespace content | 396 } // namespace content |
OLD | NEW |