diff --git a/resourceschedule/ffrt/BUILD.gn b/resourceschedule/ffrt/BUILD.gn index f9ad3084ea728edb153ca2498c4319845cf7fbfe..0e48ac34e8c364d7830b42ff57c733b5a1402973 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") @@ -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", diff --git a/resourceschedule/ffrt/c/fiber.h b/resourceschedule/ffrt/c/fiber.h new file mode 100644 index 0000000000000000000000000000000000000000..c5096c38ad1463d707134d88cca289cca5a7eef4 --- /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 7fc8c6ed9ffbcd36287417236ddc23ba43a4cf84..b91ceeb5162b823d3de363081a248103411daf74 100644 --- a/resourceschedule/ffrt/c/type_def.h +++ b/resourceschedule/ffrt/c/type_def.h @@ -128,6 +128,25 @@ 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; /** @@ -302,6 +321,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. * diff --git a/resourceschedule/ffrt/ffrt.ndk.json b/resourceschedule/ffrt/ffrt.ndk.json index 84474b74e9e78a4696851d5e8fb3a719875d21b7..3ab20c1207d048ce613800956e2b5aaeba381411 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" },