From 7cdc362e9a2999e3bb091efc06d54938953bc115 Mon Sep 17 00:00:00 2001 From: wangyulie Date: Fri, 13 Jun 2025 10:30:48 +0800 Subject: [PATCH 1/2] add fiber --- resourceschedule/ffrt/c/fiber.h | 71 ++++++++++++++++++++++++++++++ resourceschedule/ffrt/c/type_def.h | 30 ++++++++++++- 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 resourceschedule/ffrt/c/fiber.h diff --git a/resourceschedule/ffrt/c/fiber.h b/resourceschedule/ffrt/c/fiber.h new file mode 100644 index 000000000..c5096c38a --- /dev/null +++ b/resourceschedule/ffrt/c/fiber.h @@ -0,0 +1,71 @@ +/* + * 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 Provides FFRT C APIs. + * + * @since 20 + */ + +/** + * @file fiber.h + * + * @brief Declares the fiber interfaces in 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 Initializes a fiber. + * + * This function initializes a fiber structure, preparing it for execution. + * + * @param fiber Indicates the pointer to the fiber structure to be initialized. + * @param func Indicates the entry point function that the fiber will execute. + * @param arg Indicates the argument to be passed to the entry point function. + * @param stack Indicates the pointer to the memory region to be used as the fiber's stack. + * @param stack_size Indicates the size of the stack in bytes. + * @return Returns ffrt_success if the fiber is initialized; + returns ffrt_error otherwise. + * @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 Switch execution context between two fibers. + * + * Switches the execution context by saving the current context into the fiber specified + * by @c from and restoring the context from the fiber specified by @c to. + * + * @param from Indicates the pointer to the fiber into which the current context will be saved. + * @param to Indicates the pointer to the fiber from which the context will be restored. + * @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/resourceschedule/ffrt/c/type_def.h b/resourceschedule/ffrt/c/type_def.h index 7fc8c6ed9..3afb6cab9 100644 --- a/resourceschedule/ffrt/c/type_def.h +++ b/resourceschedule/ffrt/c/type_def.h @@ -128,8 +128,26 @@ typedef enum { * @since 18 */ ffrt_rwlock_storage_size = 64, + /** Fiber storage size. + * + * This constant defines the fiber storage size. + * The actual value depends on the target architecture: + * - __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; - /** * @brief Enumerates the task types. * @@ -302,6 +320,16 @@ typedef struct { uint32_t storage[(ffrt_cond_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; } ffrt_cond_t; +/** + * @brief Defines the fiber structure. + * + * @since 20 + */ +typedef struct { + /** An array of uint32_t used to store the fiber. */ + uintptr_t storage[ffrt_fiber_storage_size]; +} ffrt_fiber_t; + /** * @brief Defines the poller callback function type. * -- Gitee From 30ac49db62dfff93f09f1a1ef025d717aae7737d Mon Sep 17 00:00:00 2001 From: wangyulie Date: Fri, 13 Jun 2025 10:30:48 +0800 Subject: [PATCH 2/2] add fiber Signed-off-by: wangyulie --- resourceschedule/ffrt/c/fiber.h | 70 ++++++++++++++++++++++++++++++ resourceschedule/ffrt/c/type_def.h | 30 ++++++++++++- 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 resourceschedule/ffrt/c/fiber.h diff --git a/resourceschedule/ffrt/c/fiber.h b/resourceschedule/ffrt/c/fiber.h new file mode 100644 index 000000000..d81138814 --- /dev/null +++ b/resourceschedule/ffrt/c/fiber.h @@ -0,0 +1,70 @@ +/* + * 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 Provides FFRT C APIs. + * + * @since 20 + */ + +/** + * @file fiber.h + * + * @brief Declares the fiber interfaces in 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 Initializes a fiber. + * + * This function initializes a fiber structure, preparing it for execution. + * + * @param fiber Indicates the pointer to the fiber structure to be initialized. + * @param func Indicates the entry point function that the fiber will execute. + * @param arg Indicates the argument to be passed to the entry point function. + * @param stack Indicates the pointer to the memory region to be used as the fiber's stack. + * @param stack_size Indicates the size of the stack in bytes. + * @return Returns ffrt_success if the fiber is initialized; + returns ffrt_error otherwise. + * @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 Switch execution context between two fibers. + * + * Switches the execution context by saving the current context into the fiber specified + * by @c from and restoring the context from the fiber specified by @c to. + * + * @param from Indicates the pointer to the fiber into which the current context will be saved. + * @param to Indicates the pointer to the fiber from which the context will be restored. + * @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/resourceschedule/ffrt/c/type_def.h b/resourceschedule/ffrt/c/type_def.h index 7fc8c6ed9..3afb6cab9 100644 --- a/resourceschedule/ffrt/c/type_def.h +++ b/resourceschedule/ffrt/c/type_def.h @@ -128,8 +128,26 @@ typedef enum { * @since 18 */ ffrt_rwlock_storage_size = 64, + /** Fiber storage size. + * + * This constant defines the fiber storage size. + * The actual value depends on the target architecture: + * - __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; - /** * @brief Enumerates the task types. * @@ -302,6 +320,16 @@ typedef struct { uint32_t storage[(ffrt_cond_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; } ffrt_cond_t; +/** + * @brief Defines the fiber structure. + * + * @since 20 + */ +typedef struct { + /** An array of uint32_t used to store the fiber. */ + uintptr_t storage[ffrt_fiber_storage_size]; +} ffrt_fiber_t; + /** * @brief Defines the poller callback function type. * -- Gitee