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

Side by Side Diff: content/public/browser/browser_thread.h

Issue 8477004: Have content/ create and destroy its own threads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Introduce and use BrowserThreadDelegate, BMP::Pre/PostStartThread, to make this a safe refactoring. 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 #ifndef CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ 5 #ifndef CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_
6 #define CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ 6 #define CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_
7 #pragma once 7 #pragma once
8 8
9 #include "base/basictypes.h"
9 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/task.h"
12 #include "base/tracked_objects.h"
13 #include "content/common/content_export.h"
14 #include "content/public/browser/browser_thread_delegate.h"
15
16 // TODO(joi): Remove these in a follow-up change and IWYU in files
17 // that were getting them directly or indirectly from here.
18 #include "base/memory/ref_counted.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "base/message_loop.h"
21 #include "base/message_loop_proxy.h"
10 #include "base/synchronization/lock.h" 22 #include "base/synchronization/lock.h"
11 #include "base/task.h"
12 #include "base/threading/thread.h" 23 #include "base/threading/thread.h"
13 #include "content/common/content_export.h"
14 24
15 #if defined(UNIT_TEST) 25 #if defined(UNIT_TEST)
16 #include "base/logging.h" 26 #include "base/logging.h"
17 #endif // UNIT_TEST 27 #endif // UNIT_TEST
18 28
19 namespace base { 29 namespace base {
20 class MessageLoopProxy; 30 class MessageLoopProxy;
31 class Thread;
21 } 32 }
22 33
23 namespace content { 34 namespace content {
24 35
25 class BrowserThreadImpl; 36 class BrowserThreadImpl;
26 class DeprecatedBrowserThread;
27 37
28 /////////////////////////////////////////////////////////////////////////////// 38 ///////////////////////////////////////////////////////////////////////////////
29 // BrowserThread 39 // BrowserThread
30 // 40 //
31 // Utility functions for threads that are known by a browser-wide 41 // Utility functions for threads that are known by a browser-wide
32 // name. For example, there is one IO thread for the entire browser 42 // name. For example, there is one IO thread for the entire browser
33 // process, and various pieces of code find it useful to retrieve a 43 // process, and various pieces of code find it useful to retrieve a
34 // pointer to the IO thread's message loop. 44 // pointer to the IO thread's message loop.
35 // 45 //
36 // Invoke a task by thread ID: 46 // Invoke a task by thread ID:
37 // 47 //
38 // BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task); 48 // BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task);
39 // 49 //
40 // The return value is false if the task couldn't be posted because the target 50 // The return value is false if the task couldn't be posted because the target
41 // thread doesn't exist. If this could lead to data loss, you need to check the 51 // thread doesn't exist. If this could lead to data loss, you need to check the
42 // result and restructure the code to ensure it doesn't occur. 52 // result and restructure the code to ensure it doesn't occur.
43 // 53 //
44 // This class automatically handles the lifetime of different threads. 54 // This class automatically handles the lifetime of different threads.
45 // It's always safe to call PostTask on any thread. If it's not yet created, 55 // It's always safe to call PostTask on any thread. If it's not yet created,
46 // the task is deleted. There are no race conditions. If the thread that the 56 // the task is deleted. There are no race conditions. If the thread that the
47 // task is posted to is guaranteed to outlive the current thread, then no locks 57 // task is posted to is guaranteed to outlive the current thread, then no locks
48 // are used. You should never need to cache pointers to MessageLoops, since 58 // are used. You should never need to cache pointers to MessageLoops, since
49 // they're not thread safe. 59 // they're not thread safe.
50 class CONTENT_EXPORT BrowserThread : public base::Thread { 60 class CONTENT_EXPORT BrowserThread {
51 public: 61 public:
52 // An enumeration of the well-known threads. 62 // An enumeration of the well-known threads.
53 // NOTE: threads must be listed in the order of their life-time, with each 63 // NOTE: threads must be listed in the order of their life-time, with each
54 // thread outliving every other thread below it. 64 // thread outliving every other thread below it.
55 enum ID { 65 enum ID {
56 // The main thread in the browser. 66 // The main thread in the browser.
57 UI, 67 UI,
58 68
59 // This is the thread that interacts with the database. 69 // This is the thread that interacts with the database.
60 DB, 70 DB,
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 173
164 // If the current message loop is one of the known threads, returns true and 174 // If the current message loop is one of the known threads, returns true and
165 // sets identifier to its ID. Otherwise returns false. 175 // sets identifier to its ID. Otherwise returns false.
166 static bool GetCurrentThreadIdentifier(ID* identifier); 176 static bool GetCurrentThreadIdentifier(ID* identifier);
167 177
168 // Callers can hold on to a refcounted MessageLoopProxy beyond the lifetime 178 // Callers can hold on to a refcounted MessageLoopProxy beyond the lifetime
169 // of the thread. 179 // of the thread.
170 static scoped_refptr<base::MessageLoopProxy> GetMessageLoopProxyForThread( 180 static scoped_refptr<base::MessageLoopProxy> GetMessageLoopProxyForThread(
171 ID identifier); 181 ID identifier);
172 182
183 // Gets the Thread object for the specified thread, or NULL if the
184 // thread has not been created (or has been destroyed during
185 // shutdown).
186 //
187 // Before calling this, you must have called content::ContentMain
188 // with a command-line that would specify a browser process (e.g. an
189 // empty command line).
190 //
191 // This is unsafe as your pointer may become invalid close to
192 // shutdown.
193 //
194 // TODO(joi): Remove this once clients such as BrowserProcessImpl
195 // (and classes that call things like
196 // g_browser_process->file_thread()) are switched to using
197 // MessageLoopProxy.
198 static base::Thread* UnsafeGetBrowserThread(ID identifier);
199
200 // Sets the delegate for the specified BrowserThread.
201 //
202 // This may be called only once per BrowserThread::ID, and must be
203 // called with a non-NULL delegate.
204 //
205 // The caller must guaranteed that the delegate lives until CleanUp
206 // has been called on it. It is however OK to not set a delegate
207 // until after BrowserThread initialization, in which case the
208 // delegate never receives the Init method call.
209 //
210 // Note that delegates that are set after a thread has terminated
211 // will never receive a CleanUp call, meaning delegates that delete
212 // themselves after CleanUp will leak if set after the thread has
213 // terminated.
214 static void SetDelegate(ID identifier, BrowserThreadDelegate* delegate);
215
173 // Use these templates in conjuction with RefCountedThreadSafe when you want 216 // Use these templates in conjuction with RefCountedThreadSafe when you want
174 // to ensure that an object is deleted on a specific thread. This is needed 217 // to ensure that an object is deleted on a specific thread. This is needed
175 // when an object can hop between threads (i.e. IO -> FILE -> IO), and thread 218 // when an object can hop between threads (i.e. IO -> FILE -> IO), and thread
176 // switching delays can mean that the final IO tasks executes before the FILE 219 // switching delays can mean that the final IO tasks executes before the FILE
177 // task's stack unwinds. This would lead to the object destructing on the 220 // task's stack unwinds. This would lead to the object destructing on the
178 // FILE thread, which often is not what you want (i.e. to unregister from 221 // FILE thread, which often is not what you want (i.e. to unregister from
179 // NotificationService, to notify other objects on the creating thread etc). 222 // NotificationService, to notify other objects on the creating thread etc).
180 template<ID thread> 223 template<ID thread>
181 struct DeleteOnThread { 224 struct DeleteOnThread {
182 template<typename T> 225 template<typename T>
(...skipping 23 matching lines...) Expand all
206 // friend class DeleteTask<Foo>; 249 // friend class DeleteTask<Foo>;
207 // 250 //
208 // ~Foo(); 251 // ~Foo();
209 struct DeleteOnUIThread : public DeleteOnThread<UI> { }; 252 struct DeleteOnUIThread : public DeleteOnThread<UI> { };
210 struct DeleteOnIOThread : public DeleteOnThread<IO> { }; 253 struct DeleteOnIOThread : public DeleteOnThread<IO> { };
211 struct DeleteOnFileThread : public DeleteOnThread<FILE> { }; 254 struct DeleteOnFileThread : public DeleteOnThread<FILE> { };
212 struct DeleteOnDBThread : public DeleteOnThread<DB> { }; 255 struct DeleteOnDBThread : public DeleteOnThread<DB> { };
213 struct DeleteOnWebKitThread : public DeleteOnThread<WEBKIT> { }; 256 struct DeleteOnWebKitThread : public DeleteOnThread<WEBKIT> { };
214 257
215 private: 258 private:
216 // Construct a BrowserThread with the supplied identifier. It is an error 259 friend class BrowserThreadImpl;
217 // to construct a BrowserThread that already exists.
218 explicit BrowserThread(ID identifier);
219 260
220 // Special constructor for the main (UI) thread and unittests. We use a dummy 261 BrowserThread() {}
221 // thread here since the main thread already exists. 262 DISALLOW_COPY_AND_ASSIGN(BrowserThread);
222 BrowserThread(ID identifier, MessageLoop* message_loop);
223
224 virtual ~BrowserThread();
225
226 // Common initialization code for the constructors.
227 void Initialize();
228
229 // Constructors are only available through this subclass.
230 friend class content::BrowserThreadImpl;
231
232 // TODO(joi): Remove.
233 friend class DeprecatedBrowserThread;
234
235 // The identifier of this thread. Only one thread can exist with a given
236 // identifier at a given time.
237 // TODO(joi): Move to BrowserThreadImpl, and make constructors here
238 // do-nothing.
239 ID identifier_;
240 };
241
242 // Temporary escape hatch for chrome/ to construct BrowserThread,
243 // until we make content/ construct its own threads.
244 class CONTENT_EXPORT DeprecatedBrowserThread : public BrowserThread {
245 public:
246 explicit DeprecatedBrowserThread(BrowserThread::ID identifier);
247 DeprecatedBrowserThread(BrowserThread::ID identifier,
248 MessageLoop* message_loop);
249 virtual ~DeprecatedBrowserThread();
250 }; 263 };
251 264
252 } // namespace content 265 } // namespace content
253 266
254 #endif // CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ 267 #endif // CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698