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 | |
Prabir Pradhan | 4810866 | 2022-09-09 21:22:04 +0000 | [diff] [blame] | 17 | #pragma once |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 18 | |
| 19 | #include <condition_variable> |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 20 | #include <list> |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 21 | #include <mutex> |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 22 | #include <optional> |
| 23 | #include "android-base/thread_annotations.h" |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | /** |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 28 | * A thread-safe FIFO queue. This list-backed queue stores up to <i>capacity</i> objects if |
| 29 | * a capacity is provided at construction, and is otherwise unbounded. |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 30 | * Objects can always be added. Objects are added immediately. |
| 31 | * If the queue is full, new objects cannot be added. |
| 32 | * |
| 33 | * The action of retrieving an object will block until an element is available. |
| 34 | */ |
| 35 | template <class T> |
| 36 | class BlockingQueue { |
| 37 | public: |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 38 | explicit BlockingQueue() = default; |
| 39 | |
| 40 | explicit BlockingQueue(size_t capacity) : mCapacity(capacity){}; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Retrieve and remove the oldest object. |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 44 | * Blocks execution indefinitely while queue is empty. |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 45 | */ |
| 46 | T pop() { |
Siarhei Vishniakou | 443ad90 | 2019-03-06 17:25:41 -0800 | [diff] [blame] | 47 | std::unique_lock lock(mLock); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 48 | android::base::ScopedLockAssertion assumeLock(mLock); |
Siarhei Vishniakou | e3021d7 | 2020-02-28 15:25:41 -0800 | [diff] [blame] | 49 | mHasElements.wait(lock, [this]() REQUIRES(mLock) { return !this->mQueue.empty(); }); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 50 | T t = std::move(mQueue.front()); |
| 51 | mQueue.erase(mQueue.begin()); |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 52 | return t; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | /** |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 56 | * Retrieve and remove the oldest object. |
| 57 | * Blocks execution for the given duration while queue is empty, and returns std::nullopt |
| 58 | * if the queue was empty for the entire duration. |
| 59 | */ |
| 60 | std::optional<T> popWithTimeout(std::chrono::nanoseconds duration) { |
| 61 | std::unique_lock lock(mLock); |
| 62 | android::base::ScopedLockAssertion assumeLock(mLock); |
| 63 | if (!mHasElements.wait_for(lock, duration, |
| 64 | [this]() REQUIRES(mLock) { return !this->mQueue.empty(); })) { |
| 65 | return {}; |
| 66 | } |
| 67 | T t = std::move(mQueue.front()); |
| 68 | mQueue.erase(mQueue.begin()); |
| 69 | return t; |
| 70 | }; |
| 71 | |
| 72 | /** |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 73 | * Add a new object to the queue. |
| 74 | * Does not block. |
| 75 | * Return true if an element was successfully added. |
| 76 | * Return false if the queue is full. |
| 77 | */ |
| 78 | bool push(T&& t) { |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 79 | { // acquire lock |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 80 | std::scoped_lock lock(mLock); |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 81 | if (mCapacity && mQueue.size() == mCapacity) { |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | mQueue.push_back(std::move(t)); |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 85 | } // release lock |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 86 | mHasElements.notify_one(); |
| 87 | return true; |
| 88 | }; |
| 89 | |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 90 | /** |
| 91 | * Construct a new object into the queue. |
| 92 | * Does not block. |
| 93 | * Return true if an element was successfully added. |
| 94 | * Return false if the queue is full. |
| 95 | */ |
| 96 | template <class... Args> |
| 97 | bool emplace(Args&&... args) { |
| 98 | { // acquire lock |
| 99 | std::scoped_lock lock(mLock); |
| 100 | if (mCapacity && mQueue.size() == mCapacity) { |
| 101 | return false; |
| 102 | } |
| 103 | mQueue.emplace_back(args...); |
| 104 | } // release lock |
| 105 | mHasElements.notify_one(); |
| 106 | return true; |
| 107 | }; |
| 108 | |
| 109 | void erase_if(const std::function<bool(const T&)>& pred) { |
Siarhei Vishniakou | 61291d4 | 2019-02-11 18:13:20 -0800 | [diff] [blame] | 110 | std::scoped_lock lock(mLock); |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 111 | std::erase_if(mQueue, pred); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Remove all elements. |
| 116 | * Does not block. |
| 117 | */ |
| 118 | void clear() { |
| 119 | std::scoped_lock lock(mLock); |
| 120 | mQueue.clear(); |
| 121 | }; |
| 122 | |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 123 | /** |
| 124 | * How many elements are currently stored in the queue. |
| 125 | * Primary used for debugging. |
| 126 | * Does not block. |
| 127 | */ |
| 128 | size_t size() { |
| 129 | std::scoped_lock lock(mLock); |
| 130 | return mQueue.size(); |
| 131 | } |
| 132 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 133 | private: |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 134 | const std::optional<size_t> mCapacity; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 135 | /** |
| 136 | * Used to signal that mQueue is non-empty. |
| 137 | */ |
| 138 | std::condition_variable mHasElements; |
| 139 | /** |
| 140 | * Lock for accessing and waiting on elements. |
| 141 | */ |
| 142 | std::mutex mLock; |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 143 | std::list<T> mQueue GUARDED_BY(mLock); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 144 | }; |
| 145 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 146 | } // namespace android |