OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CONTENT_BROWSER_BROWSER_THREAD_H_ | |
6 #define CONTENT_BROWSER_BROWSER_THREAD_H_ | |
7 #pragma once | |
8 | |
9 #include "base/callback.h" | |
10 #include "base/synchronization/lock.h" | |
11 #include "base/task.h" | |
12 #include "base/threading/thread.h" | |
13 #include "content/common/content_export.h" | |
14 | |
15 #if defined(UNIT_TEST) | |
16 #include "base/logging.h" | |
17 #endif // UNIT_TEST | |
18 | |
19 namespace base { | |
20 class MessageLoopProxy; | |
21 } | |
22 | |
23 /////////////////////////////////////////////////////////////////////////////// | |
24 // BrowserThread | |
25 // | |
26 // This class represents a thread that is known by a browser-wide name. For | |
27 // example, there is one IO thread for the entire browser process, and various | |
28 // pieces of code find it useful to retrieve a pointer to the IO thread's | |
29 // Invoke a task by thread ID: | |
30 // | |
31 // BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task); | |
32 // | |
33 // The return value is false if the task couldn't be posted because the target | |
34 // thread doesn't exist. If this could lead to data loss, you need to check the | |
35 // result and restructure the code to ensure it doesn't occur. | |
36 // | |
37 // This class automatically handles the lifetime of different threads. | |
38 // It's always safe to call PostTask on any thread. If it's not yet created, | |
39 // the task is deleted. There are no race conditions. If the thread that the | |
40 // task is posted to is guaranteed to outlive the current thread, then no locks | |
41 // are used. You should never need to cache pointers to MessageLoops, since | |
42 // they're not thread safe. | |
43 class CONTENT_EXPORT BrowserThread : public base::Thread { | |
44 public: | |
45 // An enumeration of the well-known threads. | |
46 // NOTE: threads must be listed in the order of their life-time, with each | |
47 // thread outliving every other thread below it. | |
48 enum ID { | |
49 // The main thread in the browser. | |
50 UI, | |
51 | |
52 // This is the thread that interacts with the database. | |
53 DB, | |
54 | |
55 // This is the "main" thread for WebKit within the browser process when | |
56 // NOT in --single-process mode. | |
57 WEBKIT, | |
58 | |
59 // This is the thread that interacts with the file system. | |
60 FILE, | |
61 | |
62 // Used to launch and terminate Chrome processes. | |
63 PROCESS_LAUNCHER, | |
64 | |
65 // This is the thread to handle slow HTTP cache operations. | |
66 CACHE, | |
67 | |
68 // This is the thread that processes IPC and network messages. | |
69 IO, | |
70 | |
71 #if defined(OS_CHROMEOS) | |
72 // This thread runs websocket to TCP proxy. | |
73 // TODO(dilmah): remove this thread, instead implement this functionality | |
74 // as hooks into websocket layer. | |
75 WEB_SOCKET_PROXY, | |
76 #endif | |
77 | |
78 // This identifier does not represent a thread. Instead it counts the | |
79 // number of well-known threads. Insert new well-known threads before this | |
80 // identifier. | |
81 ID_COUNT | |
82 }; | |
83 | |
84 // Construct a BrowserThread with the supplied identifier. It is an error | |
85 // to construct a BrowserThread that already exists. | |
86 explicit BrowserThread(ID identifier); | |
87 | |
88 // Special constructor for the main (UI) thread and unittests. We use a dummy | |
89 // thread here since the main thread already exists. | |
90 BrowserThread(ID identifier, MessageLoop* message_loop); | |
91 | |
92 virtual ~BrowserThread(); | |
93 | |
94 // These are the same methods in message_loop.h, but are guaranteed to either | |
95 // get posted to the MessageLoop if it's still alive, or be deleted otherwise. | |
96 // They return true iff the thread existed and the task was posted. Note that | |
97 // even if the task is posted, there's no guarantee that it will run, since | |
98 // the target thread may already have a Quit message in its queue. | |
99 static bool PostTask(ID identifier, | |
100 const tracked_objects::Location& from_here, | |
101 const base::Closure& task); | |
102 static bool PostDelayedTask(ID identifier, | |
103 const tracked_objects::Location& from_here, | |
104 const base::Closure& task, | |
105 int64 delay_ms); | |
106 static bool PostNonNestableTask(ID identifier, | |
107 const tracked_objects::Location& from_here, | |
108 const base::Closure& task); | |
109 static bool PostNonNestableDelayedTask( | |
110 ID identifier, | |
111 const tracked_objects::Location& from_here, | |
112 const base::Closure& task, | |
113 int64 delay_ms); | |
114 | |
115 // TODO(brettw) remove these when Task->Closure conversion is done. | |
116 static bool PostTask(ID identifier, | |
117 const tracked_objects::Location& from_here, | |
118 Task* task); | |
119 static bool PostDelayedTask(ID identifier, | |
120 const tracked_objects::Location& from_here, | |
121 Task* task, | |
122 int64 delay_ms); | |
123 static bool PostNonNestableTask(ID identifier, | |
124 const tracked_objects::Location& from_here, | |
125 Task* task); | |
126 static bool PostNonNestableDelayedTask( | |
127 ID identifier, | |
128 const tracked_objects::Location& from_here, | |
129 Task* task, | |
130 int64 delay_ms); | |
131 | |
132 static bool PostTaskAndReply( | |
133 ID identifier, | |
134 const tracked_objects::Location& from_here, | |
135 const base::Closure& task, | |
136 const base::Closure& reply); | |
137 | |
138 template <class T> | |
139 static bool DeleteSoon(ID identifier, | |
140 const tracked_objects::Location& from_here, | |
141 const T* object) { | |
142 return PostNonNestableTask( | |
143 identifier, from_here, new DeleteTask<T>(object)); | |
144 } | |
145 | |
146 template <class T> | |
147 static bool ReleaseSoon(ID identifier, | |
148 const tracked_objects::Location& from_here, | |
149 const T* object) { | |
150 return PostNonNestableTask( | |
151 identifier, from_here, new ReleaseTask<T>(object)); | |
152 } | |
153 | |
154 // Callable on any thread. Returns whether the given ID corresponds to a well | |
155 // known thread. | |
156 static bool IsWellKnownThread(ID identifier); | |
157 | |
158 // Callable on any thread. Returns whether you're currently on a particular | |
159 // thread. | |
160 static bool CurrentlyOn(ID identifier); | |
161 | |
162 // Callable on any thread. Returns whether the threads message loop is valid. | |
163 // If this returns false it means the thread is in the process of shutting | |
164 // down. | |
165 static bool IsMessageLoopValid(ID identifier); | |
166 | |
167 // If the current message loop is one of the known threads, returns true and | |
168 // sets identifier to its ID. Otherwise returns false. | |
169 static bool GetCurrentThreadIdentifier(ID* identifier); | |
170 | |
171 // Callers can hold on to a refcounted MessageLoopProxy beyond the lifetime | |
172 // of the thread. | |
173 static scoped_refptr<base::MessageLoopProxy> GetMessageLoopProxyForThread( | |
174 ID identifier); | |
175 | |
176 // Use these templates in conjuction with RefCountedThreadSafe when you want | |
177 // to ensure that an object is deleted on a specific thread. This is needed | |
178 // when an object can hop between threads (i.e. IO -> FILE -> IO), and thread | |
179 // switching delays can mean that the final IO tasks executes before the FILE | |
180 // task's stack unwinds. This would lead to the object destructing on the | |
181 // FILE thread, which often is not what you want (i.e. to unregister from | |
182 // NotificationService, to notify other objects on the creating thread etc). | |
183 template<ID thread> | |
184 struct DeleteOnThread { | |
185 template<typename T> | |
186 static void Destruct(const T* x) { | |
187 if (CurrentlyOn(thread)) { | |
188 delete x; | |
189 } else { | |
190 if (!DeleteSoon(thread, FROM_HERE, x)) { | |
191 #if defined(UNIT_TEST) | |
192 // Only logged under unit testing because leaks at shutdown | |
193 // are acceptable under normal circumstances. | |
194 LOG(ERROR) << "DeleteSoon failed on thread " << thread; | |
195 #endif // UNIT_TEST | |
196 } | |
197 } | |
198 } | |
199 }; | |
200 | |
201 // Sample usage: | |
202 // class Foo | |
203 // : public base::RefCountedThreadSafe< | |
204 // Foo, BrowserThread::DeleteOnIOThread> { | |
205 // | |
206 // ... | |
207 // private: | |
208 // friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>; | |
209 // friend class DeleteTask<Foo>; | |
210 // | |
211 // ~Foo(); | |
212 struct DeleteOnUIThread : public DeleteOnThread<UI> { }; | |
213 struct DeleteOnIOThread : public DeleteOnThread<IO> { }; | |
214 struct DeleteOnFileThread : public DeleteOnThread<FILE> { }; | |
215 struct DeleteOnDBThread : public DeleteOnThread<DB> { }; | |
216 struct DeleteOnWebKitThread : public DeleteOnThread<WEBKIT> { }; | |
217 | |
218 private: | |
219 // Common initialization code for the constructors. | |
220 void Initialize(); | |
221 | |
222 // TODO(brettw) remove this variant when Task->Closure migration is complete. | |
223 static bool PostTaskHelper( | |
224 ID identifier, | |
225 const tracked_objects::Location& from_here, | |
226 Task* task, | |
227 int64 delay_ms, | |
228 bool nestable); | |
229 static bool PostTaskHelper( | |
230 ID identifier, | |
231 const tracked_objects::Location& from_here, | |
232 const base::Closure& task, | |
233 int64 delay_ms, | |
234 bool nestable); | |
235 | |
236 // The identifier of this thread. Only one thread can exist with a given | |
237 // identifier at a given time. | |
238 ID identifier_; | |
239 | |
240 // This lock protects |browser_threads_|. Do not read or modify that array | |
241 // without holding this lock. Do not block while holding this lock. | |
242 static base::Lock lock_; | |
243 | |
244 // An array of the BrowserThread objects. This array is protected by |lock_|. | |
245 // The threads are not owned by this array. Typically, the threads are owned | |
246 // on the UI thread by the g_browser_process object. BrowserThreads remove | |
247 // themselves from this array upon destruction. | |
248 static BrowserThread* browser_threads_[ID_COUNT]; | |
249 }; | |
250 | |
251 #endif // CONTENT_BROWSER_BROWSER_THREAD_H_ | |
OLD | NEW |