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_EXECUTE_CTX_HPP 17 #define FFRT_EXECUTE_CTX_HPP 18 #include <mutex> 19 #include <condition_variable> 20 #include <functional> 21 #include <atomic> 22 23 #include "util/linked_list.h" 24 #include "c/executor_task.h" 25 #include "util/spmc_queue.h" 26 #ifdef USE_OHOS_QOS 27 #include "qos.h" 28 #else 29 #include "staging_qos/sched/qos.h" 30 #endif 31 32 namespace ffrt { 33 using TimePoint = std::chrono::steady_clock::time_point; 34 35 enum class TaskTimeoutState { 36 INIT, 37 NOTIFIED, 38 TIMEOUT, 39 }; 40 41 enum class SharedMutexWaitType { 42 NORMAL, 43 READ, 44 WRITE, 45 }; 46 47 enum class ThreadType { 48 USER_THREAD, // 非FFRT的线程,属于用户线程 49 FFRT_WORKER, // 执行ACPU任务的FFRT线程 50 DELAY_WORKER, // 执行定时和监控事件的FFRT线程 51 IO_POLLER // 执行fd监听的FFRT线程 52 }; 53 54 enum class WaitEntryStatus { 55 INIT, 56 NOTIFYING, 57 TIMEOUT_DONE, 58 }; // namespace WaitEntryStatus 59 60 class TaskBase; 61 class CoTask; 62 63 struct WaitEntry { WaitEntryWaitEntry64 WaitEntry() : prev(this), next(this), task(nullptr), wtType(SharedMutexWaitType::NORMAL) { 65 } WaitEntryWaitEntry66 explicit WaitEntry(TaskBase *task) : prev(nullptr), next(nullptr), task(task), 67 wtType(SharedMutexWaitType::NORMAL) { 68 } 69 LinkedList node; 70 WaitEntry* prev; 71 WaitEntry* next; 72 TaskBase* task; 73 SharedMutexWaitType wtType; 74 }; 75 76 struct WaitUntilEntry : WaitEntry { WaitUntilEntryWaitUntilEntry77 WaitUntilEntry() : WaitEntry(), status(WaitEntryStatus::INIT), hasWaitTime(false) 78 { 79 } WaitUntilEntryWaitUntilEntry80 explicit WaitUntilEntry(TaskBase* task) : WaitEntry(task), status(WaitEntryStatus::INIT), hasWaitTime(false) 81 { 82 } 83 std::atomic<WaitEntryStatus> status; 84 bool hasWaitTime; 85 TimePoint tp; 86 std::function<void(WaitEntry*)> cb; 87 std::mutex wl; 88 std::condition_variable cv; 89 }; 90 // 当前Worker线程的状态信息 91 struct ExecuteCtx { 92 ExecuteCtx(); 93 virtual ~ExecuteCtx(); 94 95 QoS qos; 96 TaskBase* task; // 当前正在执行的Task 97 WaitUntilEntry wn; 98 uint64_t lastGid_ = 0; 99 pid_t tid; 100 ThreadType threadType_ = ffrt::ThreadType::USER_THREAD; 101 102 /** 103 * @param init Should ExecuteCtx be initialized if it cannot be obtained 104 */ 105 static ExecuteCtx* Cur(bool init = true); 106 }; 107 } // namespace ffrt 108 #endif 109