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 */ 35 36 #ifndef FFRT_API_C_TYPE_DEF_H 37 #define FFRT_API_C_TYPE_DEF_H 38 39 #include <stdint.h> 40 #include <errno.h> 41 42 #ifdef __cplusplus 43 #define FFRT_C_API extern "C" 44 #else 45 #define FFRT_C_API 46 #endif 47 48 /** 49 * @brief Enumerates the task priority types. 50 * 51 * @since 12 52 */ 53 typedef enum { 54 /** Should be distributed at once if possible, handle time equals to send time, prior to high level. */ 55 ffrt_queue_priority_immediate = 0, 56 /** High priority, sorted by handle time, prior to low level. */ 57 ffrt_queue_priority_high, 58 /** Low priority, sorted by handle time, prior to idle level. */ 59 ffrt_queue_priority_low, 60 /** Lowest priority, sorted by handle time, only distribute when there is no other level inside queue. */ 61 ffrt_queue_priority_idle, 62 } ffrt_queue_priority_t; 63 64 /** 65 * @brief Enumerates the task QoS types. 66 * 67 * @since 10 68 */ 69 typedef enum { 70 /** Inheritance. */ 71 ffrt_qos_inherit = -1, 72 /** Background task. */ 73 ffrt_qos_background, 74 /** Real-time tool. */ 75 ffrt_qos_utility, 76 /** Default type. */ 77 ffrt_qos_default, 78 /** User initiated. */ 79 ffrt_qos_user_initiated, 80 } ffrt_qos_default_t; 81 82 /** 83 * @brief Defines the QoS type. 84 * 85 * @since 10 86 */ 87 typedef int ffrt_qos_t; 88 89 /** 90 * @brief Defines the task function pointer type. 91 * 92 * @since 10 93 */ 94 typedef void(*ffrt_function_t)(void*); 95 96 /** 97 * @brief Defines a task executor. 98 * 99 * @since 10 100 */ 101 typedef struct { 102 /** Function used to execute a task. */ 103 ffrt_function_t exec; 104 /** Function used to destroy a task. */ 105 ffrt_function_t destroy; 106 /** Need to be set to 0. */ 107 uint64_t reserve[2]; 108 } ffrt_function_header_t; 109 110 /** 111 * @brief Defines the storage size of multiple types of structs. 112 * 113 * @since 10 114 */ 115 typedef enum { 116 /** Task attribute storage size. */ 117 ffrt_task_attr_storage_size = 128, 118 /** Task executor storage size. */ 119 ffrt_auto_managed_function_storage_size = 64 + sizeof(ffrt_function_header_t), 120 /** Mutex storage size. */ 121 ffrt_mutex_storage_size = 64, 122 /** Condition variable storage size. */ 123 ffrt_cond_storage_size = 64, 124 /** Queue storage size. */ 125 ffrt_queue_attr_storage_size = 128, 126 /** Rwlock storage size. 127 * 128 * @since 18 129 */ 130 ffrt_rwlock_storage_size = 64, 131 } ffrt_storage_size_t; 132 133 /** 134 * @brief Enumerates the task types. 135 * 136 * @since 10 137 */ 138 typedef enum { 139 /** General task. */ 140 ffrt_function_kind_general, 141 /** Queue task. */ 142 ffrt_function_kind_queue, 143 } ffrt_function_kind_t; 144 145 /** 146 * @brief Enumerates the dependency types. 147 * 148 * @since 10 149 */ 150 typedef enum { 151 /** Data dependency type. */ 152 ffrt_dependence_data, 153 /** Task dependency type. */ 154 ffrt_dependence_task, 155 } ffrt_dependence_type_t; 156 157 /** 158 * @brief Defines the dependency data structure. 159 * 160 * @since 10 161 */ 162 typedef struct { 163 /** Dependency type. */ 164 ffrt_dependence_type_t type; 165 /** Dependency pointer. */ 166 const void* ptr; 167 } ffrt_dependence_t; 168 169 /** 170 * @brief Defines the dependency structure. 171 * 172 * @since 10 173 */ 174 typedef struct { 175 /** Number of dependencies. */ 176 uint32_t len; 177 /** Dependency data. */ 178 const ffrt_dependence_t* items; 179 } ffrt_deps_t; 180 181 /** 182 * @brief Defines the task attribute structure. 183 * 184 * @since 10 185 */ 186 typedef struct { 187 /** An array of uint32_t used to store the task attribute. */ 188 uint32_t storage[(ffrt_task_attr_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; 189 } ffrt_task_attr_t; 190 191 /** 192 * @brief Defines the queue attribute structure. 193 * 194 * @since 10 195 */ 196 typedef struct { 197 /** An array of uint32_t used to store the queue attribute. */ 198 uint32_t storage[(ffrt_queue_attr_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; 199 } ffrt_queue_attr_t; 200 201 /** 202 * @brief Defines the task handle, which identifies different tasks. 203 * 204 * @since 10 205 */ 206 typedef void* ffrt_task_handle_t; 207 208 /** 209 * @brief Enumerates the ffrt error codes. 210 * 211 * @since 10 212 */ 213 typedef enum { 214 /** A generic error. */ 215 ffrt_error = -1, 216 /** Success. */ 217 ffrt_success = 0, 218 /** An out of memory error. */ 219 ffrt_error_nomem = ENOMEM, 220 /** A timeout error. */ 221 ffrt_error_timedout = ETIMEDOUT, 222 /** A busy error. */ 223 ffrt_error_busy = EBUSY, 224 /** A invalid value error. */ 225 ffrt_error_inval = EINVAL 226 } ffrt_error_t; 227 228 /** 229 * @brief Defines the condition variable attribute structure. 230 * 231 * @since 10 232 */ 233 typedef struct { 234 /** A long integer used to store the condition variable attribute. */ 235 long storage; 236 } ffrt_condattr_t; 237 238 /** 239 * @brief Defines the mutex attribute structure. 240 * 241 * @since 10 242 */ 243 typedef struct { 244 /** A long integer used to store the mutex attribute. */ 245 long storage; 246 } ffrt_mutexattr_t; 247 248 /** 249 * @brief Defines the rwlock attribute structure. 250 * 251 * @since 18 252 */ 253 typedef struct { 254 /** A long integer used to store the rwlock attribute. */ 255 long storage; 256 } ffrt_rwlockattr_t; 257 258 /** 259 * @brief Enumerates the mutex types. 260 * 261 * Describes the mutex type, ffrt_mutex_normal is normal mutex; 262 * ffrt_mutex_recursive is recursive mutex, ffrt_mutex_default is normal mutex. 263 * 264 * @since 12 265 */ 266 typedef enum { 267 /** Normal mutex type. */ 268 ffrt_mutex_normal = 0, 269 /** Recursive mutex type. */ 270 ffrt_mutex_recursive = 2, 271 /** Default mutex type. */ 272 ffrt_mutex_default = ffrt_mutex_normal 273 } ffrt_mutex_type; 274 275 /** 276 * @brief Defines the mutex structure. 277 * 278 * @since 10 279 */ 280 typedef struct { 281 /** An array of uint32_t used to store the mutex. */ 282 uint32_t storage[(ffrt_mutex_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; 283 } ffrt_mutex_t; 284 285 /** 286 * @brief Defines the rwlock structure. 287 * 288 * @since 18 289 */ 290 typedef struct { 291 /** An array of uint32_t used to store the rwlock. */ 292 uint32_t storage[(ffrt_rwlock_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; 293 } ffrt_rwlock_t; 294 295 /** 296 * @brief Defines the condition variable structure. 297 * 298 * @since 10 299 */ 300 typedef struct { 301 /** An array of uint32_t used to store the condition variable. */ 302 uint32_t storage[(ffrt_cond_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; 303 } ffrt_cond_t; 304 305 /** 306 * @brief Defines the poller callback function type. 307 * 308 * @since 12 309 */ 310 typedef void (*ffrt_poller_cb)(void* data, uint32_t event); 311 312 /** 313 * @brief Defines the timer callback function type. 314 * 315 * @since 12 316 */ 317 typedef void (*ffrt_timer_cb)(void* data); 318 319 /** 320 * @brief Defines the timer handler. 321 * 322 * @since 12 323 */ 324 typedef int ffrt_timer_t; 325 326 #ifdef __cplusplus 327 namespace ffrt { 328 329 /** 330 * @brief Enumerates the task QoS types. 331 * 332 * @since 10 333 */ 334 enum qos_default { 335 /** Inheritance. */ 336 qos_inherit = ffrt_qos_inherit, 337 /** Background task. */ 338 qos_background = ffrt_qos_background, 339 /** Real-time tool. */ 340 qos_utility = ffrt_qos_utility, 341 /** Default type. */ 342 qos_default = ffrt_qos_default, 343 /** User initiated. */ 344 qos_user_initiated = ffrt_qos_user_initiated, 345 }; 346 347 /** 348 * @brief Defines the QoS type. 349 * 350 * @since 10 351 */ 352 using qos = int; 353 354 } 355 356 #endif // __cplusplus 357 #endif // FFRT_API_C_TYPE_DEF_H 358 /** @} */