Chromium Code Reviews| 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 * By default, any non-NULL callback will always be 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 PP_COMPLETIONCALLBACK_FLAG_NONE = 0 << 0, | |
| 59 /** | |
| 60 * This flag allows any method taking such callback to complete synchronously | |
| 61 * if the operation would not block. This is useful when performance is an | |
| 62 * issue, and the operation bandwidth should not be limited to the processing | |
| 63 * speed of the message loop. | |
| 64 * | |
| 65 * On synchronous method completion, the callback will not be invoked, and the | |
| 66 * completion result will be returned by the method itself. Otherwise, | |
| 67 * the method will return PP_OK_COMPLETIONPENDING, and the callback will be | |
| 68 * invoked asynchronously on the main thread of PPAPI execution. | |
| 69 */ | |
| 70 PP_COMPLETIONCALLBACK_FLAG_NOFORCEASYNC = 1 << 0 // or SYNCOK? | |
|
darin (slow to review)
2011/04/25 18:31:06
it looks like you are inverting the sense of this
polina
2011/04/25 19:44:44
Yes, I have. As I was writing the comments, I real
brettw
2011/04/25 19:55:46
I wasn't at the meeting and haven't done any think
| |
| 71 } PP_CompletionCallback_Flag; | |
| 72 PP_COMPILE_ASSERT_SIZE_IN_BYTES(PP_CompletionCallback_Flag, 4); | |
| 73 | |
| 74 | |
| 75 /** | |
| 33 * @addtogroup Structs | 76 * @addtogroup Structs |
| 34 * @{ | 77 * @{ |
| 35 */ | 78 */ |
| 36 | 79 |
| 37 /** | 80 /* Any method that takes a PP_CompletionCallback can complete asynchronously. |
| 38 * Any method that takes a PP_CompletionCallback has the option of completing | 81 * 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 * | 82 * |
| 47 * The result parameter passes an int32_t that, if negative or equal to 0, | 83 * If PP_CompletionCallback_Func is NULL, the operation might block if necessary |
| 48 * indicate if the call will completely asynchronously (the callback will be | 84 * to complete the work. Refer to PP_BlockUntilComplete for more information. |
| 49 * called with a status code). A value greater than zero indicates additional | 85 * |
| 50 * information such as bytes read. | 86 * See PP_MakeCompletionCallback() for the description of each field. |
| 51 */ | 87 */ |
| 52 struct PP_CompletionCallback { | 88 struct PP_CompletionCallback { |
| 53 PP_CompletionCallback_Func func; | 89 PP_CompletionCallback_Func func; |
| 54 void* user_data; | 90 void* user_data; |
| 91 int32_t flags; | |
| 55 }; | 92 }; |
| 56 /** | 93 /** |
| 57 * @} | 94 * @} |
| 58 */ | 95 */ |
| 59 | 96 |
| 60 /** | 97 /** |
| 61 * @addtogroup Functions | 98 * @addtogroup Functions |
| 62 * @{ | 99 * @{ |
| 63 */ | 100 */ |
| 64 | |
| 65 /** | 101 /** |
| 66 * PP_MakeCompletionCallback() is used to create a PP_CompletionCallback. | 102 * PP_MakeCompletionCallback() is used to create a PP_CompletionCallback |
| 103 * without flags. If you want to alter the default callback behavior, set the | |
| 104 * flags to a bit field combination of PP_CompletionCallback_Flag's. | |
| 67 * | 105 * |
| 68 * @param[in] func A PP_CompletionCallback_Func that will be called. | 106 * Example: |
| 69 * @param[in] user_data A pointer to user data passed to your callback | 107 * struct PP_CompletionCallback cc = PP_MakeCompletionCallback(Foo, NULL); |
| 70 * function. This is optional and is typically used to help track state | 108 * cc.flags = cc.flags | PP_COMPLETIONCALLBACK_FLAG_NOFORCEASYNC; |
| 71 * when you may have multiple callbacks pending. | 109 * |
| 110 * @param[in] func A PP_CompletionCallback_Func to be called on completion. | |
| 111 * @param[in] user_data A pointer to user data passed to be passed to the | |
| 112 * callback function. This is optional and is typically used to help track state | |
| 113 * in case of multiple pending callbacks. | |
| 114 * | |
| 72 * @return A PP_CompletionCallback structure. | 115 * @return A PP_CompletionCallback structure. |
| 73 */ | 116 */ |
| 74 PP_INLINE struct PP_CompletionCallback PP_MakeCompletionCallback( | 117 PP_INLINE struct PP_CompletionCallback PP_MakeCompletionCallback( |
| 75 PP_CompletionCallback_Func func, | 118 PP_CompletionCallback_Func func, |
| 76 void* user_data) { | 119 void* user_data) { |
| 77 struct PP_CompletionCallback cc; | 120 struct PP_CompletionCallback cc; |
| 78 cc.func = func; | 121 cc.func = func; |
| 79 cc.user_data = user_data; | 122 cc.user_data = user_data; |
| 123 cc.flags = PP_COMPLETIONCALLBACK_FLAG_NONE; | |
| 80 return cc; | 124 return cc; |
| 81 } | 125 } |
| 82 /** | 126 /** |
| 83 * @} | 127 * @} |
| 84 */ | 128 */ |
| 85 | 129 |
| 86 /** | 130 /** |
| 87 * @addtogroup Functions | 131 * @addtogroup Functions |
| 88 * @{ | 132 * @{ |
| 89 */ | 133 */ |
| 90 | 134 |
| 91 /** | 135 /** |
| 92 * PP_RunCompletionCallback() is used to run a callback. | 136 * PP_RunCompletionCallback() is used to run a callback. It invokes |
| 137 * the callback function passing it user data specified on creation and | |
| 138 * completion |result|. | |
| 93 * | 139 * |
| 94 * @param[in] cc A pointer to a PP_CompletionCallback that will be run. | 140 * @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, | 141 * @param[in] result The result of the operation. Non-positive values correspond |
| 96 * indicate if the call will completely asynchronously (the callback will be | 142 * 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 | 143 * Positive values indicate additional information such as bytes read. |
| 98 * information such as bytes read. | |
| 99 */ | 144 */ |
| 100 PP_INLINE void PP_RunCompletionCallback(struct PP_CompletionCallback* cc, | 145 PP_INLINE void PP_RunCompletionCallback(struct PP_CompletionCallback* cc, |
| 101 int32_t res) { | 146 int32_t result) { |
| 102 cc->func(cc->user_data, res); | 147 cc->func(cc->user_data, result); |
| 103 } | 148 } |
| 104 /** | 149 /** |
| 105 * @} | 150 * @} |
| 106 */ | 151 */ |
| 107 | 152 |
| 108 /** | 153 /** |
| 109 * @addtogroup Functions | 154 * @addtogroup Functions |
| 110 * @{ | 155 * @{ |
| 111 */ | 156 */ |
| 112 | 157 |
| 113 /** | 158 /** |
| 114 * PP_BlockUntilComplete() is used in place of an actual completion callback | 159 * PP_BlockUntilComplete() is used in place of an actual completion callback |
| 115 * to request blocking behavior. If specified, the calling thread will block | 160 * to request blocking behavior. If specified, the calling thread will block |
| 116 * until the function completes. Blocking completion callbacks are only usable | 161 * until the function completes. Blocking completion callbacks are only allowed |
| 117 * from background threads. | 162 * from background threads. |
| 118 * | 163 * |
| 119 * @return A PP_CompletionCallback structure. | 164 * @return A PP_CompletionCallback structure corresponding to a NULL callback. |
| 120 */ | 165 */ |
| 121 PP_INLINE struct PP_CompletionCallback PP_BlockUntilComplete() { | 166 PP_INLINE struct PP_CompletionCallback PP_BlockUntilComplete() { |
| 122 return PP_MakeCompletionCallback(NULL, NULL); | 167 return PP_MakeCompletionCallback(NULL, NULL); |
| 123 } | 168 } |
| 124 /** | 169 /** |
| 125 * @} | 170 * @} |
| 126 */ | 171 */ |
| 127 | 172 |
| 128 #endif /* PPAPI_C_PP_COMPLETION_CALLBACK_H_ */ | 173 #endif /* PPAPI_C_PP_COMPLETION_CALLBACK_H_ */ |
| OLD | NEW |