OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PPAPI_CPP_MESSAGE_LOOP_H_ | 5 #ifndef PPAPI_CPP_MESSAGE_LOOP_H_ |
6 #define PPAPI_CPP_MESSAGE_LOOP_H_ | 6 #define PPAPI_CPP_MESSAGE_LOOP_H_ |
7 | 7 |
8 #include "ppapi/cpp/resource.h" | 8 #include "ppapi/cpp/resource.h" |
9 | 9 |
10 /// @file | |
11 /// This file defines the PPB_MessageLoop API, which is used to allow | |
12 /// PPAPI calls from any thread | |
dmichael (off chromium)
2013/05/30 22:35:33
nit: I'd just leave off the ", which is used..." p
Andy
2013/05/30 22:49:05
Done.
| |
13 | |
10 namespace pp { | 14 namespace pp { |
11 | 15 |
12 class CompletionCallback; | 16 class CompletionCallback; |
13 class InstanceHandle; | 17 class InstanceHandle; |
14 | 18 |
15 /// A message loop allows PPAPI calls to be issued on a thread. You may not | 19 /// A message loop allows PPAPI calls to be issued on a thread. You may not |
16 /// issue any API calls on a thread without creating a message loop. It also | 20 /// issue any API calls on a thread without creating a message loop. It also |
17 /// allows you to post work to the message loop for a thread. | 21 /// allows you to post work to the message loop for a thread. |
18 /// | 22 /// |
19 /// To process work posted to the message loop, as well as completion callbacks | 23 /// To process work posted to the message loop, as well as completion callbacks |
(...skipping 12 matching lines...) Expand all Loading... | |
32 /// - Create the thread yourself (using pthreads). | 36 /// - Create the thread yourself (using pthreads). |
33 /// - Create the message loop resource. | 37 /// - Create the message loop resource. |
34 /// - Pass the message loop resource to your thread's main function. | 38 /// - Pass the message loop resource to your thread's main function. |
35 /// - Call PostWork() on the message loop to run functions on the thread. | 39 /// - Call PostWork() on the message loop to run functions on the thread. |
36 /// | 40 /// |
37 /// From the background thread's main function: | 41 /// From the background thread's main function: |
38 /// - Call AttachToCurrentThread() with the message loop resource. | 42 /// - Call AttachToCurrentThread() with the message loop resource. |
39 /// - Call Run() with the message loop resource. | 43 /// - Call Run() with the message loop resource. |
40 /// | 44 /// |
41 /// Your callbacks should look like this: | 45 /// Your callbacks should look like this: |
42 /// void DoMyWork(void* user_data, int32_t status) { | 46 /// @code |
43 /// if (status != PP_OK) { | 47 /// void DoMyWork(void* user_data, int32_t status) { |
44 /// Cleanup(); // e.g. free user_data. | 48 /// if (status != PP_OK) { |
45 /// return; | 49 /// Cleanup(); // e.g. free user_data. |
46 /// } | 50 /// return; |
47 /// ... do your work... | 51 /// } |
48 /// } | 52 /// ... do your work... |
53 /// } | |
54 /// @endcode | |
49 /// For a C++ example, see ppapi/utility/threading/simple_thread.h | 55 /// For a C++ example, see ppapi/utility/threading/simple_thread.h |
50 /// | 56 /// |
51 /// (You can also create the message loop resource on the background thread, | 57 /// (You can also create the message loop resource on the background thread, |
52 /// but then the main thread will have no reference to it should you want to | 58 /// but then the main thread will have no reference to it should you want to |
53 /// call PostWork()). | 59 /// call PostWork()). |
54 /// | 60 /// |
55 /// | 61 /// |
56 /// THREAD HANDLING | 62 /// THREAD HANDLING |
57 /// | 63 /// |
58 /// The main thread has an implicitly created message loop. The main thread is | 64 /// The main thread has an implicitly created message loop. The main thread is |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 /// | 115 /// |
110 /// If you post a callback to a message loop that's been destroyed, or to an | 116 /// If you post a callback to a message loop that's been destroyed, or to an |
111 /// invalid message loop, PostWork will return an error and will not run the | 117 /// invalid message loop, PostWork will return an error and will not run the |
112 /// callback. This is true even for callbacks with the "required" flag set, | 118 /// callback. This is true even for callbacks with the "required" flag set, |
113 /// since the system may not even know what thread to issue the error callback | 119 /// since the system may not even know what thread to issue the error callback |
114 /// on. | 120 /// on. |
115 /// | 121 /// |
116 /// Therefore, you should check for errors from PostWork and destroy any | 122 /// Therefore, you should check for errors from PostWork and destroy any |
117 /// associated memory to avoid leaks. If you're using the C++ | 123 /// associated memory to avoid leaks. If you're using the C++ |
118 /// CompletionCallbackFactory, use the following pattern: | 124 /// CompletionCallbackFactory, use the following pattern: |
119 /// | 125 /// @code |
120 /// pp::CompletionCallback callback = factory_.NewOptionalCallback(...); | 126 /// pp::CompletionCallback callback = factory_.NewOptionalCallback(...); |
121 /// int32_t result = message_loop.PostWork(callback); | 127 /// int32_t result = message_loop.PostWork(callback); |
122 /// if (result != PP_OK) | 128 /// if (result != PP_OK) |
123 /// callback.Run(result); | 129 /// callback.Run(result); |
124 /// | 130 /// @endcode |
125 /// This will run the callback with an error value, and assumes that the | 131 /// This will run the callback with an error value, and assumes that the |
126 /// implementation of your callback checks the "result" argument and returns | 132 /// implementation of your callback checks the "result" argument and returns |
127 /// immediately on error. | 133 /// immediately on error. |
128 class MessageLoop : public Resource { | 134 class MessageLoop : public Resource { |
129 public: | 135 public: |
130 /// Creates an is_null() MessageLoop resource. | 136 /// Creates an is_null() MessageLoop resource. |
131 MessageLoop(); | 137 MessageLoop(); |
132 | 138 |
133 /// Creates a message loop associated with the given instance. The resource | 139 /// Creates a message loop associated with the given instance. The resource |
134 /// will be is_null() on failure. | 140 /// will be is_null() on failure. |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
254 /// - PP_ERROR_BADRESOURCE: The message loop was invalid. | 260 /// - PP_ERROR_BADRESOURCE: The message loop was invalid. |
255 /// - PP_ERROR_WRONG_THREAD: You are attempting to quit the main thread. | 261 /// - PP_ERROR_WRONG_THREAD: You are attempting to quit the main thread. |
256 /// The main thread's message loop is managed by the system and can't be | 262 /// The main thread's message loop is managed by the system and can't be |
257 /// quit. | 263 /// quit. |
258 int32_t PostQuit(bool should_destroy); | 264 int32_t PostQuit(bool should_destroy); |
259 }; | 265 }; |
260 | 266 |
261 } // namespace pp | 267 } // namespace pp |
262 | 268 |
263 #endif // PPAPI_CPP_MESSAGE_LOOP_H_ | 269 #endif // PPAPI_CPP_MESSAGE_LOOP_H_ |
OLD | NEW |