From 3ec67681e8548cbf51f84697c77a48ab7d728686 Mon Sep 17 00:00:00 2001 From: wangyulie Date: Fri, 13 Jun 2025 10:42:42 +0800 Subject: [PATCH 1/6] add fiber Signed-off-by: wangyulie --- 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 785d635d38a2c6b475134b8002acdf9bc1bc39bb Mon Sep 17 00:00:00 2001 From: wangyulie <549976081@qq.com> Date: Fri, 13 Jun 2025 02:44:22 +0000 Subject: [PATCH 2/6] update resourceschedule/ffrt/c/type_def.h. Signed-off-by: wangyulie <549976081@qq.com> --- resourceschedule/ffrt/c/type_def.h | 1 + 1 file changed, 1 insertion(+) diff --git a/resourceschedule/ffrt/c/type_def.h b/resourceschedule/ffrt/c/type_def.h index 3afb6cab9..b91ceeb51 100644 --- a/resourceschedule/ffrt/c/type_def.h +++ b/resourceschedule/ffrt/c/type_def.h @@ -148,6 +148,7 @@ typedef enum { #error "unsupported architecture" #endif } ffrt_storage_size_t; + /** * @brief Enumerates the task types. * -- Gitee From 6274562f4aa0d7d2dd3bc797966ff27a968d536c Mon Sep 17 00:00:00 2001 From: wangyulie Date: Mon, 16 Jun 2025 11:03:54 +0800 Subject: [PATCH 3/6] add fiber interface Signed-off-by: wangyulie --- resourceschedule/ffrt/BUILD.gn | 4 +++- resourceschedule/ffrt/ffrt.ndk.json | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/resourceschedule/ffrt/BUILD.gn b/resourceschedule/ffrt/BUILD.gn index f9ad3084e..58b20d09f 100644 --- a/resourceschedule/ffrt/BUILD.gn +++ b/resourceschedule/ffrt/BUILD.gn @@ -9,7 +9,7 @@ # 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. +# limitations under the License. import("//build/ohos.gni") import("//build/ohos/ndk/ndk.gni") @@ -26,6 +26,7 @@ ohos_ndk_headers("ffrt_header") { "c/task.h", "c/timer.h", "c/type_def.h", + "c/fiber.h", ] } @@ -43,5 +44,6 @@ ohos_ndk_library("libffrt_ndk") { "ffrt/task.h", "ffrt/timer.h", "ffrt/type_def.h", + "ffrt/fiber.h", ] } diff --git a/resourceschedule/ffrt/ffrt.ndk.json b/resourceschedule/ffrt/ffrt.ndk.json index 84474b74e..3ab20c120 100644 --- a/resourceschedule/ffrt/ffrt.ndk.json +++ b/resourceschedule/ffrt/ffrt.ndk.json @@ -115,6 +115,14 @@ "first_introduced": "12", "name": "ffrt_task_handle_dec_ref" }, + { + "first_introduced": "20", + "name": "ffrt_fiber_init" + }, + { + "first_introduced": "20", + "name": "ffrt_fiber_switch" + }, { "name": "ffrt_task_handle_destroy" }, { "name": "ffrt_wait_deps" }, { "name": "ffrt_wait" }, -- Gitee From 03816dc383cbdfc674bd6ba82323a5b4113c5912 Mon Sep 17 00:00:00 2001 From: wangyulie Date: Mon, 16 Jun 2025 11:30:23 +0800 Subject: [PATCH 4/6] add fiber Signed-off-by: wangyulie --- resourceschedule/ffrt/BUILD.gn | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resourceschedule/ffrt/BUILD.gn b/resourceschedule/ffrt/BUILD.gn index 58b20d09f..20a274721 100644 --- a/resourceschedule/ffrt/BUILD.gn +++ b/resourceschedule/ffrt/BUILD.gn @@ -18,6 +18,7 @@ ohos_ndk_headers("ffrt_header") { dest_dir = "$ndk_headers_out_dir/ffrt" sources = [ "c/condition_variable.h", + "c/fiber.h", "c/loop.h", "c/mutex.h", "c/queue.h", @@ -26,7 +27,6 @@ ohos_ndk_headers("ffrt_header") { "c/task.h", "c/timer.h", "c/type_def.h", - "c/fiber.h", ] } @@ -36,6 +36,7 @@ ohos_ndk_library("libffrt_ndk") { system_capability = "SystemCapability.Resourceschedule.Ffrt.Core" system_capability_headers = [ "ffrt/condition_variable.h", + "ffrt/fiber.h", "ffrt/loop.h", "ffrt/mutex.h", "ffrt/queue.h", @@ -44,6 +45,5 @@ ohos_ndk_library("libffrt_ndk") { "ffrt/task.h", "ffrt/timer.h", "ffrt/type_def.h", - "ffrt/fiber.h", ] } -- Gitee From 7497eca5714611078a790cd8db377d1e6ab4ccd1 Mon Sep 17 00:00:00 2001 From: wangyulie Date: Mon, 16 Jun 2025 11:55:19 +0800 Subject: [PATCH 5/6] add fiber Signed-off-by: wangyulie --- resourceschedule/ffrt/BUILD.gn | 2 -- 1 file changed, 2 deletions(-) diff --git a/resourceschedule/ffrt/BUILD.gn b/resourceschedule/ffrt/BUILD.gn index 20a274721..4d327443a 100644 --- a/resourceschedule/ffrt/BUILD.gn +++ b/resourceschedule/ffrt/BUILD.gn @@ -18,7 +18,6 @@ ohos_ndk_headers("ffrt_header") { dest_dir = "$ndk_headers_out_dir/ffrt" sources = [ "c/condition_variable.h", - "c/fiber.h", "c/loop.h", "c/mutex.h", "c/queue.h", @@ -36,7 +35,6 @@ ohos_ndk_library("libffrt_ndk") { system_capability = "SystemCapability.Resourceschedule.Ffrt.Core" system_capability_headers = [ "ffrt/condition_variable.h", - "ffrt/fiber.h", "ffrt/loop.h", "ffrt/mutex.h", "ffrt/queue.h", -- Gitee From 171ac90d6367504a0104228c466855b8de44e5aa Mon Sep 17 00:00:00 2001 From: wangyulie Date: Mon, 16 Jun 2025 13:57:44 +0800 Subject: [PATCH 6/6] add fiber Signed-off-by: wangyulie --- resourceschedule/ffrt/BUILD.gn | 2 ++ 1 file changed, 2 insertions(+) diff --git a/resourceschedule/ffrt/BUILD.gn b/resourceschedule/ffrt/BUILD.gn index 4d327443a..0e48ac34e 100644 --- a/resourceschedule/ffrt/BUILD.gn +++ b/resourceschedule/ffrt/BUILD.gn @@ -18,6 +18,7 @@ ohos_ndk_headers("ffrt_header") { dest_dir = "$ndk_headers_out_dir/ffrt" sources = [ "c/condition_variable.h", + "c/fiber.h", "c/loop.h", "c/mutex.h", "c/queue.h", @@ -35,6 +36,7 @@ ohos_ndk_library("libffrt_ndk") { system_capability = "SystemCapability.Resourceschedule.Ffrt.Core" system_capability_headers = [ "ffrt/condition_variable.h", + "ffrt/fiber.h", "ffrt/loop.h", "ffrt/mutex.h", "ffrt/queue.h", -- Gitee