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 /* Any method that takes a PP_CompletionCallback can complete asynchronously. |
brettw
2011/06/21 22:56:41
You lost the ** here which I assume is needed for
polina
2011/06/21 23:40:01
Done.
| |
38 * Any method that takes a PP_CompletionCallback has the option of completing | 83 * Refer to PP_CompletionCallback_Flag for more information. |
39 * asynchronously if the operation would block. Such a method should return | |
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 * | 84 * |
47 * The result parameter passes an int32_t that, if negative or equal to 0, | 85 * If PP_CompletionCallback_Func is NULL, the operation might block if necessary |
48 * indicate if the call will completely asynchronously (the callback will be | 86 * to complete the work. Refer to PP_BlockUntilComplete for more information. |
49 * called with a status code). A value greater than zero indicates additional | 87 * |
50 * information such as bytes read. | 88 * See PP_MakeCompletionCallback() for the description of each field. |
51 */ | 89 */ |
52 struct PP_CompletionCallback { | 90 struct PP_CompletionCallback { |
53 PP_CompletionCallback_Func func; | 91 PP_CompletionCallback_Func func; |
54 void* user_data; | 92 void* user_data; |
93 int32_t flags; | |
55 }; | 94 }; |
56 /** | 95 /** |
57 * @} | 96 * @} |
58 */ | 97 */ |
59 | 98 |
60 /** | 99 /** |
61 * @addtogroup Functions | 100 * @addtogroup Functions |
62 * @{ | 101 * @{ |
63 */ | 102 */ |
64 | |
65 /** | 103 /** |
66 * PP_MakeCompletionCallback() is used to create a PP_CompletionCallback. | 104 * PP_MakeCompletionCallback() is used to create a PP_CompletionCallback |
105 * without flags. If you want to alter the default callback behavior, set the | |
106 * flags to a bit field combination of PP_CompletionCallback_Flag's. | |
67 * | 107 * |
68 * @param[in] func A PP_CompletionCallback_Func that will be called. | 108 * Example: |
69 * @param[in] user_data A pointer to user data passed to your callback | 109 * struct PP_CompletionCallback cc = PP_MakeCompletionCallback(Foo, NULL); |
70 * function. This is optional and is typically used to help track state | 110 * cc.flags = cc.flags | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; |
71 * when you may have multiple callbacks pending. | 111 * |
112 * @param[in] func A PP_CompletionCallback_Func to be called on completion. | |
113 * @param[in] user_data A pointer to user data passed to be passed to the | |
114 * callback function. This is optional and is typically used to help track state | |
115 * in case of multiple pending callbacks. | |
116 * | |
72 * @return A PP_CompletionCallback structure. | 117 * @return A PP_CompletionCallback structure. |
73 */ | 118 */ |
74 PP_INLINE struct PP_CompletionCallback PP_MakeCompletionCallback( | 119 PP_INLINE struct PP_CompletionCallback PP_MakeCompletionCallback( |
75 PP_CompletionCallback_Func func, | 120 PP_CompletionCallback_Func func, |
76 void* user_data) { | 121 void* user_data) { |
77 struct PP_CompletionCallback cc; | 122 struct PP_CompletionCallback cc; |
78 cc.func = func; | 123 cc.func = func; |
79 cc.user_data = user_data; | 124 cc.user_data = user_data; |
125 /* TODO(polina): switch the default to PP_COMPLETIONCALLBACK_FLAG_NONE. */ | |
126 cc.flags = PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; | |
127 return cc; | |
128 } | |
129 | |
130 /** | |
131 * PP_MakeOptionalCompletionCallback() is used to create a PP_CompletionCallback | |
132 * with PP_COMPLETIONCALLBACK_FLAG_OPTIONAL set. | |
133 * | |
134 * @param[in] func A PP_CompletionCallback_Func to be called on completion. | |
135 * @param[in] user_data A pointer to user data passed to be passed to the | |
136 * callback function. This is optional and is typically used to help track state | |
137 * in case of multiple pending callbacks. | |
138 * | |
139 * @return A PP_CompletionCallback structure. | |
140 */ | |
141 PP_INLINE struct PP_CompletionCallback PP_MakeOptionalCompletionCallback( | |
142 PP_CompletionCallback_Func func, | |
143 void* user_data) { | |
144 struct PP_CompletionCallback cc = PP_MakeCompletionCallback(func, user_data); | |
145 cc.flags = cc.flags | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; | |
80 return cc; | 146 return cc; |
81 } | 147 } |
82 /** | 148 /** |
83 * @} | 149 * @} |
84 */ | 150 */ |
85 | 151 |
86 /** | 152 /** |
87 * @addtogroup Functions | 153 * @addtogroup Functions |
88 * @{ | 154 * @{ |
89 */ | 155 */ |
90 | 156 |
91 /** | 157 /** |
92 * PP_RunCompletionCallback() is used to run a callback. | 158 * PP_RunCompletionCallback() is used to run a callback. It invokes |
159 * the callback function passing it user data specified on creation and | |
160 * completion |result|. | |
93 * | 161 * |
94 * @param[in] cc A pointer to a PP_CompletionCallback that will be run. | 162 * @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, | 163 * @param[in] result The result of the operation. Non-positive values correspond |
96 * indicate if the call will completely asynchronously (the callback will be | 164 * 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 | 165 * Positive values indicate additional information such as bytes read. |
98 * information such as bytes read. | |
99 */ | 166 */ |
100 PP_INLINE void PP_RunCompletionCallback(struct PP_CompletionCallback* cc, | 167 PP_INLINE void PP_RunCompletionCallback(struct PP_CompletionCallback* cc, |
101 int32_t res) { | 168 int32_t result) { |
102 cc->func(cc->user_data, res); | 169 cc->func(cc->user_data, result); |
103 } | 170 } |
104 | 171 |
105 /** | 172 /** |
106 * @} | 173 * @} |
107 */ | 174 */ |
108 | 175 |
109 /** | 176 /** |
110 * @addtogroup Functions | 177 * @addtogroup Functions |
111 * @{ | 178 * @{ |
112 */ | 179 */ |
113 | 180 |
114 /** | 181 /** |
115 * PP_BlockUntilComplete() is used in place of an actual completion callback | 182 * PP_BlockUntilComplete() is used in place of an actual completion callback |
116 * to request blocking behavior. If specified, the calling thread will block | 183 * to request blocking behavior. If specified, the calling thread will block |
117 * until the function completes. Blocking completion callbacks are only usable | 184 * until the function completes. Blocking completion callbacks are only allowed |
118 * from background threads. | 185 * from background threads. |
119 * | 186 * |
120 * @return A PP_CompletionCallback structure. | 187 * @return A PP_CompletionCallback structure corresponding to a NULL callback. |
121 */ | 188 */ |
122 PP_INLINE struct PP_CompletionCallback PP_BlockUntilComplete() { | 189 PP_INLINE struct PP_CompletionCallback PP_BlockUntilComplete() { |
123 return PP_MakeCompletionCallback(NULL, NULL); | 190 return PP_MakeCompletionCallback(NULL, NULL); |
124 } | 191 } |
125 | 192 |
126 /** | 193 /** |
127 * Runs a callback and clears the reference to it. | 194 * Runs a callback and clears the reference to it. |
128 * | 195 * |
129 * This is used when the null-ness of a completion callback is used as a signal | 196 * 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 | 197 * for whether a completion callback has been registered. In this case, after |
(...skipping 10 matching lines...) Expand all Loading... | |
141 int32_t res) { | 208 int32_t res) { |
142 struct PP_CompletionCallback temp = *cc; | 209 struct PP_CompletionCallback temp = *cc; |
143 *cc = PP_BlockUntilComplete(); | 210 *cc = PP_BlockUntilComplete(); |
144 PP_RunCompletionCallback(&temp, res); | 211 PP_RunCompletionCallback(&temp, res); |
145 } | 212 } |
146 /** | 213 /** |
147 * @} | 214 * @} |
148 */ | 215 */ |
149 | 216 |
150 #endif /* PPAPI_C_PP_COMPLETION_CALLBACK_H_ */ | 217 #endif /* PPAPI_C_PP_COMPLETION_CALLBACK_H_ */ |
OLD | NEW |