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