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 "internal_inc/osal.h" 25 26 namespace ffrt { 27 using time_point_t = std::chrono::steady_clock::time_point; 28 29 enum class TaskTimeoutState { 30 INIT, 31 NOTIFIED, 32 TIMEOUT, 33 }; 34 35 namespace we_status { 36 const int INIT = 0; 37 const int NOTIFIED = 1; 38 const int TIMEOUT = 2; 39 } // namespace we_status 40 41 struct TaskCtx; 42 43 struct WaitEntry { WaitEntryWaitEntry44 WaitEntry() : prev(this), next(this), task(nullptr), weType(0) { 45 } WaitEntryWaitEntry46 WaitEntry(TaskCtx *inTask) : prev(nullptr), next(nullptr), task(inTask), weType(0) { 47 } 48 LinkedList node; 49 WaitEntry* prev; 50 WaitEntry* next; 51 TaskCtx* task; 52 int weType; 53 }; 54 55 struct WaitUntilEntry : WaitEntry { WaitUntilEntryWaitUntilEntry56 WaitUntilEntry() : WaitEntry(), status(we_status::INIT), hasWaitTime(false) 57 { 58 } WaitUntilEntryWaitUntilEntry59 WaitUntilEntry(TaskCtx* inTask) : WaitEntry(inTask), status(we_status::INIT), hasWaitTime(false) 60 { 61 } 62 std::atomic_int32_t status; 63 bool hasWaitTime; 64 time_point_t tp; 65 std::function<void(WaitEntry*)> cb; 66 std::mutex wl; 67 std::condition_variable cv; 68 }; 69 // 当前Worker线程的状态信息 70 struct ExecuteCtx { ExecuteCtxExecuteCtx71 ExecuteCtx() 72 { 73 task = nullptr; 74 wn.weType = 2; 75 } 76 TaskCtx* task; // 当前正在执行的Task 77 WaitUntilEntry wn; 78 CurExecuteCtx79 static inline ExecuteCtx* Cur() 80 { 81 thread_local static ExecuteCtx ctx; 82 return &ctx; 83 } 84 }; 85 } // namespace ffrt 86 #endif 87