Skip to content

future.h: replace std::mutex with firebase::Mutex #798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/src/include/firebase/future.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
#include <stddef.h>
#include <stdint.h>

#include <mutex>
#include <utility>

#include "firebase/internal/common.h"
#include "firebase/internal/mutex.h"

#ifdef FIREBASE_USE_STD_FUNCTION
#include <functional>
Expand Down Expand Up @@ -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<std::mutex> lock(mutex_);
MutexLock lock(mutex_);
return api_ == rhs.api_ && handle_ == rhs.handle_;
}

Expand All @@ -320,15 +320,15 @@ class FutureBase {
#if defined(INTERNAL_EXPERIMENTAL)
/// Returns the API-specific handle. Should only be called by the API.
FutureHandle GetHandle() const {
std::lock_guard<std::mutex> lock(mutex_);
MutexLock lock(mutex_);
return handle_;
}
#endif // defined(INTERNAL_EXPERIMENTAL)

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.
Expand Down
41 changes: 23 additions & 18 deletions app/src/include/firebase/internal/future_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,14 @@ inline FutureBase::CompletionCallbackHandle Future<ResultType>::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();
Expand All @@ -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;
}
Expand All @@ -214,13 +218,13 @@ inline FutureBase& FutureBase::operator=(const FutureBase& rhs) {
detail::FutureApiInterface* new_api;
FutureHandle new_handle;
{
std::lock_guard<std::mutex> lock(rhs.mutex_);
MutexLock lock(rhs.mutex_);
new_api = rhs.api_;
new_handle = rhs.handle_;
}

{
std::lock_guard<std::mutex> lock(mutex_);
MutexLock lock(mutex_);
api_ = new_api;
handle_ = new_handle;

Expand All @@ -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);
}
Expand All @@ -246,14 +251,14 @@ inline FutureBase& FutureBase::operator=(FutureBase&& rhs) noexcept {
detail::FutureApiInterface* new_api;
FutureHandle new_handle;
{
std::lock_guard<std::mutex> 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<std::mutex> lock(mutex_);
MutexLock lock(mutex_);
api_ = new_api;
handle_ = new_handle;
detail::RegisterForCleanup(api_, this);
Expand All @@ -262,7 +267,7 @@ inline FutureBase& FutureBase::operator=(FutureBase&& rhs) noexcept {
#endif // defined(FIREBASE_USE_MOVE_OPERATORS)

inline void FutureBase::Release() {
std::lock_guard<std::mutex> lock(mutex_);
MutexLock lock(mutex_);
if (api_ != NULL) { // NOLINT
detail::UnregisterForCleanup(api_, this);
api_->ReleaseFuture(handle_);
Expand All @@ -271,30 +276,30 @@ inline void FutureBase::Release() {
}

inline FutureStatus FutureBase::status() const {
std::lock_guard<std::mutex> lock(mutex_);
MutexLock lock(mutex_);
return api_ == NULL ? // NOLINT
kFutureStatusInvalid
: api_->GetFutureStatus(handle_);
}

inline int FutureBase::error() const {
std::lock_guard<std::mutex> lock(mutex_);
MutexLock lock(mutex_);
return api_ == NULL ? -1 : api_->GetFutureError(handle_); // NOLINT
}

inline const char* FutureBase::error_message() const {
std::lock_guard<std::mutex> lock(mutex_);
MutexLock lock(mutex_);
return api_ == NULL ? NULL : api_->GetFutureErrorMessage(handle_); // NOLINT
}

inline const void* FutureBase::result_void() const {
std::lock_guard<std::mutex> 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<std::mutex> lock(mutex_);
MutexLock lock(mutex_);
if (api_ != NULL) { // NOLINT
api_->AddCompletionCallback(handle_, callback, user_data, nullptr,
/*clear_existing_callbacks=*/true);
Expand All @@ -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<std::mutex> lock(mutex_);
MutexLock lock(mutex_);
if (api_ != NULL) { // NOLINT
return api_->AddCompletionCallback(handle_, callback, user_data, nullptr,
/*clear_existing_callbacks=*/false);
Expand All @@ -314,7 +319,7 @@ inline FutureBase::CompletionCallbackHandle FutureBase::AddOnCompletion(

inline void FutureBase::RemoveOnCompletion(
CompletionCallbackHandle completion_handle) const {
std::lock_guard<std::mutex> lock(mutex_);
MutexLock lock(mutex_);
if (api_ != NULL) { // NOLINT
api_->RemoveCompletionCallback(handle_, completion_handle);
}
Expand All @@ -324,7 +329,7 @@ inline void FutureBase::RemoveOnCompletion(
#if defined(FIREBASE_USE_STD_FUNCTION)
inline void FutureBase::OnCompletion(
std::function<void(const FutureBase&)> callback) const {
std::lock_guard<std::mutex> lock(mutex_);
MutexLock lock(mutex_);
if (api_ != NULL) { // NOLINT
api_->AddCompletionCallbackLambda(handle_, callback,
/*clear_existing_callbacks=*/true);
Expand All @@ -334,7 +339,7 @@ inline void FutureBase::OnCompletion(
#if defined(INTERNAL_EXPERIMENTAL)
inline FutureBase::CompletionCallbackHandle FutureBase::AddOnCompletion(
std::function<void(const FutureBase&)> callback) const {
std::lock_guard<std::mutex> lock(mutex_);
MutexLock lock(mutex_);
if (api_ != NULL) { // NOLINT
return api_->AddCompletionCallbackLambda(
handle_, callback,
Expand Down