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_CONCURRENT_QUEUE_H
16 #define FFRT_CONCURRENT_QUEUE_H
17 
18 #include "base_queue.h"
19 
20 namespace ffrt {
21 class ConcurrentQueue : public BaseQueue {
22 public:
23     explicit ConcurrentQueue(const int maxConcurrency = 1)
24         : maxConcurrency_(maxConcurrency)
25     {
26         dequeFunc_ = QueueStrategy<QueueTask>::DequeSingleByPriority;
27     }
28     ~ConcurrentQueue() override;
29 
30     int Push(QueueTask* task) override;
31     QueueTask* Pull() override;
GetActiveStatus()32     bool GetActiveStatus() override
33     {
34         return concurrency_.load();
35     }
36 
GetQueueType()37     int GetQueueType() const override
38     {
39         return ffrt_queue_concurrent;
40     }
41 
42     void Stop() override;
43     bool SetLoop(Loop* loop);
44 
ClearLoop()45     inline bool ClearLoop()
46     {
47         if (loop_ == nullptr) {
48             return false;
49         }
50 
51         loop_ = nullptr;
52         return true;
53     }
54 
IsOnLoop()55     bool IsOnLoop() override
56     {
57         return isOnLoop_.load();
58     }
59 
60 private:
61     int PushDelayTaskToTimer(QueueTask* task);
62 
63     Loop* loop_ { nullptr };
64     std::atomic_bool isOnLoop_ { false };
65 
66     int maxConcurrency_ {1};
67     std::atomic_int concurrency_ {0};
68 };
69 
70 std::unique_ptr<BaseQueue> CreateConcurrentQueue(const ffrt_queue_attr_t* attr);
71 } // namespace ffrt
72 #endif // FFRT_CONCURRENT_QUEUE_H
73