1 /* 2 * Copyright (C) 2021 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 #pragma once 18 19 #include <atomic> 20 #include <condition_variable> 21 #include <mutex> 22 #include <thread> 23 24 namespace android::hardware::sensors::V2_0::subhal::implementation { 25 26 class SensorBase; 27 28 class SensorThread { 29 public: 30 explicit SensorThread(SensorBase*); 31 ~SensorThread(); 32 33 void notifyAll(); 34 void start(); 35 void stop(); 36 std::unique_lock<std::mutex> lock(); 37 void join(); 38 39 bool isStopped() const; 40 41 template <typename Predicate> wait(Predicate p)42 void wait(Predicate p) { 43 auto lck(lock()); 44 mWaitCV.wait(lck, p); 45 } 46 47 private: 48 SensorBase* mSensor; 49 std::atomic_bool mStopThread; 50 std::condition_variable mWaitCV; 51 std::mutex mRunMutex; 52 std::thread mThread; 53 }; 54 55 } // namespace android::hardware::sensors::V2_0::subhal::implementation 56