1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 
16 #include "data_queue.h"
17 #include "common/media_log.h"
18 namespace OHOS {
19 namespace Sharing {
20 
21 template <class T>
DataQueue(const size_t sizeMax)22 DataQueue<T>::DataQueue(const size_t sizeMax) : sizeMax_(sizeMax)
23 {
24     quitFlag_ = false;
25     finishFlag_ = false;
26 }
27 
28 template <class T>
~DataQueue()29 DataQueue<T>::~DataQueue()
30 {
31     Quit();
32 }
33 
34 template <class T>
Push(const T &data)35 bool DataQueue<T>::Push(const T &data)
36 {
37     std::lock_guard<std::mutex> lock(mutex_);
38     while (!quitFlag_ && !finishFlag_) {
39         if (queue_.size() < sizeMax_) {
40             queue_.push(std::move(data));
41             emptyQueue_.notify_all();
42             return true;
43         } else {
44             SHARING_LOGD("DataQueue push failed 1.");
45             return false;
46         }
47     }
48 
49     SHARING_LOGD("DataQueue push failed.");
50     return false;
51 }
52 
53 template <class T>
Pop(T &data)54 bool DataQueue<T>::Pop(T &data)
55 {
56     std::lock_guard<std::mutex> lock(mutex_);
57     while (!quitFlag_) {
58         if (!queue_.empty()) {
59             data = std::move(queue_.front());
60             queue_.pop();
61             fullQueue_.notify_all();
62             return true;
63         } else if (queue_.empty() && finishFlag_) {
64             return false;
65         } else {
66             return false;
67         }
68     }
69 
70     return false;
71 }
72 
73 template <class T>
Finished()74 void DataQueue<T>::Finished()
75 {
76     finishFlag_ = true;
77     emptyQueue_.notify_all();
78 }
79 
80 template <class T>
Quit()81 void DataQueue<T>::Quit()
82 {
83     quitFlag_ = true;
84     emptyQueue_.notify_all();
85     fullQueue_.notify_all();
86 }
87 
88 template <class T>
Size()89 uint16_t DataQueue<T>::Size()
90 {
91     return static_cast<int>(queue_.size());
92 }
93 
94 template <class T>
IsEmpty()95 bool DataQueue<T>::IsEmpty()
96 {
97     std::lock_guard<std::mutex> lock(mutex_);
98     return (0 == queue_.size());
99 }
100 
101 template <class T>
Clear()102 bool DataQueue<T>::Clear()
103 {
104     std::lock_guard<std::mutex> lock(mutex_);
105     while (!queue_.empty()) {
106         queue_.pop();
107     }
108 
109     return true;
110 }
111 
112 template <class T>
Pop(void)113 std::shared_ptr<T> DataQueue<T>::Pop(void)
114 {
115     std::unique_lock<std::mutex> lock(mutex_);
116     std::shared_ptr<T> res = nullptr;
117     while (!quitFlag_) {
118         if (!queue_.empty()) {
119             res = std::make_shared<T>(queue_.front());
120             queue_.pop();
121             fullQueue_.notify_all();
122             return res;
123         } else if (queue_.empty() && finishFlag_) {
124             return res;
125         } else {
126             emptyQueue_.wait(lock);
127         }
128     }
129 
130     return nullptr;
131 }
132 
133 } // namespace Sharing
134 } // namespace OHOS