1*ec779b8eSAndroid Build Coastguard Worker /* 2*ec779b8eSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project 3*ec779b8eSAndroid Build Coastguard Worker * 4*ec779b8eSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*ec779b8eSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*ec779b8eSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*ec779b8eSAndroid Build Coastguard Worker * 8*ec779b8eSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*ec779b8eSAndroid Build Coastguard Worker * 10*ec779b8eSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*ec779b8eSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*ec779b8eSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*ec779b8eSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*ec779b8eSAndroid Build Coastguard Worker * limitations under the License. 15*ec779b8eSAndroid Build Coastguard Worker */ 16*ec779b8eSAndroid Build Coastguard Worker 17*ec779b8eSAndroid Build Coastguard Worker #pragma once 18*ec779b8eSAndroid Build Coastguard Worker 19*ec779b8eSAndroid Build Coastguard Worker namespace android { 20*ec779b8eSAndroid Build Coastguard Worker 21*ec779b8eSAndroid Build Coastguard Worker // T is FastMixer or FastCapture 22*ec779b8eSAndroid Build Coastguard Worker template<typename T> class AutoPark { 23*ec779b8eSAndroid Build Coastguard Worker public: 24*ec779b8eSAndroid Build Coastguard Worker 25*ec779b8eSAndroid Build Coastguard Worker // Park the specific FastThread, which can be nullptr, in hot idle if not currently idling AutoPark(const sp<T> & fastThread)26*ec779b8eSAndroid Build Coastguard Worker explicit AutoPark(const sp<T>& fastThread) : mFastThread(fastThread) 27*ec779b8eSAndroid Build Coastguard Worker { 28*ec779b8eSAndroid Build Coastguard Worker if (fastThread != nullptr) { 29*ec779b8eSAndroid Build Coastguard Worker auto sq = mFastThread->sq(); 30*ec779b8eSAndroid Build Coastguard Worker FastThreadState *state = sq->begin(); 31*ec779b8eSAndroid Build Coastguard Worker if (!(state->mCommand & FastThreadState::IDLE)) { 32*ec779b8eSAndroid Build Coastguard Worker mPreviousCommand = state->mCommand; 33*ec779b8eSAndroid Build Coastguard Worker state->mCommand = FastThreadState::HOT_IDLE; 34*ec779b8eSAndroid Build Coastguard Worker sq->end(); 35*ec779b8eSAndroid Build Coastguard Worker sq->push(sq->BLOCK_UNTIL_ACKED); 36*ec779b8eSAndroid Build Coastguard Worker } else { 37*ec779b8eSAndroid Build Coastguard Worker sq->end(false /*didModify*/); 38*ec779b8eSAndroid Build Coastguard Worker } 39*ec779b8eSAndroid Build Coastguard Worker } 40*ec779b8eSAndroid Build Coastguard Worker } 41*ec779b8eSAndroid Build Coastguard Worker 42*ec779b8eSAndroid Build Coastguard Worker // Remove the FastThread from hot idle if necessary ~AutoPark()43*ec779b8eSAndroid Build Coastguard Worker ~AutoPark() 44*ec779b8eSAndroid Build Coastguard Worker { 45*ec779b8eSAndroid Build Coastguard Worker if (!(mPreviousCommand & FastThreadState::IDLE)) { 46*ec779b8eSAndroid Build Coastguard Worker ALOG_ASSERT(mFastThread != nullptr); 47*ec779b8eSAndroid Build Coastguard Worker auto sq = mFastThread->sq(); 48*ec779b8eSAndroid Build Coastguard Worker FastThreadState *state = sq->begin(); 49*ec779b8eSAndroid Build Coastguard Worker ALOG_ASSERT(state->mCommand == FastThreadState::HOT_IDLE); 50*ec779b8eSAndroid Build Coastguard Worker state->mCommand = mPreviousCommand; 51*ec779b8eSAndroid Build Coastguard Worker sq->end(); 52*ec779b8eSAndroid Build Coastguard Worker sq->push(sq->BLOCK_UNTIL_PUSHED); 53*ec779b8eSAndroid Build Coastguard Worker } 54*ec779b8eSAndroid Build Coastguard Worker } 55*ec779b8eSAndroid Build Coastguard Worker 56*ec779b8eSAndroid Build Coastguard Worker private: 57*ec779b8eSAndroid Build Coastguard Worker const sp<T> mFastThread; 58*ec779b8eSAndroid Build Coastguard Worker // if !&IDLE, holds the FastThread state to restore after new parameters processed 59*ec779b8eSAndroid Build Coastguard Worker FastThreadState::Command mPreviousCommand = FastThreadState::HOT_IDLE; 60*ec779b8eSAndroid Build Coastguard Worker }; // class AutoPark 61*ec779b8eSAndroid Build Coastguard Worker 62*ec779b8eSAndroid Build Coastguard Worker } // namespace android 63