1 /* 2 * Copyright (C) 2018 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 #ifndef POWER_LIBPERFMGR_INTERACTIONHANDLER_H_ 18 #define POWER_LIBPERFMGR_INTERACTIONHANDLER_H_ 19 20 #include <condition_variable> 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 #include <thread> 25 26 #include <perfmgr/HintManager.h> 27 28 using ::android::perfmgr::HintManager; 29 30 enum interaction_state { 31 INTERACTION_STATE_UNINITIALIZED, 32 INTERACTION_STATE_IDLE, 33 INTERACTION_STATE_INTERACTION, 34 INTERACTION_STATE_WAITING, 35 }; 36 37 class InteractionHandler { 38 public: 39 InteractionHandler(std::shared_ptr<HintManager> const &hint_manager); 40 ~InteractionHandler(); 41 bool Init(); 42 void Exit(); 43 void Acquire(int32_t duration); 44 45 private: 46 void Release(); 47 void WaitForIdle(int32_t wait_ms, int32_t timeout_ms); 48 void AbortWaitLocked(); 49 void Routine(); 50 51 void PerfLock(); 52 void PerfRel(); 53 54 size_t CalcTimespecDiffMs(struct timespec start, struct timespec end); 55 56 enum interaction_state mState; 57 58 int mIdleFd; 59 int mEventFd; 60 61 int32_t mWaitMs; 62 int32_t mMinDurationMs; 63 int32_t mMaxDurationMs; 64 int32_t mDurationMs; 65 66 struct timespec mLastTimespec; 67 68 std::unique_ptr<std::thread> mThread; 69 std::mutex mLock; 70 std::condition_variable mCond; 71 std::shared_ptr<HintManager> mHintManager; 72 }; 73 74 #endif // POWER_LIBPERFMGR_INTERACTIONHANDLER_H_ 75