blob: 78add4ce447b6cdde03334d0d83afb95d4afe872 [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() {
25 bool *running = mRunning = new bool();
26 SynchronizedQueue<std::function<void(void)>> *q
27 = mQueue = new SynchronizedQueue<std::function<void(void)>>();
28 mThread = new std::thread([running, q] {
29 *running = true;
30 while (*running) {
31 (q->wait_pop())();
32 }
33 delete q;
34 delete running;
35 });
36}
37TaskRunner::~TaskRunner() {
38 bool *running = mRunning;
39 std::thread *t = mThread;
40 mThread->detach();
41 mQueue->push([running, t] {
42 *running = false;
43 delete t;
44 });
45}
46
Yifan Hong0a351392017-03-20 17:17:52 -070047} // namespace details
Yifan Hong5e2318c2016-10-27 17:19:21 -070048} // namespace hardware
49} // namespace android
50