blob: fe3728789de7375d36368492e5dc5648f2f533a1 [file] [log] [blame]
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001/*
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 Pradhan48108662022-09-09 21:22:04 +000017#pragma once
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080018
Siarhei Vishniakou61291d42019-02-11 18:13:20 -080019#include "android-base/thread_annotations.h"
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080020#include <condition_variable>
21#include <mutex>
22#include <vector>
23
24namespace 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 */
33template <class T>
34class BlockingQueue {
35public:
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() {
Siarhei Vishniakou443ad902019-03-06 17:25:41 -080045 std::unique_lock lock(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -080046 android::base::ScopedLockAssertion assumeLock(mLock);
Siarhei Vishniakoue3021d72020-02-28 15:25:41 -080047 mHasElements.wait(lock, [this]() REQUIRES(mLock) { return !this->mQueue.empty(); });
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080048 T t = std::move(mQueue.front());
49 mQueue.erase(mQueue.begin());
Siarhei Vishniakou61291d42019-02-11 18:13:20 -080050 return t;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080051 };
52
53 /**
54 * Add a new object to the queue.
55 * Does not block.
56 * Return true if an element was successfully added.
57 * Return false if the queue is full.
58 */
59 bool push(T&& t) {
Siarhei Vishniakou61291d42019-02-11 18:13:20 -080060 {
61 std::scoped_lock lock(mLock);
62 if (mQueue.size() == mCapacity) {
63 return false;
64 }
65 mQueue.push_back(std::move(t));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080066 }
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080067 mHasElements.notify_one();
68 return true;
69 };
70
71 void erase(const std::function<bool(const T&)>& lambda) {
Siarhei Vishniakou61291d42019-02-11 18:13:20 -080072 std::scoped_lock lock(mLock);
Siarhei Vishniakouf47c339e2021-12-30 11:22:26 -080073 std::erase_if(mQueue, [&lambda](const auto& t) { return lambda(t); });
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080074 }
75
76 /**
77 * Remove all elements.
78 * Does not block.
79 */
80 void clear() {
81 std::scoped_lock lock(mLock);
82 mQueue.clear();
83 };
84
Siarhei Vishniakoua028c442019-02-04 14:33:23 -080085 /**
86 * How many elements are currently stored in the queue.
87 * Primary used for debugging.
88 * Does not block.
89 */
90 size_t size() {
91 std::scoped_lock lock(mLock);
92 return mQueue.size();
93 }
94
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080095private:
Siarhei Vishniakou61291d42019-02-11 18:13:20 -080096 const size_t mCapacity;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080097 /**
98 * Used to signal that mQueue is non-empty.
99 */
100 std::condition_variable mHasElements;
101 /**
102 * Lock for accessing and waiting on elements.
103 */
104 std::mutex mLock;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800105 std::vector<T> mQueue GUARDED_BY(mLock);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800106};
107
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800108} // namespace android