1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 /** 17 * @addtogroup FFRT 18 * @{ 19 * 20 * @brief Provides FFRT C APIs. 21 * 22 * @since 10 23 */ 24 25 /** 26 * @file type_def.h 27 * 28 * @brief Declares common types. 29 * 30 * @library libffrt.z.so 31 * @kit FunctionFlowRuntimeKit 32 * @syscap SystemCapability.Resourceschedule.Ffrt.Core 33 * @since 10 34 * @version 1.0 35 */ 36 37 #ifndef FFRT_API_C_TYPE_DEF_H 38 #define FFRT_API_C_TYPE_DEF_H 39 40 #include <stdint.h> 41 #include <errno.h> 42 43 #ifdef __cplusplus 44 #define FFRT_C_API extern "C" 45 #else 46 #define FFRT_C_API 47 #endif 48 49 /** 50 * @brief Enumerates the task priority types. 51 * 52 * @since 12 53 */ 54 typedef enum { 55 /** Should be distributed at once if possible, handle time equals to send time, prior to high level. */ 56 ffrt_queue_priority_immediate = 0, 57 /** High priority, sorted by handle time, prior to low level. */ 58 ffrt_queue_priority_high, 59 /** Low priority, sorted by handle time, prior to idle level. */ 60 ffrt_queue_priority_low, 61 /** Lowest priority, sorted by handle time, only distribute when there is no other level inside queue. */ 62 ffrt_queue_priority_idle, 63 } ffrt_queue_priority_t; 64 65 /** 66 * @brief Enumerates the task QoS types. 67 * 68 * @since 10 69 */ 70 typedef enum { 71 /** Inheritance. */ 72 ffrt_qos_inherit = -1, 73 /** Background task. */ 74 ffrt_qos_background, 75 /** Real-time tool. */ 76 ffrt_qos_utility, 77 /** Default type. */ 78 ffrt_qos_default, 79 /** User initiated. */ 80 ffrt_qos_user_initiated, 81 } ffrt_qos_default_t; 82 83 /** 84 * @brief Defines the QoS type. 85 * 86 * @since 10 87 */ 88 typedef int ffrt_qos_t; 89 90 /** 91 * @brief Defines the task function pointer type. 92 * 93 * @since 10 94 */ 95 typedef void(*ffrt_function_t)(void*); 96 97 /** 98 * @brief Defines a task executor. 99 * 100 * @since 10 101 */ 102 typedef struct { 103 /** Function used to execute a task. */ 104 ffrt_function_t exec; 105 /** Function used to destroy a task. */ 106 ffrt_function_t destroy; 107 /** Need to be set to 0. */ 108 uint64_t reserve[2]; 109 } ffrt_function_header_t; 110 111 /** 112 * @brief Defines the storage size of multiple types of structs. 113 * 114 * @since 10 115 */ 116 typedef enum { 117 /** Task attribute storage size. */ 118 ffrt_task_attr_storage_size = 128, 119 /** Task executor storage size. */ 120 ffrt_auto_managed_function_storage_size = 64 + sizeof(ffrt_function_header_t), 121 /** Mutex storage size. */ 122 ffrt_mutex_storage_size = 64, 123 /** Condition variable storage size. */ 124 ffrt_cond_storage_size = 64, 125 /** Queue storage size. */ 126 ffrt_queue_attr_storage_size = 128, 127 } ffrt_storage_size_t; 128 129 /** 130 * @brief Enumerates the task types. 131 * 132 * @since 10 133 */ 134 typedef enum { 135 /** General task. */ 136 ffrt_function_kind_general, 137 /** Queue task. */ 138 ffrt_function_kind_queue, 139 } ffrt_function_kind_t; 140 141 /** 142 * @brief Enumerates the dependency types. 143 * 144 * @since 10 145 */ 146 typedef enum { 147 /** Data dependency type. */ 148 ffrt_dependence_data, 149 /** Task dependency type. */ 150 ffrt_dependence_task, 151 } ffrt_dependence_type_t; 152 153 /** 154 * @brief Defines the dependency data structure. 155 * 156 * @since 10 157 */ 158 typedef struct { 159 /** Dependency type. */ 160 ffrt_dependence_type_t type; 161 /** Dependency pointer. */ 162 const void* ptr; 163 } ffrt_dependence_t; 164 165 /** 166 * @brief Defines the dependency structure. 167 * 168 * @since 10 169 */ 170 typedef struct { 171 /** Number of dependencies. */ 172 uint32_t len; 173 /** Dependency data. */ 174 const ffrt_dependence_t* items; 175 } ffrt_deps_t; 176 177 /** 178 * @brief Defines the task attribute structure. 179 * 180 * @since 10 181 */ 182 typedef struct { 183 /** An array of uint32_t used to store the task attribute. */ 184 uint32_t storage[(ffrt_task_attr_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; 185 } ffrt_task_attr_t; 186 187 /** 188 * @brief Defines the queue attribute structure. 189 * 190 * @since 10 191 */ 192 typedef struct { 193 /** An array of uint32_t used to store the queue attribute. */ 194 uint32_t storage[(ffrt_queue_attr_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; 195 } ffrt_queue_attr_t; 196 197 /** 198 * @brief Defines the task handle, which identifies different tasks. 199 * 200 * @since 10 201 */ 202 typedef void* ffrt_task_handle_t; 203 204 /** 205 * @brief Enumerates the ffrt error codes. 206 * 207 * @since 10 208 */ 209 typedef enum { 210 /** A generic error. */ 211 ffrt_error = -1, 212 /** Success. */ 213 ffrt_success = 0, 214 /** An out of memory error. */ 215 ffrt_error_nomem = ENOMEM, 216 /** A timeout error. */ 217 ffrt_error_timedout = ETIMEDOUT, 218 /** A busy error. */ 219 ffrt_error_busy = EBUSY, 220 /** A invalid value error. */ 221 ffrt_error_inval = EINVAL 222 } ffrt_error_t; 223 224 /** 225 * @brief Defines the condition variable attribute structure. 226 * 227 * @since 10 228 */ 229 typedef struct { 230 /** A long integer used to store the condition variable attribute. */ 231 long storage; 232 } ffrt_condattr_t; 233 234 /** 235 * @brief Defines the mutex attribute structure. 236 * 237 * @since 10 238 */ 239 typedef struct { 240 /** A long integer used to store the mutex attribute. */ 241 long storage; 242 } ffrt_mutexattr_t; 243 244 /** 245 * @brief Enumerates the mutex types. 246 * 247 * Describes the mutex type, ffrt_mutex_normal is normal mutex; 248 * ffrt_mutex_recursive is recursive mutex, ffrt_mutex_default is normal mutex. 249 * 250 * @since 12 251 */ 252 typedef enum { 253 /** Normal mutex type. */ 254 ffrt_mutex_normal = 0, 255 /** Recursive mutex type. */ 256 ffrt_mutex_recursive = 2, 257 /** Default mutex type. */ 258 ffrt_mutex_default = ffrt_mutex_normal 259 } ffrt_mutex_type; 260 261 /** 262 * @brief Defines the mutex structure. 263 * 264 * @since 10 265 */ 266 typedef struct { 267 /** An array of uint32_t used to store the mutex. */ 268 uint32_t storage[(ffrt_mutex_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; 269 } ffrt_mutex_t; 270 271 /** 272 * @brief Defines the condition variable structure. 273 * 274 * @since 10 275 */ 276 typedef struct { 277 /** An array of uint32_t used to store the condition variable. */ 278 uint32_t storage[(ffrt_cond_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; 279 } ffrt_cond_t; 280 281 /** 282 * @brief Defines the poller callback function type. 283 * 284 * @since 12 285 */ 286 typedef void (*ffrt_poller_cb)(void* data, uint32_t event); 287 288 /** 289 * @brief Defines the timer callback function type. 290 * 291 * @since 12 292 */ 293 typedef void (*ffrt_timer_cb)(void* data); 294 295 /** 296 * @brief Defines the timer handler. 297 * 298 * @since 12 299 */ 300 typedef int ffrt_timer_t; 301 302 #ifdef __cplusplus 303 namespace ffrt { 304 305 /** 306 * @brief Enumerates the task QoS types. 307 * 308 * @since 10 309 */ 310 enum qos_default { 311 /** Inheritance. */ 312 qos_inherit = ffrt_qos_inherit, 313 /** Background task. */ 314 qos_background = ffrt_qos_background, 315 /** Real-time tool. */ 316 qos_utility = ffrt_qos_utility, 317 /** Default type. */ 318 qos_default = ffrt_qos_default, 319 /** User initiated. */ 320 qos_user_initiated = ffrt_qos_user_initiated, 321 }; 322 323 /** 324 * @brief Defines the QoS type. 325 * 326 * @since 10 327 */ 328 using qos = int; 329 330 } 331 332 #endif // __cplusplus 333 #endif // FFRT_API_C_TYPE_DEF_H 334 /** @} */