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

Side by Side Diff: ppapi/c/dev/ppb_message_loop_dev.h

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

Powered by Google App Engine
This is Rietveld 408576698