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 * 31 * @brief Declares common types. 32 * 33 * @syscap SystemCapability.Resourceschedule.Ffrt.Core 34 * @since 10 35 * @version 1.0 36 */ 37 #ifndef FFRT_API_C_TYPE_DEF_H 38 #define FFRT_API_C_TYPE_DEF_H 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 QoS types. 50 * 51 */ 52 typedef enum { 53 /** Inheritance. */ 54 ffrt_qos_inherit = -1, 55 /** Background task. */ 56 ffrt_qos_background, 57 /** Real-time tool. */ 58 ffrt_qos_utility, 59 /** Default type. */ 60 ffrt_qos_default, 61 /** User initiated. */ 62 ffrt_qos_user_initiated, 63 } ffrt_qos_default_t; 64 typedef int ffrt_qos_t; 65 66 typedef void(*ffrt_function_t)(void*); 67 68 /** 69 * @brief Defines a task executor. 70 * 71 */ 72 typedef struct { 73 /** Function used to execute a task. */ 74 ffrt_function_t exec; 75 /** Function used to destroy a task. */ 76 ffrt_function_t destroy; 77 /** Need to be set to 0. */ 78 uint64_t reserve[2]; 79 } ffrt_function_header_t; 80 81 /** 82 * @brief Defines the storage size of multiple types of structs. 83 * 84 */ 85 typedef enum { 86 /** Task attribute storage size. */ 87 ffrt_task_attr_storage_size = 128, 88 /** Task executor storage size. */ 89 ffrt_auto_managed_function_storage_size = 64 + sizeof(ffrt_function_header_t), 90 /* Mutex storage size. */ 91 ffrt_mutex_storage_size = 64, 92 /** Condition variable storage size. */ 93 ffrt_cond_storage_size = 64, 94 /** Queue storage size. */ 95 ffrt_queue_attr_storage_size = 128, 96 } ffrt_storage_size_t; 97 98 /** 99 * @brief Enumerates the task types. 100 * 101 */ 102 typedef enum { 103 /** General task. */ 104 ffrt_function_kind_general, 105 /** Queue task. */ 106 ffrt_function_kind_queue, 107 #ifdef FFRT_IO_TASK_SCHEDULER 108 ffrt_function_kind_io, 109 #endif 110 } ffrt_function_kind_t; 111 112 /** 113 * @brief dependency type. 114 * 115 */ 116 typedef enum { 117 /** Data dependency type. */ 118 ffrt_dependence_data, 119 /** Task dependency type. */ 120 ffrt_dependence_task, 121 } ffrt_dependence_type_t; 122 123 /** 124 * @brief dependency data structure. 125 * 126 */ 127 typedef struct { 128 /** Dependency type. */ 129 ffrt_dependence_type_t type; 130 /** Dependency pointer. */ 131 const void* ptr; 132 } ffrt_dependence_t; 133 134 /** 135 * @brief Defines the dependency struct. 136 * 137 */ 138 typedef struct { 139 /** Number of dependencies. */ 140 uint32_t len; 141 /** Dependent data. */ 142 const ffrt_dependence_t* items; 143 } ffrt_deps_t; 144 145 typedef struct { 146 uint32_t storage[(ffrt_task_attr_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; 147 } ffrt_task_attr_t; 148 149 typedef struct { 150 uint32_t storage[(ffrt_queue_attr_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; 151 } ffrt_queue_attr_t; 152 153 typedef void* ffrt_task_handle_t; 154 155 typedef enum { 156 ffrt_error = -1, 157 ffrt_success = 0, 158 ffrt_error_nomem = ENOMEM, 159 ffrt_error_timedout = ETIMEDOUT, 160 ffrt_error_busy = EBUSY, 161 ffrt_error_inval = EINVAL 162 } ffrt_error_t; 163 164 typedef struct { 165 long storage; 166 } ffrt_condattr_t; 167 168 typedef struct { 169 long storage; 170 } ffrt_mutexattr_t; 171 172 typedef struct { 173 uint32_t storage[(ffrt_mutex_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; 174 } ffrt_mutex_t; 175 176 typedef struct { 177 uint32_t storage[(ffrt_cond_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; 178 } ffrt_cond_t; 179 180 #ifdef FFRT_IO_TASK_SCHEDULER 181 typedef enum { 182 ffrt_coroutine_stackless, 183 ffrt_coroutine_with_stack, 184 } ffrt_coroutine_t; 185 186 typedef enum { 187 ffrt_coroutine_pending = 0, 188 ffrt_coroutine_ready = 1, 189 } ffrt_coroutine_ret_t; 190 191 typedef ffrt_coroutine_ret_t(*ffrt_coroutine_ptr_t)(void*); 192 193 typedef struct { 194 int fd; 195 void* data; 196 void(*cb)(void*, uint32_t); 197 } ffrt_poller_t; 198 199 typedef enum { 200 ffrt_timer_notfound = -1, 201 ffrt_timer_not_executed = 0, 202 ffrt_timer_executed = 1, 203 } ffrt_timer_query_t; 204 #endif 205 206 #ifdef __cplusplus 207 namespace ffrt { 208 enum qos_default { 209 qos_inherit = ffrt_qos_inherit, 210 qos_background = ffrt_qos_background, 211 qos_utility = ffrt_qos_utility, 212 qos_default = ffrt_qos_default, 213 qos_user_initiated = ffrt_qos_user_initiated, 214 }; 215 using qos = int; 216 } 217 #endif 218 #endif 219