OLD | NEW |
---|---|
(Empty) | |
1 /* Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
dmichael (off chromium)
2012/01/18 21:16:53
nit: 2012
| |
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. | |
dmichael (off chromium)
2012/01/18 21:16:53
suggest: might read better rephrased:
"allows you
| |
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; | |
dmichael (off chromium)
2012/01/18 21:16:53
Would it be better to add a smidge more detail to
| |
43 * ... do your work... | |
44 * } | |
45 * For a C++ example, see ppapi/utility/threading/simple_thread.h | |
dmichael (off chromium)
2012/01/18 21:16:53
I don't see this in the CL; did you intend to add
brettw
2012/01/19 03:04:44
That's in my impl patch: http://codereview.chromiu
| |
46 * | |
47 * (You can also create the message loop resource on the background thread, | |
48 * but then the main thread will have no reference to it should you want to | |
49 * call PostWork()). | |
50 * | |
51 * | |
52 * THREAD HANDLING | |
53 * | |
54 * The main thread has an implicitly created message loop. The main thread is | |
55 * the thread where PPP_InitializeModule and PPP_Instance functions are called. | |
56 * You can retrieve a reference to this message loop by calling | |
57 * GetForMainThread() or, if your code is on the main thread, | |
58 * GetForCurrentThread() will also work. | |
59 * | |
60 * Some special threads created by the system can not have message loops. In | |
61 * particular, the background thread created for audio processing has this | |
62 * requirement because it's intended to be highly responsive to keep up with | |
63 * the realtime requirements of audio processing. You can not make PPAPI calls | |
64 * from these threads. | |
65 * | |
66 * Once you associate a message loop for a thread, you don't have to keep a | |
dmichael (off chromium)
2012/01/18 21:16:53
for->with?
| |
67 * reference to it. The system will hold a reference to the message loop for as | |
68 * long as the thread is running. The current message loop can be retrieved | |
69 * using the GetCurrent() function. | |
70 * | |
71 * It is legal to create threads in your plugin without message loops, but | |
72 * PPAPI calls will fail. | |
dmichael (off chromium)
2012/01/18 21:16:53
should we have a whitelist, or say "(unless the op
| |
73 * | |
74 * You can create a message loop object on a thread and never actually run the | |
75 * message loop. This will allow you to call blocking PPAPI calls (via | |
76 * PP_BlockUntilComplete()). If you make any asynchronous calls, the callbacks | |
77 * from those calls will be queued in the message loop and never run. The same | |
78 * thing will happen if work is scheduled after the message loop exits and | |
79 * the message loop is not run again. | |
80 * | |
81 * | |
82 * DESTRUCTION AND ERROR HANDLING | |
83 * | |
84 * Often, your application will associate memory with completion callbacks. For | |
85 * example, the C++ CompletionCallbackFactory has a small amount of | |
86 * heap-allocated memory for each callback. This memory will be leaked if the | |
87 * callback is never run. To avoid this memory leak, you need to be careful | |
88 * about error handling and shutdown. | |
89 * | |
90 * There are a number of cases where posted callbacks will never be run: | |
91 * | |
92 * - You tear down the thread (via pthreads) without "destroying" the message | |
93 * loop (via PostQuit with should_destroy = PP_TRUE). In this case, any | |
94 * tasks in the message queue will be lost. | |
95 * | |
96 * - You create a message loop, post callbacks to it, and never run it. | |
97 * | |
98 * - You quit the message loop via QuitNow or PostQuit with should_destroy | |
dmichael (off chromium)
2012/01/18 21:16:53
QuitNow is gone
| |
99 * set to PP_FALSE. In this case, the system will assume the message loop | |
100 * will be run again later and keep your tasks. | |
101 * | |
102 * To do proper shutdown, call PostQuit with should_destroy = PP_TRUE. This | |
103 * will prohibit future work from being posted, and will allow the message loop | |
104 * to run until all pending tasks are run. | |
105 * | |
106 * If you post a callback to a message loop that's been destroyed, or to an | |
107 * invalid message loop, PostTask will return an error and will not run the | |
108 * callback. This is true even for callbacks with the "required" flag set, | |
109 * since the system may not even know what thread to issue the error callback | |
110 * on. | |
111 * | |
112 * Therefore, you should check for errors from PostWork and destroy any | |
113 * associated memory to avoid leaks. If you're using the C++ | |
114 * CompletionCallbackFactory, use the following pattern: | |
115 * | |
116 * pp::CompletionCallback callback = factory_.NewOptionalCallback(...); | |
117 * int32_t result = message_loop.PostWork(callback); | |
118 * if (result != PP_OK_COMPLETIONPENDING) | |
119 * callback.Run(result); | |
120 * | |
121 * This will run the callback with an error value, and assumes that the | |
122 * implementation of your callback checks the "result" argument and returns | |
123 * immediately on error. | |
124 */ | |
125 interface PPB_MessageLoop_Dev { | |
126 /** | |
127 * Creates a message loop resource. | |
128 * | |
129 * This may be called from any thread. After your thread starts but before | |
130 * issuing any other PPAPI calls on it, you must associate it with a message | |
131 * loop by calling AttachToCurrentThread. | |
132 */ | |
133 PP_Resource Create(PP_Instance instance); | |
134 | |
135 /** | |
136 * Returns a resource identifying the message loop for the main thread. The | |
137 * main thread always has a message loop created by the system. | |
138 */ | |
139 PP_Resource GetForMainThread(); | |
140 | |
141 /** | |
142 * Returns a reference to the PPB_MessageLoop object attached to the current | |
143 * thread. If there is no attached message loop, the return value will be 0. | |
dmichael (off chromium)
2012/01/18 21:16:53
Maybe we should define something like:
const PP_Re
brettw
2012/01/19 03:04:44
IMO 0 is fine and makes perfect sense to me, and I
| |
144 */ | |
145 PP_Resource GetCurrent(); | |
146 | |
147 /** | |
148 * Sets the given message loop resource as being the associated message loop | |
149 * for the currently running thread. | |
150 * | |
151 * You must call this function exactly once on a thread before making any | |
152 * PPAPI calls. A message loop can only be attached to one thread, and the | |
153 * message loop can not be changed later. The message loop will be attached | |
154 * as long as the thread is running or until you quit with should_destroy | |
155 * set to PP_TRUE. | |
156 * | |
157 * If this function fails, attempting to run the message loop will fail. | |
158 * Note that you can still post work to the message loop: it will get queued | |
159 * up should the message loop eventually be successfully attached and run. | |
160 * | |
161 * @return | |
162 * - PP_OK: The message loop was successfully attached to the thread and is | |
163 * ready to use. | |
164 * - PP_ERROR_BADRESOURCE: The given message loop resource is invalid. | |
165 * - PP_ERROR_INPROGRESS: The current thread already has a message loop | |
166 * attached. This will always be the case for the main thread, which has | |
167 * an implicit system-created message loop attached. | |
168 * - PP_ERROR_WRONG_THREAD: The current thread type can not have a message | |
169 * loop attached to it. See the interface level discussion about these | |
170 * special threads, which include realtime audio threads. | |
171 */ | |
172 int32_t AttachToCurrentThread([in] PP_Resource message_loop); | |
173 | |
174 /** | |
175 * Runs the thread message loop. Running the message loop is required for you | |
176 * to get issued completion callbacks on the thread. | |
177 * | |
178 * The message loop identified by the argument must have been previously | |
179 * successfully attached to the current thread. | |
180 * | |
181 * You may not run nested message loops. Since the main thread has an | |
182 * implicit message loop that the system runs, you may not call Run on the | |
183 * main thread. | |
184 * | |
185 * @return | |
186 * - PP_OK: The message loop was successfully run. Note that on | |
187 * success, the message loop will only exit when you call PostQuit(). | |
dmichael (off chromium)
2012/01/18 21:16:53
Should we have a code to distinguish "quit without
brettw
2012/01/19 03:04:44
The thread needs to keep a flag anyway.
| |
188 * - PP_ERROR_BADRESOURCE: The given message loop resource is invalid. | |
189 * - PP_ERROR_WRONG_THREAD: You are attempting to run a message loop that | |
190 * has not been successfully attached to the current thread. Call | |
191 * AttachToCurrentThread(). | |
192 * - PP_ERROR_INPROGRESS: You are attempting to call Run in a nested | |
193 * fashion (Run is already on the stack). This will occur if you attempt | |
194 * to call run on the main thread's message loop (see above). | |
195 */ | |
196 int32_t Run([in] PP_Resource message_loop); | |
197 | |
198 /** | |
199 * Schedules work to run on the given message loop. This may be called from | |
200 * any thread. Posted work will be executed in the order it was posted when | |
201 * the message loop is Run(). | |
202 * | |
203 * @arg message_loop The message loop resource. | |
204 * | |
205 * @arg callback A pointer to the completion callback to execute from the | |
206 * message loop. | |
207 * | |
208 * @arg delay_ms The number of millseconds to delay execution of the given | |
209 * completion callback. Passing 0 means it will get queued normally and | |
210 * executed in order. | |
211 * | |
212 * @arg prevent_nested Controls whether the callback can be executed in a | |
213 * nested message loop. If you're not sure what you want, pass PP_FALSE here. | |
214 * Normal tasks posted via PostWork() will execute in nested message loops | |
215 * (prevent_nested = PP_FALSE). If you pass PP_TRUE, only the outermost | |
216 * invocation of the message loop's Run() function on the stack will execute | |
217 * the given completion callback. This can be useful if you need to do some | |
218 * cleanup (like deleting a pointer) that must be done after all code | |
219 * currently on the stack executes. | |
dmichael (off chromium)
2012/01/18 21:16:53
delete prevent_nested comment
| |
220 * | |
221 * | |
222 * The completion callback will be called with PP_OK as the "result" parameter | |
223 * if it is run normally. It is good practice to check for PP_OK and return | |
224 * early otherwise. | |
225 * | |
226 * The "required" flag on the completion callback is ignored. If there is an | |
227 * error posting your callback, the error will be returned from PostWork and | |
228 * the callback will never be run (because there is no appropriate place to | |
229 * run your callback with an error without causing unexpected threading | |
230 * problems). If you associate memory with the completion callback (for | |
231 * example, you're using the C++ CompletionCallbackFactory), you will need to | |
232 * free this or manually run the callback. See "Desctruction and error | |
233 * handling" above. | |
234 * | |
235 * | |
236 * You can call this function before the message loop has started and the | |
237 * work will get queued until the message loop is run. You can also post | |
238 * work after the message loop has exited as long as should_destroy was | |
239 * PP_FALSE. It will be queued until the next invocation of Run(). | |
240 * | |
241 * @return | |
242 * - PP_OK_COMPLETIONPENDING: The work was posted to the message loop's | |
243 * queue. As described above, this does not mean that the work has been or | |
244 * will be executed (if you never run the message loop after posting). | |
245 * - PP_ERROR_BADRESOURCE: The given message loop resource is invalid. | |
246 * - PP_ERROR_BADARGUMENT: The function pointer for the completion callback | |
247 * is null (this will be the case if you pass PP_BlockUntilComplete()). | |
248 * - PP_ERROR_FAILED: The message loop has been destroyed. | |
249 */ | |
250 int32_t PostWork([in] PP_Resource message_loop, | |
251 [in] PP_CompletionCallback callback, | |
252 [in] int64_t delay_ms); | |
253 | |
254 /** | |
255 * Posts a quit message to the given message loop's work queue. Work posted | |
256 * before that point will be processed before quitting. | |
257 * | |
258 * This may be called on the message loop registered for the current thread, | |
259 * or it may be called on the message loop registered for another thread. | |
260 * | |
261 * @arg should_destroy Marks the message loop as being in a destroyed state | |
262 * and prevents further posting of messages. | |
263 * | |
264 * If you quit a message loop without setting should_destroy, it will still | |
265 * be attached to the thread and you can still run it again by calling Run() | |
266 * again. If you destroy it, it will be detached from the current thread. | |
267 * | |
268 * @return | |
269 * - PP_OK: The request to quit was successfully posted. | |
270 * - PP_ERROR_BADRESOURCE: The message loop was invalid. | |
271 * - PP_ERROR_WRONG_THREAD: You are attempting to quit the main thread. | |
272 * The main thread's message loop is managed by the system and can't be | |
273 * quit. | |
274 */ | |
275 int32_t PostQuit([in] PP_Resource message_loop, PP_Bool should_destroy); | |
276 }; | |
OLD | NEW |