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 namespace we_status {
48 const int INIT = 0;
49 const int NOTIFING = 1;
50 const int TIMEOUT_DONE = 2;
51 } // namespace we_status
52 
53 class CPUEUTask;
54 
55 struct WaitEntry {
WaitEntryWaitEntry56     WaitEntry() : prev(this), next(this), task(nullptr), weType(0), wtType(SharedMutexWaitType::NORMAL) {
57     }
WaitEntryWaitEntry58     explicit WaitEntry(CPUEUTask *task) : prev(nullptr), next(nullptr), task(task), weType(0),
59         wtType(SharedMutexWaitType::NORMAL) {
60     }
61     LinkedList node;
62     WaitEntry* prev;
63     WaitEntry* next;
64     CPUEUTask* task;
65     int weType;
66     SharedMutexWaitType wtType;
67 };
68 
69 struct WaitUntilEntry : WaitEntry {
WaitUntilEntryWaitUntilEntry70     WaitUntilEntry() : WaitEntry(), status(we_status::INIT), hasWaitTime(false)
71     {
72     }
WaitUntilEntryWaitUntilEntry73     explicit WaitUntilEntry(CPUEUTask* task) : WaitEntry(task), status(we_status::INIT), hasWaitTime(false)
74     {
75     }
76     std::atomic_int32_t status;
77     bool hasWaitTime;
78     TimePoint tp;
79     std::function<void(WaitEntry*)> cb;
80     std::mutex wl;
81     std::condition_variable cv;
82 };
83 // 当前Worker线程的状态信息
84 struct ExecuteCtx {
85     ExecuteCtx();
86     virtual ~ExecuteCtx();
87 
88     ffrt_executor_task_t* exec_task = nullptr;
89     void** priority_task_ptr = nullptr;
90     SpmcQueue* localFifo = nullptr;
91     QoS qos;
92     CPUEUTask* task; // 当前正在执行的Task
93     WaitUntilEntry wn;
94     uint64_t lastGid_ = 0;
95     pid_t tid;
96 
PushTaskToPriorityStackExecuteCtx97     inline bool PushTaskToPriorityStack(ffrt_executor_task_t* executorTask)
98     {
99         if (priority_task_ptr == nullptr) {
100             return false;
101         }
102         if (*priority_task_ptr == nullptr) {
103             *priority_task_ptr = executorTask;
104             return true;
105         }
106         return false;
107     }
108 
109     static ExecuteCtx* Cur();
110 };
111 } // namespace ffrt
112 #endif
113