Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -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 | |
| 17 | #include <hidl/TaskRunner.h> |
Steven Moreland | c03f943 | 2017-08-16 14:17:49 -0700 | [diff] [blame] | 18 | |
| 19 | #include "SynchronizedQueue.h" |
| 20 | |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 21 | #include <thread> |
| 22 | |
| 23 | namespace android { |
| 24 | namespace hardware { |
Yifan Hong | 0a35139 | 2017-03-20 17:17:52 -0700 | [diff] [blame] | 25 | namespace details { |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 26 | |
| 27 | TaskRunner::TaskRunner() { |
Yifan Hong | 6f66754 | 2017-03-20 19:04:05 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void TaskRunner::start(size_t limit) { |
| 31 | mQueue = std::make_shared<SynchronizedQueue<Task>>(limit); |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 32 | } |
Yifan Hong | 8184c12 | 2017-03-20 18:26:43 -0700 | [diff] [blame] | 33 | |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 34 | TaskRunner::~TaskRunner() { |
Yifan Hong | 6f66754 | 2017-03-20 19:04:05 -0700 | [diff] [blame] | 35 | if (mQueue) { |
| 36 | mQueue->push(nullptr); |
| 37 | } |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Steven Moreland | 0540d28 | 2017-05-08 16:03:11 -0700 | [diff] [blame] | 40 | bool TaskRunner::push(const Task &t) { |
Steven Moreland | c03f943 | 2017-08-16 14:17:49 -0700 | [diff] [blame] | 41 | if (mQueue == nullptr || !t) { |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | { |
| 46 | std::unique_lock<std::mutex> lock = mQueue->lock(); |
| 47 | |
| 48 | if (!mQueue->isInitializedLocked()) { |
| 49 | // Allow the thread to continue running in background; |
| 50 | // TaskRunner do not care about the std::thread object. |
| 51 | std::thread{[q = mQueue] { |
| 52 | Task nextTask; |
| 53 | while (!!(nextTask = q->wait_pop())) { |
| 54 | nextTask(); |
| 55 | } |
| 56 | }}.detach(); |
| 57 | |
| 58 | mQueue->setInitializedLocked(true); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return this->mQueue->push(t); |
Steven Moreland | 0540d28 | 2017-05-08 16:03:11 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Yifan Hong | 0a35139 | 2017-03-20 17:17:52 -0700 | [diff] [blame] | 65 | } // namespace details |
Yifan Hong | 5e2318c | 2016-10-27 17:19:21 -0700 | [diff] [blame] | 66 | } // namespace hardware |
| 67 | } // namespace android |
| 68 | |