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 "util/task_deleter.h" 24 25 namespace ffrt { 26 static std::atomic_uint64_t s_gid(0); 27 static constexpr uint64_t cacheline_size = 64; 28 class TaskBase { 29 public: 30 uintptr_t reserved = 0; 31 uintptr_t type = 0; 32 WaitEntry fq_we; // used on fifo fast que TaskBase()33 TaskBase(): gid(++s_gid) {} 34 virtual ~TaskBase() = default; 35 const uint64_t gid; // global unique id in this process 36 #ifdef FFRT_ASYNC_STACKTRACE 37 uint64_t stackId = 0; 38 #endif 39 GetQos()40 virtual int GetQos() const 41 { 42 return qos_default; 43 } 44 45 #if (FFRT_TRACE_RECORD_LEVEL >= FFRT_TRACE_RECORD_LEVEL_1) 46 uint64_t createTime; 47 uint64_t executeTime; 48 #endif 49 int32_t fromTid; 50 }; 51 52 class CoTask : public TaskBase, public TaskDeleter { 53 public: 54 CoTask() = default; 55 ~CoTask() override = default; 56 virtual void Execute() = 0; 57 58 std::string label; 59 std::vector<std::string> traceTag; 60 bool wakeupTimeOut = false; 61 int floCtxId = -1; 62 WaitUntilEntry* wue = nullptr; 63 // lifecycle connection between task and coroutine is shown as below: 64 // |*task pending*|*task ready*|*task executing*|*task done*|*task release*| 65 // |**********coroutine*********| 66 CoRoutine* coRoutine = nullptr; 67 uint64_t stack_size = STACK_SIZE; 68 std::atomic<pthread_t> runningTid = 0; 69 int legacyCountNum = 0; // dynamic switch controlled by set_legacy_mode api 70 BlockType blockType { BlockType::BLOCK_COROUTINE }; // block type for lagacy mode changing 71 std::mutex mutex_; // used in coroute 72 std::condition_variable waitCond_; // cv for thread wait 73 SetTraceTag(const char * name)74 void SetTraceTag(const char* name) 75 { 76 traceTag.emplace_back(name); 77 } 78 ClearTraceTag()79 void ClearTraceTag() 80 { 81 if (!traceTag.empty()) { 82 traceTag.pop_back(); 83 } 84 } 85 }; 86 } 87 #endif