Skip to content

Commit 283082f

Browse files
authored
future.h: replace std::mutex with firebase::Mutex (#798)
1 parent fdf6ffe commit 283082f

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

app/src/include/firebase/future.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
#include <stddef.h>
2121
#include <stdint.h>
2222

23-
#include <mutex>
2423
#include <utility>
2524

2625
#include "firebase/internal/common.h"
26+
#include "firebase/internal/mutex.h"
2727

2828
#ifdef FIREBASE_USE_STD_FUNCTION
2929
#include <functional>
@@ -310,7 +310,7 @@ class FutureBase {
310310

311311
/// Returns true if the two Futures reference the same result.
312312
bool operator==(const FutureBase& rhs) const {
313-
std::lock_guard<std::mutex> lock(mutex_);
313+
MutexLock lock(mutex_);
314314
return api_ == rhs.api_ && handle_ == rhs.handle_;
315315
}
316316

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

328328
protected:
329329
/// @cond FIREBASE_APP_INTERNAL
330330

331-
mutable std::mutex mutex_;
331+
mutable Mutex mutex_;
332332

333333
/// Backpointer to the issuing API class.
334334
/// Set to nullptr when Future is invalidated.

app/src/include/firebase/internal/future_impl.h

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,14 @@ inline FutureBase::CompletionCallbackHandle Future<ResultType>::AddOnCompletion(
189189

190190
#endif // defined(INTERNAL_EXPERIMENTAL)
191191

192-
inline FutureBase::FutureBase() : api_(NULL), handle_(0) {} // NOLINT
192+
inline FutureBase::FutureBase()
193+
: mutex_(Mutex::Mode::kModeNonRecursive),
194+
api_(NULL),
195+
handle_(0) {} // NOLINT
193196

194197
inline FutureBase::FutureBase(detail::FutureApiInterface* api,
195198
const FutureHandle& handle)
196-
: api_(api), handle_(handle) {
199+
: mutex_(Mutex::Mode::kModeNonRecursive), api_(api), handle_(handle) {
197200
api_->ReferenceFuture(handle_);
198201
// Once the FutureBase has reference, we don't need extra handle reference.
199202
handle_.Detach();
@@ -203,7 +206,8 @@ inline FutureBase::FutureBase(detail::FutureApiInterface* api,
203206
inline FutureBase::~FutureBase() { Release(); }
204207

205208
inline FutureBase::FutureBase(const FutureBase& rhs)
206-
: api_(NULL) // NOLINT
209+
: mutex_(Mutex::Mode::kModeNonRecursive),
210+
api_(NULL) // NOLINT
207211
{ // NOLINT
208212
*this = rhs;
209213
}
@@ -214,13 +218,13 @@ inline FutureBase& FutureBase::operator=(const FutureBase& rhs) {
214218
detail::FutureApiInterface* new_api;
215219
FutureHandle new_handle;
216220
{
217-
std::lock_guard<std::mutex> lock(rhs.mutex_);
221+
MutexLock lock(rhs.mutex_);
218222
new_api = rhs.api_;
219223
new_handle = rhs.handle_;
220224
}
221225

222226
{
223-
std::lock_guard<std::mutex> lock(mutex_);
227+
MutexLock lock(mutex_);
224228
api_ = new_api;
225229
handle_ = new_handle;
226230

@@ -235,7 +239,8 @@ inline FutureBase& FutureBase::operator=(const FutureBase& rhs) {
235239

236240
#if defined(FIREBASE_USE_MOVE_OPERATORS)
237241
inline FutureBase::FutureBase(FutureBase&& rhs) noexcept
238-
: api_(NULL) // NOLINT
242+
: mutex_(Mutex::Mode::kModeNonRecursive),
243+
api_(NULL) // NOLINT
239244
{
240245
*this = std::move(rhs);
241246
}
@@ -246,14 +251,14 @@ inline FutureBase& FutureBase::operator=(FutureBase&& rhs) noexcept {
246251
detail::FutureApiInterface* new_api;
247252
FutureHandle new_handle;
248253
{
249-
std::lock_guard<std::mutex> lock(rhs.mutex_);
254+
MutexLock lock(rhs.mutex_);
250255
detail::UnregisterForCleanup(rhs.api_, &rhs);
251256
new_api = rhs.api_;
252257
new_handle = rhs.handle_;
253258
rhs.api_ = NULL; // NOLINT
254259
}
255260

256-
std::lock_guard<std::mutex> lock(mutex_);
261+
MutexLock lock(mutex_);
257262
api_ = new_api;
258263
handle_ = new_handle;
259264
detail::RegisterForCleanup(api_, this);
@@ -262,7 +267,7 @@ inline FutureBase& FutureBase::operator=(FutureBase&& rhs) noexcept {
262267
#endif // defined(FIREBASE_USE_MOVE_OPERATORS)
263268

264269
inline void FutureBase::Release() {
265-
std::lock_guard<std::mutex> lock(mutex_);
270+
MutexLock lock(mutex_);
266271
if (api_ != NULL) { // NOLINT
267272
detail::UnregisterForCleanup(api_, this);
268273
api_->ReleaseFuture(handle_);
@@ -271,30 +276,30 @@ inline void FutureBase::Release() {
271276
}
272277

273278
inline FutureStatus FutureBase::status() const {
274-
std::lock_guard<std::mutex> lock(mutex_);
279+
MutexLock lock(mutex_);
275280
return api_ == NULL ? // NOLINT
276281
kFutureStatusInvalid
277282
: api_->GetFutureStatus(handle_);
278283
}
279284

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

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

290295
inline const void* FutureBase::result_void() const {
291-
std::lock_guard<std::mutex> lock(mutex_);
296+
MutexLock lock(mutex_);
292297
return api_ == NULL ? NULL : api_->GetFutureResult(handle_); // NOLINT
293298
}
294299

295300
inline void FutureBase::OnCompletion(CompletionCallback callback,
296301
void* user_data) const {
297-
std::lock_guard<std::mutex> lock(mutex_);
302+
MutexLock lock(mutex_);
298303
if (api_ != NULL) { // NOLINT
299304
api_->AddCompletionCallback(handle_, callback, user_data, nullptr,
300305
/*clear_existing_callbacks=*/true);
@@ -304,7 +309,7 @@ inline void FutureBase::OnCompletion(CompletionCallback callback,
304309
#if defined(INTERNAL_EXPERIMENTAL)
305310
inline FutureBase::CompletionCallbackHandle FutureBase::AddOnCompletion(
306311
CompletionCallback callback, void* user_data) const {
307-
std::lock_guard<std::mutex> lock(mutex_);
312+
MutexLock lock(mutex_);
308313
if (api_ != NULL) { // NOLINT
309314
return api_->AddCompletionCallback(handle_, callback, user_data, nullptr,
310315
/*clear_existing_callbacks=*/false);
@@ -314,7 +319,7 @@ inline FutureBase::CompletionCallbackHandle FutureBase::AddOnCompletion(
314319

315320
inline void FutureBase::RemoveOnCompletion(
316321
CompletionCallbackHandle completion_handle) const {
317-
std::lock_guard<std::mutex> lock(mutex_);
322+
MutexLock lock(mutex_);
318323
if (api_ != NULL) { // NOLINT
319324
api_->RemoveCompletionCallback(handle_, completion_handle);
320325
}
@@ -324,7 +329,7 @@ inline void FutureBase::RemoveOnCompletion(
324329
#if defined(FIREBASE_USE_STD_FUNCTION)
325330
inline void FutureBase::OnCompletion(
326331
std::function<void(const FutureBase&)> callback) const {
327-
std::lock_guard<std::mutex> lock(mutex_);
332+
MutexLock lock(mutex_);
328333
if (api_ != NULL) { // NOLINT
329334
api_->AddCompletionCallbackLambda(handle_, callback,
330335
/*clear_existing_callbacks=*/true);
@@ -334,7 +339,7 @@ inline void FutureBase::OnCompletion(
334339
#if defined(INTERNAL_EXPERIMENTAL)
335340
inline FutureBase::CompletionCallbackHandle FutureBase::AddOnCompletion(
336341
std::function<void(const FutureBase&)> callback) const {
337-
std::lock_guard<std::mutex> lock(mutex_);
342+
MutexLock lock(mutex_);
338343
if (api_ != NULL) { // NOLINT
339344
return api_->AddCompletionCallbackLambda(
340345
handle_, callback,

0 commit comments

Comments
 (0)