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