Yifan Hong | b93f050 | 2016-10-28 10:42:57 -0700 | [diff] [blame] | 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 | |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_SYNCHRONIZED_QUEUE_H |
| 18 | #define ANDROID_SYNCHRONIZED_QUEUE_H |
| 19 | |
Steven Moreland | 7e3a2a2 | 2016-09-15 09:04:37 -0700 | [diff] [blame] | 20 | #include <condition_variable> |
| 21 | #include <mutex> |
| 22 | #include <queue> |
| 23 | #include <thread> |
| 24 | |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace hardware { |
Steven Moreland | 7e3a2a2 | 2016-09-15 09:04:37 -0700 | [diff] [blame] | 27 | /* Threadsafe queue. |
| 28 | */ |
| 29 | template <typename T> |
| 30 | struct SynchronizedQueue { |
| 31 | |
| 32 | /* Gets an item from the front of the queue. |
| 33 | * |
| 34 | * Blocks until the item is available. |
| 35 | */ |
| 36 | T wait_pop(); |
| 37 | |
| 38 | /* Puts an item onto the end of the queue. |
| 39 | */ |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 40 | bool push(const T& item); |
Steven Moreland | 7e3a2a2 | 2016-09-15 09:04:37 -0700 | [diff] [blame] | 41 | |
| 42 | /* Gets the size of the array. |
| 43 | */ |
| 44 | size_t size(); |
| 45 | |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 46 | /* Sets the limit to the queue. Will fail |
| 47 | * the push operation if the limit is reached. |
| 48 | */ |
| 49 | void setLimit(size_t limit); |
| 50 | |
Steven Moreland | 7e3a2a2 | 2016-09-15 09:04:37 -0700 | [diff] [blame] | 51 | private: |
| 52 | std::condition_variable mCondition; |
| 53 | std::mutex mMutex; |
| 54 | std::queue<T> mQueue; |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 55 | size_t mQueueLimit = SIZE_MAX; |
Steven Moreland | 7e3a2a2 | 2016-09-15 09:04:37 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | template <typename T> |
| 59 | T SynchronizedQueue<T>::wait_pop() { |
| 60 | std::unique_lock<std::mutex> lock(mMutex); |
| 61 | |
| 62 | mCondition.wait(lock, [this]{ |
| 63 | return !this->mQueue.empty(); |
| 64 | }); |
| 65 | |
| 66 | T item = mQueue.front(); |
| 67 | mQueue.pop(); |
| 68 | |
| 69 | return item; |
| 70 | } |
| 71 | |
| 72 | template <typename T> |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 73 | bool SynchronizedQueue<T>::push(const T &item) { |
| 74 | bool success; |
Steven Moreland | 7e3a2a2 | 2016-09-15 09:04:37 -0700 | [diff] [blame] | 75 | { |
| 76 | std::unique_lock<std::mutex> lock(mMutex); |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 77 | if (mQueue.size() < mQueueLimit) { |
| 78 | mQueue.push(item); |
| 79 | success = true; |
| 80 | } else { |
| 81 | success = false; |
| 82 | } |
Steven Moreland | 7e3a2a2 | 2016-09-15 09:04:37 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | mCondition.notify_one(); |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 86 | return success; |
Steven Moreland | 7e3a2a2 | 2016-09-15 09:04:37 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | template <typename T> |
| 90 | size_t SynchronizedQueue<T>::size() { |
| 91 | std::unique_lock<std::mutex> lock(mMutex); |
| 92 | |
| 93 | return mQueue.size(); |
Yifan Hong | b93f050 | 2016-10-28 10:42:57 -0700 | [diff] [blame] | 94 | } |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 95 | |
| 96 | template <typename T> |
| 97 | void SynchronizedQueue<T>::setLimit(size_t limit) { |
| 98 | std::unique_lock<std::mutex> lock(mMutex); |
| 99 | |
| 100 | mQueueLimit = limit; |
| 101 | } |
| 102 | |
| 103 | } // namespace hardware |
| 104 | } // namespace android |
| 105 | |
| 106 | #endif |