From 52cd71ddc7e8a9ddbde3ca5f9c07a132ac599214 Mon Sep 17 00:00:00 2001 From: huanghf Date: Thu, 3 Jul 2025 14:14:10 +0800 Subject: [PATCH] update doc Signed-off-by: huanghf --- .../ffrt/ffrt-api-guideline-c.md | 58 ++++++++++++++++++ .../ffrt/ffrt-api-guideline-c.md | 59 +++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/en/application-dev/ffrt/ffrt-api-guideline-c.md b/en/application-dev/ffrt/ffrt-api-guideline-c.md index 11a4639bf85..1b5640373da 100644 --- a/en/application-dev/ffrt/ffrt-api-guideline-c.md +++ b/en/application-dev/ffrt/ffrt-api-guideline-c.md @@ -2361,3 +2361,61 @@ Description return 0; } ``` + +## Fiber process + +### ffrt_fiber_t + +#### Declaration + +```c +struct ffrt_fiber_t; +``` + +#### Description + +`ffrt_fiber_t` is a fiber storage entity type used to store and restore execution context. + +#### Method + +##### ffrt_fiber_init + +Declaration + +```c +FFRT_C_API int ffrt_fiber_init(ffrt_fiber_t* fiber, void(*func)(void*), void* arg, void* stack, size_t stack_size); +``` + +Parameters + +- `fiber`: Fiber pointer. +- `func`: The function pointer entry at fiber startup. +- `arg`: Function parameter at fiber startup. +- `stack`: The starting address of the stack space used by the fiber during runtime. +- `stack_size`: The size of the fiber stack, measured in bytes. + +Return Values + +- Initialize successfully and return `ffrt_success`; otherwise, return `ffrt_error`. The common reason is that the `stack_size` does not meet the minimum stack space limit, which varies across platforms. It is recommended to set a stack space of 4KB or more. + +Description + +- This function initializes the fiber by passing the function pointer, parameters for starting the fiber, and the runtime stack space. The caller manages the fiber stack's lifecycle. + +##### ffrt_fiber_switch + +Declaration + +```c +FFRT_C_API void ffrt_fiber_switch(ffrt_fiber_t* from, ffrt_fiber_t* to); +``` + +Parameters + +- `from`: The thread calling this function will pause the execution of the current task and save the current context to the fiber pointed to by `from`. +- `to`: Restore the fiber pointed to by `to` to the current context, and the thread calling this function will execute the task corresponding to `to`. + +Description + +- Switching the fiber context, the thread calling this function will pause the execution of the current task, save the current context to the fiber pointed to by `from`, and restore the fiber pointed to by `to` to the current context, executing the task corresponding to `to`. +- Note: This interface cannot determine the validity of `from` and `to`. The caller needs to verify the address validity themselves, otherwise it will cause the process to crash. diff --git a/zh-cn/application-dev/ffrt/ffrt-api-guideline-c.md b/zh-cn/application-dev/ffrt/ffrt-api-guideline-c.md index 41b4ba82fa9..2de3fc883e2 100644 --- a/zh-cn/application-dev/ffrt/ffrt-api-guideline-c.md +++ b/zh-cn/application-dev/ffrt/ffrt-api-guideline-c.md @@ -2360,3 +2360,62 @@ FFRT_C_API int ffrt_loop_timer_stop(ffrt_loop_t loop, ffrt_timer_t handle); return 0; } ``` + +## 纤程 + +### ffrt_fiber_t + +#### 声明 + +```c +struct ffrt_fiber_t; +``` + +#### 描述 + +- 纤程是一种轻量级的用户态线程,用于在用户空间内实现高效的任务调度和上下文切换。 +- `ffrt_fiber_t`为纤程存储实体类型,用于保存和恢复执行上下文。 + +#### 方法 + +##### ffrt_fiber_init + +声明 + +```c +FFRT_C_API int ffrt_fiber_init(ffrt_fiber_t* fiber, void(*func)(void*), void* arg, void* stack, size_t stack_size); +``` + +参数 + +- `fiber`:纤程指针。 +- `func`:纤程启动时的函数指针入口。 +- `arg`:纤程启动时的函数入参。 +- `stack`:纤程运行时使用的栈空间起始地址。 +- `stack_size`:纤程栈大小,单位为字节。 + +返回值 + +- 初始化成功返回`ffrt_success`,否则返回`ffrt_error`。返回错误的常见原因是`stack_size`不满足最小栈空间限制(不同平台存在差异),建议设置栈空间大小为4KB或以上。 + +描述 + +- 该函数用于初始化纤程,需要传入启动纤程的函数指针和入参,以及运行时使用的栈空间,纤程不管理任何的内存,栈的生命周期由调用方管理。 + +##### ffrt_fiber_switch + +声明 + +```c +FFRT_C_API void ffrt_fiber_switch(ffrt_fiber_t* from, ffrt_fiber_t* to); +``` + +参数 + +- `from`:调用该函数的线程会暂停当前任务的执行,并保存当前上下文到`from`指向的纤程。 +- `to`:将`to`指向的纤程恢复到当前上下文,调用该函数的线程将执行`to`对应的任务。 + +描述 + +- 切换纤程上下文时,调用该函数的线程会暂停当前任务,保存上下文到`from`纤程,并恢复`to`纤程上下文,执行`to`对应的任务。 +- 注意:本接口不校验对`from`、`to`的有效性,调用方需自行校验地址有效性,否则会导致该进程崩溃。 -- Gitee