Skip to content

Commit 287f998

Browse files
committed
Add empty, dump methods to BlockingQueue
BlockingQueue can now be dumped (since the user can't access its internal state, and it's too clunky to be making a copy just so that we can destructively print it). Also, "empty" method is useful when converting code from std containers to the BlockingQueue. At the same time, mark the "size" method as const. Bug: 376713684 Test: used in another CL in frameworks/base. build only here Flag: EXEMPT refactor Change-Id: I3fda00b793b512bdba4c85dbf561dfc5e9724601
1 parent 4dcf2fa commit 287f998

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

include/input/BlockingQueue.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#pragma once
1818

19+
#include <input/PrintTools.h>
1920
#include <condition_variable>
2021
#include <functional>
2122
#include <list>
@@ -126,11 +127,21 @@ class BlockingQueue {
126127
* Primary used for debugging.
127128
* Does not block.
128129
*/
129-
size_t size() {
130+
size_t size() const {
130131
std::scoped_lock lock(mLock);
131132
return mQueue.size();
132133
}
133134

135+
bool empty() const {
136+
std::scoped_lock lock(mLock);
137+
return mQueue.empty();
138+
}
139+
140+
std::string dump(std::string (*toString)(const T&) = constToString) const {
141+
std::scoped_lock lock(mLock);
142+
return dumpContainer(mQueue, toString);
143+
}
144+
134145
private:
135146
const std::optional<size_t> mCapacity;
136147
/**
@@ -140,7 +151,7 @@ class BlockingQueue {
140151
/**
141152
* Lock for accessing and waiting on elements.
142153
*/
143-
std::mutex mLock;
154+
mutable std::mutex mLock;
144155
std::list<T> mQueue GUARDED_BY(mLock);
145156
};
146157

0 commit comments

Comments
 (0)