| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2019 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #ifndef _UI_INPUT_BLOCKING_QUEUE_H | 
|  | 18 | #define _UI_INPUT_BLOCKING_QUEUE_H | 
|  | 19 |  | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 20 | #include "android-base/thread_annotations.h" | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 21 | #include <condition_variable> | 
|  | 22 | #include <mutex> | 
|  | 23 | #include <vector> | 
|  | 24 |  | 
|  | 25 | namespace android { | 
|  | 26 |  | 
|  | 27 | /** | 
|  | 28 | * A FIFO queue that stores up to <i>capacity</i> objects. | 
|  | 29 | * Objects can always be added. Objects are added immediately. | 
|  | 30 | * If the queue is full, new objects cannot be added. | 
|  | 31 | * | 
|  | 32 | * The action of retrieving an object will block until an element is available. | 
|  | 33 | */ | 
|  | 34 | template <class T> | 
|  | 35 | class BlockingQueue { | 
|  | 36 | public: | 
|  | 37 | BlockingQueue(size_t capacity) : mCapacity(capacity) { | 
|  | 38 | mQueue.reserve(mCapacity); | 
|  | 39 | }; | 
|  | 40 |  | 
|  | 41 | /** | 
|  | 42 | * Retrieve and remove the oldest object. | 
|  | 43 | * Blocks execution while queue is empty. | 
|  | 44 | */ | 
|  | 45 | T pop() { | 
| Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 46 | std::unique_lock lock(mLock); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 47 | android::base::ScopedLockAssertion assumeLock(mLock); | 
| Siarhei Vishniakou | e3021d7 | 2020-02-28 15:25:41 -0800 | [diff] [blame] | 48 | mHasElements.wait(lock, [this]() REQUIRES(mLock) { return !this->mQueue.empty(); }); | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 49 | T t = std::move(mQueue.front()); | 
|  | 50 | mQueue.erase(mQueue.begin()); | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 51 | return t; | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 52 | }; | 
|  | 53 |  | 
|  | 54 | /** | 
|  | 55 | * Add a new object to the queue. | 
|  | 56 | * Does not block. | 
|  | 57 | * Return true if an element was successfully added. | 
|  | 58 | * Return false if the queue is full. | 
|  | 59 | */ | 
|  | 60 | bool push(T&& t) { | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 61 | { | 
|  | 62 | std::scoped_lock lock(mLock); | 
|  | 63 | if (mQueue.size() == mCapacity) { | 
|  | 64 | return false; | 
|  | 65 | } | 
|  | 66 | mQueue.push_back(std::move(t)); | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 67 | } | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 68 | mHasElements.notify_one(); | 
|  | 69 | return true; | 
|  | 70 | }; | 
|  | 71 |  | 
|  | 72 | void erase(const std::function<bool(const T&)>& lambda) { | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 73 | std::scoped_lock lock(mLock); | 
| Siarhei Vishniakou | f47c339e | 2021-12-30 11:22:26 -0800 | [diff] [blame] | 74 | std::erase_if(mQueue, [&lambda](const auto& t) { return lambda(t); }); | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
|  | 77 | /** | 
|  | 78 | * Remove all elements. | 
|  | 79 | * Does not block. | 
|  | 80 | */ | 
|  | 81 | void clear() { | 
|  | 82 | std::scoped_lock lock(mLock); | 
|  | 83 | mQueue.clear(); | 
|  | 84 | }; | 
|  | 85 |  | 
| Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 86 | /** | 
|  | 87 | * How many elements are currently stored in the queue. | 
|  | 88 | * Primary used for debugging. | 
|  | 89 | * Does not block. | 
|  | 90 | */ | 
|  | 91 | size_t size() { | 
|  | 92 | std::scoped_lock lock(mLock); | 
|  | 93 | return mQueue.size(); | 
|  | 94 | } | 
|  | 95 |  | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 96 | private: | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 97 | const size_t mCapacity; | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 98 | /** | 
|  | 99 | * Used to signal that mQueue is non-empty. | 
|  | 100 | */ | 
|  | 101 | std::condition_variable mHasElements; | 
|  | 102 | /** | 
|  | 103 | * Lock for accessing and waiting on elements. | 
|  | 104 | */ | 
|  | 105 | std::mutex mLock; | 
| Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 106 | std::vector<T> mQueue GUARDED_BY(mLock); | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 107 | }; | 
|  | 108 |  | 
|  | 109 |  | 
|  | 110 | } // namespace android | 
|  | 111 | #endif |