Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
7 changes: 7 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ set(common_SRCS
src/variant.cc
src/base64.cc)

if (MSVC)
set(mutex_SRCS src/mutex_windows.cc)
else()
set(mutex_SRCS src/mutex_pthread.cc)
endif()

set(invites_SRCS
src/invites/cached_receiver.cc
src/invites/invites_receiver_internal.cc)
Expand Down Expand Up @@ -313,6 +319,7 @@ add_library(firebase_app STATIC
${log_HDRS}
${common_SRCS}
${invites_SRCS}
${mutex_SRCS}
${app_platform_SRCS}
${internal_HDRS}
${utility_HDRS}
Expand Down
79 changes: 5 additions & 74 deletions app/src/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#ifndef FIREBASE_APP_SRC_MUTEX_H_
#define FIREBASE_APP_SRC_MUTEX_H_
#include <errno.h>

#include "app/src/include/firebase/internal/platform.h"

Expand All @@ -26,8 +25,6 @@
#include <windows.h>
#endif // !FIREBASE_PLATFORM_WINDOWS

#include "app/src/assert.h"

namespace firebase {

/// @brief A simple synchronization lock. Only one thread at a time can Acquire.
Expand All @@ -39,58 +36,15 @@ class Mutex {
kModeRecursive = (1 << 0),
};

Mutex() { Initialize(kModeRecursive); }
Mutex() : Mutex(kModeRecursive) {}

explicit Mutex(Mode mode) { Initialize(mode); }
explicit Mutex(Mode mode);

~Mutex() {
#if !FIREBASE_PLATFORM_WINDOWS
int ret = pthread_mutex_destroy(&mutex_);
FIREBASE_ASSERT(ret == 0);
(void)ret;
#else
CloseHandle(synchronization_object_);
#endif // !FIREBASE_PLATFORM_WINDOWS
}
~Mutex();

void Acquire() {
#if !FIREBASE_PLATFORM_WINDOWS
int ret = pthread_mutex_lock(&mutex_);
if (ret == EINVAL) {
return;
}
#if defined(__APPLE__)
// Lock / unlock will fail in a static initializer on OSX and iOS.
FIREBASE_ASSERT(ret == 0 || ret == EINVAL);
#else
FIREBASE_ASSERT(ret == 0);
#endif // defined(__APPLE__)
(void)ret;
#else
DWORD ret = WaitForSingleObject(synchronization_object_, INFINITE);
FIREBASE_ASSERT(ret == WAIT_OBJECT_0);
(void)ret;
#endif // !FIREBASE_PLATFORM_WINDOWS
}
void Acquire();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but maybe add a short comment above each one as long as we're in here.


void Release() {
#if !FIREBASE_PLATFORM_WINDOWS
int ret = pthread_mutex_unlock(&mutex_);
#if defined(__APPLE__)
// Lock / unlock will fail in a static initializer on OSX and iOS.
FIREBASE_ASSERT(ret == 0 || ret == EINVAL);
#else
FIREBASE_ASSERT(ret == 0);
#endif // defined(__APPLE__)
(void)ret;
#else
if (mode_ & kModeRecursive) {
ReleaseMutex(synchronization_object_);
} else {
ReleaseSemaphore(synchronization_object_, 1, 0);
}
#endif // !FIREBASE_PLATFORM_WINDOWS
}
void Release();

// Returns the implementation-defined native mutex handle.
// Used by firebase::Thread implementation.
Expand All @@ -104,29 +58,6 @@ class Mutex {
Mutex(const Mutex&) = delete;
Mutex& operator=(const Mutex&) = delete;

void Initialize(Mode mode) {
#if !FIREBASE_PLATFORM_WINDOWS
pthread_mutexattr_t attr;
int ret = pthread_mutexattr_init(&attr);
FIREBASE_ASSERT(ret == 0);
if (mode & kModeRecursive) {
ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
FIREBASE_ASSERT(ret == 0);
}
ret = pthread_mutex_init(&mutex_, &attr);
FIREBASE_ASSERT(ret == 0);
ret = pthread_mutexattr_destroy(&attr);
FIREBASE_ASSERT(ret == 0);
#else
mode_ = mode;
if (mode & kModeRecursive) {
synchronization_object_ = CreateMutex(nullptr, FALSE, nullptr);
} else {
synchronization_object_ = CreateSemaphore(nullptr, 1, 1, nullptr);
}
#endif // !FIREBASE_PLATFORM_WINDOWS
}

#if !FIREBASE_PLATFORM_WINDOWS
pthread_mutex_t mutex_;
#else
Expand Down
69 changes: 69 additions & 0 deletions app/src/mutex_pthread.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <errno.h>
#include <pthread.h>

#include "app/src/assert.h"
#include "app/src/mutex.h"

namespace firebase {

Mutex::Mutex(Mode mode) {
pthread_mutexattr_t attr;
int ret = pthread_mutexattr_init(&attr);
FIREBASE_ASSERT(ret == 0);
if (mode & kModeRecursive) {
ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
FIREBASE_ASSERT(ret == 0);
}
ret = pthread_mutex_init(&mutex_, &attr);
FIREBASE_ASSERT(ret == 0);
ret = pthread_mutexattr_destroy(&attr);
FIREBASE_ASSERT(ret == 0);
}

Mutex::~Mutex() {
int ret = pthread_mutex_destroy(&mutex_);
FIREBASE_ASSERT(ret == 0);
(void)ret;
}

void Mutex::Acquire() {
int ret = pthread_mutex_lock(&mutex_);
if (ret == EINVAL) {
return;
}
#if defined(__APPLE__)
// Lock / unlock will fail in a static initializer on OSX and iOS.
FIREBASE_ASSERT(ret == 0 || ret == EINVAL);
#else
FIREBASE_ASSERT(ret == 0);
#endif // defined(__APPLE__)
(void)ret;
}

void Mutex::Release() {
int ret = pthread_mutex_unlock(&mutex_);
#if defined(__APPLE__)
// Lock / unlock will fail in a static initializer on OSX and iOS.
FIREBASE_ASSERT(ret == 0 || ret == EINVAL);
#else
FIREBASE_ASSERT(ret == 0);
#endif // defined(__APPLE__)
(void)ret;
}

} // namespace firebase
48 changes: 48 additions & 0 deletions app/src/mutex_windows.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <windows.h>

#include "app/src/assert.h"
#include "app/src/include/firebase/internal/platform.h"
#include "app/src/mutex.h"

namespace firebase {

Mutex::Mutex(Mode mode) : mode_(mode) {
if (mode & kModeRecursive) {
synchronization_object_ = CreateMutex(nullptr, FALSE, nullptr);
} else {
synchronization_object_ = CreateSemaphore(nullptr, 1, 1, nullptr);
}
}

Mutex::~Mutex() { CloseHandle(synchronization_object_); }

void Mutex::Acquire() {
DWORD ret = WaitForSingleObject(synchronization_object_, INFINITE);
FIREBASE_ASSERT(ret == WAIT_OBJECT_0);
(void)ret;
}

void Mutex::Release() {
if (mode_ & kModeRecursive) {
ReleaseMutex(synchronization_object_);
} else {
ReleaseSemaphore(synchronization_object_, 1, 0);
}
}

} // namespace firebase
1 change: 1 addition & 0 deletions app/src/reference_counted_future_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <map>
#include <vector>

#include "app/src/assert.h"
#include "app/src/cleanup_notifier.h"
#include "app/src/include/firebase/future.h"
#include "app/src/include/firebase/internal/common.h"
Expand Down
1 change: 1 addition & 0 deletions app/src/secure/user_secure_fake_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "app/src/secure/user_secure_fake_internal.h"

#include "app/src/include/firebase/internal/platform.h"
#include "app/src/log.h"

#if FIREBASE_PLATFORM_WINDOWS
#include <direct.h>
Expand Down
2 changes: 2 additions & 0 deletions app/src/secure/user_secure_windows_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#define NOMINMAX
#include <wincred.h>

#include "app/src/log.h"

namespace firebase {
namespace app {
namespace secure {
Expand Down
2 changes: 2 additions & 0 deletions database/src/desktop/persistence/noop_persistence_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "database/src/desktop/persistence/noop_persistence_manager.h"

#include "app/src/assert.h"

#define VERIFY_INSIDE_TRANSACTION() \
FIREBASE_DEV_ASSERT_MESSAGE( \
this->inside_transaction_, \
Expand Down