Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2023 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 | #pragma once |
| 18 | |
| 19 | #include <utils/threads.h> |
| 20 | #include <list> |
| 21 | #include <mutex> |
| 22 | #include <optional> |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | /** A thread-safe FIFO queue. */ |
| 27 | template <class T> |
| 28 | class SyncQueue { |
| 29 | public: |
Prabir Pradhan | 047695b | 2023-06-30 01:48:45 +0000 | [diff] [blame] | 30 | SyncQueue() = default; |
| 31 | |
| 32 | SyncQueue(size_t capacity) : mCapacity(capacity) {} |
| 33 | |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 34 | /** Retrieve and remove the oldest object. Returns std::nullopt if the queue is empty. */ |
| 35 | std::optional<T> pop() { |
| 36 | std::scoped_lock lock(mLock); |
| 37 | if (mQueue.empty()) { |
| 38 | return {}; |
| 39 | } |
| 40 | T t = std::move(mQueue.front()); |
| 41 | mQueue.erase(mQueue.begin()); |
| 42 | return t; |
| 43 | }; |
| 44 | |
Prabir Pradhan | 047695b | 2023-06-30 01:48:45 +0000 | [diff] [blame] | 45 | /** |
| 46 | * Add a new object to the queue. |
| 47 | * Return true if an element was successfully added. |
| 48 | * Return false if the queue is full. |
| 49 | */ |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 50 | template <class... Args> |
Prabir Pradhan | 047695b | 2023-06-30 01:48:45 +0000 | [diff] [blame] | 51 | bool push(Args&&... args) { |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 52 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 047695b | 2023-06-30 01:48:45 +0000 | [diff] [blame] | 53 | if (mCapacity && mQueue.size() == mCapacity) { |
| 54 | return false; |
| 55 | } |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 56 | mQueue.emplace_back(args...); |
Prabir Pradhan | 047695b | 2023-06-30 01:48:45 +0000 | [diff] [blame] | 57 | return true; |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | private: |
Prabir Pradhan | 047695b | 2023-06-30 01:48:45 +0000 | [diff] [blame] | 61 | const std::optional<size_t> mCapacity; |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 62 | std::mutex mLock; |
| 63 | std::list<T> mQueue GUARDED_BY(mLock); |
| 64 | }; |
| 65 | |
| 66 | } // namespace android |