• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class TaskBase;
55 
56 struct WaitEntry {
WaitEntryWaitEntry57     WaitEntry() : prev(this), next(this), task(nullptr), weType(0), wtType(SharedMutexWaitType::NORMAL) {
58     }
WaitEntryWaitEntry59     explicit WaitEntry(CPUEUTask *task) : prev(nullptr), next(nullptr), task(task), weType(0),
60         wtType(SharedMutexWaitType::NORMAL) {
61     }
62     LinkedList node;
63     WaitEntry* prev;
64     WaitEntry* next;
65     CPUEUTask* task;
66     int weType;
67     SharedMutexWaitType wtType;
68 };
69 
70 struct WaitUntilEntry : WaitEntry {
WaitUntilEntryWaitUntilEntry71     WaitUntilEntry() : WaitEntry(), status(we_status::INIT), hasWaitTime(false)
72     {
73     }
WaitUntilEntryWaitUntilEntry74     explicit WaitUntilEntry(CPUEUTask* task) : WaitEntry(task), status(we_status::INIT), hasWaitTime(false)
75     {
76     }
77     std::atomic_int32_t status;
78     bool hasWaitTime;
79     TimePoint tp;
80     std::function<void(WaitEntry*)> cb;
81     std::mutex wl;
82     std::condition_variable cv;
83 };
84 // 当前Worker线程的状态信息
85 struct ExecuteCtx {
86     ExecuteCtx();
87     virtual ~ExecuteCtx();
88 
89     TaskBase* exec_task = nullptr;
90     void** priority_task_ptr = nullptr;
91     SpmcQueue* localFifo = nullptr;
92     QoS qos;
93     CPUEUTask* task; // 当前正在执行的Task
94     WaitUntilEntry wn;
95     uint64_t lastGid_ = 0;
96     pid_t tid;
97 
PushTaskToPriorityStackExecuteCtx98     inline bool PushTaskToPriorityStack(TaskBase* executorTask)
99     {
100         if (priority_task_ptr == nullptr) {
101             return false;
102         }
103         if (*priority_task_ptr == nullptr) {
104             *priority_task_ptr = reinterpret_cast<void*>(executorTask);
105             return true;
106         }
107         return false;
108     }
109 
110     /**
111      * @param init Should ExecuteCtx be initialized if it cannot be obtained
112      */
113     static ExecuteCtx* Cur(bool init = true);
114 };
115 } // namespace ffrt
116 #endif
117