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