Move BlockingQueue.h to libinput
This is already useful in several places, and it's going to be also
useful in the future when InputConsumer is refactored.
So let's move it to include/input.
Bug: 311142655
Test: m inputflinger_blocking_queue_fuzzer
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:3782af62ea590e8945f2175a90aa2b6f4995814d)
Merged-In: Idfc492c6bfc3cccab7e0b0d12b21a41a954cc44b
Change-Id: Ie662961b77d331cd62b529bae0fd66e56a69bd19
diff --git a/services/inputflinger/BlockingQueue.h b/services/inputflinger/BlockingQueue.h
deleted file mode 100644
index f848c82..0000000
--- a/services/inputflinger/BlockingQueue.h
+++ /dev/null
@@ -1,147 +0,0 @@
-/*
- * Copyright (C) 2019 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#pragma once
-
-#include <condition_variable>
-#include <functional>
-#include <list>
-#include <mutex>
-#include <optional>
-#include "android-base/thread_annotations.h"
-
-namespace android {
-
-/**
- * A thread-safe FIFO queue. This list-backed queue stores up to <i>capacity</i> objects if
- * a capacity is provided at construction, and is otherwise unbounded.
- * Objects can always be added. Objects are added immediately.
- * If the queue is full, new objects cannot be added.
- *
- * The action of retrieving an object will block until an element is available.
- */
-template <class T>
-class BlockingQueue {
-public:
- explicit BlockingQueue() = default;
-
- explicit BlockingQueue(size_t capacity) : mCapacity(capacity){};
-
- /**
- * Retrieve and remove the oldest object.
- * Blocks execution indefinitely while queue is empty.
- */
- T pop() {
- std::unique_lock lock(mLock);
- android::base::ScopedLockAssertion assumeLock(mLock);
- mHasElements.wait(lock, [this]() REQUIRES(mLock) { return !this->mQueue.empty(); });
- T t = std::move(mQueue.front());
- mQueue.erase(mQueue.begin());
- return t;
- };
-
- /**
- * Retrieve and remove the oldest object.
- * Blocks execution for the given duration while queue is empty, and returns std::nullopt
- * if the queue was empty for the entire duration.
- */
- std::optional<T> popWithTimeout(std::chrono::nanoseconds duration) {
- std::unique_lock lock(mLock);
- android::base::ScopedLockAssertion assumeLock(mLock);
- if (!mHasElements.wait_for(lock, duration,
- [this]() REQUIRES(mLock) { return !this->mQueue.empty(); })) {
- return {};
- }
- T t = std::move(mQueue.front());
- mQueue.erase(mQueue.begin());
- return t;
- };
-
- /**
- * Add a new object to the queue.
- * Does not block.
- * Return true if an element was successfully added.
- * Return false if the queue is full.
- */
- bool push(T&& t) {
- { // acquire lock
- std::scoped_lock lock(mLock);
- if (mCapacity && mQueue.size() == mCapacity) {
- return false;
- }
- mQueue.push_back(std::move(t));
- } // release lock
- mHasElements.notify_one();
- return true;
- };
-
- /**
- * Construct a new object into the queue.
- * Does not block.
- * Return true if an element was successfully added.
- * Return false if the queue is full.
- */
- template <class... Args>
- bool emplace(Args&&... args) {
- { // acquire lock
- std::scoped_lock lock(mLock);
- if (mCapacity && mQueue.size() == mCapacity) {
- return false;
- }
- mQueue.emplace_back(args...);
- } // release lock
- mHasElements.notify_one();
- return true;
- };
-
- void erase_if(const std::function<bool(const T&)>& pred) {
- std::scoped_lock lock(mLock);
- std::erase_if(mQueue, pred);
- }
-
- /**
- * Remove all elements.
- * Does not block.
- */
- void clear() {
- std::scoped_lock lock(mLock);
- mQueue.clear();
- };
-
- /**
- * How many elements are currently stored in the queue.
- * Primary used for debugging.
- * Does not block.
- */
- size_t size() {
- std::scoped_lock lock(mLock);
- return mQueue.size();
- }
-
-private:
- const std::optional<size_t> mCapacity;
- /**
- * Used to signal that mQueue is non-empty.
- */
- std::condition_variable mHasElements;
- /**
- * Lock for accessing and waiting on elements.
- */
- std::mutex mLock;
- std::list<T> mQueue GUARDED_BY(mLock);
-};
-
-} // namespace android
diff --git a/services/inputflinger/InputProcessor.h b/services/inputflinger/InputProcessor.h
index dcbfebc..7a00a2d 100644
--- a/services/inputflinger/InputProcessor.h
+++ b/services/inputflinger/InputProcessor.h
@@ -22,7 +22,7 @@
#include <unordered_map>
#include <aidl/android/hardware/input/processor/IInputProcessor.h>
-#include "BlockingQueue.h"
+#include <input/BlockingQueue.h>
#include "InputListener.h"
namespace android {
diff --git a/services/inputflinger/tests/Android.bp b/services/inputflinger/tests/Android.bp
index 2a03ecc..9c9f643 100644
--- a/services/inputflinger/tests/Android.bp
+++ b/services/inputflinger/tests/Android.bp
@@ -39,7 +39,6 @@
],
srcs: [
"AnrTracker_test.cpp",
- "BlockingQueue_test.cpp",
"CapturedTouchpadEventConverter_test.cpp",
"CursorInputMapper_test.cpp",
"EventHub_test.cpp",
diff --git a/services/inputflinger/tests/BlockingQueue_test.cpp b/services/inputflinger/tests/BlockingQueue_test.cpp
deleted file mode 100644
index 754a5c4..0000000
--- a/services/inputflinger/tests/BlockingQueue_test.cpp
+++ /dev/null
@@ -1,155 +0,0 @@
-/*
- * Copyright (C) 2019 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "../BlockingQueue.h"
-
-
-#include <gtest/gtest.h>
-#include <thread>
-
-namespace android {
-
-using std::chrono_literals::operator""ns;
-
-// --- BlockingQueueTest ---
-
-/**
- * Validate basic pop and push operation.
- */
-TEST(BlockingQueueTest, Queue_AddAndRemove) {
- constexpr size_t capacity = 10;
- BlockingQueue<int> queue(capacity);
-
- 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));
-}
-
-/**
- * Make sure the queue has strict capacity limits.
- */
-TEST(BlockingQueueTest, Queue_ReachesCapacity) {
- constexpr size_t capacity = 3;
- BlockingQueue<int> queue(capacity);
-
- // First 3 elements should be added successfully
- ASSERT_TRUE(queue.push(1));
- ASSERT_TRUE(queue.push(2));
- ASSERT_TRUE(queue.push(3));
- ASSERT_FALSE(queue.push(4)) << "Queue should reach capacity at size " << capacity;
-}
-
-/**
- * Make sure the queue maintains FIFO order.
- * Add elements and remove them, and check the order.
- */
-TEST(BlockingQueueTest, Queue_isFIFO) {
- constexpr size_t capacity = 10;
- BlockingQueue<int> queue(capacity);
-
- for (size_t i = 0; i < capacity; i++) {
- ASSERT_TRUE(queue.push(static_cast<int>(i)));
- }
- for (size_t i = 0; i < capacity; i++) {
- ASSERT_EQ(queue.pop(), static_cast<int>(i));
- }
-}
-
-TEST(BlockingQueueTest, Queue_Clears) {
- constexpr size_t capacity = 2;
- BlockingQueue<int> queue(capacity);
-
- queue.push(1);
- queue.push(2);
- queue.clear();
- queue.push(3);
- // Should no longer receive elements 1 and 2
- ASSERT_EQ(3, queue.pop());
-}
-
-TEST(BlockingQueueTest, Queue_Erases) {
- constexpr size_t capacity = 4;
- BlockingQueue<int> queue(capacity);
-
- queue.push(1);
- queue.push(2);
- queue.push(3);
- queue.push(4);
- // Erase elements 2 and 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());
-}
-
-// --- BlockingQueueTest - Multiple threads ---
-
-TEST(BlockingQueueTest, Queue_AllowsMultipleThreads) {
- constexpr size_t capacity = 100; // large capacity to increase likelihood that threads overlap
- BlockingQueue<int> queue(capacity);
-
- // Fill queue from a different thread
- std::thread fillQueue([&queue](){
- for (size_t i = 0; i < capacity; i++) {
- ASSERT_TRUE(queue.push(static_cast<int>(i)));
- }
- });
-
- // Make sure all elements are received in correct order
- for (size_t i = 0; i < capacity; i++) {
- ASSERT_EQ(queue.pop(), static_cast<int>(i));
- }
-
- fillQueue.join();
-}
-
-/**
- * When the queue has no elements, and pop is called, it should block
- * the current thread until an element is added to the queue (from another thread).
- * Here we create a separate thread and call pop on an empty queue. Next,
- * we check that the thread is blocked.
- */
-TEST(BlockingQueueTest, Queue_BlocksWhileWaitingForElements) {
- constexpr size_t capacity = 1;
- BlockingQueue<int> queue(capacity);
-
- std::atomic_bool hasReceivedElement = false;
-
- // fill queue from a different thread
- std::thread waitUntilHasElements([&queue, &hasReceivedElement](){
- queue.pop(); // This should block until an element has been added
- hasReceivedElement = true;
- });
-
- ASSERT_FALSE(hasReceivedElement);
- queue.push(1);
- waitUntilHasElements.join();
- ASSERT_TRUE(hasReceivedElement);
-}
-
-TEST(BlockingQueueTest, Queue_TimesOut) {
- BlockingQueue<int> queue;
- ASSERT_EQ(std::nullopt, queue.popWithTimeout(1ns));
-}
-
-} // namespace android
diff --git a/services/inputflinger/tests/InputDispatcher_test.cpp b/services/inputflinger/tests/InputDispatcher_test.cpp
index 5002391..c2e67fa 100644
--- a/services/inputflinger/tests/InputDispatcher_test.cpp
+++ b/services/inputflinger/tests/InputDispatcher_test.cpp
@@ -15,7 +15,6 @@
*/
#include "../dispatcher/InputDispatcher.h"
-#include "../BlockingQueue.h"
#include "FakeApplicationHandle.h"
#include "TestEventMatchers.h"
@@ -31,6 +30,7 @@
#include <flag_macros.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
+#include <input/BlockingQueue.h>
#include <input/Input.h>
#include <input/PrintTools.h>
#include <linux/input.h>
diff --git a/services/inputflinger/tests/fuzzers/BlockingQueueFuzzer.cpp b/services/inputflinger/tests/fuzzers/BlockingQueueFuzzer.cpp
index 219b662..863d0a1 100644
--- a/services/inputflinger/tests/fuzzers/BlockingQueueFuzzer.cpp
+++ b/services/inputflinger/tests/fuzzers/BlockingQueueFuzzer.cpp
@@ -15,8 +15,8 @@
*/
#include <fuzzer/FuzzedDataProvider.h>
+#include <input/BlockingQueue.h>
#include <thread>
-#include "BlockingQueue.h"
// Chosen to be a number large enough for variation in fuzzer runs, but not consume too much memory.
static constexpr size_t MAX_CAPACITY = 1024;