Improve BlockingQueue and add SyncQueue
Changes to BlockingQueue:
- Change BlockingQueue to be list-backed instead of vector-backed so
that removals are O(1) instead of O(N).
- Rename erase to erase_if.
- Make providing a fixed capacity at construction optional.
- Add emplace function to push and construct element in-place.
- Add popWithTimeout function.
Bug: 275726706
Test: atest inputflinger_tests
Change-Id: I1be02b0887df2c21b28f4f1cb43a8e208d996a87
diff --git a/services/inputflinger/tests/BlockingQueue_test.cpp b/services/inputflinger/tests/BlockingQueue_test.cpp
index fd9d9d5..754a5c4 100644
--- a/services/inputflinger/tests/BlockingQueue_test.cpp
+++ b/services/inputflinger/tests/BlockingQueue_test.cpp
@@ -22,6 +22,7 @@
namespace android {
+using std::chrono_literals::operator""ns;
// --- BlockingQueueTest ---
@@ -34,6 +35,14 @@
ASSERT_TRUE(queue.push(1));
ASSERT_EQ(queue.pop(), 1);
+
+ ASSERT_TRUE(queue.emplace(2));
+ ASSERT_EQ(queue.popWithTimeout(0ns), 2);
+
+ ASSERT_TRUE(queue.push(3));
+ ASSERT_EQ(queue.popWithTimeout(100ns), 3);
+
+ ASSERT_EQ(std::nullopt, queue.popWithTimeout(0ns));
}
/**
@@ -87,7 +96,7 @@
queue.push(3);
queue.push(4);
// Erase elements 2 and 4
- queue.erase([](int element) { return element == 2 || element == 4; });
+ queue.erase_if([](int element) { return element == 2 || element == 4; });
// Should no longer receive elements 2 and 4
ASSERT_EQ(1, queue.pop());
ASSERT_EQ(3, queue.pop());
@@ -138,5 +147,9 @@
ASSERT_TRUE(hasReceivedElement);
}
+TEST(BlockingQueueTest, Queue_TimesOut) {
+ BlockingQueue<int> queue;
+ ASSERT_EQ(std::nullopt, queue.popWithTimeout(1ns));
+}
} // namespace android