Apollo
11.0
自动驾驶开放平台
sync_queue.h
浏览该文件的文档.
1
/*********************************************************************************************************************
2
Copyright (c) 2020 RoboSense
3
All rights reserved
4
5
By downloading, copying, installing or using the software you agree to this
6
license. If you do not agree to this license, do not download, install, copy or
7
use the software.
8
9
License Agreement
10
For RoboSense LiDAR SDK Library
11
(3-clause BSD License)
12
13
Redistribution and use in source and binary forms, with or without modification,
14
are permitted provided that the following conditions are met:
15
16
1. Redistributions of source code must retain the above copyright notice, this
17
list of conditions and the following disclaimer.
18
19
2. Redistributions in binary form must reproduce the above copyright notice,
20
this list of conditions and the following disclaimer in the documentation and/or
21
other materials provided with the distribution.
22
23
3. Neither the names of the RoboSense, nor Suteng Innovation Technology, nor the
24
names of other contributors may be used to endorse or promote products derived
25
from this software without specific prior written permission.
26
27
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
28
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
31
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
34
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
*********************************************************************************************************************/
38
/******************************************************************************
39
* Copyright 2024 The Apollo Authors. All Rights Reserved.
40
*
41
* Licensed under the Apache License, Version 2.0 (the "License");
42
* you may not use this file except in compliance with the License.
43
* You may obtain a copy of the License at
44
*
45
* http://www.apache.org/licenses/LICENSE-2.0
46
*
47
* Unless required by applicable law or agreed to in writing, software
48
* distributed under the License is distributed on an "AS IS" BASIS,
49
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
50
* See the License for the specific language governing permissions and
51
* limitations under the License.
52
*****************************************************************************/
53
#pragma once
54
55
#include <condition_variable>
56
#include <mutex>
57
#include <queue>
58
#include <thread>
59
#include <utility>
60
61
namespace
apollo
{
62
namespace
lidar {
63
namespace
drivers {
64
65
template
<
typename
T>
66
class
SyncQueue
{
67
public
:
68
inline
size_t
push
(
const
T& value) {
69
bool
empty =
false
;
70
size_t
size = 0;
71
72
{
73
std::lock_guard<std::mutex> lg(mtx_);
74
empty = queue_.empty();
75
queue_.push(value);
76
size = queue_.size();
77
}
78
79
if
(empty)
80
cv_.notify_one();
81
return
size;
82
}
83
84
inline
T
pop
() {
85
T value;
86
87
std::lock_guard<std::mutex> lg(mtx_);
88
if
(!queue_.empty()) {
89
value = queue_.front();
90
queue_.pop();
91
}
92
93
return
value;
94
}
95
96
inline
bool
popWait
(T& ret_ele,
unsigned
int
usec = 1000000) {
97
{
98
std::lock_guard<std::mutex> lg(mtx_);
99
if
(!queue_.empty()) {
100
ret_ele = queue_.front();
101
queue_.pop();
102
return
true
;
103
}
104
}
105
106
std::this_thread::sleep_for(std::chrono::microseconds(1000));
107
return
false
;
108
}
109
110
inline
void
clear
() {
111
std::queue<T> empty;
112
std::lock_guard<std::mutex> lg(mtx_);
113
swap(empty, queue_);
114
}
115
116
private
:
117
std::queue<T> queue_;
118
std::mutex mtx_;
119
std::condition_variable cv_;
120
};
121
122
}
// namespace drivers
123
}
// namespace lidar
124
}
// namespace apollo
apollo::lidar::drivers::SyncQueue
Definition
sync_queue.h:66
apollo::lidar::drivers::SyncQueue::popWait
bool popWait(T &ret_ele, unsigned int usec=1000000)
Definition
sync_queue.h:96
apollo::lidar::drivers::SyncQueue::push
size_t push(const T &value)
Definition
sync_queue.h:68
apollo::lidar::drivers::SyncQueue::pop
T pop()
Definition
sync_queue.h:84
apollo::lidar::drivers::SyncQueue::clear
void clear()
Definition
sync_queue.h:110
apollo
class register implement
Definition
arena_queue.h:37
modules
drivers
lidar
common
sync_queue.h