OLD | NEW |
1 /* Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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_C_PP_COMPLETION_CALLBACK_H_ | 5 #ifndef PPAPI_C_PP_COMPLETION_CALLBACK_H_ |
6 #define PPAPI_C_PP_COMPLETION_CALLBACK_H_ | 6 #define PPAPI_C_PP_COMPLETION_CALLBACK_H_ |
7 | 7 |
8 /** | 8 /** |
9 * @file | 9 * @file |
10 * This file defines the API to create and run a callback. | 10 * This file defines the API to create and run a callback. |
11 */ | 11 */ |
12 | 12 |
13 #include <stdlib.h> | 13 #include <stdlib.h> |
14 | 14 |
15 #include "ppapi/c/pp_macros.h" | 15 #include "ppapi/c/pp_macros.h" |
16 #include "ppapi/c/pp_stdint.h" | 16 #include "ppapi/c/pp_stdint.h" |
17 | 17 |
18 /** | 18 /** |
19 * @addtogroup Typedefs | 19 * @addtogroup Typedefs |
20 * @{ | 20 * @{ |
21 */ | 21 */ |
22 | 22 |
23 /** | 23 /** |
24 * PP_CompletionCallback_Func defines the signature that you implement to | 24 * PP_CompletionCallback_Func defines the function signature that you implement |
25 * receive callbacks on asynchronous completion. | 25 * to receive callbacks on asynchronous completion of an operation. |
| 26 * |
| 27 * |user_data| is a pointer to user-specified data associated with this |
| 28 * function at callback creation. See PP_MakeCompletionCallback() for details. |
| 29 * |
| 30 * |result| is the result of the operation. Non-positive values correspond to |
| 31 * the error codes from pp_errors.h (excluding PP_OK_COMPLETIONPENDING). |
| 32 * Positive values indicate additional information such as bytes read. |
26 */ | 33 */ |
27 typedef void (*PP_CompletionCallback_Func)(void* user_data, int32_t result); | 34 typedef void (*PP_CompletionCallback_Func)(void* user_data, int32_t result); |
28 /** | 35 /** |
29 * @} | 36 * @} |
30 */ | 37 */ |
31 | 38 |
32 /** | 39 /** |
| 40 * |
| 41 * @addtogroup Enums |
| 42 * @{ |
| 43 */ |
| 44 |
| 45 /** |
| 46 * This enumeration contains flags used to control how non-NULL callbacks are |
| 47 * scheduled by asynchronous methods. |
| 48 */ |
| 49 typedef enum { |
| 50 /** |
| 51 * This flag allows any non-NULL callback to be always invoked asynchronously, |
| 52 * on success or error, even if the operation could complete synchronously |
| 53 * without blocking. |
| 54 * |
| 55 * The method taking such callback will always return PP_OK_COMPLETIONPENDING. |
| 56 * The callback will be invoked on the main thread of PPAPI execution. |
| 57 * |
| 58 * TODO(polina): make this the default once all the clients use flags. |
| 59 */ |
| 60 PP_COMPLETIONCALLBACK_FLAG_NONE = 0 << 0, |
| 61 /** |
| 62 * This flag allows any method taking such callback to complete synchronously |
| 63 * and not call the callback if the operation would not block. This is useful |
| 64 * when performance is an issue, and the operation bandwidth should not be |
| 65 * limited to the processing speed of the message loop. |
| 66 * |
| 67 * On synchronous method completion, the completion result will be returned |
| 68 * by the method itself. Otherwise, the method will return |
| 69 * PP_OK_COMPLETIONPENDING, and the callback will be invoked asynchronously on |
| 70 * the main thread of PPAPI execution. |
| 71 */ |
| 72 PP_COMPLETIONCALLBACK_FLAG_OPTIONAL = 1 << 0 |
| 73 } PP_CompletionCallback_Flag; |
| 74 PP_COMPILE_ASSERT_SIZE_IN_BYTES(PP_CompletionCallback_Flag, 4); |
| 75 |
| 76 |
| 77 /** |
33 * @addtogroup Structs | 78 * @addtogroup Structs |
34 * @{ | 79 * @{ |
35 */ | 80 */ |
36 | 81 |
37 /** | 82 /** |
38 * Any method that takes a PP_CompletionCallback has the option of completing | 83 * Any method that takes a PP_CompletionCallback can complete asynchronously. |
39 * asynchronously if the operation would block. Such a method should return | 84 * Refer to PP_CompletionCallback_Flag for more information. |
40 * PP_OK_COMPLETIONPENDING to indicate that the method will complete | |
41 * asynchronously and will always be invoked from the main thread of PPAPI | |
42 * execution. If the completion callback is NULL, then the operation will | |
43 * block if necessary to complete its work. PP_BlockUntilComplete() provides a | |
44 * convenient way to specify blocking behavior. Refer to PP_BlockUntilComplete | |
45 * for more information. | |
46 * | 85 * |
47 * The result parameter passes an int32_t that, if negative or equal to 0, | 86 * If PP_CompletionCallback_Func is NULL, the operation might block if necessary |
48 * indicate if the call will completely asynchronously (the callback will be | 87 * to complete the work. Refer to PP_BlockUntilComplete for more information. |
49 * called with a status code). A value greater than zero indicates additional | 88 * |
50 * information such as bytes read. | 89 * See PP_MakeCompletionCallback() for the description of each field. |
51 */ | 90 */ |
52 struct PP_CompletionCallback { | 91 struct PP_CompletionCallback { |
53 PP_CompletionCallback_Func func; | 92 PP_CompletionCallback_Func func; |
54 void* user_data; | 93 void* user_data; |
| 94 int32_t flags; |
55 }; | 95 }; |
56 /** | 96 /** |
57 * @} | 97 * @} |
58 */ | 98 */ |
59 | 99 |
60 /** | 100 /** |
61 * @addtogroup Functions | 101 * @addtogroup Functions |
62 * @{ | 102 * @{ |
63 */ | 103 */ |
64 | |
65 /** | 104 /** |
66 * PP_MakeCompletionCallback() is used to create a PP_CompletionCallback. | 105 * PP_MakeCompletionCallback() is used to create a PP_CompletionCallback |
| 106 * without flags. If you want to alter the default callback behavior, set the |
| 107 * flags to a bit field combination of PP_CompletionCallback_Flag's. |
67 * | 108 * |
68 * @param[in] func A PP_CompletionCallback_Func that will be called. | 109 * Example: |
69 * @param[in] user_data A pointer to user data passed to your callback | 110 * struct PP_CompletionCallback cc = PP_MakeCompletionCallback(Foo, NULL); |
70 * function. This is optional and is typically used to help track state | 111 * cc.flags = cc.flags | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; |
71 * when you may have multiple callbacks pending. | 112 * |
| 113 * @param[in] func A PP_CompletionCallback_Func to be called on completion. |
| 114 * @param[in] user_data A pointer to user data passed to be passed to the |
| 115 * callback function. This is optional and is typically used to help track state |
| 116 * in case of multiple pending callbacks. |
| 117 * |
72 * @return A PP_CompletionCallback structure. | 118 * @return A PP_CompletionCallback structure. |
73 */ | 119 */ |
74 PP_INLINE struct PP_CompletionCallback PP_MakeCompletionCallback( | 120 PP_INLINE struct PP_CompletionCallback PP_MakeCompletionCallback( |
75 PP_CompletionCallback_Func func, | 121 PP_CompletionCallback_Func func, |
76 void* user_data) { | 122 void* user_data) { |
77 struct PP_CompletionCallback cc; | 123 struct PP_CompletionCallback cc; |
78 cc.func = func; | 124 cc.func = func; |
79 cc.user_data = user_data; | 125 cc.user_data = user_data; |
| 126 /* TODO(polina): switch the default to PP_COMPLETIONCALLBACK_FLAG_NONE. */ |
| 127 cc.flags = PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; |
| 128 return cc; |
| 129 } |
| 130 |
| 131 /** |
| 132 * PP_MakeOptionalCompletionCallback() is used to create a PP_CompletionCallback |
| 133 * with PP_COMPLETIONCALLBACK_FLAG_OPTIONAL set. |
| 134 * |
| 135 * @param[in] func A PP_CompletionCallback_Func to be called on completion. |
| 136 * @param[in] user_data A pointer to user data passed to be passed to the |
| 137 * callback function. This is optional and is typically used to help track state |
| 138 * in case of multiple pending callbacks. |
| 139 * |
| 140 * @return A PP_CompletionCallback structure. |
| 141 */ |
| 142 PP_INLINE struct PP_CompletionCallback PP_MakeOptionalCompletionCallback( |
| 143 PP_CompletionCallback_Func func, |
| 144 void* user_data) { |
| 145 struct PP_CompletionCallback cc = PP_MakeCompletionCallback(func, user_data); |
| 146 cc.flags = cc.flags | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; |
80 return cc; | 147 return cc; |
81 } | 148 } |
82 /** | 149 /** |
83 * @} | 150 * @} |
84 */ | 151 */ |
85 | 152 |
86 /** | 153 /** |
87 * @addtogroup Functions | 154 * @addtogroup Functions |
88 * @{ | 155 * @{ |
89 */ | 156 */ |
90 | 157 |
91 /** | 158 /** |
92 * PP_RunCompletionCallback() is used to run a callback. | 159 * PP_RunCompletionCallback() is used to run a callback. It invokes |
| 160 * the callback function passing it user data specified on creation and |
| 161 * completion |result|. |
93 * | 162 * |
94 * @param[in] cc A pointer to a PP_CompletionCallback that will be run. | 163 * @param[in] cc A pointer to a PP_CompletionCallback that will be run. |
95 * @param[in] res The result parameter that, if negative or equal to 0, | 164 * @param[in] result The result of the operation. Non-positive values correspond |
96 * indicate if the call will completely asynchronously (the callback will be | 165 * to the error codes from pp_errors.h (excluding PP_OK_COMPLETIONPENDING). |
97 * called with a status code). A value greater than zero indicates additional | 166 * Positive values indicate additional information such as bytes read. |
98 * information such as bytes read. | |
99 */ | 167 */ |
100 PP_INLINE void PP_RunCompletionCallback(struct PP_CompletionCallback* cc, | 168 PP_INLINE void PP_RunCompletionCallback(struct PP_CompletionCallback* cc, |
101 int32_t res) { | 169 int32_t result) { |
102 cc->func(cc->user_data, res); | 170 cc->func(cc->user_data, result); |
103 } | 171 } |
104 | 172 |
105 /** | 173 /** |
106 * @} | 174 * @} |
107 */ | 175 */ |
108 | 176 |
109 /** | 177 /** |
110 * @addtogroup Functions | 178 * @addtogroup Functions |
111 * @{ | 179 * @{ |
112 */ | 180 */ |
113 | 181 |
114 /** | 182 /** |
115 * PP_BlockUntilComplete() is used in place of an actual completion callback | 183 * PP_BlockUntilComplete() is used in place of an actual completion callback |
116 * to request blocking behavior. If specified, the calling thread will block | 184 * to request blocking behavior. If specified, the calling thread will block |
117 * until the function completes. Blocking completion callbacks are only usable | 185 * until the function completes. Blocking completion callbacks are only allowed |
118 * from background threads. | 186 * from background threads. |
119 * | 187 * |
120 * @return A PP_CompletionCallback structure. | 188 * @return A PP_CompletionCallback structure corresponding to a NULL callback. |
121 */ | 189 */ |
122 PP_INLINE struct PP_CompletionCallback PP_BlockUntilComplete() { | 190 PP_INLINE struct PP_CompletionCallback PP_BlockUntilComplete() { |
123 return PP_MakeCompletionCallback(NULL, NULL); | 191 return PP_MakeCompletionCallback(NULL, NULL); |
124 } | 192 } |
125 | 193 |
126 /** | 194 /** |
127 * Runs a callback and clears the reference to it. | 195 * Runs a callback and clears the reference to it. |
128 * | 196 * |
129 * This is used when the null-ness of a completion callback is used as a signal | 197 * This is used when the null-ness of a completion callback is used as a signal |
130 * for whether a completion callback has been registered. In this case, after | 198 * for whether a completion callback has been registered. In this case, after |
(...skipping 10 matching lines...) Expand all Loading... |
141 int32_t res) { | 209 int32_t res) { |
142 struct PP_CompletionCallback temp = *cc; | 210 struct PP_CompletionCallback temp = *cc; |
143 *cc = PP_BlockUntilComplete(); | 211 *cc = PP_BlockUntilComplete(); |
144 PP_RunCompletionCallback(&temp, res); | 212 PP_RunCompletionCallback(&temp, res); |
145 } | 213 } |
146 /** | 214 /** |
147 * @} | 215 * @} |
148 */ | 216 */ |
149 | 217 |
150 #endif /* PPAPI_C_PP_COMPLETION_CALLBACK_H_ */ | 218 #endif /* PPAPI_C_PP_COMPLETION_CALLBACK_H_ */ |
OLD | NEW |