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 "WorkerThread.h" 18 19 #define LOG_NDEBUG 0 20 #define LOG_TAG "EmulatedCamera_WorkerThread" 21 #include <cutils/log.h> 22 23 #include <algorithm> 24 25 namespace android { 26 27 WorkerThread::WorkerThread(const char* threadName, 28 EmulatedCameraDevice* cameraDevice, 29 EmulatedCamera* cameraHAL) 30 : Thread(true), // Callbacks may involve Java calls. 31 mCameraDevice(cameraDevice), 32 mCameraHAL(cameraHAL), 33 mRunning(false), 34 mThreadName(threadName) { 35 } 36 37 status_t WorkerThread::startThread(bool oneBurst) { 38 ALOGV("Starting worker thread, oneBurst=%s", oneBurst ? "true" : "false"); 39 mOneBurst = oneBurst; 40 { 41 Mutex::Autolock lock(mRunningMutex); 42 mRunning = true; 43 } 44 return run(mThreadName, ANDROID_PRIORITY_URGENT_DISPLAY, 0); 45 } 46 47 status_t WorkerThread::stopThread() { 48 ALOGV("%s: Stopping worker thread...", __FUNCTION__); 49 50 Mutex::Autolock lock(mRunningMutex); 51 mRunning = false; 52 mRunningCondition.signal(); 53 return NO_ERROR; 54 } 55 56 status_t WorkerThread::wakeThread() { 57 ALOGV("%s: Waking emulated camera device's worker thread...", __FUNCTION__); 58 59 mRunningCondition.signal(); 60 return NO_ERROR; 61 } 62 63 status_t WorkerThread::joinThread() { 64 return join(); 65 } 66 67 status_t WorkerThread::readyToRun() 68 { 69 status_t res = onThreadStart(); 70 if (res != NO_ERROR) { 71 return res; 72 } 73 return NO_ERROR; 74 } 75 76 bool WorkerThread::threadLoop() { 77 if (inWorkerThread() && !mOneBurst) { 78 /* Only return true if we're running. If mRunning has been set to false 79 * we fall through to ensure that onThreadExit is called. */ 80 Mutex::Autolock lock(mRunningMutex); 81 if (mRunning) { 82 return true; 83 } 84 } 85 onThreadExit(); 86 ALOGV("%s: Exiting thread, mOneBurst=%s", 87 __FUNCTION__, mOneBurst ? "true" : "false"); 88 return false; 89 } 90 91 } // namespace android 92 93