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

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

Issue 8718012: Revert 111695 - Have content/ create and destroy its own threads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: 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"
10 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/synchronization/lock.h"
11 #include "base/task.h" 11 #include "base/task.h"
12 #include "base/tracked_objects.h" 12 #include "base/threading/thread.h"
13 #include "content/common/content_export.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"
22 #include "base/synchronization/lock.h"
23 #include "base/threading/thread.h"
24 14
25 #if defined(UNIT_TEST) 15 #if defined(UNIT_TEST)
26 #include "base/logging.h" 16 #include "base/logging.h"
27 #endif // UNIT_TEST 17 #endif // UNIT_TEST
28 18
29 namespace base { 19 namespace base {
30 class MessageLoopProxy; 20 class MessageLoopProxy;
31 class Thread;
32 } 21 }
33 22
34 namespace content { 23 namespace content {
35 24
36 class BrowserThreadImpl; 25 class BrowserThreadImpl;
26 class DeprecatedBrowserThread;
37 27
38 /////////////////////////////////////////////////////////////////////////////// 28 ///////////////////////////////////////////////////////////////////////////////
39 // BrowserThread 29 // BrowserThread
40 // 30 //
41 // Utility functions for threads that are known by a browser-wide 31 // Utility functions for threads that are known by a browser-wide
42 // name. For example, there is one IO thread for the entire browser 32 // name. For example, there is one IO thread for the entire browser
43 // process, and various pieces of code find it useful to retrieve a 33 // process, and various pieces of code find it useful to retrieve a
44 // pointer to the IO thread's message loop. 34 // pointer to the IO thread's message loop.
45 // 35 //
46 // Invoke a task by thread ID: 36 // Invoke a task by thread ID:
47 // 37 //
48 // BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task); 38 // BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task);
49 // 39 //
50 // The return value is false if the task couldn't be posted because the target 40 // The return value is false if the task couldn't be posted because the target
51 // thread doesn't exist. If this could lead to data loss, you need to check the 41 // thread doesn't exist. If this could lead to data loss, you need to check the
52 // result and restructure the code to ensure it doesn't occur. 42 // result and restructure the code to ensure it doesn't occur.
53 // 43 //
54 // This class automatically handles the lifetime of different threads. 44 // This class automatically handles the lifetime of different threads.
55 // It's always safe to call PostTask on any thread. If it's not yet created, 45 // It's always safe to call PostTask on any thread. If it's not yet created,
56 // the task is deleted. There are no race conditions. If the thread that the 46 // the task is deleted. There are no race conditions. If the thread that the
57 // task is posted to is guaranteed to outlive the current thread, then no locks 47 // task is posted to is guaranteed to outlive the current thread, then no locks
58 // are used. You should never need to cache pointers to MessageLoops, since 48 // are used. You should never need to cache pointers to MessageLoops, since
59 // they're not thread safe. 49 // they're not thread safe.
60 class CONTENT_EXPORT BrowserThread { 50 class CONTENT_EXPORT BrowserThread : public base::Thread {
61 public: 51 public:
62 // An enumeration of the well-known threads. 52 // An enumeration of the well-known threads.
63 // NOTE: threads must be listed in the order of their life-time, with each 53 // NOTE: threads must be listed in the order of their life-time, with each
64 // thread outliving every other thread below it. 54 // thread outliving every other thread below it.
65 enum ID { 55 enum ID {
66 // The main thread in the browser. 56 // The main thread in the browser.
67 UI, 57 UI,
68 58
69 // This is the thread that interacts with the database. 59 // This is the thread that interacts with the database.
70 DB, 60 DB,
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 163
174 // If the current message loop is one of the known threads, returns true and 164 // If the current message loop is one of the known threads, returns true and
175 // sets identifier to its ID. Otherwise returns false. 165 // sets identifier to its ID. Otherwise returns false.
176 static bool GetCurrentThreadIdentifier(ID* identifier); 166 static bool GetCurrentThreadIdentifier(ID* identifier);
177 167
178 // Callers can hold on to a refcounted MessageLoopProxy beyond the lifetime 168 // Callers can hold on to a refcounted MessageLoopProxy beyond the lifetime
179 // of the thread. 169 // of the thread.
180 static scoped_refptr<base::MessageLoopProxy> GetMessageLoopProxyForThread( 170 static scoped_refptr<base::MessageLoopProxy> GetMessageLoopProxyForThread(
181 ID identifier); 171 ID identifier);
182 172
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 // Only one delegate may be registered at a time. Delegates may be
203 // unregistered by providing a NULL pointer.
204 //
205 // If the caller unregisters a delegate before CleanUp has been
206 // called, it must perform its own locking to ensure the delegate is
207 // not deleted while unregistering.
208 static void SetDelegate(ID identifier, BrowserThreadDelegate* delegate);
209
210 // Use these templates in conjuction with RefCountedThreadSafe when you want 173 // Use these templates in conjuction with RefCountedThreadSafe when you want
211 // to ensure that an object is deleted on a specific thread. This is needed 174 // to ensure that an object is deleted on a specific thread. This is needed
212 // when an object can hop between threads (i.e. IO -> FILE -> IO), and thread 175 // when an object can hop between threads (i.e. IO -> FILE -> IO), and thread
213 // switching delays can mean that the final IO tasks executes before the FILE 176 // switching delays can mean that the final IO tasks executes before the FILE
214 // task's stack unwinds. This would lead to the object destructing on the 177 // task's stack unwinds. This would lead to the object destructing on the
215 // FILE thread, which often is not what you want (i.e. to unregister from 178 // FILE thread, which often is not what you want (i.e. to unregister from
216 // NotificationService, to notify other objects on the creating thread etc). 179 // NotificationService, to notify other objects on the creating thread etc).
217 template<ID thread> 180 template<ID thread>
218 struct DeleteOnThread { 181 struct DeleteOnThread {
219 template<typename T> 182 template<typename T>
(...skipping 23 matching lines...) Expand all
243 // friend class DeleteTask<Foo>; 206 // friend class DeleteTask<Foo>;
244 // 207 //
245 // ~Foo(); 208 // ~Foo();
246 struct DeleteOnUIThread : public DeleteOnThread<UI> { }; 209 struct DeleteOnUIThread : public DeleteOnThread<UI> { };
247 struct DeleteOnIOThread : public DeleteOnThread<IO> { }; 210 struct DeleteOnIOThread : public DeleteOnThread<IO> { };
248 struct DeleteOnFileThread : public DeleteOnThread<FILE> { }; 211 struct DeleteOnFileThread : public DeleteOnThread<FILE> { };
249 struct DeleteOnDBThread : public DeleteOnThread<DB> { }; 212 struct DeleteOnDBThread : public DeleteOnThread<DB> { };
250 struct DeleteOnWebKitThread : public DeleteOnThread<WEBKIT> { }; 213 struct DeleteOnWebKitThread : public DeleteOnThread<WEBKIT> { };
251 214
252 private: 215 private:
253 friend class BrowserThreadImpl; 216 // Construct a BrowserThread with the supplied identifier. It is an error
217 // to construct a BrowserThread that already exists.
218 explicit BrowserThread(ID identifier);
254 219
255 BrowserThread() {} 220 // Special constructor for the main (UI) thread and unittests. We use a dummy
256 DISALLOW_COPY_AND_ASSIGN(BrowserThread); 221 // thread here since the main thread already exists.
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();
257 }; 250 };
258 251
259 } // namespace content 252 } // namespace content
260 253
261 #endif // CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ 254 #endif // CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_
OLDNEW
« no previous file with comments | « content/public/browser/browser_shutdown.h ('k') | content/public/browser/browser_thread_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698