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 #ifndef FFRT_QUEUE_TASK_H 16 #define FFRT_QUEUE_TASK_H 17 18 #include <atomic> 19 #include <regex> 20 #include "queue/queue_attr_private.h" 21 #include "queue/queue_handler.h" 22 #include "tm/task_factory.h" 23 #ifdef FFRT_ENABLE_HITRACE_CHAIN 24 #include "dfx/trace/ffrt_trace_chain.h" 25 #endif 26 27 #define GetQueueTaskByFuncStorageOffset(f) \ 28 (reinterpret_cast<QueueTask*>(static_cast<uintptr_t>(static_cast<size_t>(reinterpret_cast<uintptr_t>(f)) - \ 29 (reinterpret_cast<size_t>(&((reinterpret_cast<QueueTask*>(0))->func_storage)))))) 30 31 namespace ffrt { 32 class QueueTask : public CoTask { 33 public: 34 explicit QueueTask(QueueHandler* handler, const task_attr_private* attr = nullptr, bool insertHead = false); 35 ~QueueTask() override; 36 37 void Wait(); 38 void Notify(); 39 void Destroy(); 40 41 uint32_t GetQueueId() const; 42 GetDelay()43 inline uint64_t GetDelay() const 44 { 45 return delay_; 46 } 47 GetUptime()48 inline uint64_t GetUptime() const 49 { 50 return uptime_; 51 } 52 GetHandler()53 inline QueueHandler* GetHandler() const 54 { 55 return handler_; 56 } 57 GetFinishStatus()58 inline bool GetFinishStatus() const 59 { 60 return isFinished_.load(); 61 } 62 GetNextTask()63 inline QueueTask* GetNextTask() const 64 { 65 return nextTask_; 66 } 67 SetNextTask(QueueTask * task)68 inline void SetNextTask(QueueTask* task) 69 { 70 nextTask_ = task; 71 } 72 SetPriority(const ffrt_queue_priority_t prio)73 inline void SetPriority(const ffrt_queue_priority_t prio) 74 { 75 prio_ = prio; 76 } 77 GetPriority()78 inline ffrt_queue_priority_t GetPriority() 79 { 80 return prio_; 81 } 82 IsMatch(std::string name)83 inline bool IsMatch(std::string name) const 84 { 85 std::string pattern = ".*_" + name + "_.*"; 86 return std::regex_match(label, std::regex(pattern)); 87 } 88 InsertHead()89 inline bool InsertHead() const 90 { 91 return insertHead_; 92 } 93 GetSchedTimeout()94 inline uint64_t GetSchedTimeout() const 95 { 96 return schedTimeout_; 97 } 98 SetMonitorTask(WaitUntilEntry * monitorWe)99 inline void SetMonitorTask(WaitUntilEntry* monitorWe) 100 { 101 monitorWe_ = monitorWe; 102 } 103 GetMonitorTask()104 inline WaitUntilEntry* GetMonitorTask() 105 { 106 return monitorWe_; 107 } 108 MonitorTaskStart()109 inline void MonitorTaskStart() 110 { 111 isWeStart_.store(true); 112 } 113 IsMonitorTaskStart()114 inline bool IsMonitorTaskStart() 115 { 116 return isWeStart_; 117 } 118 int curTaskIdx = 0; 119 120 void Prepare() override; 121 void Ready() override; 122 123 // dequeue means task has been pulled out from it's queue Dequeue()124 inline void Dequeue() 125 { 126 SetStatus(TaskStatus::DEQUEUED); 127 } 128 129 // pop means task has been popped from scheduler Pop()130 void Pop() override 131 { 132 SetStatus(TaskStatus::POPPED); 133 } 134 135 void Execute() override; 136 Block()137 BlockType Block() override 138 { 139 if (USE_COROUTINE && !threadMode_ && legacyCountNum <= 0 && (handler_ && !handler_->IsOnLoop())) { 140 blockType = BlockType::BLOCK_COROUTINE; 141 SetStatus(TaskStatus::COROUTINE_BLOCK); 142 } else { 143 blockType = BlockType::BLOCK_THREAD; 144 SetStatus(TaskStatus::THREAD_BLOCK); 145 } 146 return blockType; 147 } 148 Wake()149 void Wake() override 150 { 151 SetStatus(TaskStatus::EXECUTING); 152 blockType = BlockType::BLOCK_COROUTINE; 153 } 154 155 void Finish() override; 156 Cancel()157 void Cancel() override 158 { 159 FFRT_LOGD("cancel task[%llu] %s succ", gid, label.c_str()); 160 SetStatus(TaskStatus::CANCELED); 161 Notify(); 162 Destroy(); 163 } 164 165 void FreeMem() override; 166 SetQos(const QoS & newQos)167 void SetQos(const QoS& newQos) override 168 { 169 qos_ = newQos; 170 } 171 GetBlockType()172 BlockType GetBlockType() const override 173 { 174 return blockType; 175 } 176 177 private: 178 uint64_t uptime_; 179 QueueHandler* handler_; 180 bool insertHead_ = false; 181 uint64_t delay_ = 0; 182 uint64_t schedTimeout_ = 0; 183 184 QueueTask* nextTask_ = nullptr; 185 std::atomic_bool isFinished_ = {false}; 186 bool onWait_ = {false}; 187 std::atomic_bool isWeStart_ = {false}; 188 189 ffrt_queue_priority_t prio_ = ffrt_queue_priority_low; 190 WaitUntilEntry* monitorWe_ = nullptr; 191 }; 192 } // namespace ffrt 193 194 #endif // FFRT_QUEUE_TASK_H 195