Apollo 10.0
自动驾驶开放平台
apollo::cyber::base::BoundedQueue< T > 模板类 参考

#include <bounded_queue.h>

apollo::cyber::base::BoundedQueue< T > 的协作图:

Public 类型

using value_type = T
 
using size_type = uint64_t
 

Public 成员函数

 BoundedQueue ()
 
BoundedQueueoperator= (const BoundedQueue &other)=delete
 
 BoundedQueue (const BoundedQueue &other)=delete
 
 ~BoundedQueue ()
 
bool Init (uint64_t size)
 
bool Init (uint64_t size, WaitStrategy *strategy)
 
bool Enqueue (const T &element)
 
bool Enqueue (T &&element)
 
bool WaitEnqueue (const T &element)
 
bool WaitEnqueue (T &&element)
 
bool Dequeue (T *element)
 
bool WaitDequeue (T *element)
 
uint64_t Size ()
 
bool Empty ()
 
void SetWaitStrategy (WaitStrategy *WaitStrategy)
 
void BreakAllWait ()
 
uint64_t Head ()
 
uint64_t Tail ()
 
uint64_t Commit ()
 

详细描述

template<typename T>
class apollo::cyber::base::BoundedQueue< T >

在文件 bounded_queue.h37 行定义.

成员类型定义说明

◆ size_type

template<typename T >
using apollo::cyber::base::BoundedQueue< T >::size_type = uint64_t

在文件 bounded_queue.h40 行定义.

◆ value_type

template<typename T >
using apollo::cyber::base::BoundedQueue< T >::value_type = T

在文件 bounded_queue.h39 行定义.

构造及析构函数说明

◆ BoundedQueue() [1/2]

template<typename T >
apollo::cyber::base::BoundedQueue< T >::BoundedQueue ( )
inline

在文件 bounded_queue.h43 行定义.

43{}

◆ BoundedQueue() [2/2]

template<typename T >
apollo::cyber::base::BoundedQueue< T >::BoundedQueue ( const BoundedQueue< T > &  other)
delete

◆ ~BoundedQueue()

template<typename T >
apollo::cyber::base::BoundedQueue< T >::~BoundedQueue ( )

在文件 bounded_queue.h77 行定义.

77 {
78 if (wait_strategy_) {
80 }
81 if (pool_) {
82 for (uint64_t i = 0; i < pool_size_; ++i) {
83 pool_[i].~T();
84 }
85 std::free(pool_);
86 }
87}

成员函数说明

◆ BreakAllWait()

template<typename T >
void apollo::cyber::base::BoundedQueue< T >::BreakAllWait ( )
inline

在文件 bounded_queue.h240 行定义.

240 {
241 break_all_wait_ = true;
242 wait_strategy_->BreakAllWait();
243}

◆ Commit()

template<typename T >
uint64_t apollo::cyber::base::BoundedQueue< T >::Commit ( )
inline

在文件 bounded_queue.h61 行定义.

61{ return commit_.load(); }

◆ Dequeue()

template<typename T >
bool apollo::cyber::base::BoundedQueue< T >::Dequeue ( T *  element)

在文件 bounded_queue.h156 行定义.

156 {
157 uint64_t new_head = 0;
158 uint64_t old_head = head_.load(std::memory_order_acquire);
159 do {
160 new_head = old_head + 1;
161 if (new_head == commit_.load(std::memory_order_acquire)) {
162 return false;
163 }
164 *element = pool_[GetIndex(new_head)];
165 } while (!head_.compare_exchange_weak(old_head, new_head,
166 std::memory_order_acq_rel,
167 std::memory_order_relaxed));
168 return true;
169}

◆ Empty()

template<typename T >
bool apollo::cyber::base::BoundedQueue< T >::Empty ( )
inline

在文件 bounded_queue.h225 行定义.

225 {
226 return Size() == 0;
227}

◆ Enqueue() [1/2]

template<typename T >
bool apollo::cyber::base::BoundedQueue< T >::Enqueue ( const T &  element)

在文件 bounded_queue.h110 行定义.

110 {
111 uint64_t new_tail = 0;
112 uint64_t old_commit = 0;
113 uint64_t old_tail = tail_.load(std::memory_order_acquire);
114 do {
115 new_tail = old_tail + 1;
116 if (GetIndex(new_tail) == GetIndex(head_.load(std::memory_order_acquire))) {
117 return false;
118 }
119 } while (!tail_.compare_exchange_weak(old_tail, new_tail,
120 std::memory_order_acq_rel,
121 std::memory_order_relaxed));
122 pool_[GetIndex(old_tail)] = element;
123 do {
124 old_commit = old_tail;
125 } while (cyber_unlikely(!commit_.compare_exchange_weak(
126 old_commit, new_tail, std::memory_order_acq_rel,
127 std::memory_order_relaxed)));
128 wait_strategy_->NotifyOne();
129 return true;
130}
#define cyber_unlikely(x)
Definition macros.h:30

◆ Enqueue() [2/2]

template<typename T >
bool apollo::cyber::base::BoundedQueue< T >::Enqueue ( T &&  element)

在文件 bounded_queue.h133 行定义.

133 {
134 uint64_t new_tail = 0;
135 uint64_t old_commit = 0;
136 uint64_t old_tail = tail_.load(std::memory_order_acquire);
137 do {
138 new_tail = old_tail + 1;
139 if (GetIndex(new_tail) == GetIndex(head_.load(std::memory_order_acquire))) {
140 return false;
141 }
142 } while (!tail_.compare_exchange_weak(old_tail, new_tail,
143 std::memory_order_acq_rel,
144 std::memory_order_relaxed));
145 pool_[GetIndex(old_tail)] = std::move(element);
146 do {
147 old_commit = old_tail;
148 } while (cyber_unlikely(!commit_.compare_exchange_weak(
149 old_commit, new_tail, std::memory_order_acq_rel,
150 std::memory_order_relaxed)));
151 wait_strategy_->NotifyOne();
152 return true;
153}

◆ Head()

template<typename T >
uint64_t apollo::cyber::base::BoundedQueue< T >::Head ( )
inline

在文件 bounded_queue.h59 行定义.

59{ return head_.load(); }

◆ Init() [1/2]

template<typename T >
bool apollo::cyber::base::BoundedQueue< T >::Init ( uint64_t  size)
inline

在文件 bounded_queue.h90 行定义.

90 {
91 return Init(size, new SleepWaitStrategy());
92}

◆ Init() [2/2]

template<typename T >
bool apollo::cyber::base::BoundedQueue< T >::Init ( uint64_t  size,
WaitStrategy strategy 
)

在文件 bounded_queue.h95 行定义.

95 {
96 // Head and tail each occupy a space
97 pool_size_ = size + 2;
98 pool_ = reinterpret_cast<T*>(std::calloc(pool_size_, sizeof(T)));
99 if (pool_ == nullptr) {
100 return false;
101 }
102 for (uint64_t i = 0; i < pool_size_; ++i) {
103 new (&(pool_[i])) T();
104 }
105 wait_strategy_.reset(strategy);
106 return true;
107}

◆ operator=()

template<typename T >
BoundedQueue & apollo::cyber::base::BoundedQueue< T >::operator= ( const BoundedQueue< T > &  other)
delete

◆ SetWaitStrategy()

template<typename T >
void apollo::cyber::base::BoundedQueue< T >::SetWaitStrategy ( WaitStrategy WaitStrategy)
inline

在文件 bounded_queue.h235 行定义.

235 {
236 wait_strategy_.reset(strategy);
237}

◆ Size()

template<typename T >
uint64_t apollo::cyber::base::BoundedQueue< T >::Size ( )
inline

在文件 bounded_queue.h220 行定义.

220 {
221 return tail_ - head_ - 1;
222}

◆ Tail()

template<typename T >
uint64_t apollo::cyber::base::BoundedQueue< T >::Tail ( )
inline

在文件 bounded_queue.h60 行定义.

60{ return tail_.load(); }

◆ WaitDequeue()

template<typename T >
bool apollo::cyber::base::BoundedQueue< T >::WaitDequeue ( T *  element)

在文件 bounded_queue.h204 行定义.

204 {
205 while (!break_all_wait_) {
206 if (Dequeue(element)) {
207 return true;
208 }
209 if (wait_strategy_->EmptyWait()) {
210 continue;
211 }
212 // wait timeout
213 break;
214 }
215
216 return false;
217}

◆ WaitEnqueue() [1/2]

template<typename T >
bool apollo::cyber::base::BoundedQueue< T >::WaitEnqueue ( const T &  element)

在文件 bounded_queue.h172 行定义.

172 {
173 while (!break_all_wait_) {
174 if (Enqueue(element)) {
175 return true;
176 }
177 if (wait_strategy_->EmptyWait()) {
178 continue;
179 }
180 // wait timeout
181 break;
182 }
183
184 return false;
185}
bool Enqueue(const T &element)

◆ WaitEnqueue() [2/2]

template<typename T >
bool apollo::cyber::base::BoundedQueue< T >::WaitEnqueue ( T &&  element)

在文件 bounded_queue.h188 行定义.

188 {
189 while (!break_all_wait_) {
190 if (Enqueue(std::move(element))) {
191 return true;
192 }
193 if (wait_strategy_->EmptyWait()) {
194 continue;
195 }
196 // wait timeout
197 break;
198 }
199
200 return false;
201}

该类的文档由以下文件生成: