blob: b87f7a1243334a7572adb0c0166e662a83f28079 [file] [log] [blame]
Prabir Pradhan5a57cff2019-10-31 18:40:33 -07001/*
2 * Copyright (C) 2020 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 "InputThread.h"
18
19namespace android {
20
21namespace {
22
23// Implementation of Thread from libutils.
24class InputThreadImpl : public Thread {
25public:
26 explicit InputThreadImpl(std::function<void()> loop)
27 : Thread(/* canCallJava */ true), mThreadLoop(loop) {}
28
29 ~InputThreadImpl() {}
30
31private:
32 std::function<void()> mThreadLoop;
33
34 bool threadLoop() override {
35 mThreadLoop();
36 return true;
37 }
38};
39
40} // namespace
41
42InputThread::InputThread(std::string name, std::function<void()> loop, std::function<void()> wake)
43 : mName(name), mThreadWake(wake) {
44 mThread = new InputThreadImpl(loop);
45 mThread->run(mName.c_str(), ANDROID_PRIORITY_URGENT_DISPLAY);
46}
47
48InputThread::~InputThread() {
49 mThread->requestExit();
50 if (mThreadWake) {
51 mThreadWake();
52 }
53 mThread->requestExitAndWait();
54}
55
56bool InputThread::isCallingThread() {
57 return gettid() == mThread->getTid();
58}
59
60} // namespace android