• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
16 #ifndef UTILS_BASE_SAFE_QUEUE_H
17 #define UTILS_BASE_SAFE_QUEUE_H
18 
19 #include <deque>
20 #include <mutex>
21 
22 namespace OHOS {
23 
24 template <typename T>
25 class SafeQueueInner {
26 
27 public:
SafeQueueInner()28     SafeQueueInner() {}
29 
~SafeQueueInner()30     virtual ~SafeQueueInner()
31     {
32         if (!deque_.empty()) {
33             deque_.clear();
34         }
35     }
36 
Erase(T & Object)37     void Erase(T& Object)
38     {
39         std::lock_guard<std::mutex> lock(mutex_);
40         deque_.remove(Object);
41     }
42 
Empty()43     bool Empty()
44     {
45         std::lock_guard<std::mutex> lock(mutex_);
46         return deque_.empty();
47     }
48 
Push(const T & pt)49     void Push(const T& pt)
50     {
51         std::lock_guard<std::mutex> lock(mutex_);
52         return DoPush(pt);
53     }
54 
Clear()55     void Clear()
56     {
57         std::lock_guard<std::mutex> lock(mutex_);
58         if (!deque_.empty()) {
59             deque_.clear();
60         }
61 
62         return;
63     }
64 
Size()65     int Size()
66     {
67         std::lock_guard<std::mutex> lock(mutex_);
68         return deque_.size();
69     }
70 
Pop(T & pt)71     bool Pop(T& pt)
72     {
73         std::lock_guard<std::mutex> lock(mutex_);
74         return DoPop(pt);
75     }
76 
77 protected:
78     virtual void DoPush(const T& pt) = 0;
79     virtual bool DoPop(T& pt) = 0;
80 
81     std::deque<T> deque_;
82     std::mutex mutex_;
83 };
84 
85 template <typename T>
86 class SafeQueue : public SafeQueueInner<T> {
87 
88 protected:
89     using SafeQueueInner<T>::deque_;
90     using SafeQueueInner<T>::mutex_;
91 
DoPush(const T & pt)92     virtual void DoPush(const T& pt) override
93     {
94         deque_.push_back(pt);
95     }
96 
DoPop(T & pt)97     virtual bool DoPop(T& pt) override
98     {
99         if (deque_.size() > 0) {
100             pt = deque_.front();
101             deque_.pop_front();
102             return true;
103         }
104 
105         return false;
106     }
107 };
108 
109 template <typename T>
110 class SafeStack : public SafeQueueInner<T> {
111 
112 protected:
113     using SafeQueueInner<T>::deque_;
114     using SafeQueueInner<T>::mutex_;
115 
DoPush(const T & pt)116     virtual void DoPush(const T& pt) override
117     {
118         deque_.push_back(pt);
119     }
120 
DoPop(T & pt)121     virtual bool DoPop(T& pt) override
122     {
123         if (deque_.size() > 0) {
124             pt = deque_.back();
125             deque_.pop_back();
126             return true;
127         }
128 
129         return false;
130     }
131 };
132 
133 } // namespace OHOS
134 #endif
135