From 2af30bf4e9ff90fd755c902ca8e153a6366cecf2 Mon Sep 17 00:00:00 2001 From: Denver Coneybeare Date: Thu, 16 Dec 2021 22:33:40 -0500 Subject: [PATCH] future.h: replace std::mutex with firebase::Mutex --- app/src/include/firebase/future.h | 8 ++-- .../include/firebase/internal/future_impl.h | 41 +++++++++++-------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/app/src/include/firebase/future.h b/app/src/include/firebase/future.h index 5b57b14021..0d09fc079a 100644 --- a/app/src/include/firebase/future.h +++ b/app/src/include/firebase/future.h @@ -20,10 +20,10 @@ #include #include -#include #include #include "firebase/internal/common.h" +#include "firebase/internal/mutex.h" #ifdef FIREBASE_USE_STD_FUNCTION #include @@ -310,7 +310,7 @@ class FutureBase { /// Returns true if the two Futures reference the same result. bool operator==(const FutureBase& rhs) const { - std::lock_guard lock(mutex_); + MutexLock lock(mutex_); return api_ == rhs.api_ && handle_ == rhs.handle_; } @@ -320,7 +320,7 @@ class FutureBase { #if defined(INTERNAL_EXPERIMENTAL) /// Returns the API-specific handle. Should only be called by the API. FutureHandle GetHandle() const { - std::lock_guard lock(mutex_); + MutexLock lock(mutex_); return handle_; } #endif // defined(INTERNAL_EXPERIMENTAL) @@ -328,7 +328,7 @@ class FutureBase { protected: /// @cond FIREBASE_APP_INTERNAL - mutable std::mutex mutex_; + mutable Mutex mutex_; /// Backpointer to the issuing API class. /// Set to nullptr when Future is invalidated. diff --git a/app/src/include/firebase/internal/future_impl.h b/app/src/include/firebase/internal/future_impl.h index 86bad3e8d2..59e7771d01 100644 --- a/app/src/include/firebase/internal/future_impl.h +++ b/app/src/include/firebase/internal/future_impl.h @@ -189,11 +189,14 @@ inline FutureBase::CompletionCallbackHandle Future::AddOnCompletion( #endif // defined(INTERNAL_EXPERIMENTAL) -inline FutureBase::FutureBase() : api_(NULL), handle_(0) {} // NOLINT +inline FutureBase::FutureBase() + : mutex_(Mutex::Mode::kModeNonRecursive), + api_(NULL), + handle_(0) {} // NOLINT inline FutureBase::FutureBase(detail::FutureApiInterface* api, const FutureHandle& handle) - : api_(api), handle_(handle) { + : mutex_(Mutex::Mode::kModeNonRecursive), api_(api), handle_(handle) { api_->ReferenceFuture(handle_); // Once the FutureBase has reference, we don't need extra handle reference. handle_.Detach(); @@ -203,7 +206,8 @@ inline FutureBase::FutureBase(detail::FutureApiInterface* api, inline FutureBase::~FutureBase() { Release(); } inline FutureBase::FutureBase(const FutureBase& rhs) - : api_(NULL) // NOLINT + : mutex_(Mutex::Mode::kModeNonRecursive), + api_(NULL) // NOLINT { // NOLINT *this = rhs; } @@ -214,13 +218,13 @@ inline FutureBase& FutureBase::operator=(const FutureBase& rhs) { detail::FutureApiInterface* new_api; FutureHandle new_handle; { - std::lock_guard lock(rhs.mutex_); + MutexLock lock(rhs.mutex_); new_api = rhs.api_; new_handle = rhs.handle_; } { - std::lock_guard lock(mutex_); + MutexLock lock(mutex_); api_ = new_api; handle_ = new_handle; @@ -235,7 +239,8 @@ inline FutureBase& FutureBase::operator=(const FutureBase& rhs) { #if defined(FIREBASE_USE_MOVE_OPERATORS) inline FutureBase::FutureBase(FutureBase&& rhs) noexcept - : api_(NULL) // NOLINT + : mutex_(Mutex::Mode::kModeNonRecursive), + api_(NULL) // NOLINT { *this = std::move(rhs); } @@ -246,14 +251,14 @@ inline FutureBase& FutureBase::operator=(FutureBase&& rhs) noexcept { detail::FutureApiInterface* new_api; FutureHandle new_handle; { - std::lock_guard lock(rhs.mutex_); + MutexLock lock(rhs.mutex_); detail::UnregisterForCleanup(rhs.api_, &rhs); new_api = rhs.api_; new_handle = rhs.handle_; rhs.api_ = NULL; // NOLINT } - std::lock_guard lock(mutex_); + MutexLock lock(mutex_); api_ = new_api; handle_ = new_handle; detail::RegisterForCleanup(api_, this); @@ -262,7 +267,7 @@ inline FutureBase& FutureBase::operator=(FutureBase&& rhs) noexcept { #endif // defined(FIREBASE_USE_MOVE_OPERATORS) inline void FutureBase::Release() { - std::lock_guard lock(mutex_); + MutexLock lock(mutex_); if (api_ != NULL) { // NOLINT detail::UnregisterForCleanup(api_, this); api_->ReleaseFuture(handle_); @@ -271,30 +276,30 @@ inline void FutureBase::Release() { } inline FutureStatus FutureBase::status() const { - std::lock_guard lock(mutex_); + MutexLock lock(mutex_); return api_ == NULL ? // NOLINT kFutureStatusInvalid : api_->GetFutureStatus(handle_); } inline int FutureBase::error() const { - std::lock_guard lock(mutex_); + MutexLock lock(mutex_); return api_ == NULL ? -1 : api_->GetFutureError(handle_); // NOLINT } inline const char* FutureBase::error_message() const { - std::lock_guard lock(mutex_); + MutexLock lock(mutex_); return api_ == NULL ? NULL : api_->GetFutureErrorMessage(handle_); // NOLINT } inline const void* FutureBase::result_void() const { - std::lock_guard lock(mutex_); + MutexLock lock(mutex_); return api_ == NULL ? NULL : api_->GetFutureResult(handle_); // NOLINT } inline void FutureBase::OnCompletion(CompletionCallback callback, void* user_data) const { - std::lock_guard lock(mutex_); + MutexLock lock(mutex_); if (api_ != NULL) { // NOLINT api_->AddCompletionCallback(handle_, callback, user_data, nullptr, /*clear_existing_callbacks=*/true); @@ -304,7 +309,7 @@ inline void FutureBase::OnCompletion(CompletionCallback callback, #if defined(INTERNAL_EXPERIMENTAL) inline FutureBase::CompletionCallbackHandle FutureBase::AddOnCompletion( CompletionCallback callback, void* user_data) const { - std::lock_guard lock(mutex_); + MutexLock lock(mutex_); if (api_ != NULL) { // NOLINT return api_->AddCompletionCallback(handle_, callback, user_data, nullptr, /*clear_existing_callbacks=*/false); @@ -314,7 +319,7 @@ inline FutureBase::CompletionCallbackHandle FutureBase::AddOnCompletion( inline void FutureBase::RemoveOnCompletion( CompletionCallbackHandle completion_handle) const { - std::lock_guard lock(mutex_); + MutexLock lock(mutex_); if (api_ != NULL) { // NOLINT api_->RemoveCompletionCallback(handle_, completion_handle); } @@ -324,7 +329,7 @@ inline void FutureBase::RemoveOnCompletion( #if defined(FIREBASE_USE_STD_FUNCTION) inline void FutureBase::OnCompletion( std::function callback) const { - std::lock_guard lock(mutex_); + MutexLock lock(mutex_); if (api_ != NULL) { // NOLINT api_->AddCompletionCallbackLambda(handle_, callback, /*clear_existing_callbacks=*/true); @@ -334,7 +339,7 @@ inline void FutureBase::OnCompletion( #if defined(INTERNAL_EXPERIMENTAL) inline FutureBase::CompletionCallbackHandle FutureBase::AddOnCompletion( std::function callback) const { - std::lock_guard lock(mutex_); + MutexLock lock(mutex_); if (api_ != NULL) { // NOLINT return api_->AddCompletionCallbackLambda( handle_, callback,