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 ffrt provides APIs. 21 * 22 * 23 * @syscap SystemCapability.Resourceschedule.Ffrt.Core 24 * 25 * @since 10 26 */ 27 28 /** 29 * @file type_def.h 30 * @kit FunctionFlowRuntimeKit 31 * 32 * @brief Declares common types. 33 * 34 * @syscap SystemCapability.Resourceschedule.Ffrt.Core 35 * @since 10 36 * @version 1.0 37 */ 38 #ifndef FFRT_API_C_TYPE_DEF_H 39 #define FFRT_API_C_TYPE_DEF_H 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 */ 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 typedef int ffrt_qos_t; 82 83 typedef void(*ffrt_function_t)(void*); 84 85 /** 86 * @brief Defines a task executor. 87 * 88 */ 89 typedef struct { 90 /** Function used to execute a task. */ 91 ffrt_function_t exec; 92 /** Function used to destroy a task. */ 93 ffrt_function_t destroy; 94 /** Need to be set to 0. */ 95 uint64_t reserve[2]; 96 } ffrt_function_header_t; 97 98 /** 99 * @brief Defines the storage size of multiple types of structs. 100 * 101 */ 102 typedef enum { 103 /** Task attribute storage size. */ 104 ffrt_task_attr_storage_size = 128, 105 /** Task executor storage size. */ 106 ffrt_auto_managed_function_storage_size = 64 + sizeof(ffrt_function_header_t), 107 /* Mutex storage size. */ 108 ffrt_mutex_storage_size = 64, 109 /** Condition variable storage size. */ 110 ffrt_cond_storage_size = 64, 111 /** Queue storage size. */ 112 ffrt_queue_attr_storage_size = 128, 113 } ffrt_storage_size_t; 114 115 /** 116 * @brief Enumerates the task types. 117 * 118 */ 119 typedef enum { 120 /** General task. */ 121 ffrt_function_kind_general, 122 /** Queue task. */ 123 ffrt_function_kind_queue, 124 } ffrt_function_kind_t; 125 126 /** 127 * @brief dependency type. 128 * 129 */ 130 typedef enum { 131 /** Data dependency type. */ 132 ffrt_dependence_data, 133 /** Task dependency type. */ 134 ffrt_dependence_task, 135 } ffrt_dependence_type_t; 136 137 /** 138 * @brief dependency data structure. 139 * 140 */ 141 typedef struct { 142 /** Dependency type. */ 143 ffrt_dependence_type_t type; 144 /** Dependency pointer. */ 145 const void* ptr; 146 } ffrt_dependence_t; 147 148 /** 149 * @brief Defines the dependency struct. 150 * 151 */ 152 typedef struct { 153 /** Number of dependencies. */ 154 uint32_t len; 155 /** Dependent data. */ 156 const ffrt_dependence_t* items; 157 } ffrt_deps_t; 158 159 typedef struct { 160 uint32_t storage[(ffrt_task_attr_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; 161 } ffrt_task_attr_t; 162 163 typedef struct { 164 uint32_t storage[(ffrt_queue_attr_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; 165 } ffrt_queue_attr_t; 166 167 typedef void* ffrt_task_handle_t; 168 169 typedef enum { 170 ffrt_error = -1, 171 ffrt_success = 0, 172 ffrt_error_nomem = ENOMEM, 173 ffrt_error_timedout = ETIMEDOUT, 174 ffrt_error_busy = EBUSY, 175 ffrt_error_inval = EINVAL 176 } ffrt_error_t; 177 178 typedef struct { 179 long storage; 180 } ffrt_condattr_t; 181 182 typedef struct { 183 long storage; 184 } ffrt_mutexattr_t; 185 186 /** 187 * @brief ffrt mutex type enum 188 * 189 * Describes the mutex type, ffrt_mutex_normal is normal mutex; 190 * ffrt_mutex_recursive is recursive mutex, ffrt_mutex_default is normal mutex. 191 * 192 * @since 12 193 */ 194 typedef enum { 195 /** ffrt normal mutex type */ 196 ffrt_mutex_normal = 0, 197 /** ffrt recursive mutex type */ 198 ffrt_mutex_recursive = 2, 199 /** ffrt default mutex type */ 200 ffrt_mutex_default = ffrt_mutex_normal 201 } ffrt_mutex_type; 202 203 typedef struct { 204 uint32_t storage[(ffrt_mutex_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; 205 } ffrt_mutex_t; 206 207 typedef struct { 208 uint32_t storage[(ffrt_cond_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; 209 } ffrt_cond_t; 210 211 /** 212 * @brief poller callback. 213 * 214 * @since 12 215 */ 216 typedef void (*ffrt_poller_cb)(void* data, uint32_t event); 217 218 /** 219 * @brief timer callback. 220 * 221 * @since 12 222 */ 223 typedef void (*ffrt_timer_cb)(void* data); 224 225 /** 226 * @brief timer handler. 227 * 228 * @since 12 229 */ 230 typedef int ffrt_timer_t; 231 232 233 #ifdef __cplusplus 234 namespace ffrt { 235 enum qos_default { 236 qos_inherit = ffrt_qos_inherit, 237 qos_background = ffrt_qos_background, 238 qos_utility = ffrt_qos_utility, 239 qos_default = ffrt_qos_default, 240 qos_user_initiated = ffrt_qos_user_initiated, 241 }; 242 using qos = int; 243 244 } 245 #endif 246 #endif 247