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_BASE_QUEUE_H
16#define FFRT_BASE_QUEUE_H
17
18#include <atomic>
19#include <chrono>
20#include <map>
21#include <mutex>
22#include <memory>
23#include "c/queue.h"
24#include "internal_inc/non_copyable.h"
25#include "queue_strategy.h"
26#include "sync/record_condition_variable.h"
27#include "sync/record_mutex.h"
28
29namespace ffrt {
30class QueueTask;
31class Loop;
32
33enum QueueAction {
34    INACTIVE = -1, // queue is nullptr or serial queue is empty
35    SUCC,
36    FAILED,
37    CONCURRENT, // concurrency less than max concurrency
38};
39
40class BaseQueue : public NonCopyable {
41public:
42    explicit BaseQueue() : queueId_(queueId++) {}
43    virtual ~BaseQueue() = default;
44
45    virtual int Push(QueueTask* task) = 0;
46    virtual QueueTask* Pull() = 0;
47    virtual bool GetActiveStatus() = 0;
48    virtual int GetQueueType() const = 0;
49    virtual void Remove();
50    virtual int Remove(const char* name);
51    virtual int Remove(const QueueTask* task);
52    virtual void Stop();
53
54    virtual bool IsOnLoop()
55    {
56        return false;
57    }
58
59    inline uint64_t GetMapSize()
60    {
61        std::unique_lock lock(mutex_);
62        return whenMap_.size();
63    }
64
65    inline uint32_t GetQueueId() const
66    {
67        return queueId_;
68    }
69
70    bool HasTask(const char* name);
71
72    bool HasLock()
73    {
74        return mutex_.HasLock();
75    }
76
77    bool IsLockTimeout()
78    {
79        return mutex_.IsTimeout();
80    }
81
82    void PrintMutexOwner();
83
84protected:
85    inline uint64_t GetNow() const
86    {
87        return std::chrono::duration_cast<std::chrono::microseconds>(
88            std::chrono::steady_clock::now().time_since_epoch()).count();
89    }
90
91    void ClearWhenMap();
92
93    const uint32_t queueId_;
94    bool isExit_ { false };
95    std::atomic_bool isActiveState_ { false };
96    std::multimap<uint64_t, QueueTask*> whenMap_;
97    QueueStrategy<QueueTask>::DequeFunc dequeFunc_ { nullptr };
98
99    RecordMutex mutex_;
100    RecordConditionVariable cond_;
101
102private:
103    static std::atomic_uint32_t queueId;
104};
105
106std::unique_ptr<BaseQueue> CreateQueue(int queueType, const ffrt_queue_attr_t* attr);
107} // namespace ffrt
108
109#endif // FFRT_BASE_QUEUE_H
110