| Prabir Pradhan | 5a57cff | 2019-10-31 18:40:33 -0700 | [diff] [blame] | 1 | /* | 
 | 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 |  | 
 | 19 | namespace android { | 
 | 20 |  | 
 | 21 | namespace { | 
 | 22 |  | 
 | 23 | // Implementation of Thread from libutils. | 
 | 24 | class InputThreadImpl : public Thread { | 
 | 25 | public: | 
 | 26 |     explicit InputThreadImpl(std::function<void()> loop) | 
 | 27 |           : Thread(/* canCallJava */ true), mThreadLoop(loop) {} | 
 | 28 |  | 
 | 29 |     ~InputThreadImpl() {} | 
 | 30 |  | 
 | 31 | private: | 
 | 32 |     std::function<void()> mThreadLoop; | 
 | 33 |  | 
 | 34 |     bool threadLoop() override { | 
 | 35 |         mThreadLoop(); | 
 | 36 |         return true; | 
 | 37 |     } | 
 | 38 | }; | 
 | 39 |  | 
 | 40 | } // namespace | 
 | 41 |  | 
 | 42 | InputThread::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 |  | 
 | 48 | InputThread::~InputThread() { | 
 | 49 |     mThread->requestExit(); | 
 | 50 |     if (mThreadWake) { | 
 | 51 |         mThreadWake(); | 
 | 52 |     } | 
 | 53 |     mThread->requestExitAndWait(); | 
 | 54 | } | 
 | 55 |  | 
 | 56 | bool InputThread::isCallingThread() { | 
 | 57 |     return gettid() == mThread->getTid(); | 
 | 58 | } | 
 | 59 |  | 
 | 60 | } // namespace android |