• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
30     SyncQueue() = default;
31 
SyncQueue(size_t capacity)32     SyncQueue(size_t capacity) : mCapacity(capacity) {}
33 
34     /** Retrieve and remove the oldest object. Returns std::nullopt if the queue is empty. */
pop()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 
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      */
50     template <class... Args>
push(Args &&...args)51     bool push(Args&&... args) {
52         std::scoped_lock lock(mLock);
53         if (mCapacity && mQueue.size() == mCapacity) {
54             return false;
55         }
56         mQueue.emplace_back(args...);
57         return true;
58     };
59 
60 private:
61     const std::optional<size_t> mCapacity;
62     std::mutex mLock;
63     std::list<T> mQueue GUARDED_BY(mLock);
64 };
65 
66 } // namespace android
67