新增ffrt_get_cur_cached_task_id接口

Signed-off-by: chendi <cdrom000@126,com>
This commit is contained in:
chendi 2024-08-06 14:17:42 +08:00
parent f193b551df
commit 38991d3315
5 changed files with 40 additions and 0 deletions

View File

@ -113,4 +113,12 @@ FFRT_C_API bool ffrt_task_attr_get_local(ffrt_task_attr_t* attr);
* @version 1.0
*/
FFRT_C_API pthread_t ffrt_task_get_tid(void* task_handle);
/**
* @brief Obtains the task id cached by the current thread.
*
* @return Returns the task id.
* @version 1.0
*/
FFRT_C_API uint64_t ffrt_get_cur_cached_task_id();
#endif

View File

@ -636,6 +636,17 @@ pthread_t ffrt_task_get_tid(void* task_handle)
auto task = reinterpret_cast<ffrt::CPUEUTask*>(task_handle);
return task->runningTid.load();
}
API_ATTRIBUTE((visibility("default")))
uint64_t ffrt_get_cur_cached_task_id()
{
uint64_t gid = ffrt_this_task_get_id();
if (gid == 0) {
return ffrt::ExecuteCtx::Cur()->lastGid_;
}
return gid;
}
#ifdef __cplusplus
}
#endif

View File

@ -115,6 +115,7 @@ void CPUWorker::RunTask(ffrt_executor_task_t* curtask, CPUWorker* worker)
case ffrt_normal_task:
case ffrt_queue_task: {
ctx->task = task;
ctx->lastGid_ = task->gid;
Run(task);
ctx->task = nullptr;
break;

View File

@ -91,6 +91,7 @@ struct ExecuteCtx {
QoS qos;
CPUEUTask* task; // 当前正在执行的Task
WaitUntilEntry wn;
uint64_t lastGid_ = 0;
inline bool PushTaskToPriorityStack(ffrt_executor_task_t* task)
{

View File

@ -23,6 +23,7 @@
#include "dfx/log/ffrt_log_api.h"
#include "dfx/bbox/bbox.h"
#include "tm/cpu_task.h"
#include "tm/scpu_task.h"
using namespace std;
using namespace testing;
@ -96,4 +97,22 @@ HWTEST_F(CoreTest, ffrt_submit_wait_success_01, TestSize.Level1)
ffrt_task_attr_destroy(attr);
free(attr);
attr = nullptr;
}
/*
* ffrt_get_cur_cached_task_id_test
* ffrt_get_cur_cached_task_id接口
* ExecuteCtx::Cur->lastGid_为自定义值
* ffrt_get_cur_cached_task_id接口
* ffrt_get_cur_cached_task_id返回值与自定义值相同
*/
HWTEST_F(CoreTest, ffrt_get_cur_cached_task_id_test, TestSize.Level1)
{
auto ctx = ffrt::ExecuteCtx::Cur();
ctx->lastGid_ = 15;
EXPECT_EQ(ffrt_get_cur_cached_task_id(), 15);
ffrt::CPUEUTask* task = new ffrt::SCPUEUTask(nullptr, nullptr, 20, ffrt::QoS(2));
ctx->task = task;
EXPECT_EQ(ffrt_get_cur_cached_task_id(), 1);
}