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
29namespace ffrt {
30class QueueTask : public CoTask {
31public:
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
42    inline int GetQos() const override
43    {
44        return qos_;
45    }
46
47    inline void SetQos(int qos)
48    {
49        qos_ = qos;
50    }
51
52    inline uint64_t GetDelay() const
53    {
54        return delay_;
55    }
56
57    inline uint64_t GetUptime() const
58    {
59        return uptime_;
60    }
61
62    inline QueueHandler* GetHandler() const
63    {
64        return handler_;
65    }
66
67    inline bool GetFinishStatus() const
68    {
69        return isFinished_.load();
70    }
71
72    inline QueueTask* GetNextTask() const
73    {
74        return nextTask_;
75    }
76
77    inline void SetNextTask(QueueTask* task)
78    {
79        nextTask_ = task;
80    }
81
82    inline void SetPriority(const ffrt_queue_priority_t prio)
83    {
84        prio_ = prio;
85    }
86
87    inline ffrt_queue_priority_t GetPriority()
88    {
89        return prio_;
90    }
91
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
98    inline bool InsertHead() const
99    {
100        return insertHead_;
101    }
102
103    uint8_t func_storage[ffrt_auto_managed_function_storage_size];
104
105private:
106    void FreeMem() override;
107    uint64_t uptime_;
108    QueueHandler* handler_;
109    bool insertHead_ = false;
110    uint64_t delay_ = 0;
111    int qos_ = qos_inherit;
112
113    QueueTask* nextTask_ = nullptr;
114    std::atomic_bool isFinished_ = {false};
115    bool onWait_ = {false};
116
117    ffrt_queue_priority_t prio_ = ffrt_queue_priority_low;
118};
119} // namespace ffrt
120
121#endif // FFRT_QUEUE_TASK_H
122