• 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_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 "cpp/condition_variable.h"
25 #include "internal_inc/non_copyable.h"
26 #include "queue_strategy.h"
27 
28 namespace ffrt {
29 class QueueTask;
30 class Loop;
31 
32 enum QueueAction {
33     INACTIVE = -1, // queue is nullptr or serial queue is empty
34     SUCC,
35     FAILED,
36     CONCURRENT, // concurrency less than max concurrency
37 };
38 
39 class BaseQueue : public NonCopyable {
40 public:
41     BaseQueue();
42     virtual ~BaseQueue() = default;
43 
44     virtual int Push(QueueTask* task) = 0;
45     virtual QueueTask* Pull() = 0;
46     virtual bool GetActiveStatus() = 0;
47     virtual int GetQueueType() const = 0;
48     virtual int Remove();
49     virtual int Remove(const char* name);
50     virtual int Remove(const QueueTask* task);
51     virtual void Stop();
52     virtual uint64_t GetDueTaskCount();
53 
IsOnLoop()54     virtual bool IsOnLoop()
55     {
56         return false;
57     }
58 
WaitAll()59     virtual int WaitAll()
60     {
61         return -1;
62     }
63 
GetMapSize()64     virtual inline uint64_t GetMapSize()
65     {
66         std::lock_guard lock(mutex_);
67         return whenMap_.size();
68     }
69 
GetQueueId()70     inline uint32_t GetQueueId() const
71     {
72         return queueId_;
73     }
74 
DelayStatus()75     inline bool DelayStatus()
76     {
77         return delayStatus_.load();
78     }
79 
80     virtual bool HasTask(const char* name);
81     virtual std::vector<QueueTask*> GetHeadTask();
82     ffrt::mutex mutex_;
83 protected:
GetNow()84     inline uint64_t GetNow() const
85     {
86         return std::chrono::duration_cast<std::chrono::microseconds>(
87             std::chrono::steady_clock::now().time_since_epoch()).count();
88     }
89 
90     void Stop(std::multimap<uint64_t, QueueTask*>& whenMap);
91     int Remove(std::multimap<uint64_t, QueueTask*>& whenMap);
92     int Remove(const QueueTask* task, std::multimap<uint64_t, QueueTask*>& whenMap);
93     int Remove(const char* name, std::multimap<uint64_t, QueueTask*>& whenMap);
94     bool HasTask(const char* name, std::multimap<uint64_t, QueueTask*> whenMap);
95     uint64_t GetDueTaskCount(std::multimap<uint64_t, QueueTask*>& whenMap);
96 
97     const uint32_t queueId_;
98     std::atomic_bool delayStatus_ { false };
99     bool isExit_ { false };
100     std::atomic_bool isActiveState_ { false };
101     std::multimap<uint64_t, QueueTask*> whenMap_;
102     std::vector<QueueTask*> headTaskVec_;
103     QueueStrategy<QueueTask>::DequeFunc dequeFunc_ { nullptr };
104 
105     ffrt::condition_variable cond_;
106 };
107 
108 std::unique_ptr<BaseQueue> CreateQueue(int queueType, const ffrt_queue_attr_t* attr);
109 } // namespace ffrt
110 
111 #endif // FFRT_BASE_QUEUE_H
112