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 | |
| 20 | #include <condition_variable> |
| 21 | #include <mutex> |
| 22 | #include <vector> |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | /** |
| 27 | * A FIFO queue that stores up to <i>capacity</i> objects. |
| 28 | * Objects can always be added. Objects are added immediately. |
| 29 | * If the queue is full, new objects cannot be added. |
| 30 | * |
| 31 | * The action of retrieving an object will block until an element is available. |
| 32 | */ |
| 33 | template <class T> |
| 34 | class BlockingQueue { |
| 35 | public: |
| 36 | BlockingQueue(size_t capacity) : mCapacity(capacity) { |
| 37 | mQueue.reserve(mCapacity); |
| 38 | }; |
| 39 | |
| 40 | /** |
| 41 | * Retrieve and remove the oldest object. |
| 42 | * Blocks execution while queue is empty. |
| 43 | */ |
| 44 | T pop() { |
| 45 | std::unique_lock<std::mutex> lock(mLock); |
| 46 | mHasElements.wait(lock, [this]{ return !this->mQueue.empty(); }); |
| 47 | T t = std::move(mQueue.front()); |
| 48 | mQueue.erase(mQueue.begin()); |
| 49 | return std::move(t); |
| 50 | }; |
| 51 | |
| 52 | /** |
| 53 | * Add a new object to the queue. |
| 54 | * Does not block. |
| 55 | * Return true if an element was successfully added. |
| 56 | * Return false if the queue is full. |
| 57 | */ |
| 58 | bool push(T&& t) { |
| 59 | std::unique_lock<std::mutex> lock(mLock); |
| 60 | if (mQueue.size() == mCapacity) { |
| 61 | return false; |
| 62 | } |
| 63 | mQueue.push_back(std::move(t)); |
| 64 | mHasElements.notify_one(); |
| 65 | return true; |
| 66 | }; |
| 67 | |
| 68 | void erase(const std::function<bool(const T&)>& lambda) { |
| 69 | std::unique_lock<std::mutex> lock(mLock); |
| 70 | mQueue.erase(std::remove_if(mQueue.begin(), mQueue.end(), |
| 71 | [&lambda](const T& t) { return lambda(t); }), mQueue.end()); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Remove all elements. |
| 76 | * Does not block. |
| 77 | */ |
| 78 | void clear() { |
| 79 | std::scoped_lock lock(mLock); |
| 80 | mQueue.clear(); |
| 81 | }; |
| 82 | |
| 83 | private: |
| 84 | size_t mCapacity; |
| 85 | /** |
| 86 | * Used to signal that mQueue is non-empty. |
| 87 | */ |
| 88 | std::condition_variable mHasElements; |
| 89 | /** |
| 90 | * Lock for accessing and waiting on elements. |
| 91 | */ |
| 92 | std::mutex mLock; |
| 93 | std::vector<T> mQueue; //GUARDED_BY(mLock) |
| 94 | }; |
| 95 | |
| 96 | |
| 97 | } // namespace android |
| 98 | #endif |