blob: 032cb6dea3ab7c710e5015835916d44124f4d54c [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
17#ifndef _UI_INPUT_BLOCKING_QUEUE_H
18#define _UI_INPUT_BLOCKING_QUEUE_H
19
20#include <condition_variable>
Ryan Prichardbba43422023-09-19 23:54:04 -070021#include <functional>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080022#include <mutex>
23#include <vector>
Ryan Prichardbba43422023-09-19 23:54:04 -070024#include "android-base/thread_annotations.h"
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080025
26namespace android {
27
28/**
29 * A FIFO queue that stores up to <i>capacity</i> objects.
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 */
35template <class T>
36class BlockingQueue {
37public:
38 BlockingQueue(size_t capacity) : mCapacity(capacity) {
39 mQueue.reserve(mCapacity);
40 };
41
42 /**
43 * Retrieve and remove the oldest object.
44 * Blocks execution while queue is empty.
45 */
46 T pop() {
Siarhei Vishniakou443ad902019-03-06 17:25:41 -080047 std::unique_lock lock(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -080048 android::base::ScopedLockAssertion assumeLock(mLock);
Siarhei Vishniakoue3021d72020-02-28 15:25:41 -080049 mHasElements.wait(lock, [this]() REQUIRES(mLock) { return !this->mQueue.empty(); });
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080050 T t = std::move(mQueue.front());
51 mQueue.erase(mQueue.begin());
Siarhei Vishniakou61291d42019-02-11 18:13:20 -080052 return t;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080053 };
54
55 /**
56 * Add a new object to the queue.
57 * Does not block.
58 * Return true if an element was successfully added.
59 * Return false if the queue is full.
60 */
61 bool push(T&& t) {
Siarhei Vishniakou61291d42019-02-11 18:13:20 -080062 {
63 std::scoped_lock lock(mLock);
64 if (mQueue.size() == mCapacity) {
65 return false;
66 }
67 mQueue.push_back(std::move(t));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080068 }
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080069 mHasElements.notify_one();
70 return true;
71 };
72
73 void erase(const std::function<bool(const T&)>& lambda) {
Siarhei Vishniakou61291d42019-02-11 18:13:20 -080074 std::scoped_lock lock(mLock);
Siarhei Vishniakouf47c339e2021-12-30 11:22:26 -080075 std::erase_if(mQueue, [&lambda](const auto& t) { return lambda(t); });
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080076 }
77
78 /**
79 * Remove all elements.
80 * Does not block.
81 */
82 void clear() {
83 std::scoped_lock lock(mLock);
84 mQueue.clear();
85 };
86
Siarhei Vishniakoua028c442019-02-04 14:33:23 -080087 /**
88 * How many elements are currently stored in the queue.
89 * Primary used for debugging.
90 * Does not block.
91 */
92 size_t size() {
93 std::scoped_lock lock(mLock);
94 return mQueue.size();
95 }
96
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080097private:
Siarhei Vishniakou61291d42019-02-11 18:13:20 -080098 const size_t mCapacity;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080099 /**
100 * Used to signal that mQueue is non-empty.
101 */
102 std::condition_variable mHasElements;
103 /**
104 * Lock for accessing and waiting on elements.
105 */
106 std::mutex mLock;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800107 std::vector<T> mQueue GUARDED_BY(mLock);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800108};
109
110
111} // namespace android
112#endif