xref: /aosp_15_r20/system/chre/util/include/chre/util/system/fixed_size_blocking_queue_impl.h (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2016 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 CHRE_UTIL_SYSTEM_FIXED_SIZE_BLOCKING_QUEUE_IMPL_H_
18 #define CHRE_UTIL_SYSTEM_FIXED_SIZE_BLOCKING_QUEUE_IMPL_H_
19 
20 // IWYU pragma: private
21 #include "chre/util/lock_guard.h"
22 #include "chre/util/system/fixed_size_blocking_queue.h"
23 
24 namespace chre {
25 
26 namespace blocking_queue_internal {
27 
28 template <typename ElementType, typename QueueStorageType>
empty()29 bool BlockingQueueCore<ElementType, QueueStorageType>::empty() {
30   LockGuard<Mutex> lock(mMutex);
31   return QueueStorageType::empty();
32 }
33 
34 template <typename ElementType, typename QueueStorageType>
size()35 size_t BlockingQueueCore<ElementType, QueueStorageType>::size() {
36   LockGuard<Mutex> lock(mMutex);
37   return QueueStorageType::size();
38 }
39 
40 template <typename ElementType, typename QueueStorageType>
remove(size_t index)41 bool BlockingQueueCore<ElementType, QueueStorageType>::remove(size_t index) {
42   LockGuard<Mutex> lock(mMutex);
43   return QueueStorageType::remove(index);
44 }
45 
46 template <typename ElementType, typename QueueStorageType>
47 ElementType &BlockingQueueCore<ElementType, QueueStorageType>::operator[](
48     size_t index) {
49   LockGuard<Mutex> lock(mMutex);
50   return QueueStorageType::operator[](index);
51 }
52 
53 template <typename ElementType, typename QueueStorageType>
54 const ElementType &BlockingQueueCore<ElementType, QueueStorageType>::operator[](
55     size_t index) const {
56   LockGuard<Mutex> lock(mMutex);
57   return QueueStorageType::operator[](index);
58 }
59 
60 template <typename ElementType, typename QueueStorageType>
push(const ElementType & element)61 bool BlockingQueueCore<ElementType, QueueStorageType>::push(
62     const ElementType &element) {
63   bool success;
64   {
65     LockGuard<Mutex> lock(mMutex);
66     success = QueueStorageType::push(element);
67   }
68   if (success) {
69     mConditionVariable.notify_one();
70   }
71   return success;
72 }
73 
74 template <typename ElementType, typename QueueStorageType>
push(ElementType && element)75 bool BlockingQueueCore<ElementType, QueueStorageType>::push(
76     ElementType &&element) {
77   bool success;
78   {
79     LockGuard<Mutex> lock(mMutex);
80     success = QueueStorageType::push(std::move(element));
81   }
82   if (success) {
83     mConditionVariable.notify_one();
84   }
85   return success;
86 }
87 
88 template <typename ElementType, typename QueueStorageType>
pop()89 ElementType BlockingQueueCore<ElementType, QueueStorageType>::pop() {
90   LockGuard<Mutex> lock(mMutex);
91   while (QueueStorageType::empty()) {
92     mConditionVariable.wait(mMutex);
93   }
94 
95   ElementType element(std::move(QueueStorageType::front()));
96   QueueStorageType::pop();
97   return element;
98 }
99 
100 }  // namespace blocking_queue_internal
101 
102 }  // namespace chre
103 
104 #endif  // CHRE_UTIL_BLOCKING_QUEUE_IMPL_H_
105