• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "SensorThread.h"
18 #include "Sensor.h"
19 
20 namespace android::hardware::sensors::V2_0::subhal::implementation {
21 
SensorThread(SensorBase * sensor)22 SensorThread::SensorThread(SensorBase* sensor)
23     : mSensor(sensor), mStopThread(false), mWaitCV(), mRunMutex() {}
24 
start()25 void SensorThread::start() {
26     mThread = std::thread([this]() -> void {
27         if (mSensor) {
28             while (!mStopThread) {
29                 mSensor->pollSensor();
30             }
31         }
32     });
33 }
34 
~SensorThread()35 SensorThread::~SensorThread() {
36     {
37         std::unique_lock<std::mutex> lck(lock());
38         stop();
39         notifyAll();
40     }
41     join();
42 }
43 
isStopped() const44 bool SensorThread::isStopped() const {
45     return mStopThread;
46 }
47 
join()48 void SensorThread::join() {
49     mThread.join();
50 }
51 
notifyAll()52 void SensorThread::notifyAll() {
53     mWaitCV.notify_all();
54 }
55 
stop()56 void SensorThread::stop() {
57     mStopThread = true;
58 }
59 
lock()60 std::unique_lock<std::mutex> SensorThread::lock() {
61     return std::move(std::unique_lock<std::mutex>(mRunMutex));
62 }
63 
64 }  // namespace android::hardware::sensors::V2_0::subhal::implementation
65