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