Fast RTPS  Version 2.4.1
Fast RTPS
DBQueue.h
1 // Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2 //
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 DBQUEUE_H
16 #define DBQUEUE_H
17 
18 #include <queue>
19 #include <mutex>
20 #include <memory>
21 #include <condition_variable>
22 
23 namespace eprosima {
24 namespace fastrtps{
25 
29 template<class T>
30 class DBQueue {
31 
32 public:
34  mForegroundQueue(&mQueueAlpha),
35  mBackgroundQueue(&mQueueBeta)
36  {}
37 
39  void Swap()
40  {
41  std::unique_lock<std::mutex> fgGuard(mForegroundMutex);
42  std::unique_lock<std::mutex> bgGuard(mBackgroundMutex);
43 
44  // Clear the foreground queue.
45  std::queue<T>().swap(*mForegroundQueue);
46 
47  auto* swap = mBackgroundQueue;
48  mBackgroundQueue = mForegroundQueue;
49  mForegroundQueue = swap;
50  }
51 
53  void Push(const T& item)
54  {
55  std::unique_lock<std::mutex> guard(mBackgroundMutex);
56  mBackgroundQueue->push(item);
57  }
58 
61  T& Front()
62  {
63  std::unique_lock<std::mutex> guard(mForegroundMutex);
64  return mForegroundQueue->front();
65  }
66 
67  const T& Front() const
68  {
69  std::unique_lock<std::mutex> guard(mForegroundMutex);
70  return mForegroundQueue->front();
71  }
72 
74  void Pop()
75  {
76  std::unique_lock<std::mutex> guard(mForegroundMutex);
77  mForegroundQueue->pop();
78  }
79 
81  bool Empty() const
82  {
83  std::unique_lock<std::mutex> guard(mForegroundMutex);
84  return mForegroundQueue->empty();
85  }
86 
88  bool BothEmpty() const
89  {
90  std::unique_lock<std::mutex> guard(mForegroundMutex);
91  std::unique_lock<std::mutex> bgGuard(mBackgroundMutex);
92  return mForegroundQueue->empty() && mBackgroundQueue->empty();
93  }
94 
96  size_t Size() const
97  {
98  std::unique_lock<std::mutex> guard(mForegroundMutex);
99  return mForegroundQueue->size();
100  }
101 
103  void Clear()
104  {
105  std::unique_lock<std::mutex> fgGuard(mForegroundMutex);
106  std::unique_lock<std::mutex> bgGuard(mBackgroundMutex);
107  std::queue<T>().swap(*mForegroundQueue);
108  std::queue<T>().swap(*mBackgroundQueue);
109  }
110 
111 private:
112  // Underlying queues
113  std::queue<T> mQueueAlpha;
114  std::queue<T> mQueueBeta;
115 
116  // Front and background queue references (double buffering)
117  std::queue<T>* mForegroundQueue;
118  std::queue<T>* mBackgroundQueue;
119 
120  mutable std::mutex mForegroundMutex;
121  mutable std::mutex mBackgroundMutex;
122 };
123 
124 
125 } // namespace fastrtps
126 } // namespace eprosima
127 
128 #endif
Double buffered, threadsafe queue for MPSC (multi-producer, single-consumer) comms.
Definition: DBQueue.h:30
void Swap()
Clears foreground queue and swaps queues.
Definition: DBQueue.h:39
DBQueue()
Definition: DBQueue.h:33
size_t Size() const
Reports the size of the foreground queue.
Definition: DBQueue.h:96
void Pop()
Pops from the foreground queue.
Definition: DBQueue.h:74
T & Front()
Returns a reference to the front element in the foregrund queue.
Definition: DBQueue.h:61
void Push(const T &item)
Pushes to the background queue.
Definition: DBQueue.h:53
void Clear()
Clears foreground and background.
Definition: DBQueue.h:103
bool Empty() const
Reports whether the foreground queue is empty.
Definition: DBQueue.h:81
bool BothEmpty() const
Reports whether the both queues are empty.
Definition: DBQueue.h:88
const T & Front() const
Definition: DBQueue.h:67
eProsima namespace.
Definition: LibrarySettingsAttributes.h:23