From c3ff59188160d5b4b75428e571c99215c70c79d3 Mon Sep 17 00:00:00 2001 From: linyu Date: Wed, 26 Jul 2023 09:54:30 +0800 Subject: [PATCH 1/2] multitask support Signed-off-by: linyu --- interfaces/kits/c/task.h | 2 +- interfaces/kits/c/type_def.h | 8 +++++++- src/core/task.cpp | 4 ++-- src/eu/cpu_worker.cpp | 13 +++++++++---- src/eu/cpu_worker.h | 2 +- src/eu/func_manager.h | 13 ++++++++----- 6 files changed, 28 insertions(+), 14 deletions(-) diff --git a/interfaces/kits/c/task.h b/interfaces/kits/c/task.h index e383f06..bfebc5a 100644 --- a/interfaces/kits/c/task.h +++ b/interfaces/kits/c/task.h @@ -60,7 +60,7 @@ FFRT_C_API void ffrt_wake_coroutine(void *task); FFRT_C_API void ffrt_task_handle_destroy(ffrt_task_handle_t handle); -FFRT_C_API void ffrt_executor_task_register_func(ffrt_executor_task_func func, const char* name); +FFRT_C_API void ffrt_executor_task_register_func(ffrt_executor_task_func func, ffrt_executor_task_type_t type); FFRT_C_API void ffrt_executor_task_submit(ffrt_executor_task_t *task, const ffrt_task_attr_t *attr); FFRT_C_API int ffrt_executor_task_cancel(ffrt_executor_task_t *task, const ffrt_qos_t qos); diff --git a/interfaces/kits/c/type_def.h b/interfaces/kits/c/type_def.h index f2d3b09..38c4a1e 100644 --- a/interfaces/kits/c/type_def.h +++ b/interfaces/kits/c/type_def.h @@ -55,6 +55,12 @@ typedef enum { } ffrt_qos_default_t; typedef int ffrt_qos_t; +typedef enum { + ffrt_normal_task = 0; + ffrt_rust_task = 1; + ffrt_uv_task // only used to register func for libuv +} ffrt_executor_task_type_t + typedef enum { ffrt_stack_protect_weak, ffrt_stack_protect_strong @@ -170,7 +176,7 @@ typedef struct ffrt_executor_task { void* wq[2]; } ffrt_executor_task_t; -typedef void (*ffrt_executor_task_func)(ffrt_executor_task_t* data); +typedef void (*ffrt_executor_task_func)(ffrt_executor_task_t* data, ffrt_qos_t qos); #ifdef __cplusplus namespace ffrt { diff --git a/src/core/task.cpp b/src/core/task.cpp index 3a26956..291d803 100644 --- a/src/core/task.cpp +++ b/src/core/task.cpp @@ -454,10 +454,10 @@ void ffrt_executor_task_submit(ffrt_executor_task_t *task, const ffrt_task_attr_ } API_ATTRIBUTE((visibility("default"))) -void ffrt_executor_task_register_func(ffrt_executor_task_func func, const char* name) +void ffrt_executor_task_register_func(ffrt_executor_task_func func, ffrt_executor_task_type_t type) { ffrt::FuncManager* func_mg = ffrt::FuncManager::Instance(); - func_mg->insert(std::string(name), func); + func_mg->insert(type, func); } API_ATTRIBUTE((visibility("default"))) diff --git a/src/eu/cpu_worker.cpp b/src/eu/cpu_worker.cpp index 17ca1e7..e4001fc 100644 --- a/src/eu/cpu_worker.cpp +++ b/src/eu/cpu_worker.cpp @@ -47,17 +47,22 @@ void CPUWorker::Run(TaskCtx* task) #endif } -void CPUWorker::Run(ffrt_executor_task_t* data) +void CPUWorker::Run(ffrt_executor_task_t* task, ffrt_qos_t qos) { #ifdef FFRT_BBOX_ENABLE TaskRunCounterInc(); #endif - ffrt_executor_task_func func = FuncManager::Instance()->getFunc("uv"); + ffrt_executor_task_func func = nullptr; + if (task->type == ffrt_rust_task) { + func = FuncManager::Instance()->getFunc(ffrt_rust_task); + } else { + func = FuncManager::Instance()->getFunc(ffrt_uv_task); + } if (func == nullptr) { FFRT_LOGE("func is nullptr"); return; } - func(data); + func(task, qos); #ifdef FFRT_BBOX_ENABLE TaskFinishCounterInc(); #endif @@ -91,7 +96,7 @@ void CPUWorker::Dispatch(CPUWorker* worker) if (task->type != 0) { ffrt_executor_task_t* work = (ffrt_executor_task_t*)task; - Run(work); + Run(work, (int)worker->GetQos()); } else { UserSpaceLoadRecord::UpdateTaskSwitch(lastTask, task); task->UpdateState(TaskState::RUNNING); diff --git a/src/eu/cpu_worker.h b/src/eu/cpu_worker.h index c5c046a..e6db411 100644 --- a/src/eu/cpu_worker.h +++ b/src/eu/cpu_worker.h @@ -33,7 +33,7 @@ public: private: static void Dispatch(CPUWorker* worker); static void Run(TaskCtx* task); - static void Run(ffrt_executor_task_t* data); + static void Run(ffrt_executor_task_t* data, ffrt_qos_t qos); }; } // namespace ffrt #endif diff --git a/src/eu/func_manager.h b/src/eu/func_manager.h index 9b14012..0a0a715 100644 --- a/src/eu/func_manager.h +++ b/src/eu/func_manager.h @@ -34,21 +34,24 @@ public: return &func_mg; } - void insert(std::string name, ffrt_executor_task_func func) + void insert(ffrt_executor_task_type_t type, ffrt_executor_task_func func) { - func_map[name] = func; + func_map[type] = func; } - ffrt_executor_task_func getFunc(std::string name) + ffrt_executor_task_func getFunc(ffrt_executor_task_type_t type) { - return func_map[name]; + if (func_map.find(type) == func_map.end()) { + return nullptr; + } + return func_map[type]; } private: FuncManager() { } - std::map func_map; + std::map func_map; }; } #endif /* FFRT_FUNC_MANAGER_HPP */ From 5c7180a58e3a32513c10cd1238c0272afae6c8c4 Mon Sep 17 00:00:00 2001 From: linyu Date: Wed, 26 Jul 2023 10:11:22 +0800 Subject: [PATCH 2/2] multitask support Signed-off-by: linyu --- interfaces/kits/c/type_def.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/interfaces/kits/c/type_def.h b/interfaces/kits/c/type_def.h index 38c4a1e..c06a4ca 100644 --- a/interfaces/kits/c/type_def.h +++ b/interfaces/kits/c/type_def.h @@ -56,10 +56,10 @@ typedef enum { typedef int ffrt_qos_t; typedef enum { - ffrt_normal_task = 0; - ffrt_rust_task = 1; + ffrt_normal_task = 0, + ffrt_rust_task = 1, ffrt_uv_task // only used to register func for libuv -} ffrt_executor_task_type_t +} ffrt_executor_task_type_t; typedef enum { ffrt_stack_protect_weak,