15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/** 72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * Defines the PPB_MessageLoop interface. 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)label Chrome { 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) M25 = 1.0 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/** 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * A message loop allows PPAPI calls to be issued on a thread. You may not 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * issue any API calls on a thread without creating a message loop. It also 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * allows you to post work to the message loop for a thread. 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * To process work posted to the message loop, as well as completion callbacks 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * for asynchronous operations, you must run the message loop via Run(). 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Note the system manages the lifetime of the instance (and all associated 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * resources). If the instance is deleted from the page, background threads may 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * suddenly see their PP_Resource handles become invalid. In this case, calls 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * will fail with PP_ERROR_BADRESOURCE. If you need to access data associated 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * with your instance, you will probably want to create some kind of threadsafe 26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) * proxy object that can handle asynchronous destruction of the instance object. 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Typical usage: 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * On the main thread: 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - Create the thread yourself (using pthreads). 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - Create the message loop resource. 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - Pass the message loop resource to your thread's main function. 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - Call PostWork() on the message loop to run functions on the thread. 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * From the background thread's main function: 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - Call AttachToCurrentThread() with the message loop resource. 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - Call Run() with the message loop resource. 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) * Your callbacks should look like this: 40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * @code 41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * void DoMyWork(void* user_data, int32_t status) { 42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * if (status != PP_OK) { 43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * Cleanup(); // e.g. free user_data. 44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * return; 45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * } 46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * ... do your work... 47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * } 48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * @endcode 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * For a C++ example, see ppapi/utility/threading/simple_thread.h 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * (You can also create the message loop resource on the background thread, 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * but then the main thread will have no reference to it should you want to 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * call PostWork()). 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THREAD HANDLING 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The main thread has an implicitly created message loop. The main thread is 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the thread where PPP_InitializeModule and PPP_Instance functions are called. 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * You can retrieve a reference to this message loop by calling 615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * GetForMainThread() or, if your code is on the main thread, GetCurrent() will 625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * also work. 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Some special threads created by the system can not have message loops. In 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * particular, the background thread created for audio processing has this 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * requirement because it's intended to be highly responsive to keep up with 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the realtime requirements of audio processing. You can not make PPAPI calls 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * from these threads. 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Once you associate a message loop with a thread, you don't have to keep a 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * reference to it. The system will hold a reference to the message loop for as 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * long as the thread is running. The current message loop can be retrieved 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * using the GetCurrent() function. 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * It is legal to create threads in your plugin without message loops, but 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * PPAPI calls will fail unless explicitly noted in the documentation. 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * You can create a message loop object on a thread and never actually run the 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * message loop. This will allow you to call blocking PPAPI calls (via 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * PP_BlockUntilComplete()). If you make any asynchronous calls, the callbacks 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * from those calls will be queued in the message loop and never run. The same 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * thing will happen if work is scheduled after the message loop exits and 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the message loop is not run again. 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * DESTRUCTION AND ERROR HANDLING 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Often, your application will associate memory with completion callbacks. For 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * example, the C++ CompletionCallbackFactory has a small amount of 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * heap-allocated memory for each callback. This memory will be leaked if the 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * callback is never run. To avoid this memory leak, you need to be careful 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * about error handling and shutdown. 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * There are a number of cases where posted callbacks will never be run: 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - You tear down the thread (via pthreads) without "destroying" the message 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * loop (via PostQuit with should_destroy = PP_TRUE). In this case, any 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * tasks in the message queue will be lost. 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - You create a message loop, post callbacks to it, and never run it. 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - You quit the message loop via PostQuit with should_destroy set to 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * PP_FALSE. In this case, the system will assume the message loop will be 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * run again later and keep your tasks. 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * To do proper shutdown, call PostQuit with should_destroy = PP_TRUE. This 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * will prohibit future work from being posted, and will allow the message loop 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * to run until all pending tasks are run. 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * If you post a callback to a message loop that's been destroyed, or to an 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * invalid message loop, PostWork will return an error and will not run the 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * callback. This is true even for callbacks with the "required" flag set, 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * since the system may not even know what thread to issue the error callback 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * on. 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Therefore, you should check for errors from PostWork and destroy any 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * associated memory to avoid leaks. If you're using the C++ 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * CompletionCallbackFactory, use the following pattern: 119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * @code 120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * pp::CompletionCallback callback = factory_.NewOptionalCallback(...); 121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * int32_t result = message_loop.PostWork(callback); 122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * if (result != PP_OK) 123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * callback.Run(result); 124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * @endcode 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This will run the callback with an error value, and assumes that the 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * implementation of your callback checks the "result" argument and returns 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * immediately on error. 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)interface PPB_MessageLoop { 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /** 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Creates a message loop resource. 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This may be called from any thread. After your thread starts but before 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * issuing any other PPAPI calls on it, you must associate it with a message 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * loop by calling AttachToCurrentThread. 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PP_Resource Create(PP_Instance instance); 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /** 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Returns a resource identifying the message loop for the main thread. The 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * main thread always has a message loop created by the system. 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PP_Resource GetForMainThread(); 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /** 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Returns a reference to the PPB_MessageLoop object attached to the current 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * thread. If there is no attached message loop, the return value will be 0. 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PP_Resource GetCurrent(); 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /** 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Sets the given message loop resource as being the associated message loop 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * for the currently running thread. 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * You must call this function exactly once on a thread before making any 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * PPAPI calls. A message loop can only be attached to one thread, and the 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * message loop can not be changed later. The message loop will be attached 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * as long as the thread is running or until you quit with should_destroy 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * set to PP_TRUE. 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * If this function fails, attempting to run the message loop will fail. 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Note that you can still post work to the message loop: it will get queued 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * up should the message loop eventually be successfully attached and run. 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @return 1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - PP_OK: The message loop was successfully attached to the thread and is 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * ready to use. 1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - PP_ERROR_BADRESOURCE: The given message loop resource is invalid. 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - PP_ERROR_INPROGRESS: The current thread already has a message loop 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * attached. This will always be the case for the main thread, which has 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * an implicit system-created message loop attached. 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - PP_ERROR_WRONG_THREAD: The current thread type can not have a message 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * loop attached to it. See the interface level discussion about these 1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * special threads, which include realtime audio threads. 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int32_t AttachToCurrentThread([in] PP_Resource message_loop); 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /** 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Runs the thread message loop. Running the message loop is required for you 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * to get issued completion callbacks on the thread. 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The message loop identified by the argument must have been previously 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * successfully attached to the current thread. 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * You may not run nested message loops. Since the main thread has an 1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * implicit message loop that the system runs, you may not call Run on the 1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * main thread. 1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @return 1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - PP_OK: The message loop was successfully run. Note that on 1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * success, the message loop will only exit when you call PostQuit(). 1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - PP_ERROR_BADRESOURCE: The given message loop resource is invalid. 1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - PP_ERROR_WRONG_THREAD: You are attempting to run a message loop that 1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * has not been successfully attached to the current thread. Call 1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * AttachToCurrentThread(). 1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - PP_ERROR_INPROGRESS: You are attempting to call Run in a nested 1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * fashion (Run is already on the stack). This will occur if you attempt 1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * to call run on the main thread's message loop (see above). 1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int32_t Run([in] PP_Resource message_loop); 2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /** 2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Schedules work to run on the given message loop. This may be called from 2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * any thread. Posted work will be executed in the order it was posted when 2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the message loop is Run(). 2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @param message_loop The message loop resource. 2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @param callback The completion callback to execute from the message loop. 2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 211c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) * @param delay_ms The number of milliseconds to delay execution of the given 2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * completion callback. Passing 0 means it will get queued normally and 2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * executed in order. 2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The completion callback will be called with PP_OK as the "result" parameter 2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * if it is run normally. It is good practice to check for PP_OK and return 2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * early otherwise. 2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The "required" flag on the completion callback is ignored. If there is an 2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * error posting your callback, the error will be returned from PostWork and 2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the callback will never be run (because there is no appropriate place to 2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * run your callback with an error without causing unexpected threading 2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * problems). If you associate memory with the completion callback (for 2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * example, you're using the C++ CompletionCallbackFactory), you will need to 226c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) * free this or manually run the callback. See "Destruction and error 2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * handling" above. 2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * You can call this function before the message loop has started and the 2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * work will get queued until the message loop is run. You can also post 2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * work after the message loop has exited as long as should_destroy was 2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * PP_FALSE. It will be queued until the next invocation of Run(). 2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @return 2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * - PP_OK: The work was posted to the message loop's queue. As described 2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * above, this does not mean that the work has been or will be executed 2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * (if you never run the message loop after posting). 2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - PP_ERROR_BADRESOURCE: The given message loop resource is invalid. 2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - PP_ERROR_BADARGUMENT: The function pointer for the completion callback 2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * is null (this will be the case if you pass PP_BlockUntilComplete()). 2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - PP_ERROR_FAILED: The message loop has been destroyed. 2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int32_t PostWork([in] PP_Resource message_loop, 2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) [in] PP_CompletionCallback callback, 2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) [in] int64_t delay_ms); 2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /** 2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Posts a quit message to the given message loop's work queue. Work posted 2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * before that point will be processed before quitting. 2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This may be called on the message loop registered for the current thread, 2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * or it may be called on the message loop registered for another thread. It 2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * is an error to attempt to PostQuit() the main thread loop. 2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @param should_destroy Marks the message loop as being in a destroyed state 2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * and prevents further posting of messages. 2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * If you quit a message loop without setting should_destroy, it will still 2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * be attached to the thread and you can still run it again by calling Run() 2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * again. If you destroy it, it will be detached from the current thread. 2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @return 2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - PP_OK: The request to quit was successfully posted. 2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - PP_ERROR_BADRESOURCE: The message loop was invalid. 2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - PP_ERROR_WRONG_THREAD: You are attempting to quit the main thread. 2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The main thread's message loop is managed by the system and can't be 2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * quit. 2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int32_t PostQuit([in] PP_Resource message_loop, PP_Bool should_destroy); 2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 272