• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "tm/task_base.h"
17 #include "util/ffrt_facade.h"
18 
19 namespace {
20     std::atomic_uint64_t g_taskId(0);
21 } // namespace
22 
23 namespace ffrt {
TaskBase(ffrt_executor_task_type_t type,const task_attr_private * attr)24 TaskBase::TaskBase(ffrt_executor_task_type_t type, const task_attr_private *attr) : type(type), gid(++g_taskId)
25 {
26     // qos_inherit is an abnormal enum value, which must to be replaced by another.
27     qos_ = (attr && attr->qos_ != qos_inherit) ? QoS(attr->qos_) : QoS();
28 }
29 
GetLastGid()30 uint32_t TaskBase::GetLastGid()
31 {
32     return g_taskId.load(std::memory_order_acquire);
33 }
34 
ExecuteTask(TaskBase * task)35 void ExecuteTask(TaskBase* task)
36 {
37     bool isCoTask = IsCoTask(task);
38 
39     // set current task info to context
40     ExecuteCtx* ctx = ExecuteCtx::Cur();
41     ctx->task = task;
42     ctx->lastGid_ = task->gid;
43 
44     // run task with coroutine
45     if (USE_COROUTINE && isCoTask) {
46         while (CoStart(static_cast<CoTask*>(task), GetCoRoutineEnv()) != 0) {
47             usleep(CO_CREATE_RETRY_INTERVAL);
48         }
49     } else {
50     // run task on thread
51 #ifdef FFRT_ASYNC_STACKTRACE
52         if (isCoTask) {
53             FFRTSetStackId(task->stackId);
54         }
55 #endif
56         task->Execute();
57     }
58 
59     // reset task info in context
60     ctx->task = nullptr;
61 }
62 
63 } // namespace ffrt
64