Apollo  6.0
Open source self driving car software
concurrent_queue.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 #pragma once
17 
18 #include <queue>
19 
21 
22 namespace apollo {
23 namespace perception {
24 namespace lib {
25 
26 template <class Data>
28  public:
30  virtual ~ConcurrentQueue() {}
31 
32  virtual void Push(const Data &data) {
33  MutexLock lock(&mutex_);
34  queue_.push(data);
36  }
37 
38  virtual void Pop(Data *data) {
39  MutexLock lock(&mutex_);
40 
41  while (queue_.empty()) {
43  }
44  *data = queue_.front();
45  queue_.pop();
46  }
47 
48  bool TryPop(Data *data) {
49  MutexLock lock(&mutex_);
50 
51  if (queue_.empty()) {
52  return false;
53  }
54 
55  *data = queue_.front();
56  queue_.pop();
57  return true;
58  }
59 
60  bool Empty() {
61  MutexLock lock(&mutex_);
62  return queue_.empty();
63  }
64 
65  int Size() {
66  MutexLock lock(&mutex_);
67  return static_cast<int>(queue_.size());
68  }
69 
70  void Clear() {
71  MutexLock lock(&mutex_);
72  while (!queue_.empty()) {
73  queue_.pop();
74  }
75  }
76 
77  ConcurrentQueue(const ConcurrentQueue &) = delete;
78  ConcurrentQueue &operator=(const ConcurrentQueue &) = delete;
79 
80  protected:
81  std::queue<Data> queue_;
84 
85  private:
86 };
87 
88 template <typename Data>
89 class FixedSizeConQueue : public ConcurrentQueue<Data> {
90  public:
91  explicit FixedSizeConQueue(size_t max_count)
92  : ConcurrentQueue<Data>(), max_count_(max_count) {}
93 
94  virtual ~FixedSizeConQueue() {}
95 
96  virtual void Push(const Data &data) {
97  MutexLock lock(&this->mutex_);
98  while (this->queue_.size() >= max_count_) {
99  condition_full_.Wait(&this->mutex_);
100  }
101  this->queue_.push(data);
102  this->condition_variable_.Signal();
103  }
104 
105  virtual bool TryPush(const Data &data) {
106  MutexLock lock(&this->mutex_);
107  if (this->queue_.size() >= max_count_) {
108  return false;
109  }
110  this->queue_.push(data);
111  this->condition_variable_.Signal();
112  return true;
113  }
114 
115  virtual void Pop(Data *data) {
116  MutexLock lock(&this->mutex_);
117 
118  while (this->queue_.empty()) {
119  this->condition_variable_.Wait(&this->mutex_);
120  }
121  *data = this->queue_.front();
122  this->queue_.pop();
123  condition_full_.Signal();
124  }
125 
126  virtual bool TryPop(Data *data) {
127  MutexLock lock(&this->mutex_);
128 
129  if (this->queue_.empty()) {
130  return false;
131  }
132 
133  *data = this->queue_.front();
134  this->queue_.pop();
135  condition_full_.Signal();
136  return true;
137  }
138 
139  bool Full() const { return this->queue_.size() >= max_count_; }
140 
141  FixedSizeConQueue(const FixedSizeConQueue &) = delete;
142  FixedSizeConQueue &operator=(const FixedSizeConQueue &) = delete;
143 
144  private:
145  CondVar condition_full_;
146  const size_t max_count_;
147 };
148 
149 } // namespace lib
150 } // namespace perception
151 } // namespace apollo
FixedSizeConQueue(size_t max_count)
Definition: concurrent_queue.h:91
virtual void Pop(Data *data)
Definition: concurrent_queue.h:38
bool TryPop(Data *data)
Definition: concurrent_queue.h:48
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
CondVar condition_variable_
Definition: concurrent_queue.h:83
virtual void Push(const Data &data)
Definition: concurrent_queue.h:32
virtual void Push(const Data &data)
Definition: concurrent_queue.h:96
virtual bool TryPop(Data *data)
Definition: concurrent_queue.h:126
Definition: mutex.h:24
void Signal()
Definition: mutex.h:64
std::queue< Data > queue_
Definition: concurrent_queue.h:81
bool Empty()
Definition: concurrent_queue.h:60
Mutex mutex_
Definition: concurrent_queue.h:82
virtual ~FixedSizeConQueue()
Definition: concurrent_queue.h:94
void Clear()
Definition: concurrent_queue.h:70
bool Full() const
Definition: concurrent_queue.h:139
virtual void Pop(Data *data)
Definition: concurrent_queue.h:115
ConcurrentQueue & operator=(const ConcurrentQueue &)=delete
Definition: concurrent_queue.h:89
void Wait(Mutex *mu)
Definition: mutex.h:62
Definition: mutex.h:57
ConcurrentQueue()
Definition: concurrent_queue.h:29
int Size()
Definition: concurrent_queue.h:65
virtual ~ConcurrentQueue()
Definition: concurrent_queue.h:30
virtual bool TryPush(const Data &data)
Definition: concurrent_queue.h:105
Definition: concurrent_queue.h:27