From 287f99863b4147caa933d6bed86697dc2c605b45 Mon Sep 17 00:00:00 2001 From: Siarhei Vishniakou Date: Mon, 10 Feb 2025 18:38:51 -0800 Subject: 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 --- include/input/BlockingQueue.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/input/BlockingQueue.h b/include/input/BlockingQueue.h index f848c82c42..6e32de6d86 100644 --- a/include/input/BlockingQueue.h +++ b/include/input/BlockingQueue.h @@ -16,6 +16,7 @@ #pragma once +#include #include #include #include @@ -126,11 +127,21 @@ public: * Primary used for debugging. * Does not block. */ - size_t size() { + size_t size() const { std::scoped_lock lock(mLock); return mQueue.size(); } + bool empty() const { + std::scoped_lock lock(mLock); + return mQueue.empty(); + } + + std::string dump(std::string (*toString)(const T&) = constToString) const { + std::scoped_lock lock(mLock); + return dumpContainer(mQueue, toString); + } + private: const std::optional mCapacity; /** @@ -140,7 +151,7 @@ private: /** * Lock for accessing and waiting on elements. */ - std::mutex mLock; + mutable std::mutex mLock; std::list mQueue GUARDED_BY(mLock); }; -- cgit v1.2.3-59-g8ed1b