blob: 782b40ba047415acb28857f92060299416ae2056 [file] [log] [blame]
Yifan Hong5e2318c2016-10-27 17:19:21 -07001/*
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>
18#include <thread>
19
20namespace android {
21namespace hardware {
Yifan Hong0a351392017-03-20 17:17:52 -070022namespace details {
Yifan Hong5e2318c2016-10-27 17:19:21 -070023
24TaskRunner::TaskRunner() {
Yifan Hong6f667542017-03-20 19:04:05 -070025}
26
27void TaskRunner::start(size_t limit) {
28 mQueue = std::make_shared<SynchronizedQueue<Task>>(limit);
Yifan Hong8184c122017-03-20 18:26:43 -070029
30 // Allow the thread to continue running in background;
31 // TaskRunner do not care about the std::thread object.
32 std::thread{[q = mQueue] {
33 Task nextTask;
34 while (!!(nextTask = q->wait_pop())) {
35 nextTask();
Yifan Hong5e2318c2016-10-27 17:19:21 -070036 }
Yifan Hong8184c122017-03-20 18:26:43 -070037 }}.detach();
Yifan Hong5e2318c2016-10-27 17:19:21 -070038}
Yifan Hong8184c122017-03-20 18:26:43 -070039
Yifan Hong5e2318c2016-10-27 17:19:21 -070040TaskRunner::~TaskRunner() {
Yifan Hong6f667542017-03-20 19:04:05 -070041 if (mQueue) {
42 mQueue->push(nullptr);
43 }
Yifan Hong5e2318c2016-10-27 17:19:21 -070044}
45
Steven Moreland0540d282017-05-08 16:03:11 -070046bool TaskRunner::push(const Task &t) {
47 return (mQueue != nullptr) && (!!t) && this->mQueue->push(t);
48}
49
Yifan Hong0a351392017-03-20 17:17:52 -070050} // namespace details
Yifan Hong5e2318c2016-10-27 17:19:21 -070051} // namespace hardware
52} // namespace android
53