• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #ifndef _TASK_BASE_H_
17 #define _TASK_BASE_H_
18 #include <atomic>
19 #include <vector>
20 #include "eu/co_routine.h"
21 #include "qos.h"
22 #include "sched/execute_ctx.h"
23 #include "internal_inc/non_copyable.h"
24 #ifdef ENABLE_HITRACE_CHAIN
25 #include "hitrace/hitracechainc.h"
26 #endif
27 
28 namespace ffrt {
29 static std::atomic_uint64_t s_gid(0);
30 static constexpr uint64_t cacheline_size = 64;
31 class TaskBase : private NonCopyable {
32 public:
33     uintptr_t reserved = 0;
34     uintptr_t type = 0;
35     WaitEntry fq_we; // used on fifo fast que
TaskBase()36     TaskBase(): gid(++s_gid) {}
37     virtual ~TaskBase() = default;
38     const uint64_t gid; // global unique id in this process
39     QoS qos_ = qos_inherit;
40     std::atomic_uint32_t rc = 1; // reference count for delete
41 #ifdef FFRT_ASYNC_STACKTRACE
42     uint64_t stackId = 0;
43 #endif
44 #ifdef ENABLE_HITRACE_CHAIN
45     struct HiTraceIdStruct traceId_;
46 #endif
47 
GetQos()48     inline int GetQos() const
49     {
50         return qos_();
51     }
52 
53     virtual std::string GetLabel() const = 0;
54 
55     virtual void Execute() = 0;
56 
57     uint64_t createTime {0};
58     uint64_t executeTime {0};
59     int32_t fromTid {0};
60 
61     virtual void FreeMem() = 0;
62 
IncDeleteRef()63     inline uint32_t IncDeleteRef()
64     {
65         auto v = rc.fetch_add(1);
66         return v;
67     }
68 
DecDeleteRef()69     inline uint32_t DecDeleteRef()
70     {
71         auto v = rc.fetch_sub(1);
72         if (v == 1) {
73             FreeMem();
74         }
75         return v;
76     }
77 };
78 
79 class CoTask : public TaskBase {
80 public:
81     CoTask() = default;
82     ~CoTask() override = default;
83 
84     std::string label;
85     CoWakeType coWakeType { CoWakeType::NO_TIMEOUT_WAKE };
86     int cpuBoostCtxId = -1;
87     WaitUntilEntry* wue = nullptr;
88     // lifecycle connection between task and coroutine is shown as below:
89     // |*task pending*|*task ready*|*task executing*|*task done*|*task release*|
90     //                             |**********coroutine*********|
91     CoRoutine* coRoutine = nullptr;
92     uint64_t stack_size = STACK_SIZE;
93     std::atomic<pthread_t> runningTid = 0;
94     int legacyCountNum = 0; // dynamic switch controlled by set_legacy_mode api
95     BlockType blockType { BlockType::BLOCK_COROUTINE }; // block type for lagacy mode changing
96     std::mutex mutex_; // used in coroute
97     std::condition_variable waitCond_; // cv for thread wait
98 
99     // !deprecated
SetTraceTag(const char * name)100     void SetTraceTag(const char* name)
101     {
102         (void)name;
103     }
104 
105     // !deprecated
ClearTraceTag()106     void ClearTraceTag()
107     {
108     }
109 
GetLabel()110     std::string GetLabel() const override
111     {
112         return label;
113     }
114 };
115 } // namespace ffrt
116 #endif
117