diff --git a/zh-cn/native_sdk/resourceschedule/ffrt/c/fiber.h b/zh-cn/native_sdk/resourceschedule/ffrt/c/fiber.h new file mode 100644 index 0000000000000000000000000000000000000000..4dacdd10155ef4b4f05e78c1af06f2e3364a97fc --- /dev/null +++ b/zh-cn/native_sdk/resourceschedule/ffrt/c/fiber.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2025 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. + */ + +/** + * @addtogroup FFRT + * @{ + * + * @brief FFRT(Function Flow运行时)是支持Function Flow编程模型的软件运行时库,用于调度执行开发者基于Function Flow编程模型开发的应用。 + * + * @since 20 + */ + +/** + * @file fiber.h + * + * @brief 纤程是一种轻量级的用户态线程,用于在用户空间内实现高效的任务调度和上下文切换,此为声明纤程的C接口。 + * + * @library libffrt.z.so + * @kit FunctionFlowRuntimeKit + * @syscap SystemCapability.Resourceschedule.Ffrt.Core + * @since 20 + */ + +#ifndef FFRT_API_C_FIBER_H +#define FFRT_API_C_FIBER_H + +#include "type_def.h" + +/** + * @brief 纤程初始化函数,此函数初始化纤程实例,该实例可以存储上下文。 + * + * @param fiber 指向要初始化的纤程的指针, 具体可参考{@link ffrt_fiber_t}。 + * @param func 纤程切换后所要执行的方法。 + * @param arg 纤程切换后所要执行方法的入参。 + * @param stack 纤程堆栈内存指针。 + * @param stack_size 纤程堆栈大小, 具体可参考{@link ffrt_storage_size_t}。 + * @return 初始化成功返回ffrt_success,否则返回ffrt_error。 + * @since 20 + */ +FFRT_C_API int ffrt_fiber_init(ffrt_fiber_t* fiber, void(*func)(void*), void* arg, void* stack, size_t stack_size); + + +/** + * @brief 纤程切换函数,调用该函数的线程会暂停当前任务的执行,并将当前上下文保存到from纤程中,同时恢复to纤程中的上下文。 + * + * @param from 将要保存的纤程指针。 + * @param to 将要恢复的纤程指针。 + * @since 20 + */ +FFRT_C_API void ffrt_fiber_switch(ffrt_fiber_t* from, ffrt_fiber_t* to); + +#endif // FFRT_API_C_FIBER_H +/** @} */ \ No newline at end of file diff --git a/zh-cn/native_sdk/resourceschedule/ffrt/c/type_def.h b/zh-cn/native_sdk/resourceschedule/ffrt/c/type_def.h index be68f266fc88b5ac7d0e5dbf1fd65e9f093ec603..26cb0f690bfb93fa9dd816a1ea94d7b50cd58841 100644 --- a/zh-cn/native_sdk/resourceschedule/ffrt/c/type_def.h +++ b/zh-cn/native_sdk/resourceschedule/ffrt/c/type_def.h @@ -129,6 +129,24 @@ typedef enum { * @since 18 */ ffrt_rwlock_storage_size = 64, + /** + * + * 纤程在不同平台下所占大小,单位:Byte。 + * - AArch64: 22 + * - ARM: 64 + * - X86-64: 8 + * + * @since 20 + */ +#if defined(__aarch64__) + ffrt_fiber_storage_size = 22, +#elif defined(__arm__) + ffrt_fiber_storage_size = 64, +#elif defined(__x86_64__) + ffrt_fiber_storage_size = 8, +#else +#error "unsupported architecture" +#endif } ffrt_storage_size_t; /** @@ -303,6 +321,16 @@ typedef struct { uint32_t storage[(ffrt_cond_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; } ffrt_cond_t; +/** + * @brief 纤程结构。 + * + * @since 20 + */ +typedef struct { + /** 纤程上下文所占空间。 */ + uintptr_t storage[ffrt_fiber_storage_size]; +} ffrt_fiber_t; + /** * @brief poller回调函数定义。 *