OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 IOS_WEB_PUBLIC_WEB_THREAD_H_ | 5 #ifndef IOS_WEB_PUBLIC_WEB_THREAD_H_ |
6 #define IOS_WEB_PUBLIC_WEB_THREAD_H_ | 6 #define IOS_WEB_PUBLIC_WEB_THREAD_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/message_loop/message_loop_proxy.h" | |
15 #include "base/task_runner_util.h" | 14 #include "base/task_runner_util.h" |
16 | 15 |
17 namespace base { | 16 namespace base { |
18 class MessageLoop; | 17 class MessageLoop; |
19 class SequencedWorkerPool; | 18 class SequencedWorkerPool; |
20 } | 19 } |
21 | 20 |
22 namespace tracked_objects { | 21 namespace tracked_objects { |
23 class Location; | 22 class Location; |
24 } | 23 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 // This is the thread to handle slow HTTP cache operations. | 76 // This is the thread to handle slow HTTP cache operations. |
78 CACHE, | 77 CACHE, |
79 | 78 |
80 // This is the thread that processes non-blocking IO, i.e. IPC and network. | 79 // This is the thread that processes non-blocking IO, i.e. IPC and network. |
81 // Blocking IO should happen on other threads like DB, FILE, | 80 // Blocking IO should happen on other threads like DB, FILE, |
82 // FILE_USER_BLOCKING and CACHE depending on the usage. | 81 // FILE_USER_BLOCKING and CACHE depending on the usage. |
83 IO, | 82 IO, |
84 | 83 |
85 // NOTE: do not add new threads here that are only used by a small number of | 84 // NOTE: do not add new threads here that are only used by a small number of |
86 // files. Instead you should just use a Thread class and pass its | 85 // files. Instead you should just use a Thread class and pass its |
87 // MessageLoopProxy around. Named threads there are only for threads that | 86 // SingleThreadTaskRunner around. Named threads there are only for threads |
88 // are used in many places. | 87 // that are used in many places. |
89 | 88 |
90 // This identifier does not represent a thread. Instead it counts the | 89 // This identifier does not represent a thread. Instead it counts the |
91 // number of well-known threads. Insert new well-known threads before this | 90 // number of well-known threads. Insert new well-known threads before this |
92 // identifier. | 91 // identifier. |
93 ID_COUNT | 92 ID_COUNT |
94 }; | 93 }; |
95 | 94 |
96 // These are the same methods as in message_loop.h, but are guaranteed to | 95 // These are the same methods as in message_loop.h, but are guaranteed to |
97 // either get posted to the MessageLoop if it's still alive, or be deleted | 96 // either get posted to the MessageLoop if it's still alive, or be deleted |
98 // otherwise. | 97 // otherwise. |
(...skipping 18 matching lines...) Expand all Loading... |
117 const tracked_objects::Location& from_here, | 116 const tracked_objects::Location& from_here, |
118 const base::Closure& task, | 117 const base::Closure& task, |
119 const base::Closure& reply); | 118 const base::Closure& reply); |
120 | 119 |
121 template <typename ReturnType, typename ReplyArgType> | 120 template <typename ReturnType, typename ReplyArgType> |
122 static bool PostTaskAndReplyWithResult( | 121 static bool PostTaskAndReplyWithResult( |
123 ID identifier, | 122 ID identifier, |
124 const tracked_objects::Location& from_here, | 123 const tracked_objects::Location& from_here, |
125 const base::Callback<ReturnType(void)>& task, | 124 const base::Callback<ReturnType(void)>& task, |
126 const base::Callback<void(ReplyArgType)>& reply) { | 125 const base::Callback<void(ReplyArgType)>& reply) { |
127 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = | 126 scoped_refptr<base::SingleThreadTaskRunner> task_runner = |
128 GetMessageLoopProxyForThread(identifier); | 127 GetTaskRunnerForThread(identifier); |
129 return base::PostTaskAndReplyWithResult(message_loop_proxy.get(), from_here, | 128 return base::PostTaskAndReplyWithResult(task_runner.get(), from_here, task, |
130 task, reply); | 129 reply); |
131 } | 130 } |
132 | 131 |
133 template <class T> | 132 template <class T> |
134 static bool DeleteSoon(ID identifier, | 133 static bool DeleteSoon(ID identifier, |
135 const tracked_objects::Location& from_here, | 134 const tracked_objects::Location& from_here, |
136 const T* object) { | 135 const T* object) { |
137 return GetMessageLoopProxyForThread(identifier) | 136 return GetTaskRunnerForThread(identifier)->DeleteSoon(from_here, object); |
138 ->DeleteSoon(from_here, object); | |
139 } | 137 } |
140 | 138 |
141 // Simplified wrappers for posting to the blocking thread pool. Use this | 139 // Simplified wrappers for posting to the blocking thread pool. Use this |
142 // for doing things like blocking I/O. | 140 // for doing things like blocking I/O. |
143 // | 141 // |
144 // The first variant will run the task in the pool with no sequencing | 142 // The first variant will run the task in the pool with no sequencing |
145 // semantics, so may get run in parallel with other posted tasks. The second | 143 // semantics, so may get run in parallel with other posted tasks. The second |
146 // variant will all post a task with no sequencing semantics, and will post a | 144 // variant will all post a task with no sequencing semantics, and will post a |
147 // reply task to the origin TaskRunner upon completion. The third variant | 145 // reply task to the origin TaskRunner upon completion. The third variant |
148 // provides sequencing between tasks with the same sequence token name. | 146 // provides sequencing between tasks with the same sequence token name. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 | 193 |
196 // Callable on any thread. Returns whether the threads message loop is valid. | 194 // Callable on any thread. Returns whether the threads message loop is valid. |
197 // If this returns false it means the thread is in the process of shutting | 195 // If this returns false it means the thread is in the process of shutting |
198 // down. | 196 // down. |
199 static bool IsMessageLoopValid(ID identifier) WARN_UNUSED_RESULT; | 197 static bool IsMessageLoopValid(ID identifier) WARN_UNUSED_RESULT; |
200 | 198 |
201 // If the current message loop is one of the known threads, returns true and | 199 // If the current message loop is one of the known threads, returns true and |
202 // sets identifier to its ID. | 200 // sets identifier to its ID. |
203 static bool GetCurrentThreadIdentifier(ID* identifier) WARN_UNUSED_RESULT; | 201 static bool GetCurrentThreadIdentifier(ID* identifier) WARN_UNUSED_RESULT; |
204 | 202 |
205 // Callers can hold on to a refcounted MessageLoopProxy beyond the lifetime | 203 // Callers can hold on to a refcounted SingleThreadTaskRunner beyond the |
206 // of the thread. | 204 // lifetime of the thread. |
207 static scoped_refptr<base::MessageLoopProxy> GetMessageLoopProxyForThread( | 205 static scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunnerForThread( |
208 ID identifier); | 206 ID identifier); |
209 | 207 |
210 // Returns an appropriate error message for when | 208 // Returns an appropriate error message for when |
211 // DCHECK_CURRENTLY_ON_WEB_THREAD() fails. | 209 // DCHECK_CURRENTLY_ON_WEB_THREAD() fails. |
212 static std::string GetDCheckCurrentlyOnErrorMessage(ID expected); | 210 static std::string GetDCheckCurrentlyOnErrorMessage(ID expected); |
213 | 211 |
214 private: | 212 private: |
215 friend class WebThreadImpl; | 213 friend class WebThreadImpl; |
216 | 214 |
217 WebThread() {} | 215 WebThread() {} |
218 DISALLOW_COPY_AND_ASSIGN(WebThread); | 216 DISALLOW_COPY_AND_ASSIGN(WebThread); |
219 }; | 217 }; |
220 | 218 |
221 } // namespace web | 219 } // namespace web |
222 | 220 |
223 #endif // IOS_WEB_PUBLIC_WEB_THREAD_H_ | 221 #endif // IOS_WEB_PUBLIC_WEB_THREAD_H_ |
OLD | NEW |