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 task.h 27 * 28 * @brief Declares the task interfaces in C. 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_TASK_H 38 #define FFRT_API_C_TASK_H 39 40 #include <stdint.h> 41 #include "type_def.h" 42 43 /** 44 * @brief Initializes a task attribute. 45 * 46 * @param attr Indicates a pointer to the task attribute. 47 * @return Returns <b>0</b> if the task attribute is initialized; 48 returns <b>-1</b> otherwise. 49 * @since 10 50 * @version 1.0 51 */ 52 FFRT_C_API int ffrt_task_attr_init(ffrt_task_attr_t* attr); 53 54 /** 55 * @brief Sets the name of a task attribute. 56 * 57 * @param attr Indicates a pointer to the task attribute. 58 * @param name Indicates a pointer to the task name. 59 * @since 10 60 * @version 1.0 61 */ 62 FFRT_C_API void ffrt_task_attr_set_name(ffrt_task_attr_t* attr, const char* name); 63 64 /** 65 * @brief Gets the name of a task attribute. 66 * 67 * @param attr Indicates a pointer to the task attribute. 68 * @return Returns a non-null pointer to the task name if the name is obtained; 69 returns a null pointer otherwise. 70 * @since 10 71 * @version 1.0 72 */ 73 FFRT_C_API const char* ffrt_task_attr_get_name(const ffrt_task_attr_t* attr); 74 75 /** 76 * @brief Destroys a task attribute, the user needs to invoke this interface. 77 * 78 * @param attr Indicates a pointer to the task attribute. 79 * @since 10 80 * @version 1.0 81 */ 82 FFRT_C_API void ffrt_task_attr_destroy(ffrt_task_attr_t* attr); 83 84 /** 85 * @brief Sets the QoS of a task attribute. 86 * 87 * @param attr Indicates a pointer to the task attribute. 88 * @param qos Indicates the QoS. 89 * @since 10 90 * @version 1.0 91 */ 92 FFRT_C_API void ffrt_task_attr_set_qos(ffrt_task_attr_t* attr, ffrt_qos_t qos); 93 94 /** 95 * @brief Gets the QoS of a task attribute. 96 * 97 * @param attr Indicates a pointer to the task attribute. 98 * @return Returns the QoS, which is <b>ffrt_qos_default</b> by default. 99 * @since 10 100 * @version 1.0 101 */ 102 FFRT_C_API ffrt_qos_t ffrt_task_attr_get_qos(const ffrt_task_attr_t* attr); 103 104 /** 105 * @brief Sets the delay time of a task attribute. 106 * 107 * @param attr Indicates a pointer to the task attribute. 108 * @param delay_us Indicates the delay time, in microseconds. 109 * @since 10 110 * @version 1.0 111 */ 112 FFRT_C_API void ffrt_task_attr_set_delay(ffrt_task_attr_t* attr, uint64_t delay_us); 113 114 /** 115 * @brief Gets the delay time of a task attribute. 116 * 117 * @param attr Indicates a pointer to the task attribute. 118 * @return Returns the delay time. 119 * @since 10 120 * @version 1.0 121 */ 122 FFRT_C_API uint64_t ffrt_task_attr_get_delay(const ffrt_task_attr_t* attr); 123 124 /** 125 * @brief Sets the priority of a task attribute. 126 * 127 * @param attr Indicates a pointer to the task attribute. 128 * @param priority Indicates the execute priority of concurrent queue task. 129 * @since 12 130 * @version 1.0 131 */ 132 FFRT_C_API void ffrt_task_attr_set_queue_priority(ffrt_task_attr_t* attr, ffrt_queue_priority_t priority); 133 134 /** 135 * @brief Gets the priority of a task attribute. 136 * 137 * @param attr Indicates a pointer to the task attribute. 138 * @return Returns the priority of concurrent queue task. 139 * @since 12 140 * @version 1.0 141 */ 142 FFRT_C_API ffrt_queue_priority_t ffrt_task_attr_get_queue_priority(const ffrt_task_attr_t* attr); 143 144 /** 145 * @brief Sets the stack size of a task attribute. 146 * 147 * @param attr Indicates a pointer to the task attribute. 148 * @param size Indicates the task stack size, unit is byte. 149 * @since 12 150 * @version 1.0 151 */ 152 FFRT_C_API void ffrt_task_attr_set_stack_size(ffrt_task_attr_t* attr, uint64_t size); 153 154 /** 155 * @brief Gets the stack size of a task attribute. 156 * 157 * @param attr Indicates a pointer to the task attribute. 158 * @return Returns the task stack size, unit is byte. 159 * @since 12 160 * @version 1.0 161 */ 162 FFRT_C_API uint64_t ffrt_task_attr_get_stack_size(const ffrt_task_attr_t* attr); 163 164 /** 165 * @brief Sets the schedule timeout of a task attribute. 166 * 167 * @param attr Indicates a pointer to the task attribute. 168 * @param timeout_us task scheduler timeout. 169 * @version 1.0 170 */ 171 FFRT_C_API void ffrt_task_attr_set_timeout(ffrt_task_attr_t* attr, uint64_t timeout_us); 172 173 /** 174 * @brief Gets the schedule timeout of a task attribute. 175 * 176 * @param attr Indicates a pointer to the task attribute. 177 * @return Returns the task schedule timeout. 178 * @version 1.0 179 */ 180 FFRT_C_API uint64_t ffrt_task_attr_get_timeout(const ffrt_task_attr_t* attr); 181 182 /** 183 * @brief Updates the QoS of this task. 184 * 185 * @param qos Indicates the new QoS. 186 * @return Returns <b>0</b> if the QoS is updated; 187 returns <b>-1</b> otherwise. 188 * @since 10 189 * @version 1.0 190 */ 191 FFRT_C_API int ffrt_this_task_update_qos(ffrt_qos_t qos); 192 193 /** 194 * @brief Gets the QoS of this task. 195 * 196 * @return Returns the task qos. 197 * @since 12 198 * @version 1.0 199 */ 200 FFRT_C_API ffrt_qos_t ffrt_this_task_get_qos(void); 201 202 /** 203 * @brief Gets the ID of this task. 204 * 205 * @return Returns the task ID. 206 * @since 10 207 * @version 1.0 208 */ 209 FFRT_C_API uint64_t ffrt_this_task_get_id(void); 210 211 /** 212 * @brief Applies memory for the function execution structure. 213 * 214 * @param kind Indicates the type of the function execution structure, which can be common or queue. 215 * @return Returns a non-null pointer if the memory is allocated; 216 returns a null pointer otherwise. 217 * @since 10 218 * @version 1.0 219 */ 220 FFRT_C_API void *ffrt_alloc_auto_managed_function_storage_base(ffrt_function_kind_t kind); 221 222 /** 223 * @brief Submits a task. 224 * 225 * @param f Indicates a pointer to the task executor. 226 * @param in_deps Indicates a pointer to the input dependencies. 227 * @param out_deps Indicates a pointer to the output dependencies. 228 * @param attr Indicates a pointer to the task attribute. 229 * @since 10 230 * @version 1.0 231 */ 232 FFRT_C_API void ffrt_submit_base(ffrt_function_header_t* f, const ffrt_deps_t* in_deps, const ffrt_deps_t* out_deps, 233 const ffrt_task_attr_t* attr); 234 235 /** 236 * @brief Submits a task, and obtains a task handle. 237 * 238 * @param f Indicates a pointer to the task executor. 239 * @param in_deps Indicates a pointer to the input dependencies. 240 * @param out_deps Indicates a pointer to the output dependencies. 241 * @param attr Indicates a pointer to the task attribute. 242 * @return Returns a non-null task handle if the task is submitted; 243 returns a null pointer otherwise. 244 * @since 10 245 * @version 1.0 246 */ 247 FFRT_C_API ffrt_task_handle_t ffrt_submit_h_base(ffrt_function_header_t* f, const ffrt_deps_t* in_deps, 248 const ffrt_deps_t* out_deps, const ffrt_task_attr_t* attr); 249 250 /** 251 * @brief Increases reference count of a task. 252 * 253 * @param handle Indicates a task handle. 254 * @return Returns the task handle original reference count. 255 * @since 12 256 * @version 1.0 257 */ 258 FFRT_C_API uint32_t ffrt_task_handle_inc_ref(ffrt_task_handle_t handle); 259 260 /** 261 * @brief Decreases reference count of a task. 262 * 263 * @param handle Indicates a task handle. 264 * @return Returns the task handle original reference count. 265 * @since 12 266 * @version 1.0 267 */ 268 FFRT_C_API uint32_t ffrt_task_handle_dec_ref(ffrt_task_handle_t handle); 269 270 /** 271 * @brief Destroys a task handle, the user needs to invoke this interface. 272 * 273 * @param handle Indicates a task handle. 274 * @since 10 275 * @version 1.0 276 */ 277 FFRT_C_API void ffrt_task_handle_destroy(ffrt_task_handle_t handle); 278 279 /** 280 * @brief Waits until the dependent tasks are complete. 281 * 282 * @param deps Indicates a pointer to the dependent tasks. 283 * @since 10 284 * @version 1.0 285 */ 286 FFRT_C_API void ffrt_wait_deps(const ffrt_deps_t* deps); 287 288 /** 289 * @brief Waits until all submitted tasks are complete. 290 * 291 * @since 10 292 * @version 1.0 293 */ 294 FFRT_C_API void ffrt_wait(void); 295 296 /** 297 * @brief Sets the thread stack size of a specified QoS level. 298 * 299 * @param qos Indicates the QoS. 300 * @param stack_size Indicates worker thread stack size. 301 * @since 10 302 * @version 1.0 303 */ 304 FFRT_C_API ffrt_error_t ffrt_set_worker_stack_size(ffrt_qos_t qos, size_t stack_size); 305 306 /** 307 * @brief Gets gid of a task. 308 * 309 * @param handle Indicates a task handle. 310 * @return Returns gid. 311 * @since 10 312 * @version 1.0 313 */ 314 FFRT_C_API uint64_t ffrt_task_handle_get_id(ffrt_task_handle_t handle); 315 316 #endif // FFRT_API_C_TASK_H 317 /** @} */