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 #ifndef FFRT_QUEUE_TASK_H
16 #define FFRT_QUEUE_TASK_H
17 
18 #include <atomic>
19 #include <regex>
20 #include "util/task_deleter.h"
21 #include "cpu_task.h"
22 #include "queue/queue_attr_private.h"
23 #include "queue/queue_handler.h"
24 
25 #define GetQueueTaskByFuncStorageOffset(f)                                                                     \
26     (reinterpret_cast<QueueTask*>(static_cast<uintptr_t>(static_cast<size_t>(reinterpret_cast<uintptr_t>(f)) - \
27         (reinterpret_cast<size_t>(&((reinterpret_cast<QueueTask*>(0))->func_storage))))))
28 
29 namespace ffrt {
30 class QueueTask : public CoTask {
31 public:
32     explicit QueueTask(QueueHandler* handler, const task_attr_private* attr = nullptr, bool insertHead = false);
33     ~QueueTask() override;
34 
35     void Destroy();
36     void Wait();
37     void Notify();
38     void Execute() override;
39 
40     uint32_t GetQueueId() const;
41 
GetQos()42     inline int GetQos() const override
43     {
44         return qos_;
45     }
46 
SetQos(int qos)47     inline void SetQos(int qos)
48     {
49         qos_ = qos;
50     }
51 
GetDelay()52     inline uint64_t GetDelay() const
53     {
54         return delay_;
55     }
56 
GetUptime()57     inline uint64_t GetUptime() const
58     {
59         return uptime_;
60     }
61 
GetHandler()62     inline QueueHandler* GetHandler() const
63     {
64         return handler_;
65     }
66 
GetFinishStatus()67     inline bool GetFinishStatus() const
68     {
69         return isFinished_.load();
70     }
71 
GetNextTask()72     inline QueueTask* GetNextTask() const
73     {
74         return nextTask_;
75     }
76 
SetNextTask(QueueTask * task)77     inline void SetNextTask(QueueTask* task)
78     {
79         nextTask_ = task;
80     }
81 
SetPriority(const ffrt_queue_priority_t prio)82     inline void SetPriority(const ffrt_queue_priority_t prio)
83     {
84         prio_ = prio;
85     }
86 
GetPriority()87     inline ffrt_queue_priority_t GetPriority()
88     {
89         return prio_;
90     }
91 
IsMatch(std::string name)92     inline bool IsMatch(std::string name) const
93     {
94         std::string pattern = ".*_" + name + "_.*";
95         return std::regex_match(label, std::regex(pattern));
96     }
97 
InsertHead()98     inline bool InsertHead() const
99     {
100         return insertHead_;
101     }
102 
GetSchedTimeout()103     inline uint64_t GetSchedTimeout() const
104     {
105         return schedTimeout_;
106     }
107     uint8_t func_storage[ffrt_auto_managed_function_storage_size];
108 
109 private:
110     void FreeMem() override;
111     uint64_t uptime_;
112     QueueHandler* handler_;
113     bool insertHead_ = false;
114     uint64_t delay_ = 0;
115     uint64_t schedTimeout_ = 0;
116     int qos_ = qos_inherit;
117 
118     QueueTask* nextTask_ = nullptr;
119     std::atomic_bool isFinished_ = {false};
120     bool onWait_ = {false};
121 
122     ffrt_queue_priority_t prio_ = ffrt_queue_priority_low;
123 };
124 } // namespace ffrt
125 
126 #endif // FFRT_QUEUE_TASK_H
127