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 | |
6 /** | |
7 * Defines the PPB_MessageLoop_Dev interface. | |
8 */ | |
9 label Chrome { | |
10 M18 = 0.1 | |
11 }; | |
12 | |
13 /** | |
14 * A message loop allows PPAPI calls to be issued on a thread. You may not | |
15 * issue any API calls on a thread without creating a message loop. It also | |
16 * allows you to post work to threads that will be issued on the message loop. | |
17 * | |
18 * To process work posted to the message loop, as well as completion callbacks | |
19 * for asynchronous operations, you must run the message loop via Run(). | |
20 * | |
21 * Note the system manages the lifetime of the instance (and all associated | |
22 * resources). If the instance is deleted from the page, background threads may | |
23 * suddenly see their PP_Resource handles become invalid. In this case, calls | |
24 * will fail with PP_ERROR_BADRESOURCE. If you need to access data associated | |
25 * with your instance, you will probably want to create some kind of threadsafe | |
26 * proxy object that can handle asynchonous destruction of the instance object. | |
27 * | |
28 * Typical usage: | |
29 * On the main thread: | |
30 * - Create the thread yourself (using pthreads). | |
31 * - Create the message loop resource. | |
32 * - Pass the message loop resource to your thread's main function. | |
33 * - Call PostWork() on the message loop to run functions on the thread. | |
34 * | |
35 * From the background thread's main function: | |
36 * - Call AttachToCurrentThread() with the message loop resource. | |
37 * - Call Run() with the message loop resource. | |
38 * | |
39 * Your callacks should look like this: | |
40 * void DoMyWork(void* user_data, int32_t status) { | |
41 * if (status != PP_OK) | |
42 * return; | |
43 * ... do your work... | |
44 * } | |
45 * | |
46 * (You can also create the message loop resource on the background thread, | |
47 * but then the main thread will have no reference to it should you want to | |
48 * call PostWork()). | |
49 * | |
50 * | |
51 * THREAD HANDLING | |
52 * | |
53 * The main thread has an implicitly created message loop. The main thread is | |
54 * the thread where PPP_InitializeModule and PPP_Instance functions are called. | |
55 * You can retrieve a reference to this message loop by calling | |
56 * GetForMainThread() or, if your code is on the main thread, | |
57 * GetForCurrentThread() will also work. | |
58 * | |
59 * Some special threads created by the system can not have message loops. In | |
60 * particular, the background thread created for audio processing has this | |
61 * requirement because it's intended to be highly responsive to keep up with | |
62 * the realtime requirements of audio processing. You can not make PPAPI calls | |
63 * from these threads. | |
64 * | |
65 * Once you associate a message loop for a thread, you don't have to keep a | |
66 * reference to it. The system will hold a reference to the message loop for as | |
67 * long as the thread is running. The current message loop can be retrieved | |
dmichael (off chromium)
2012/01/04 04:30:26
"as long as the thread is running" means as long a
brettw
2012/01/05 18:34:38
The latter.
| |
68 * using the GetCurrent() function. | |
69 * | |
70 * It is legal to create threads in your plugin without message loops, but | |
71 * PPAPI calls will fail. | |
dmichael (off chromium)
2012/01/04 04:30:26
It makes me a little sad that we won't allow calls
brettw
2012/01/05 18:34:38
There are two related concepts. The first is regis
| |
72 * | |
73 * You can create a message loop object on a thread and never actually run the | |
74 * message loop. This will allow you to call blocking PPAPI calls (via | |
75 * PP_BlockUntilComplete()). If you make any asynchronous calls, the callbacks | |
76 * from those calls will be queued in the message loop and never run. The same | |
77 * thing will happen if work is scheduled after the message loop exits and | |
78 * the message loop is not run again. | |
79 * | |
80 * | |
81 * DESTRUCTION AND ERROR HANDLING | |
82 * | |
83 * Often, your application will associate memory with completion callbacks. For | |
84 * example, the C++ CompletionCallbackFactory has a small amount of | |
85 * heap-allocated memory for each callback. This memory will be leaked if the | |
86 * callback is never run. To avoid this memory leak, you need to be careful | |
87 * about error handling and shutdown. | |
88 * | |
89 * There are a number of cases where posted callbacks will never be run: | |
90 * | |
91 * - You tear down the thread (via pthreads) without "destroying" the message | |
92 * loop (via PostQuit with should_destroy = PP_TRUE). In this case, any | |
93 * tasks in the message queue will be lost. | |
94 * | |
95 * - You create a message loop, post callbacks to it, and never run it. | |
96 * | |
97 * - You quit the message loop via QuitNow or PostQuit with should_destroy | |
98 * set to PP_FALSE. In this case, the system will assume the message loop | |
99 * will be run again later and keep your tasks. | |
100 * | |
101 * To do proper shutdown, call PostQuit with should_destroy = PP_TRUE. This | |
102 * will prohibit future work from being posted, and will allow the message loop | |
103 * to run until all pending tasks are run. | |
104 * | |
105 * If you post a callback to a message loop that's been destroyed, or to an | |
106 * invalid message loop, PostTask will return an error and will not run the | |
107 * callback. This is true even for callbacks with the "required" flag set, | |
108 * since the system may not even know what thread to issue the error callback | |
109 * on. | |
110 * | |
111 * Therefore, you should check for errors from PostWork and destroy any | |
112 * associated memory to avoid leaks. If you're using the C++ | |
113 * CompletionCallbackFactory, use the following pattern: | |
114 * | |
115 * pp::CompletionCallback callback = factory_.NewOptionalCallback(...); | |
116 * int32_t result = message_loop.PostWork(callback); | |
117 * if (result != PP_OK_COMPLETIONPENDING) | |
118 * callback.Run(result); | |
119 * | |
120 * This will run the callback with an error value, and assumes that the | |
121 * implementation of your callback checks the "result" argument and returns | |
122 * immediately on error. | |
123 */ | |
124 interface PPB_MessageLoop_Dev { | |
125 /** | |
126 * Creates a message loop resource. | |
127 * | |
128 * This may be called from any thread. After your thread starts but before | |
129 * issuing any other PPAPI calls on it, you must associate it with a message | |
130 * loop by calling AttachToCurrentThread. | |
131 */ | |
132 PP_Resource Create(PP_Instance instance); | |
133 | |
134 /** | |
135 * Returns a resource identifying the message loop for the main thread. The | |
136 * main thread always has a message loop created by the system. | |
137 */ | |
138 PP_Resource GetForMainThread(); | |
139 | |
140 /** | |
141 * Returns a reference to the PPB_MessageLoop object attached to the current | |
142 * thread. If there is no attached message loop, the return value will be 0. | |
143 */ | |
144 PP_Resource GetCurrent(); | |
145 | |
146 /** | |
147 * Sets the given message loop resource as being the associated message loop | |
148 * for the currently running thread. | |
149 * | |
150 * You must call this function exactly once on a thread before making any | |
151 * PPAPI calls. A message loop can only be attached to one thread, and the | |
152 * message loop can not be changed later. The message loop will be attached | |
153 * as long as the thread is running or until you quit with should_destroy | |
154 * est to PP_TRUE. | |
dmichael (off chromium)
2012/01/04 04:30:26
nit: est->set
| |
155 * | |
156 * If this function fails, attempting to run the message loop will fail. | |
157 * Note that you can still post work to the message loop: it will get queued | |
158 * up should the message loop eventually be successfully attached and run. | |
159 * | |
160 * @return | |
161 * - PP_OK: The message loop was successfully attached to the thread and is | |
162 * ready to use. | |
163 * - PP_ERROR_BADRESOURCE: The given message loop resource is invalid. | |
164 * - PP_ERROR_INPROGRESS: The current thread already has a message loop | |
165 * attached. This will always be the case for the main thread, which has | |
166 * an implicit system-created message loop attached. | |
167 * - PP_ERROR_WRONG_THREAD: The current thread type can not have a message | |
168 * loop attached to it. See the interface level discussion about these | |
169 * special threads, which include realtime audio threads. | |
170 */ | |
171 int32_t AttachToCurrentThread([in] PP_Resource message_loop); | |
172 | |
173 /** | |
174 * Runs the thread message loop. Running the message loop is required for you | |
175 * to get issued completion callbacks on the thread. | |
176 * | |
177 * The message loop identified by the argument must have been previously | |
178 * successfully attached to the current thread. | |
179 * | |
180 * You may call Run() nested inside another message loop. This nested loop | |
181 * will run until it's requested to quit, and control will then return to the | |
182 * next outermost message loop on the stack. If you set should_destroy, all | |
183 * nested invocations of the message loop will be quit. Nested message loops | |
184 * can be tricky, so use with caution. | |
185 * | |
186 * You may not call Run() on the main thread's message loop. The system | |
187 * implicitly runs a message loop on the main thread where it issues PPP_ | |
188 * calls, and nested message loops are not permitted on the main thread to | |
189 * avoid reentrancy issues with PPAPI calls. | |
190 * | |
191 * @return | |
192 * - PP_OK: The message loop was successfully run. Note that on | |
193 * success, the message loop will only exit when you call QuitNow() or | |
194 * PostQuit(). | |
195 * - PP_ERROR_BADRESOURCE: The given message loop resource is invalid. | |
196 * - PP_ERROR_WRONG_THREAD: You are attempting to run a message loop that | |
197 * has not been successfully attached to the current thread. Call | |
198 * AttachToCurrentThread(). | |
199 * - PP_ERROR_BLOCKS_MAIN_THREAD: You are attempting to call this function | |
200 * on the main thread. This is not supported as described above. | |
201 */ | |
202 int32_t Run([in] PP_Resource message_loop); | |
203 | |
204 /** | |
205 * Quits the message loop identified by the given resource without running | |
206 * pending work. This function may only be called from within the current | |
207 * message loop for the current thread. | |
208 * | |
209 * This does not "destroy" the message loop, so it will still be possible to | |
210 * post work and Run the message loop again. If you do not Run() the loop | |
211 * again, those completion callbacks will not be run which may cause leaks. | |
212 * Generally this is used to exit nested loops. | |
213 * | |
214 * See also PostQuit(), which is usually more appropriate for most uses. It | |
215 * allows you to properly destroy the loop and will allow pending work to | |
216 * complete before exiting. | |
217 * | |
218 * @return | |
219 * - PP_OK if the message loop will be exited. | |
220 * - PP_ERROR_BADRESOURCE: The given message loop resource is invalid. | |
221 * - PP_ERROR_WRONG_THREAD: The message loop does not identify the message | |
222 * loop registered for the current thread, or the current thread is the | |
223 * main thread (which you can not exit). | |
224 */ | |
225 int32_t QuitNow([in] PP_Resource message_loop); | |
226 | |
227 /** | |
228 * Schedules work to run on the given message loop. This may be called from | |
229 * any thread. Posted work will be executed in the order it was posted when | |
230 * the message loop is Run(). | |
231 * | |
232 * @arg message_loop The message loop resource. | |
233 * | |
234 * @arg callback A pointer to the completion callback to execute from the | |
235 * message loop. | |
236 * | |
237 * @arg delay_ms The number of millseconds to delay execution of the given | |
238 * completion callback. Passing 0 means it will get queued normally and | |
239 * executed in order, as with PostWork(). In error cases, the callback may | |
dmichael (off chromium)
2012/01/04 04:30:26
nit: don't want "as with PostWork()" anymore.
| |
240 * get executed before this timeout. | |
241 * | |
242 * @arg prevent_nested Controls whether the callback can be executed in a | |
243 * nested message loop. If you're not sure what you want, pass PP_FALSE here. | |
244 * Normal tasks posted via PostWork() will execute in nested message loops | |
245 * (prevent_nested = PP_FALSE). If you pass PP_TRUE, only the outermost | |
246 * invocation of the message loop's Run() function on the stack will execute | |
247 * the given completion callback. This can be useful if you need to do some | |
248 * cleanup (like deleting a pointer) that must be done after all code | |
249 * currently on the stack executes. | |
250 * | |
251 * | |
252 * The completion callback will be called with PP_OK as the "result" parameter | |
253 * if it is run normally. It is good practice to check for PP_OK and return | |
254 * early otherwise. | |
255 * | |
256 * The "required" flag on the completion callback is ignored. If there is an | |
257 * error posting your callback, the error will be returned from PostWork and | |
258 * the callback will never be run (because there is no appropriate place to | |
259 * run your callback with an error without causing unexpected threading | |
260 * problems). If you associate memory with the completion callback (for | |
261 * example, you're using the C++ CompletionCallbackFactory), you will need to | |
262 * free this or manually run the callback. See "Desctruction and error | |
263 * handling" above. | |
264 * | |
265 * | |
266 * You can call this function before the message loop has started and the | |
267 * work will get queued until the message loop is run. You can also post | |
268 * work after the message loop has exited as long as should_destroy was | |
269 * PP_FALSE. It will be queued until the next invocation of Run(). | |
270 * | |
271 * @return | |
272 * - PP_OK_COMPLETIONPENDING: The work was posted to the message loop's | |
273 * queue. As described above, this does not mean that the work has been or | |
274 * will be executed (if you never run the message loop after posting). | |
275 * - PP_ERROR_BADRESOURCE: The given message loop resource is invalid. | |
276 * - PP_ERROR_BADARGUMENT: The function pointer for the completion callback | |
277 * is null (this will be the case if you pass PP_BlockUntilComplete()). | |
278 * - PP_ERROR_FAILED: The message loop has been destroyed. | |
279 */ | |
280 int32_t PostWork([in] PP_Resource message_loop, | |
281 [in] PP_CompletionCallback callback, | |
282 [in] int64_t delay_ms, | |
283 [in] PP_Bool prevent_nested); | |
284 | |
285 /** | |
286 * Posts a quit message to the given message loop's work queue. Work posted | |
287 * before that point will be processed before quitting. | |
288 * | |
289 * This may be called on the message loop registered for the current thread, | |
290 * or it may be called on the message loop registered for another thread. | |
291 * | |
292 * @arg should_destroy Marks the message loop as being in a destroyed state | |
293 * and prevents further posting of messages. | |
294 * | |
295 * If you quit a message loop without setting should_destroy, it will still | |
296 * be attached to the thread and you can still run it again by calling Run() | |
297 * again. If you destroy it, it will be detached from the current thread | |
298 * as soon as the outermost invocation of Run() exits. | |
299 * | |
300 * If you are running nested message loops and pass PP_FALSE to | |
301 * should_destroy, this will only quit the outermost one. This may not be | |
302 * what your calling code expects, so you will need to be extra cautious if | |
303 * you run nested message loops. If you are destroying the message loop, | |
304 * all nested invocations will be quit. | |
305 * | |
306 * @return | |
307 * - PP_OK: The request to quit was successfully posted. | |
308 * - PP_ERROR_BADRESOURCE: The message loop was invalid. | |
309 * - PP_ERROR_WRONG_THREAD: You are attempting to quit the main thread. | |
310 * The main thread's message loop is managed by the system and can't be | |
311 * quit. | |
312 */ | |
313 int32_t PostQuit([in] PP_Resource message_loop, PP_Bool should_destroy); | |
314 }; | |
OLD | NEW |