Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1138)

Side by Side Diff: content/browser/browser_thread_impl.cc

Issue 8477004: Have content/ create and destroy its own threads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Temporary - look at browser_list, browser_main_loop, content/p/b/browser_shutdown Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/bind.h" 7 #include "base/bind.h"
8 #include "base/lazy_instance.h" 8 #include "base/lazy_instance.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/message_loop_proxy.h" 10 #include "base/message_loop_proxy.h"
11 #include "base/threading/thread_restrictions.h" 11 #include "base/threading/thread_restrictions.h"
12 12
13 namespace content {
14
13 namespace { 15 namespace {
14 16
15 // Friendly names for the well-known threads. 17 // Friendly names for the well-known threads.
16 static const char* browser_thread_names[content::BrowserThread::ID_COUNT] = { 18 static const char* g_browser_thread_names[BrowserThread::ID_COUNT] = {
17 "", // UI (name assembled in browser_main.cc). 19 "", // UI (name assembled in browser_main.cc).
18 "Chrome_DBThread", // DB 20 "Chrome_DBThread", // DB
19 "Chrome_WebKitThread", // WEBKIT 21 "Chrome_WebKitThread", // WEBKIT
20 "Chrome_FileThread", // FILE 22 "Chrome_FileThread", // FILE
21 "Chrome_ProcessLauncherThread", // PROCESS_LAUNCHER 23 "Chrome_ProcessLauncherThread", // PROCESS_LAUNCHER
22 "Chrome_CacheThread", // CACHE 24 "Chrome_CacheThread", // CACHE
23 "Chrome_IOThread", // IO 25 "Chrome_IOThread", // IO
24 #if defined(OS_CHROMEOS) 26 #if defined(OS_CHROMEOS)
25 "Chrome_WebSocketproxyThread", // WEB_SOCKET_PROXY 27 "Chrome_WebSocketproxyThread", // WEB_SOCKET_PROXY
26 #endif 28 #endif
27 }; 29 };
28 30
29 } // namespace
30
31 namespace content {
32
33 namespace {
34
35 // This lock protects |g_browser_threads|. Do not read or modify that array 31 // 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. 32 // without holding this lock. Do not block while holding this lock.
37 base::LazyInstance<base::Lock, 33 base::LazyInstance<base::Lock,
38 base::LeakyLazyInstanceTraits<base::Lock> > 34 base::LeakyLazyInstanceTraits<base::Lock> >
39 g_lock = LAZY_INSTANCE_INITIALIZER; 35 g_lock = LAZY_INSTANCE_INITIALIZER;
40 36
41 37 // This array is protected by |g_lock|. The threads are not owned by this
42 // An array of the BrowserThread objects. This array is protected by |g_lock|. 38 // array. Typically, the threads are owned on the UI thread by
43 // The threads are not owned by this array. Typically, the threads are owned 39 // content::BrowserMainLoop. BrowserThreadImpl objects remove
44 // on the UI thread by the g_browser_process object. BrowserThreads remove
45 // themselves from this array upon destruction. 40 // themselves from this array upon destruction.
46 BrowserThread* g_browser_threads[BrowserThread::ID_COUNT]; 41 static BrowserThreadImpl* g_browser_threads[BrowserThread::ID_COUNT];
47 42
48 } // namespace 43 } // namespace
49 44
50 BrowserThreadImpl::BrowserThreadImpl(BrowserThread::ID identifier) 45 BrowserThreadImpl::BrowserThreadImpl(ID identifier)
51 : BrowserThread(identifier) { 46 : Thread(g_browser_thread_names[identifier]),
47 identifier_(identifier) {
48 Initialize();
52 } 49 }
53 50
54 BrowserThreadImpl::BrowserThreadImpl(BrowserThread::ID identifier, 51 BrowserThreadImpl::BrowserThreadImpl(ID identifier,
55 MessageLoop* message_loop) 52 MessageLoop* message_loop)
56 : BrowserThread(identifier, message_loop) { 53 : Thread(message_loop->thread_name().c_str()),
54 identifier_(identifier) {
55 set_message_loop(message_loop);
56 Initialize();
57 }
58
59 void BrowserThreadImpl::Initialize() {
60 base::AutoLock lock(g_lock.Get());
61 DCHECK(identifier_ >= 0 && identifier_ < ID_COUNT);
62 DCHECK(g_browser_threads[identifier_] == NULL);
63 g_browser_threads[identifier_] = this;
57 } 64 }
58 65
59 BrowserThreadImpl::~BrowserThreadImpl() { 66 BrowserThreadImpl::~BrowserThreadImpl() {
67 // All Thread subclasses must call Stop() in the destructor. This is
68 // doubly important here as various bits of code check they are on
69 // the right BrowserThread.
60 Stop(); 70 Stop();
71
72 base::AutoLock lock(g_lock.Get());
73 g_browser_threads[identifier_] = NULL;
74 #ifndef NDEBUG
75 // Double check that the threads are ordered correctly in the enumeration.
76 for (int i = identifier_ + 1; i < ID_COUNT; ++i) {
77 DCHECK(!g_browser_threads[i]) <<
78 "Threads must be listed in the reverse order that they die";
79 }
80 #endif
61 } 81 }
62 82
63 // static 83 // static
64 bool BrowserThreadImpl::PostTaskHelper( 84 bool BrowserThreadImpl::PostTaskHelper(
65 BrowserThread::ID identifier, 85 BrowserThread::ID identifier,
66 const tracked_objects::Location& from_here, 86 const tracked_objects::Location& from_here,
67 Task* task, 87 Task* task,
68 int64 delay_ms, 88 int64 delay_ms,
69 bool nestable) { 89 bool nestable) {
70 DCHECK(identifier >= 0 && identifier < ID_COUNT); 90 DCHECK(identifier >= 0 && identifier < ID_COUNT);
71 // Optimization: to avoid unnecessary locks, we listed the ID enumeration in 91 // 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 92 // order of lifetime. So no need to lock if we know that the other thread
73 // outlives this one. 93 // outlives this one.
74 // Note: since the array is so small, ok to loop instead of creating a map, 94 // 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 95 // which would require a lock because std::map isn't thread safe, defeating
76 // the whole purpose of this optimization. 96 // the whole purpose of this optimization.
77 BrowserThread::ID current_thread; 97 BrowserThread::ID current_thread;
78 bool guaranteed_to_outlive_target_thread = 98 bool guaranteed_to_outlive_target_thread =
79 GetCurrentThreadIdentifier(&current_thread) && 99 GetCurrentThreadIdentifier(&current_thread) &&
80 current_thread >= identifier; 100 current_thread <= identifier;
81 101
82 if (!guaranteed_to_outlive_target_thread) 102 if (!guaranteed_to_outlive_target_thread)
83 g_lock.Get().Acquire(); 103 g_lock.Get().Acquire();
84 104
85 MessageLoop* message_loop = g_browser_threads[identifier] ? 105 MessageLoop* message_loop = g_browser_threads[identifier] ?
86 g_browser_threads[identifier]->message_loop() : NULL; 106 g_browser_threads[identifier]->message_loop() : NULL;
87 if (message_loop) { 107 if (message_loop) {
88 if (nestable) { 108 if (nestable) {
89 message_loop->PostDelayedTask(from_here, task, delay_ms); 109 message_loop->PostDelayedTask(from_here, task, delay_ms);
90 } else { 110 } else {
(...skipping 20 matching lines...) Expand all
111 DCHECK(identifier >= 0 && identifier < ID_COUNT); 131 DCHECK(identifier >= 0 && identifier < ID_COUNT);
112 // Optimization: to avoid unnecessary locks, we listed the ID enumeration in 132 // 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 133 // order of lifetime. So no need to lock if we know that the other thread
114 // outlives this one. 134 // outlives this one.
115 // Note: since the array is so small, ok to loop instead of creating a map, 135 // 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 136 // which would require a lock because std::map isn't thread safe, defeating
117 // the whole purpose of this optimization. 137 // the whole purpose of this optimization.
118 BrowserThread::ID current_thread; 138 BrowserThread::ID current_thread;
119 bool guaranteed_to_outlive_target_thread = 139 bool guaranteed_to_outlive_target_thread =
120 GetCurrentThreadIdentifier(&current_thread) && 140 GetCurrentThreadIdentifier(&current_thread) &&
121 current_thread >= identifier; 141 current_thread <= identifier;
122 142
123 if (!guaranteed_to_outlive_target_thread) 143 if (!guaranteed_to_outlive_target_thread)
124 g_lock.Get().Acquire(); 144 g_lock.Get().Acquire();
125 145
126 MessageLoop* message_loop = g_browser_threads[identifier] ? 146 MessageLoop* message_loop = g_browser_threads[identifier] ?
127 g_browser_threads[identifier]->message_loop() : NULL; 147 g_browser_threads[identifier]->message_loop() : NULL;
128 if (message_loop) { 148 if (message_loop) {
129 if (nestable) { 149 if (nestable) {
130 message_loop->PostDelayedTask(from_here, task, delay_ms); 150 message_loop->PostDelayedTask(from_here, task, delay_ms);
131 } else { 151 } else {
132 message_loop->PostNonNestableDelayedTask(from_here, task, delay_ms); 152 message_loop->PostNonNestableDelayedTask(from_here, task, delay_ms);
133 } 153 }
134 } 154 }
135 155
136 if (!guaranteed_to_outlive_target_thread) 156 if (!guaranteed_to_outlive_target_thread)
137 g_lock.Get().Release(); 157 g_lock.Get().Release();
138 158
139 return !!message_loop; 159 return !!message_loop;
140 } 160 }
141 161
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 162 // An implementation of MessageLoopProxy to be used in conjunction
155 // with BrowserThread. 163 // with BrowserThread.
156 class BrowserThreadMessageLoopProxy : public base::MessageLoopProxy { 164 class BrowserThreadMessageLoopProxy : public base::MessageLoopProxy {
157 public: 165 public:
158 explicit BrowserThreadMessageLoopProxy(BrowserThread::ID identifier) 166 explicit BrowserThreadMessageLoopProxy(BrowserThread::ID identifier)
159 : id_(identifier) { 167 : id_(identifier) {
160 } 168 }
161 169
162 // MessageLoopProxy implementation. 170 // MessageLoopProxy implementation.
163 virtual bool PostTask(const tracked_objects::Location& from_here, 171 virtual bool PostTask(const tracked_objects::Location& from_here,
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 216
209 virtual bool BelongsToCurrentThread() { 217 virtual bool BelongsToCurrentThread() {
210 return BrowserThread::CurrentlyOn(id_); 218 return BrowserThread::CurrentlyOn(id_);
211 } 219 }
212 220
213 private: 221 private:
214 BrowserThread::ID id_; 222 BrowserThread::ID id_;
215 DISALLOW_COPY_AND_ASSIGN(BrowserThreadMessageLoopProxy); 223 DISALLOW_COPY_AND_ASSIGN(BrowserThreadMessageLoopProxy);
216 }; 224 };
217 225
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 226 // static
257 bool BrowserThread::IsWellKnownThread(ID identifier) { 227 bool BrowserThread::IsWellKnownThread(ID identifier) {
258 base::AutoLock lock(g_lock.Get()); 228 base::AutoLock lock(g_lock.Get());
259 return (identifier >= 0 && identifier < ID_COUNT && 229 return (identifier >= 0 && identifier < ID_COUNT &&
260 g_browser_threads[identifier]); 230 g_browser_threads[identifier]);
261 } 231 }
262 232
263 // static 233 // static
264 bool BrowserThread::CurrentlyOn(ID identifier) { 234 bool BrowserThread::CurrentlyOn(ID identifier) {
265 // We shouldn't use MessageLoop::current() since it uses LazyInstance which 235 // We shouldn't use MessageLoop::current() since it uses LazyInstance which
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 356
387 // static 357 // static
388 scoped_refptr<base::MessageLoopProxy> 358 scoped_refptr<base::MessageLoopProxy>
389 BrowserThread::GetMessageLoopProxyForThread( 359 BrowserThread::GetMessageLoopProxyForThread(
390 ID identifier) { 360 ID identifier) {
391 scoped_refptr<base::MessageLoopProxy> proxy( 361 scoped_refptr<base::MessageLoopProxy> proxy(
392 new BrowserThreadMessageLoopProxy(identifier)); 362 new BrowserThreadMessageLoopProxy(identifier));
393 return proxy; 363 return proxy;
394 } 364 }
395 365
366 base::Thread* BrowserThread::UnsafeGetBrowserThread(ID identifier) {
367 base::AutoLock lock(g_lock.Get());
368 base::Thread* thread = g_browser_threads[identifier];
369 DCHECK(thread);
370 return thread;
371 }
372
396 } // namespace content 373 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698