summaryrefslogtreecommitdiff
path: root/native/android/thermal.cpp
diff options
context:
space:
mode:
author Matt Buckley <mattbuckley@google.com> 2024-06-07 02:37:59 +0000
committer Matt Buckley <mattbuckley@google.com> 2024-06-07 02:47:11 +0000
commit053a1df81bb38011217800ec3b72fbf2251dbca3 (patch)
tree9779abec846b8426ff2d903beb76f944b1c232d5 /native/android/thermal.cpp
parent1f5b95ff7d34f244e6ea1e7a5c94581de99891eb (diff)
Add lock guards to performance hint NDK
Test: atest PerformanceHintNativeTest Bug: 343817997 Change-Id: Ic48949252be3122e2e13cfa5979f8831aea72a93
Diffstat (limited to 'native/android/thermal.cpp')
-rw-r--r--native/android/thermal.cpp26
1 files changed, 13 insertions, 13 deletions
diff --git a/native/android/thermal.cpp b/native/android/thermal.cpp
index b43f2f16a7cb..f7a3537d3f4a 100644
--- a/native/android/thermal.cpp
+++ b/native/android/thermal.cpp
@@ -99,21 +99,21 @@ AThermalManager::AThermalManager(sp<IThermalService> service)
: mThermalSvc(std::move(service)), mServiceListener(nullptr) {}
AThermalManager::~AThermalManager() {
- std::unique_lock<std::mutex> listenerLock(mListenerMutex);
-
- mListeners.clear();
- if (mServiceListener != nullptr) {
- bool success = false;
- mThermalSvc->unregisterThermalStatusListener(mServiceListener, &success);
- mServiceListener = nullptr;
+ {
+ std::scoped_lock<std::mutex> listenerLock(mListenerMutex);
+ mListeners.clear();
+ if (mServiceListener != nullptr) {
+ bool success = false;
+ mThermalSvc->unregisterThermalStatusListener(mServiceListener, &success);
+ mServiceListener = nullptr;
+ }
}
- listenerLock.unlock();
- std::unique_lock<std::mutex> lock(mThresholdsMutex);
+ std::scoped_lock<std::mutex> lock(mThresholdsMutex);
delete[] mThresholds;
}
status_t AThermalManager::notifyStateChange(int32_t status) {
- std::unique_lock<std::mutex> lock(mListenerMutex);
+ std::scoped_lock<std::mutex> lock(mListenerMutex);
AThermalStatus thermalStatus = static_cast<AThermalStatus>(status);
for (auto listener : mListeners) {
@@ -123,7 +123,7 @@ status_t AThermalManager::notifyStateChange(int32_t status) {
}
status_t AThermalManager::addListener(AThermal_StatusCallback callback, void *data) {
- std::unique_lock<std::mutex> lock(mListenerMutex);
+ std::scoped_lock<std::mutex> lock(mListenerMutex);
if (callback == nullptr) {
// Callback can not be nullptr
@@ -157,7 +157,7 @@ status_t AThermalManager::addListener(AThermal_StatusCallback callback, void *da
}
status_t AThermalManager::removeListener(AThermal_StatusCallback callback, void *data) {
- std::unique_lock<std::mutex> lock(mListenerMutex);
+ std::scoped_lock<std::mutex> lock(mListenerMutex);
auto it = std::remove_if(mListeners.begin(),
mListeners.end(),
@@ -216,7 +216,7 @@ status_t AThermalManager::getThermalHeadroom(int32_t forecastSeconds, float *res
status_t AThermalManager::getThermalHeadroomThresholds(const AThermalHeadroomThreshold **result,
size_t *size) {
- std::unique_lock<std::mutex> lock(mThresholdsMutex);
+ std::scoped_lock<std::mutex> lock(mThresholdsMutex);
if (mThresholds == nullptr) {
auto thresholds = std::make_unique<std::vector<float>>();
binder::Status ret = mThermalSvc->getThermalHeadroomThresholds(thresholds.get());