• 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 #ifdef FFRT_IO_TASK_SCHEDULER
25 #include "c/executor_task.h"
26 #include "util/spmc_queue.h"
27 #include "qos.h"
28 #endif
29 
30 namespace ffrt {
31 using time_point_t = std::chrono::steady_clock::time_point;
32 
33 enum class TaskTimeoutState {
34     INIT,
35     NOTIFIED,
36     TIMEOUT,
37 };
38 
39 namespace we_status {
40 const int INIT = 0;
41 const int NOTIFIED = 1;
42 const int TIMEOUT = 2;
43 const int HANDOVER = 3;
44 } // namespace we_status
45 
46 class CPUEUTask;
47 
48 struct WaitEntry {
WaitEntryWaitEntry49     WaitEntry() : prev(this), next(this), task(nullptr), weType(0) {
50     }
WaitEntryWaitEntry51     explicit WaitEntry(CPUEUTask *inTask) : prev(nullptr), next(nullptr), task(inTask), weType(0) {
52     }
53     LinkedList node;
54     WaitEntry* prev;
55     WaitEntry* next;
56     CPUEUTask* task;
57     int weType;
58 };
59 
60 struct WaitUntilEntry : WaitEntry {
WaitUntilEntryWaitUntilEntry61     WaitUntilEntry() : WaitEntry(), status(we_status::INIT), hasWaitTime(false)
62     {
63     }
WaitUntilEntryWaitUntilEntry64     explicit WaitUntilEntry(CPUEUTask* inTask) : WaitEntry(inTask), status(we_status::INIT), hasWaitTime(false)
65     {
66     }
67     std::atomic_int32_t status;
68     bool hasWaitTime;
69     time_point_t tp;
70     std::function<void(WaitEntry*)> cb;
71     std::mutex wl;
72     std::condition_variable cv;
73 };
74 // 当前Worker线程的状态信息
75 struct ExecuteCtx {
76     ExecuteCtx();
77     virtual ~ExecuteCtx();
78 #ifdef FFRT_IO_TASK_SCHEDULER
79     ffrt_executor_task_t* exec_task = nullptr;
80     void** priority_task_ptr = nullptr;
81     SpmcQueue* localFifo = nullptr;
82     QoS qos;
83 #endif
84     CPUEUTask* task; // 当前正在执行的Task
85     WaitUntilEntry wn;
86 
87 #ifdef FFRT_IO_TASK_SCHEDULER
PushTaskToPriorityStackExecuteCtx88     inline bool PushTaskToPriorityStack(ffrt_executor_task_t* task)
89     {
90         if (priority_task_ptr == nullptr) {
91             return false;
92         }
93         if (*priority_task_ptr == nullptr) {
94             *priority_task_ptr = task;
95             return true;
96         }
97         return false;
98     }
99 #endif
100 
101     static ExecuteCtx* Cur();
102 };
103 } // namespace ffrt
104 #endif
105