21
21
#include < deque>
22
22
#include < memory>
23
23
24
- #include " cartographer/common /mutex.h"
24
+ #include " absl/synchronization /mutex.h"
25
25
#include " cartographer/common/port.h"
26
26
#include " cartographer/common/time.h"
27
27
#include " glog/logging.h"
@@ -47,17 +47,22 @@ class BlockingQueue {
47
47
48
48
// Pushes a value onto the queue. Blocks if the queue is full.
49
49
void Push (T t) {
50
- MutexLocker lock (&mutex_);
51
- lock.Await ([this ]() REQUIRES (mutex_) { return QueueNotFullCondition (); });
50
+ const auto predicate = [this ]() EXCLUSIVE_LOCKS_REQUIRED (mutex_) {
51
+ return QueueNotFullCondition ();
52
+ };
53
+ absl::MutexLock lock (&mutex_);
54
+ mutex_.Await (absl::Condition (&predicate));
52
55
deque_.push_back (std::move (t));
53
56
}
54
57
55
58
// Like push, but returns false if 'timeout' is reached.
56
59
bool PushWithTimeout (T t, const common::Duration timeout) {
57
- MutexLocker lock (&mutex_);
58
- if (!lock.AwaitWithTimeout (
59
- [this ]() REQUIRES (mutex_) { return QueueNotFullCondition (); },
60
- timeout)) {
60
+ const auto predicate = [this ]() EXCLUSIVE_LOCKS_REQUIRED (mutex_) {
61
+ return QueueNotFullCondition ();
62
+ };
63
+ absl::MutexLock lock (&mutex_);
64
+ if (!mutex_.AwaitWithTimeout (absl::Condition (&predicate),
65
+ absl::FromChrono (timeout))) {
61
66
return false ;
62
67
}
63
68
deque_.push_back (std::move (t));
@@ -66,8 +71,11 @@ class BlockingQueue {
66
71
67
72
// Pops the next value from the queue. Blocks until a value is available.
68
73
T Pop () {
69
- MutexLocker lock (&mutex_);
70
- lock.Await ([this ]() REQUIRES (mutex_) { return !QueueEmptyCondition (); });
74
+ const auto predicate = [this ]() EXCLUSIVE_LOCKS_REQUIRED (mutex_) {
75
+ return !QueueEmptyCondition ();
76
+ };
77
+ absl::MutexLock lock (&mutex_);
78
+ mutex_.Await (absl::Condition (&predicate));
71
79
72
80
T t = std::move (deque_.front ());
73
81
deque_.pop_front ();
@@ -76,10 +84,12 @@ class BlockingQueue {
76
84
77
85
// Like Pop, but can timeout. Returns nullptr in this case.
78
86
T PopWithTimeout (const common::Duration timeout) {
79
- MutexLocker lock (&mutex_);
80
- if (!lock.AwaitWithTimeout (
81
- [this ]() REQUIRES (mutex_) { return !QueueEmptyCondition (); },
82
- timeout)) {
87
+ const auto predicate = [this ]() EXCLUSIVE_LOCKS_REQUIRED (mutex_) {
88
+ return !QueueEmptyCondition ();
89
+ };
90
+ absl::MutexLock lock (&mutex_);
91
+ if (!mutex_.AwaitWithTimeout (absl::Condition (&predicate),
92
+ absl::FromChrono (timeout))) {
83
93
return nullptr ;
84
94
}
85
95
T t = std::move (deque_.front ());
@@ -90,10 +100,12 @@ class BlockingQueue {
90
100
// Like Peek, but can timeout. Returns nullptr in this case.
91
101
template <typename R>
92
102
R* PeekWithTimeout (const common::Duration timeout) {
93
- MutexLocker lock (&mutex_);
94
- if (!lock.AwaitWithTimeout (
95
- [this ]() REQUIRES (mutex_) { return !QueueEmptyCondition (); },
96
- timeout)) {
103
+ const auto predicate = [this ]() EXCLUSIVE_LOCKS_REQUIRED (mutex_) {
104
+ return !QueueEmptyCondition ();
105
+ };
106
+ absl::MutexLock lock (&mutex_);
107
+ if (!mutex_.AwaitWithTimeout (absl::Condition (&predicate),
108
+ absl::FromChrono (timeout))) {
97
109
return nullptr ;
98
110
}
99
111
return deque_.front ().get ();
@@ -104,7 +116,7 @@ class BlockingQueue {
104
116
// a pointer to the given type R.
105
117
template <typename R>
106
118
const R* Peek () {
107
- MutexLocker lock (&mutex_);
119
+ absl::MutexLock lock (&mutex_);
108
120
if (deque_.empty ()) {
109
121
return nullptr ;
110
122
}
@@ -113,26 +125,31 @@ class BlockingQueue {
113
125
114
126
// Returns the number of items currently in the queue.
115
127
size_t Size () {
116
- MutexLocker lock (&mutex_);
128
+ absl::MutexLock lock (&mutex_);
117
129
return deque_.size ();
118
130
}
119
131
120
132
// Blocks until the queue is empty.
121
133
void WaitUntilEmpty () {
122
- MutexLocker lock (&mutex_);
123
- lock.Await ([this ]() REQUIRES (mutex_) { return QueueEmptyCondition (); });
134
+ const auto predicate = [this ]() EXCLUSIVE_LOCKS_REQUIRED (mutex_) {
135
+ return QueueEmptyCondition ();
136
+ };
137
+ absl::MutexLock lock (&mutex_);
138
+ mutex_.Await (absl::Condition (&predicate));
124
139
}
125
140
126
141
private:
127
142
// Returns true iff the queue is empty.
128
- bool QueueEmptyCondition () REQUIRES(mutex_) { return deque_.empty (); }
143
+ bool QueueEmptyCondition () EXCLUSIVE_LOCKS_REQUIRED(mutex_) {
144
+ return deque_.empty ();
145
+ }
129
146
130
147
// Returns true iff the queue is not full.
131
- bool QueueNotFullCondition () REQUIRES (mutex_) {
148
+ bool QueueNotFullCondition () EXCLUSIVE_LOCKS_REQUIRED (mutex_) {
132
149
return queue_size_ == kInfiniteQueueSize || deque_.size () < queue_size_;
133
150
}
134
151
135
- Mutex mutex_;
152
+ absl:: Mutex mutex_;
136
153
const size_t queue_size_ GUARDED_BY (mutex_);
137
154
std::deque<T> deque_ GUARDED_BY (mutex_);
138
155
};
0 commit comments