From 9c84b4975aa6361ebc4ef7a9f7f72acd2df90cc9 Mon Sep 17 00:00:00 2001 From: guodeqing Date: Sat, 3 Jun 2023 07:10:15 +0800 Subject: [PATCH] =?UTF-8?q?!I7AJWG=20=E6=96=B0=E5=A2=9EFFRT=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zh-cn/native_sdk/ffrt/c/condition_variable.h | 150 ++++ zh-cn/native_sdk/ffrt/c/mutex.h | 83 ++ zh-cn/native_sdk/ffrt/c/queue.h | 139 ++++ zh-cn/native_sdk/ffrt/c/sleep.h | 47 ++ zh-cn/native_sdk/ffrt/c/task.h | 205 +++++ zh-cn/native_sdk/ffrt/c/type_def.h | 155 ++++ .../native_sdk/ffrt/cpp/condition_variable.h | 122 +++ zh-cn/native_sdk/ffrt/cpp/mutex.h | 60 ++ zh-cn/native_sdk/ffrt/cpp/queue.h | 229 ++++++ zh-cn/native_sdk/ffrt/cpp/sleep.h | 51 ++ zh-cn/native_sdk/ffrt/cpp/task.h | 736 ++++++++++++++++++ zh-cn/native_sdk/ffrt/ffrt.h | 33 + 12 files changed, 2010 insertions(+) create mode 100755 zh-cn/native_sdk/ffrt/c/condition_variable.h create mode 100755 zh-cn/native_sdk/ffrt/c/mutex.h create mode 100755 zh-cn/native_sdk/ffrt/c/queue.h create mode 100755 zh-cn/native_sdk/ffrt/c/sleep.h create mode 100755 zh-cn/native_sdk/ffrt/c/task.h create mode 100755 zh-cn/native_sdk/ffrt/c/type_def.h create mode 100755 zh-cn/native_sdk/ffrt/cpp/condition_variable.h create mode 100755 zh-cn/native_sdk/ffrt/cpp/mutex.h create mode 100755 zh-cn/native_sdk/ffrt/cpp/queue.h create mode 100755 zh-cn/native_sdk/ffrt/cpp/sleep.h create mode 100755 zh-cn/native_sdk/ffrt/cpp/task.h create mode 100755 zh-cn/native_sdk/ffrt/ffrt.h diff --git a/zh-cn/native_sdk/ffrt/c/condition_variable.h b/zh-cn/native_sdk/ffrt/c/condition_variable.h new file mode 100755 index 00000000..b00f2aab --- /dev/null +++ b/zh-cn/native_sdk/ffrt/c/condition_variable.h @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + /** + * @file condition_variable.h + * + * @brief 声明条件变量提供的C接口. + * + * @since 10 + * @version 1.0 + */ +#ifndef FFRT_API_C_CONDITION_VARIABLE_H +#define FFRT_API_C_CONDITION_VARIABLE_H +#include +#include "type_def.h" + +typedef enum { + ffrt_clock_realtime = CLOCK_REALTIME, + ffrt_clock_monotonic = CLOCK_MONOTONIC +} ffrt_clockid_t; + +/** + * @brief 初始化条件变量属性. + * + * @param attr 条件变量属性指针. + * @return 初始化条件变量属性成功返回ffrt_thrd_success, + 初始化条件变量属性失败返回ffrt_thrd_error. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_condattr_init(ffrt_condattr_t* attr); + +/** + * @brief 销毁条件变量属性. + * + * @param attr 条件变量属性指针. + * @return 销毁条件变量属性成功返回ffrt_thrd_success, + 销毁条件变量属性失败返回ffrt_thrd_error. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_condattr_destroy(ffrt_condattr_t* attr); + +/** + * @brief 设置条件变量的时钟属性. + * + * @param attr 条件变量属性指针. + * @param clock 时钟类型. + * @return 设置条件变量的时钟属性成功返回ffrt_thrd_success, + 设置条件变量的时钟属性失败返回ffrt_thrd_error. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_condattr_setclock(ffrt_condattr_t* attr, ffrt_clockid_t clock); + +/** + * @brief 获取条件变量的时钟属性. + * + * @param attr 条件变量属性指针. + * @param clock 时钟属性指针. + * @return 获取条件变量的时钟属性成功返回ffrt_thrd_success, + 获取条件变量的时钟属性失败返回ffrt_thrd_error. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_condattr_getclock(const ffrt_condattr_t* attr, ffrt_clockid_t* clock); + +/** + * @brief 初始化条件变量. + * + * @param cond 条件变量指针. + * @param attr 条件变量属性指针. + * @return 初始化条件变量成功返回ffrt_thrd_success, + 初始化条件变量失败返回ffrt_thrd_error. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_cond_init(ffrt_cond_t* cond, const ffrt_condattr_t* attr); + +/** + * @brief 唤醒阻塞在条件变量上的一个任务. + * + * @param cond 条件变量指针. + * @return 唤醒成功返回ffrt_thrd_success, + 唤醒失败返回ffrt_thrd_error. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_cond_signal(ffrt_cond_t* cond); + +/** + * @brief 唤醒阻塞在条件变量上的所有任务. + * + * @param cond 条件变量指针. + * @return 唤醒成功返回ffrt_thrd_success, + 唤醒失败返回ffrt_thrd_error. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_cond_broadcast(ffrt_cond_t* cond); + +/** + * @brief 条件变量等待函数,条件变量不满足时阻塞当前任务. + * + * @param cond 条件变量指针. + * @param mutex mutex指针. + * @return 等待后被成功唤醒返回ffrt_thrd_success, + 等待失败返回ffrt_thrd_error. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_cond_wait(ffrt_cond_t* cond, ffrt_mutex_t* mutex); + +/** + * @brief 条件变量超时等待函数,条件变量不满足时阻塞当前任务,超时等待返回. + * + * @param cond 条件变量指针. + * @param mutex mutex指针. + * @param time_point 最大等待到的时间点,超过这个时间点等待返回. + * @return 等待后被成功唤醒返回ffrt_thrd_success, + 等待超时返回ffrt_thrd_timedout. + 等待失败ffrt_thrd_error. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_cond_timedwait(ffrt_cond_t* cond, ffrt_mutex_t* mutex, const struct timespec* time_point); + +/** + * @brief 销毁掉件变量. + * + * @param cond 条件变量指针. + * @return 销毁条件变量成功返回ffrt_thrd_success, + 销毁条件变量失败返回ffrt_thrd_error. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_cond_destroy(ffrt_cond_t* cond); +#endif diff --git a/zh-cn/native_sdk/ffrt/c/mutex.h b/zh-cn/native_sdk/ffrt/c/mutex.h new file mode 100755 index 00000000..21b151b7 --- /dev/null +++ b/zh-cn/native_sdk/ffrt/c/mutex.h @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + /** + * @file mutex.h + * + * @brief 声明mutex提供的C接口. + * + * @since 10 + * @version 1.0 + */ +#ifndef FFRT_API_C_MUTEX_H +#define FFRT_API_C_MUTEX_H +#include "type_def.h" + +/** + * @brief 初始化mutex. + * + * @param mutex mutex指针. + * @param attr mutex属性. + * @return 初始化mutex成功返回ffrt_thrd_success, + 初始化mutex失败返回ffrt_thrd_error. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_mutex_init(ffrt_mutex_t* mutex, const ffrt_mutexattr_t* attr); + +/** + * @brief 获取mutex. + * + * @param mutex mutex指针. + * @return 获取mutex成功返回ffrt_thrd_success, + 获取mutex失败返回ffrt_thrd_error,或者阻塞当前任务. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_mutex_lock(ffrt_mutex_t* mutex); + +/** + * @brief 释放mutex. + * + * @param mutex mutex指针. + * @return 释放mutex成功返回ffrt_thrd_success, + 释放mutex失败返回ffrt_thrd_error. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_mutex_unlock(ffrt_mutex_t* mutex); + +/** + * @brief 尝试获取mutex. + * + * @param mutex mutex指针. + * @return 获取mutex成功返回ffrt_thrd_success, + 获取mutex失败返回ffrt_thrd_error或ffrt_thrd_busy. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_mutex_trylock(ffrt_mutex_t* mutex); + +/** + * @brief 销毁mutex. + * + * @param mutex mutex指针. + * @return 销毁mutex成功返回ffrt_thrd_success, + 销毁mutex失败返回ffrt_thrd_error. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_mutex_destroy(ffrt_mutex_t* mutex); +#endif diff --git a/zh-cn/native_sdk/ffrt/c/queue.h b/zh-cn/native_sdk/ffrt/c/queue.h new file mode 100755 index 00000000..7a1a3faa --- /dev/null +++ b/zh-cn/native_sdk/ffrt/c/queue.h @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file queue.h + * + * @brief 声明串行队列提供的C接口. + * + * @since 10 + * @version 1.0 + */ +#ifndef FFRT_API_C_QUEUE_H +#define FFRT_API_C_QUEUE_H + +#include "type_def.h" + +typedef enum { ffrt_queue_serial, ffrt_queue_max } ffrt_queue_type_t; +typedef void* ffrt_queue_t; + +/** + * @brief 初始化串行队列属性. + * + * @param attr 串行队列属性指针. + * @return 执行成功时返回0, + 执行成功时返回-1. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_queue_attr_init(ffrt_queue_attr_t* attr); + +/** + * @brief 销毁串行队列属性. + * + * @param attr 串行队列属性指针. + * @since 10 + * @version 1.0 + */ +FFRT_C_API void ffrt_queue_attr_destroy(ffrt_queue_attr_t* attr); + +/** + * @brief 设置串行队列qos属性. + * + * @param attr 串行队列属性指针. + * @param attr qos属性值. + * @since 10 + * @version 1.0 + */ +FFRT_C_API void ffrt_queue_attr_set_qos(ffrt_queue_attr_t* attr, ffrt_qos_t qos); + +/** + * @brief 获取串行队列qos属性. + * + * @param attr 串行队列属性指针. + * @return 返回串行队列的qos属性 + * @since 10 + * @version 1.0 + */ +FFRT_C_API ffrt_qos_t ffrt_queue_attr_get_qos(const ffrt_queue_attr_t* attr); + +/** + * @brief 创建队列. + * + * @param type 队列类型. + * @param name 队列名字. + * @param attr 队列属性. + * @return 创建队列成功返回非空队列句柄 + 创建队列失败返回空指针 + * @since 10 + * @version 1.0 + */ +FFRT_C_API ffrt_queue_t ffrt_queue_create(ffrt_queue_type_t type, const char* name, const ffrt_queue_attr_t* attr); + +/** + * @brief 销毁队列. + * + * @param queue 队列句柄. + * @since 10 + * @version 1.0 + */ +FFRT_C_API void ffrt_queue_destroy(ffrt_queue_t queue); + +/** + * @brief 提交一个任务到队列中调度执行. + * + * @param queue 队列句柄. + * @param f 任务的执行体. + * @param attr 任务属性. + * @since 10 + * @version 1.0 + */ +FFRT_C_API void ffrt_queue_submit(ffrt_queue_t queue, ffrt_function_header_t* f, const ffrt_task_attr_t* attr); + +/** + * @brief 提交一个任务到队列中调度执行,并返回任务句柄. + * + * @param queue 队列句柄. + * @param f 任务的执行体. + * @param attr 任务属性. + * @return 提交成功返回非空任务句柄 + 提交失败返回空指针 + * @since 10 + * @version 1.0 + */ +FFRT_C_API ffrt_task_handle_t ffrt_queue_submit_h( + ffrt_queue_t queue, ffrt_function_header_t* f, const ffrt_task_attr_t* attr); + +/** + * @brief 等待队列中一个任务执行完成. + * + * @param handle 任务句柄. + * @since 10 + * @version 1.0 + */ +FFRT_C_API void ffrt_queue_wait(ffrt_task_handle_t handle); + +/** + * @brief 取消队列中一个任务. + * + * @param handle 任务句柄. + * @return 取消任务成功返回0 + 取消任务失败返回-1 + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_queue_cancel(ffrt_task_handle_t handle); + +#endif // FFRT_API_C_QUEUE_H \ No newline at end of file diff --git a/zh-cn/native_sdk/ffrt/c/sleep.h b/zh-cn/native_sdk/ffrt/c/sleep.h new file mode 100755 index 00000000..5131388e --- /dev/null +++ b/zh-cn/native_sdk/ffrt/c/sleep.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file sleep.h + * + * @brief 声明sleep和yield C接口. + * + * @since 10 + * @version 1.0 + */ +#ifndef FFRT_API_C_SLEEP_H +#define FFRT_API_C_SLEEP_H +#include +#include "type_def.h" + +/** + * @brief 延迟usec微妙. + * + * @param usec延迟时间,单位微妙. + * @return 执行成功时返回ffrt_thrd_success, + 执行成功时返回ffrt_thrd_error. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_usleep(uint64_t usec); + +/** + * @brief 当前任务主动放权,让其他任务有机会调度执行. + * + * @since 10 + * @version 1.0 + */ +FFRT_C_API void ffrt_yield(); +#endif diff --git a/zh-cn/native_sdk/ffrt/c/task.h b/zh-cn/native_sdk/ffrt/c/task.h new file mode 100755 index 00000000..4cc23205 --- /dev/null +++ b/zh-cn/native_sdk/ffrt/c/task.h @@ -0,0 +1,205 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + /** + * @file task.h + * + * @brief 声明任务提供的C接口. + * + * @since 10 + * @version 1.0 + */ +#ifndef FFRT_API_C_TASK_H +#define FFRT_API_C_TASK_H +#include "type_def.h" + +/** + * @brief 初始化任务属性. + * + * @param attr 任务属性指针. + * @return 初始化任务属性成功返回0, + 初始化任务属性失败返回-1. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_task_attr_init(ffrt_task_attr_t* attr); + +/** + * @brief 设置任务名字. + * + * @param attr 任务属性指针. + * @param name 任务名字. + * @since 10 + * @version 1.0 + */ +FFRT_C_API void ffrt_task_attr_set_name(ffrt_task_attr_t* attr, const char* name); + +/** + * @brief 获取任务名字. + * + * @param attr 任务属性指针. + * @return 获取任务名字成功返回非空指针, + 获取任务名字失败返回空指针. + * @since 10 + * @version 1.0 + */ +FFRT_C_API const char* ffrt_task_attr_get_name(const ffrt_task_attr_t* attr); + +/** + * @brief 销毁任务属性. + * + * @param attr 任务属性指针. + * @since 10 + * @version 1.0 + */ +FFRT_C_API void ffrt_task_attr_destroy(ffrt_task_attr_t* attr); + +/** + * @brief 设置任务qos. + * + * @param attr 任务属性指针. + * @param pos 任务pos类型. + * @since 10 + * @version 1.0 + */ +FFRT_C_API void ffrt_task_attr_set_qos(ffrt_task_attr_t* attr, ffrt_qos_t qos); + +/** + * @brief 获取任务qos. + * + * @param attr 任务属性指针. + * @return 返回任务的qos,默认返回ffrt_qos_default + * @since 10 + * @version 1.0 + */ +FFRT_C_API ffrt_qos_t ffrt_task_attr_get_qos(const ffrt_task_attr_t* attr); + +/** + * @brief 设置任务延迟时间. + * + * @param attr 任务属性指针. + * @param delay_us 任务延迟时间,单位微妙. + * @since 10 + * @version 1.0 + */ +FFRT_C_API void ffrt_task_attr_set_delay(ffrt_task_attr_t* attr, uint64_t delay_us); + +/** + * @brief 获取任务延迟时间. + * + * @param attr 任务属性指针. + * @return 返回任务的延迟时间 + * @since 10 + * @version 1.0 + */ +FFRT_C_API uint64_t ffrt_task_attr_get_delay(const ffrt_task_attr_t* attr); + +/** + * @brief 更新任务qos. + * + * @param qos 更新的qos,更新当前任务的qos. + * @return 更新任务qos成功返回0, + 更新任务qos成功返回-1. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_this_task_update_qos(ffrt_qos_t qos); + +/** + * @brief 获取任务id. + * + * @return 返回当前任务的id + * @since 10 + * @version 1.0 + */ +FFRT_C_API uint64_t ffrt_this_task_get_id(); + +/** + * @brief 申请函数执行结构的内存. + * + * @param kind 函数执行结构类型,支持通用和队列函数执行结构类型. + * @return 申请函数执行结构成功返回非空指针, + 申请函数执行结构失败返回空指针. + * @since 10 + * @version 1.0 + */ +FFRT_C_API void *ffrt_alloc_auto_managed_function_storage_base(ffrt_function_kind_t kind); + +/** + * @brief 提交任务调度执行. + * + * @param f 任务执行体封装的指针. + * @param in_deps 输入依赖指针. + * @param out_deps 输出依赖指针. + * @param attr 任务属性. + * @since 10 + * @version 1.0 + */ +FFRT_C_API void ffrt_submit_base(ffrt_function_header_t* f, const ffrt_deps_t* in_deps, const ffrt_deps_t* out_deps, + const ffrt_task_attr_t* attr); + +/** + * @brief 提交任务调度执行并返回任务句柄. + * + * @param f 任务执行体封装的指针. + * @param in_deps 输入依赖指针. + * @param out_deps 输出依赖指针. + * @param attr 任务属性. + * @return 提交任务成功返回非空任务句柄, + 提交任务失败返回空指针. + * @since 10 + * @version 1.0 + */ +FFRT_C_API ffrt_task_handle_t ffrt_submit_h_base(ffrt_function_header_t* f, const ffrt_deps_t* in_deps, + const ffrt_deps_t* out_deps, const ffrt_task_attr_t* attr); + +/** + * @brief 销毁任务句柄. + * + * @param handle 任务句柄. + * @since 10 + * @version 1.0 + */ +FFRT_C_API void ffrt_task_handle_destroy(ffrt_task_handle_t handle); + +/** + * @brief 跳过指定任务. + * + * @param handle 任务句柄. + * @return 跳过指定任务成功返回0, + 跳过指定任务失败返回-1. + * @since 10 + * @version 1.0 + */ +FFRT_C_API int ffrt_skip(ffrt_task_handle_t handle); + +/** + * @brief 等待依赖的任务完成,当前任务开始执行. + * + * @param deps 依赖的指针. + * @since 10 + * @version 1.0 + */ +FFRT_C_API void ffrt_wait_deps(const ffrt_deps_t* deps); + +/** + * @brief 等待之前所有提交任务完成,当前任务开始执行. + * + * @since 10 + * @version 1.0 + */ +FFRT_C_API void ffrt_wait(); + +#endif diff --git a/zh-cn/native_sdk/ffrt/c/type_def.h b/zh-cn/native_sdk/ffrt/c/type_def.h new file mode 100755 index 00000000..592fa1e1 --- /dev/null +++ b/zh-cn/native_sdk/ffrt/c/type_def.h @@ -0,0 +1,155 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + /** + * @file type_def.h + * + * @brief 定义通用类型. + * + * @since 10 + * @version 1.0 + */ +#ifndef FFRT_API_C_TYPE_DEF_H +#define FFRT_API_C_TYPE_DEF_H +#include +#include + +#ifdef __cplusplus +#define FFRT_C_API extern "C" +#else +#define FFRT_C_API +#endif + +/** + * @brief 任务的qos类型. + * + */ +typedef enum { + /** 继承当前任务qos属性 */ + ffrt_qos_inherit = -1, + /** 后台任务 */ + ffrt_qos_background, + /** 实时工具 */ + ffrt_qos_utility, + /** 默认类型 */ + ffrt_qos_default, + /** 用户期望 */ + ffrt_qos_user_initiated, +} ffrt_qos_t; + +typedef void(*ffrt_function_t)(void*); + +/** + * @brief 任务执行体. + * + */ +typedef struct { + /** 任务执行函数 */ + ffrt_function_t exec; + /** 任务销毁函数 */ + ffrt_function_t destroy; + uint64_t reserve[2]; +} ffrt_function_header_t; + +/** + * @brief 多种类型数据结构分配大小定义. + * + */ +typedef enum { + /** 任务属性 */ + ffrt_task_attr_storage_size = 128, + /** 任务执行体 */ + ffrt_auto_managed_function_storage_size = 64 + sizeof(ffrt_function_header_t), + /** 互斥锁 */ + ffrt_mutex_storage_size = 64, + /** 条件变量 */ + ffrt_cond_storage_size = 64, + /** 队列属性 */ + ffrt_queue_attr_storage_size = 128, +} ffrt_storage_size_t; + +/** + * @brief 任务类型. + * + */ +typedef enum { + /** 通用任务类型 */ + ffrt_function_kind_general, + /** 队列任务类型 */ + ffrt_function_kind_queue +} ffrt_function_kind_t; + +/** + * @brief 数据依赖数据结构. + * + */ +typedef struct { + /** 依赖个数 */ + uint32_t len; + /** 依赖数据 */ + const void* const * items; +} ffrt_deps_t; + +typedef struct { + uint32_t storage[(ffrt_task_attr_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; +} ffrt_task_attr_t; + +typedef struct { + uint32_t storage[(ffrt_queue_attr_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; +} ffrt_queue_attr_t; + +typedef void* ffrt_task_handle_t; + +typedef enum { + ffrt_error = -1, + ffrt_success = 0, + ffrt_error_nomem = ENOMEM, + ffrt_error_timedout = ETIMEDOUT, + ffrt_error_busy = EBUSY, + ffrt_error_inval = EINVAL +} ffrt_error_t; + +typedef struct { + long storage; +} ffrt_condattr_t; + +typedef struct { + long storage; +} ffrt_mutexattr_t; + +typedef struct { + uint32_t storage[(ffrt_thread_attr_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; +} ffrt_thread_attr_t; + +typedef struct { + uint32_t storage[(ffrt_mutex_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; +} ffrt_mutex_t; + +typedef struct { + uint32_t storage[(ffrt_cond_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; +} ffrt_cond_t; + +#ifdef __cplusplus +namespace ffrt { +enum qos { + qos_inherit = ffrt_qos_inherit, + qos_background = ffrt_qos_background, + qos_utility = ffrt_qos_utility, + qos_default = ffrt_qos_default, + qos_user_initiated = ffrt_qos_user_initiated, +}; +} +#endif +#endif diff --git a/zh-cn/native_sdk/ffrt/cpp/condition_variable.h b/zh-cn/native_sdk/ffrt/cpp/condition_variable.h new file mode 100755 index 00000000..60e38662 --- /dev/null +++ b/zh-cn/native_sdk/ffrt/cpp/condition_variable.h @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file condition_variable.h + * + * @brief 声明条件变量提供的C++接口. + * + * @since 10 + * @version 1.0 + */ +#ifndef FFRT_API_CPP_CONDITION_VARIABLE_H +#define FFRT_API_CPP_CONDITION_VARIABLE_H +#include +#include +#include "mutex.h" +#include "c/condition_variable.h" + +namespace ffrt { +enum class cv_status { no_timeout, timeout }; + +class condition_variable : public ffrt_cond_t { +public: + condition_variable() + { + ffrt_cond_init(this, nullptr); + } + + ~condition_variable() noexcept + { + ffrt_cond_destroy(this); + } + + condition_variable(const condition_variable&) = delete; + condition_variable& operator=(const condition_variable&) = delete; + + template + bool wait_until( + std::unique_lock& lk, const std::chrono::time_point& tp, Pred&& pred) noexcept + { + while (!pred()) { + if (wait_until(lk, tp) == cv_status::timeout) { + return pred(); + } + } + return true; + } + + template + cv_status wait_until(std::unique_lock& lk, const std::chrono::time_point& tp) noexcept + { + return _wait_for(lk, tp - Clock::now()); + } + + template + cv_status wait_for(std::unique_lock& lk, const std::chrono::duration& sleep_time) noexcept + { + return _wait_for(lk, sleep_time); + } + + template + bool wait_for( + std::unique_lock& lk, const std::chrono::duration& sleepTime, Pred&& pred) noexcept + { + return wait_until(lk, std::chrono::steady_clock::now() + sleepTime, std::forward(pred)); + } + + template + void wait(std::unique_lock& lk, Pred&& pred) + { + while (!pred()) { + wait(lk); + } + } + + void wait(std::unique_lock& lk) + { + ffrt_cond_wait(this, lk.mutex()); + } + + void notify_one() noexcept + { + ffrt_cond_signal(this); + } + + void notify_all() noexcept + { + ffrt_cond_broadcast(this); + } + +private: + template + cv_status _wait_for(std::unique_lock& lk, const std::chrono::duration& dur) noexcept + { + timespec ts; + std::chrono::nanoseconds _T0 = std::chrono::steady_clock::now().time_since_epoch(); + _T0 += std::chrono::duration_cast(dur); + ts.tv_sec = std::chrono::duration_cast(_T0).count(); + _T0 -= std::chrono::seconds(ts.tv_sec); + ts.tv_nsec = static_cast(_T0.count()); + + auto ret = ffrt_cond_timedwait(this, lk.mutex(), &ts); + if (ret == ffrt_success) { + return cv_status::no_timeout; + } + return cv_status::timeout; + } +}; +} // namespace ffrt +#endif diff --git a/zh-cn/native_sdk/ffrt/cpp/mutex.h b/zh-cn/native_sdk/ffrt/cpp/mutex.h new file mode 100755 index 00000000..7777a9a0 --- /dev/null +++ b/zh-cn/native_sdk/ffrt/cpp/mutex.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + /** + * @file mutex.h + * + * @brief 声明mutex提供的C++接口. + * + * @since 10 + * @version 1.0 + */ +#ifndef FFRT_API_CPP_MUTEX_H +#define FFRT_API_CPP_MUTEX_H +#include "c/mutex.h" + +namespace ffrt { +class mutex : public ffrt_mutex_t { +public: + mutex() + { + ffrt_mutex_init(this, nullptr); + } + + ~mutex() + { + ffrt_mutex_destroy(this); + } + + mutex(mutex const&) = delete; + void operator=(mutex const&) = delete; + + inline bool try_lock() + { + return ffrt_mutex_trylock(this) == ffrt_success ? true : false; + } + + inline void lock() + { + ffrt_mutex_lock(this); + } + + inline void unlock() + { + ffrt_mutex_unlock(this); + } +}; +} // namespace ffrt +#endif diff --git a/zh-cn/native_sdk/ffrt/cpp/queue.h b/zh-cn/native_sdk/ffrt/cpp/queue.h new file mode 100755 index 00000000..60e35a17 --- /dev/null +++ b/zh-cn/native_sdk/ffrt/cpp/queue.h @@ -0,0 +1,229 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file queue.h + * + * @brief 声明串行队列提供的C++接口. + * + * @since 10 + * @version 1.0 + */ +#ifndef FFRT_API_CPP_QUEUE_H +#define FFRT_API_CPP_QUEUE_H + +#include "c/queue.h" +#include "cpp/task.h" + +namespace ffrt { +class queue_attr : public ffrt_queue_attr_t { +public: + queue_attr() + { + ffrt_queue_attr_init(this); + } + + ~queue_attr() + { + ffrt_queue_attr_destroy(this); + } + + queue_attr(const queue_attr&) = delete; + queue_attr& operator=(const queue_attr&) = delete; + + + /** + * @brief 设置串行队列qos属性. + * + * @param attr qos属性值. + * @since 10 + * @version 1.0 + */ + inline queue_attr& qos(enum qos qos) + { + ffrt_queue_attr_set_qos(this, static_cast(qos)); + return *this; + } + + /** + * @brief 获取串行队列qos属性. + * + * @return 返回串行队列的qos属性 + * @since 10 + * @version 1.0 + */ + inline enum qos qos() const + { + return static_cast(ffrt_queue_attr_get_qos(this)); + } +}; + +class queue { +public: + queue(const char* name, const queue_attr& attr = {}) + { + queue_handle = ffrt_queue_create(ffrt_queue_serial, name, &attr); + } + + ~queue() + { + ffrt_queue_destroy(queue_handle); + } + + queue(queue const&) = delete; + void operator=(queue const&) = delete; + + /** + * @brief 提交一个任务到队列中调度执行. + * + * @param func 任务执行体函数闭包. + * @since 10 + * @version 1.0 + */ + inline void submit(std::function& func) + { + ffrt_queue_submit(queue_handle, create_function_wrapper(func, ffrt_function_kind_queue), nullptr); + } + + /** + * @brief 提交一个特定属性的任务到队列中调度执行. + * + * @param func 任务执行体函数闭包. + * @param attr 任务属性. + * @since 10 + * @version 1.0 + */ + inline void submit(std::function& func, const task_attr& attr) + { + ffrt_queue_submit(queue_handle, create_function_wrapper(func, ffrt_function_kind_queue), &attr); + } + + /** + * @brief 提交一个任务到队列中调度执行. + * + * @param func 任务执行体函数闭包. + * @since 10 + * @version 1.0 + */ + inline void submit(std::function&& func) + { + ffrt_queue_submit(queue_handle, create_function_wrapper(std::move(func), ffrt_function_kind_queue), nullptr); + } + + /** + * @brief 提交一个特定属性的任务到队列中调度执行. + * + * @param func 任务执行体函数闭包. + * @param attr 任务属性. + * @since 10 + * @version 1.0 + */ + inline void submit(std::function&& func, const task_attr& attr) + { + ffrt_queue_submit(queue_handle, create_function_wrapper(std::move(func), ffrt_function_kind_queue), &attr); + } + + /** + * @brief 提交一个任务到队列中调度执行并返回任务句柄. + * + * @param func 任务执行体函数闭包. + * @return 提交成功返回非空任务句柄 + 提交失败返回空指针 + * @since 10 + * @version 1.0 + */ + inline task_handle submit_h(std::function& func) + { + return ffrt_queue_submit_h(queue_handle, create_function_wrapper(func, ffrt_function_kind_queue), nullptr); + } + + /** + * @brief 提交一个特定属性的任务到队列中调度执行并返回任务句柄. + * + * @param func 任务执行体函数闭包. + * @param attr 任务属性. + * @return 提交成功返回非空任务句柄 + 提交失败返回空指针 + * @since 10 + * @version 1.0 + */ + inline task_handle submit_h(std::function& func, const task_attr& attr) + { + return ffrt_queue_submit_h(queue_handle, create_function_wrapper(func, ffrt_function_kind_queue), &attr); + } + + /** + * @brief 提交一个任务到队列中调度执行并返回任务句柄. + * + * @param func 任务执行体函数闭包. + * @return 提交成功返回非空任务句柄 + 提交失败返回空指针 + * @since 10 + * @version 1.0 + */ + inline task_handle submit_h(std::function&& func) + { + return ffrt_queue_submit_h( + queue_handle, create_function_wrapper(std::move(func), ffrt_function_kind_queue), nullptr); + } + + /** + * @brief 提交一个特定属性的任务到队列中调度执行并返回任务句柄. + * + * @param func 任务执行体函数闭包. + * @param attr 任务属性. + * @return 提交成功返回非空任务句柄 + 提交失败返回空指针 + * @since 10 + * @version 1.0 + */ + inline task_handle submit_h(std::function&& func, const task_attr& attr) + { + return ffrt_queue_submit_h( + queue_handle, create_function_wrapper(std::move(func), ffrt_function_kind_queue), &attr); + } + + /** + * @brief 取消任务. + * + * @param handle 任务句柄. + * @return 取消任务成功返回0 + 取消任务成功返回-1 + * @since 10 + * @version 1.0 + */ + inline int cancel(task_handle& handle) + { + return ffrt_queue_cancel(handle); + } + + /** + * @brief 等待任务执行完成. + * + * @param handle 任务句柄. + * @since 10 + * @version 1.0 + */ + inline void wait(task_handle& handle) + { + return ffrt_queue_wait(handle); + } + +private: + ffrt_queue_t queue_handle = nullptr; +}; +} // namespace ffrt + +#endif // FFRT_API_CPP_QUEUE_H \ No newline at end of file diff --git a/zh-cn/native_sdk/ffrt/cpp/sleep.h b/zh-cn/native_sdk/ffrt/cpp/sleep.h new file mode 100755 index 00000000..24df1eba --- /dev/null +++ b/zh-cn/native_sdk/ffrt/cpp/sleep.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file sleep.h + * + * @brief 声明sleep和yield C++接口. + * + * @since 10 + * @version 1.0 + */ +#ifndef FFRT_API_CPP_SLEEP_H +#define FFRT_API_CPP_SLEEP_H +#include +#include +#include "c/sleep.h" + +namespace ffrt { +namespace this_task { +static inline void yield() +{ + ffrt_yield(); +} + +template +inline void sleep_for(const std::chrono::duration<_Rep, _Period>& d) +{ + ffrt_usleep(std::chrono::duration_cast(d).count()); +} + +template +inline void sleep_until( + const std::chrono::time_point<_Clock, _Duration>& abs_time) +{ + sleep_for(abs_time.time_since_epoch() - _Clock::now().time_since_epoch()); +} +} // namespace this_task +} // namespace ffrt +#endif diff --git a/zh-cn/native_sdk/ffrt/cpp/task.h b/zh-cn/native_sdk/ffrt/cpp/task.h new file mode 100755 index 00000000..ae88d6e5 --- /dev/null +++ b/zh-cn/native_sdk/ffrt/cpp/task.h @@ -0,0 +1,736 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + /** + * @file task.h + * + * @brief 声明任务提供的C++接口. + * + * @since 10 + * @version 1.0 + */ +#ifndef FFRT_API_CPP_TASK_H +#define FFRT_API_CPP_TASK_H +#include +#include +#include +#include +#include "c/task.h" + +namespace ffrt { +class task_attr : public ffrt_task_attr_t { +public: + task_attr() + { + ffrt_task_attr_init(this); + } + + ~task_attr() + { + ffrt_task_attr_destroy(this); + } + + task_attr(const task_attr&) = delete; + task_attr& operator=(const task_attr&) = delete; + + /** + * @brief 设置任务名字. + * + * @param name 任务名字. + * @since 10 + * @version 1.0 + */ + inline task_attr& name(const char* name) + { + ffrt_task_attr_set_name(this, name); + return *this; + } + + /** + * @brief 获取任务名字. + * + * @return 返回任务名字. + * @since 10 + * @version 1.0 + */ + inline const char* name() const + { + return ffrt_task_attr_get_name(this); + } + + /** + * @brief 设置任务qos. + * + * @param qos qos类型. + * @since 10 + * @version 1.0 + */ + inline task_attr& qos(enum qos qos) + { + ffrt_task_attr_set_qos(this, static_cast(qos)); + return *this; + } + + /** + * @brief 获取任务qos. + * + * @return 返回任务qos. + * @since 10 + * @version 1.0 + */ + inline enum qos qos() const + { + return static_cast(ffrt_task_attr_get_qos(this)); + } + + /** + * @brief 设置任务延迟时间. + * + * @param delay_us 延迟时间,单位是微妙. + * @since 10 + * @version 1.0 + */ + inline task_attr& delay(uint64_t delay_us) + { + ffrt_task_attr_set_delay(this, delay_us); + return *this; + } + + /** + * @brief 获取任务延迟时间. + * + * @return 返回任务延迟时间. + * @since 10 + * @version 1.0 + */ + inline uint64_t delay() const + { + return ffrt_task_attr_get_delay(this); + } +}; + +class task_handle { +public: + task_handle() : p(nullptr) + { + } + task_handle(ffrt_task_handle_t p) : p(p) + { + } + + ~task_handle() + { + if (p) { + ffrt_task_handle_destroy(p); + } + } + + task_handle(task_handle const&) = delete; + void operator=(task_handle const&) = delete; + + inline task_handle(task_handle&& h) + { + *this = std::move(h); + } + + inline task_handle& operator=(task_handle&& h) + { + if (p) { + ffrt_task_handle_destroy(p); + } + p = h.p; + h.p = nullptr; + return *this; + } + + inline operator void* () const + { + return p; + } + +private: + ffrt_task_handle_t p = nullptr; +}; + +template +struct function { + template + function(ffrt_function_header_t h, CT&& c) : header(h), closure(std::forward(c)) {} + ffrt_function_header_t header; + T closure; +}; + +template +void exec_function_wrapper(void* t) +{ + auto f = reinterpret_cast>*>(t); + f->closure(); +} + +template +void destroy_function_wrapper(void* t) +{ + auto f = reinterpret_cast>*>(t); + f->closure = nullptr; +} + +template +inline ffrt_function_header_t* create_function_wrapper(T&& func, + ffrt_function_kind_t kind = ffrt_function_kind_general) +{ + using function_type = function>; + static_assert(sizeof(function_type) <= ffrt_auto_managed_function_storage_size, + "size of function must be less than ffrt_auto_managed_function_storage_size"); + + auto p = ffrt_alloc_auto_managed_function_storage_base(kind); + auto f = + new (p)function_type({ exec_function_wrapper, destroy_function_wrapper, { 0 } }, std::forward(func)); + return reinterpret_cast(f); +} + +/** + * @brief 提交无输入和输出依赖的任务调度执行. + * + * @param func 任务执行体函数闭包. + * @since 10 + * @version 1.0 + */ +static inline void submit(std::function&& func) +{ + return ffrt_submit_base(create_function_wrapper(std::move(func)), nullptr, nullptr, nullptr); +} + +/** + * @brief 提交有输入依赖无输出依赖的任务调度执行. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @since 10 + * @version 1.0 + */ +static inline void submit(std::function&& func, std::initializer_list in_deps) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.begin()}; + return ffrt_submit_base(create_function_wrapper(std::move(func)), &in, nullptr, nullptr); +} + +/** + * @brief 提交有输入输出依赖的任务调度执行. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @param out_deps 输出依赖. + * @since 10 + * @version 1.0 + */ +static inline void submit(std::function&& func, std::initializer_list in_deps, + std::initializer_list out_deps) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.begin()}; + ffrt_deps_t out{static_cast(out_deps.size()), out_deps.begin()}; + return ffrt_submit_base(create_function_wrapper(std::move(func)), &in, &out, nullptr); +} + +/** + * @brief 提交有输入输出依赖的特定属性的任务调度执行. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @param out_deps 输出依赖. + * @param attr 任务属性. + * @since 10 + * @version 1.0 + */ +static inline void submit(std::function&& func, std::initializer_list in_deps, + std::initializer_list out_deps, const task_attr& attr) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.begin()}; + ffrt_deps_t out{static_cast(out_deps.size()), out_deps.begin()}; + return ffrt_submit_base(create_function_wrapper(std::move(func)), &in, &out, &attr); +} + +/** + * @brief 提交有输入依赖无输出依赖的任务调度执行. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @since 10 + * @version 1.0 + */ +static inline void submit(std::function&& func, const std::vector& in_deps) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.data()}; + return ffrt_submit_base(create_function_wrapper(std::move(func)), &in, nullptr, nullptr); +} + +/** + * @brief 提交有输入输出依赖的任务调度执行. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @param out_deps 输出依赖. + * @since 10 + * @version 1.0 + */ +static inline void submit(std::function&& func, const std::vector& in_deps, + const std::vector& out_deps) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.data()}; + ffrt_deps_t out{static_cast(out_deps.size()), out_deps.data()}; + return ffrt_submit_base(create_function_wrapper(std::move(func)), &in, &out, nullptr); +} + +/** + * @brief 提交有输入输出依赖的特定属性的任务调度执行. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @param out_deps 输出依赖. + * @param attr 任务属性. + * @since 10 + * @version 1.0 + */ +static inline void submit(std::function&& func, const std::vector& in_deps, + const std::vector& out_deps, const task_attr& attr) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.data()}; + ffrt_deps_t out{static_cast(out_deps.size()), out_deps.data()}; + return ffrt_submit_base(create_function_wrapper(std::move(func)), &in, &out, &attr); +} + +/** + * @brief 提交无输入和输出依赖的任务调度执行. + * + * @param func 任务执行体函数闭包. + * @since 10 + * @version 1.0 + */ +static inline void submit(const std::function& func) +{ + return ffrt_submit_base(create_function_wrapper(func), nullptr, nullptr, nullptr); +} + +/** + * @brief 提交有输入依赖无输出依赖的任务调度执行. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @since 10 + * @version 1.0 + */ +static inline void submit(const std::function& func, std::initializer_list in_deps) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.begin()}; + return ffrt_submit_base(create_function_wrapper(func), &in, nullptr, nullptr); +} + +/** + * @brief 提交有输入输出依赖的任务调度执行. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @param out_deps 输出依赖. + * @since 10 + * @version 1.0 + */ +static inline void submit(const std::function& func, std::initializer_list in_deps, + std::initializer_list out_deps) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.begin()}; + ffrt_deps_t out{static_cast(out_deps.size()), out_deps.begin()}; + return ffrt_submit_base(create_function_wrapper(func), &in, &out, nullptr); +} + +/** + * @brief 提交有输入输出依赖的特定属性的任务调度执行. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @param out_deps 输出依赖. + * @param attr 任务属性. + * @since 10 + * @version 1.0 + */ +static inline void submit(const std::function& func, std::initializer_list in_deps, + std::initializer_list out_deps, const task_attr& attr) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.begin()}; + ffrt_deps_t out{static_cast(out_deps.size()), out_deps.begin()}; + return ffrt_submit_base(create_function_wrapper(func), &in, &out, &attr); +} + +/** + * @brief 提交有输入依赖无输出依赖的任务调度执行. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @since 10 + * @version 1.0 + */ +static inline void submit(const std::function& func, const std::vector& in_deps) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.data()}; + return ffrt_submit_base(create_function_wrapper(func), &in, nullptr, nullptr); +} + +/** + * @brief 提交有输入输出依赖的任务调度执行. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @param out_deps 输出依赖. + * @since 10 + * @version 1.0 + */ +static inline void submit(const std::function& func, const std::vector& in_deps, + const std::vector& out_deps) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.data()}; + ffrt_deps_t out{static_cast(out_deps.size()), out_deps.data()}; + return ffrt_submit_base(create_function_wrapper(func), &in, &out, nullptr); +} + +/** + * @brief 提交有输入输出依赖的特定属性的任务调度执行. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @param out_deps 输出依赖. + * @param attr 任务属性. + * @since 10 + * @version 1.0 + */ +static inline void submit(const std::function& func, const std::vector& in_deps, + const std::vector& out_deps, const task_attr& attr) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.data()}; + ffrt_deps_t out{static_cast(out_deps.size()), out_deps.data()}; + return ffrt_submit_base(create_function_wrapper(func), &in, &out, &attr); +} + +/** + * @brief 提交无输入和输出依赖的任务调度执行并返回任务句柄. + * + * @param func 任务执行体函数闭包. + * @return 提交任务成功返回非空任务句柄, + 提交任务失败返回空指针. + * @since 10 + * @version 1.0 + */ +static inline task_handle submit_h(std::function&& func) +{ + return ffrt_submit_h_base(create_function_wrapper(std::move(func)), nullptr, nullptr, nullptr); +} + +/** + * @brief 提交有输入依赖无输出依赖的任务调度执行并返回任务句柄. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @return 提交任务成功返回非空任务句柄, + 提交任务失败返回空指针. + * @since 10 + * @version 1.0 + */ +static inline task_handle submit_h(std::function&& func, std::initializer_list in_deps) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.begin()}; + return ffrt_submit_h_base(create_function_wrapper(std::move(func)), &in, nullptr, nullptr); +} + +/** + * @brief 提交有输入输出依赖的任务调度执行并返回任务句柄. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @param out_deps 输出依赖. + * @return 提交任务成功返回非空任务句柄, + 提交任务失败返回空指针. + * @since 10 + * @version 1.0 + */ +static inline task_handle submit_h(std::function&& func, std::initializer_list in_deps, + std::initializer_list out_deps) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.begin()}; + ffrt_deps_t out{static_cast(out_deps.size()), out_deps.begin()}; + return ffrt_submit_h_base(create_function_wrapper(std::move(func)), &in, &out, nullptr); +} + +/** + * @brief 提交有输入输出依赖的特定属性的任务调度执行并返回任务句柄. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @param out_deps 输出依赖. + * @param attr 任务属性. + * @return 提交任务成功返回非空任务句柄, + 提交任务失败返回空指针. + * @since 10 + * @version 1.0 + */ +static inline task_handle submit_h(std::function&& func, std::initializer_list in_deps, + std::initializer_list out_deps, const task_attr& attr) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.begin()}; + ffrt_deps_t out{static_cast(out_deps.size()), out_deps.begin()}; + return ffrt_submit_h_base(create_function_wrapper(std::move(func)), &in, &out, &attr); +} + +/** + * @brief 提交有输入依赖无输出依赖的任务调度执行并返回任务句柄. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @return 提交任务成功返回非空任务句柄, + 提交任务失败返回空指针. + * @since 10 + * @version 1.0 + */ +static inline task_handle submit_h(std::function&& func, const std::vector& in_deps) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.data()}; + return ffrt_submit_h_base(create_function_wrapper(std::move(func)), &in, nullptr, nullptr); +} + +/** + * @brief 提交有输入输出依赖的任务调度执行并返回任务句柄. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @param out_deps 输出依赖. + * @return 提交任务成功返回非空任务句柄, + 提交任务失败返回空指针. + * @since 10 + * @version 1.0 + */ +static inline task_handle submit_h(std::function&& func, const std::vector& in_deps, + const std::vector& out_deps) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.data()}; + ffrt_deps_t out{static_cast(out_deps.size()), out_deps.data()}; + return ffrt_submit_h_base(create_function_wrapper(std::move(func)), &in, &out, nullptr); +} + +/** + * @brief 提交有输入输出依赖的特定属性的任务调度执行并返回任务句柄. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @param out_deps 输出依赖. + * @param attr 任务属性. + * @return 提交任务成功返回非空任务句柄, + 提交任务失败返回空指针. + * @since 10 + * @version 1.0 + */ +static inline task_handle submit_h(std::function&& func, const std::vector& in_deps, + const std::vector& out_deps, const task_attr& attr) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.data()}; + ffrt_deps_t out{static_cast(out_deps.size()), out_deps.data()}; + return ffrt_submit_h_base(create_function_wrapper(std::move(func)), &in, &out, &attr); +} + +/** + * @brief 提交无输入和输出依赖的任务调度执行并返回任务句柄. + * + * @param func 任务执行体函数闭包. + * @return 提交任务成功返回非空任务句柄, + 提交任务失败返回空指针. + * @since 10 + * @version 1.0 + */ +static inline task_handle submit_h(const std::function& func) +{ + return ffrt_submit_h_base(create_function_wrapper(func), nullptr, nullptr, nullptr); +} + +/** + * @brief 提交有输入依赖无输出依赖的任务调度执行并返回任务句柄. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @return 提交任务成功返回非空任务句柄, + 提交任务失败返回空指针. + * @since 10 + * @version 1.0 + */ +static inline task_handle submit_h(const std::function& func, std::initializer_list in_deps) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.begin()}; + return ffrt_submit_h_base(create_function_wrapper(func), &in, nullptr, nullptr); +} + +/** + * @brief 提交有输入输出依赖的任务调度执行并返回任务句柄. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @param out_deps 输出依赖. + * @return 提交任务成功返回非空任务句柄, + 提交任务失败返回空指针. + * @since 10 + * @version 1.0 + */ +static inline task_handle submit_h(const std::function& func, std::initializer_list in_deps, + std::initializer_list out_deps) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.begin()}; + ffrt_deps_t out{static_cast(out_deps.size()), out_deps.begin()}; + return ffrt_submit_h_base(create_function_wrapper(func), &in, &out, nullptr); +} + +/** + * @brief 提交有输入输出依赖的特定属性的任务调度执行并返回任务句柄. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @param out_deps 输出依赖. + * @param attr 任务属性. + * @return 提交任务成功返回非空任务句柄, + 提交任务失败返回空指针. + * @since 10 + * @version 1.0 + */ +static inline task_handle submit_h(const std::function& func, std::initializer_list in_deps, + std::initializer_list out_deps, const task_attr& attr) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.begin()}; + ffrt_deps_t out{static_cast(out_deps.size()), out_deps.begin()}; + return ffrt_submit_h_base(create_function_wrapper(func), &in, &out, &attr); +} + +/** + * @brief 提交有输入依赖无输出依赖的任务调度执行并返回任务句柄. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @return 提交任务成功返回非空任务句柄, + 提交任务失败返回空指针. + * @since 10 + * @version 1.0 + */ +static inline task_handle submit_h(const std::function& func, const std::vector& in_deps) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.data()}; + return ffrt_submit_h_base(create_function_wrapper(func), &in, nullptr, nullptr); +} + +/** + * @brief 提交有输入输出依赖的任务调度执行并返回任务句柄. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @param out_deps 输出依赖. + * @return 提交任务成功返回非空任务句柄, + 提交任务失败返回空指针. + * @since 10 + * @version 1.0 + */ +static inline task_handle submit_h(const std::function& func, const std::vector& in_deps, + const std::vector& out_deps) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.data()}; + ffrt_deps_t out{static_cast(out_deps.size()), out_deps.data()}; + return ffrt_submit_h_base(create_function_wrapper(func), &in, &out, nullptr); +} + +/** + * @brief 提交有输入输出依赖的特定属性的任务调度执行并返回任务句柄. + * + * @param func 任务执行体函数闭包. + * @param in_deps 输入依赖. + * @param out_deps 输出依赖. + * @param attr 任务属性. + * @return 提交任务成功返回非空任务句柄, + 提交任务失败返回空指针. + * @since 10 + * @version 1.0 + */ +static inline task_handle submit_h(const std::function& func, const std::vector& in_deps, + const std::vector& out_deps, const task_attr& attr) +{ + ffrt_deps_t in{static_cast(in_deps.size()), in_deps.data()}; + ffrt_deps_t out{static_cast(out_deps.size()), out_deps.data()}; + return ffrt_submit_h_base(create_function_wrapper(func), &in, &out, &attr); +} + +/** + * @brief 跳过指定任务. + * + * @param handle 任务句柄. + * @return 跳过指定任务成功返回0, + 跳过指定任务失败返回-1. + * @since 10 + * @version 1.0 + */ +static inline int skip(task_handle &handle) +{ + return ffrt_skip(handle); +} + +/** + * @brief 等待之前所有提交任务完成,当前任务开始执行. + * + * @since 10 + * @version 1.0 + */ +static inline void wait() +{ + ffrt_wait(); +} + +/** + * @brief 等待依赖的任务完成,当前任务开始执行. + * + * @param deps 依赖的指针. + * @since 10 + * @version 1.0 + */ +static inline void wait(std::initializer_list deps) +{ + ffrt_deps_t d{static_cast(deps.size()), deps.begin()}; + ffrt_wait_deps(&d); +} + +/** + * @brief 等待依赖的任务完成,当前任务开始执行. + * + * @param deps 依赖的指针. + * @since 10 + * @version 1.0 + */ +static inline void wait(const std::vector& deps) +{ + ffrt_deps_t d{static_cast(deps.size()), deps.data()}; + ffrt_wait_deps(&d); +} + +namespace this_task { +/** + * @brief 获取当前任务id. + * + * @return 返回任务id. + * @since 10 + * @version 1.0 + */ +static inline uint64_t get_id() +{ + return ffrt_this_task_get_id(); +} +} // namespace this_task +} // namespace ffrt +#endif diff --git a/zh-cn/native_sdk/ffrt/ffrt.h b/zh-cn/native_sdk/ffrt/ffrt.h new file mode 100755 index 00000000..e20832e4 --- /dev/null +++ b/zh-cn/native_sdk/ffrt/ffrt.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef FFRT_API_FFRT_H +#define FFRT_API_FFRT_H +#ifdef __cplusplus +#include "cpp/task.h" +#include "cpp/mutex.h" +#include "cpp/condition_variable.h" +#include "cpp/sleep.h" +#include "cpp/thread.h" +#include "cpp/future.h" +#include "cpp/queue.h" +#else +#include "c/task.h" +#include "c/mutex.h" +#include "c/condition_variable.h" +#include "c/sleep.h" +#include "c/thread.h" +#include "c/queue.h" +#endif +#endif -- Gitee