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