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 FFRT_SCPU_TASK_H 17 #define FFRT_SCPU_TASK_H 18 19 #include "tm/cpu_task.h" 20 21 namespace ffrt { 22 class SCPUEUTask : public CPUEUTask { 23 public: 24 SCPUEUTask(const task_attr_private* attr, CPUEUTask* parent, const uint64_t& id, const QoS& qos = QoS()); 25 std::unordered_set<VersionCtx*> ins; 26 std::unordered_set<VersionCtx*> outs; 27 std::vector<CPUEUTask*> in_handles; 28 29 std::mutex denpenceStatusLock; 30 Denpence denpenceStatus {Denpence::DEPENCE_INIT}; 31 32 std::atomic_uint64_t depRefCnt {0}; 33 34 std::atomic_uint64_t childWaitRefCnt {0}; 35 std::condition_variable childWaitCond_; 36 37 uint64_t dataWaitRefCnt {0}; // waited data count called by ffrt_wait() 38 std::condition_variable dataWaitCond_; // wait data cond 39 IncDepRef()40 inline void IncDepRef() 41 { 42 ++depRefCnt; 43 } 44 void DecDepRef(); 45 IncChildRef()46 inline void IncChildRef() 47 { 48 ++(static_cast<SCPUEUTask*>(parent)->childWaitRefCnt); 49 } 50 void DecChildRef(); 51 IncWaitDataRef()52 inline void IncWaitDataRef() 53 { 54 ++dataWaitRefCnt; 55 } 56 void DecWaitDataRef(); 57 void MultiDepenceAdd(Denpence depType); 58 void RecycleTask() override; 59 }; 60 61 class RootTask : public SCPUEUTask { 62 public: 63 RootTask(const task_attr_private* attr, SCPUEUTask* parent, const uint64_t& id, SCPUEUTask(attr,parent,id,qos)64 const QoS& qos = QoS()) : SCPUEUTask(attr, parent, id, qos) 65 { 66 } 67 public: 68 bool thread_exit = false; 69 }; 70 71 class RootTaskCtxWrapper { 72 public: RootTaskCtxWrapper()73 RootTaskCtxWrapper() 74 { 75 task_attr_private task_attr; 76 root = new RootTask{&task_attr, nullptr, 0}; 77 } ~RootTaskCtxWrapper()78 ~RootTaskCtxWrapper() 79 { 80 std::unique_lock<decltype(root->lock) > lck(root->lock); 81 if (root->childWaitRefCnt == 0) { 82 lck.unlock(); 83 delete root; 84 } else { 85 root->thread_exit = true ; 86 } 87 } Root()88 CPUEUTask* Root() 89 { 90 return root; 91 } 92 private: 93 RootTask *root = nullptr; 94 }; 95 } /* namespace ffrt */ 96 #endif 97