Skip to content

Commit 428e197

Browse files
authored
Split mutex.h into .h and .cc files (#751)
1 parent 483c74a commit 428e197

8 files changed

+151
-89
lines changed

app/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ set(common_SRCS
136136
src/variant.cc
137137
src/base64.cc)
138138

139+
if (MSVC)
140+
set(mutex_SRCS src/mutex_windows.cc)
141+
else()
142+
set(mutex_SRCS src/mutex_pthread.cc)
143+
endif()
144+
139145
set(invites_SRCS
140146
src/invites/cached_receiver.cc
141147
src/invites/invites_receiver_internal.cc)
@@ -313,6 +319,7 @@ add_library(firebase_app STATIC
313319
${log_HDRS}
314320
${common_SRCS}
315321
${invites_SRCS}
322+
${mutex_SRCS}
316323
${app_platform_SRCS}
317324
${internal_HDRS}
318325
${utility_HDRS}

app/src/mutex.h

+21-89
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,14 @@
1616

1717
#ifndef FIREBASE_APP_SRC_MUTEX_H_
1818
#define FIREBASE_APP_SRC_MUTEX_H_
19-
#include <errno.h>
2019

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

23-
#if !FIREBASE_PLATFORM_WINDOWS
24-
#include <pthread.h>
25-
#else
22+
#if FIREBASE_PLATFORM_WINDOWS
2623
#include <windows.h>
27-
#endif // !FIREBASE_PLATFORM_WINDOWS
28-
29-
#include "app/src/assert.h"
24+
#else
25+
#include <pthread.h>
26+
#endif // FIREBASE_PLATFORM_WINDOWS
3027

3128
namespace firebase {
3229

@@ -39,100 +36,36 @@ class Mutex {
3936
kModeRecursive = (1 << 0),
4037
};
4138

42-
Mutex() { Initialize(kModeRecursive); }
39+
Mutex() : Mutex(kModeRecursive) {}
4340

44-
explicit Mutex(Mode mode) { Initialize(mode); }
41+
explicit Mutex(Mode mode);
4542

46-
~Mutex() {
47-
#if !FIREBASE_PLATFORM_WINDOWS
48-
int ret = pthread_mutex_destroy(&mutex_);
49-
FIREBASE_ASSERT(ret == 0);
50-
(void)ret;
51-
#else
52-
CloseHandle(synchronization_object_);
53-
#endif // !FIREBASE_PLATFORM_WINDOWS
54-
}
55-
56-
void Acquire() {
57-
#if !FIREBASE_PLATFORM_WINDOWS
58-
int ret = pthread_mutex_lock(&mutex_);
59-
if (ret == EINVAL) {
60-
return;
61-
}
62-
#if defined(__APPLE__)
63-
// Lock / unlock will fail in a static initializer on OSX and iOS.
64-
FIREBASE_ASSERT(ret == 0 || ret == EINVAL);
65-
#else
66-
FIREBASE_ASSERT(ret == 0);
67-
#endif // defined(__APPLE__)
68-
(void)ret;
69-
#else
70-
DWORD ret = WaitForSingleObject(synchronization_object_, INFINITE);
71-
FIREBASE_ASSERT(ret == WAIT_OBJECT_0);
72-
(void)ret;
73-
#endif // !FIREBASE_PLATFORM_WINDOWS
74-
}
75-
76-
void Release() {
77-
#if !FIREBASE_PLATFORM_WINDOWS
78-
int ret = pthread_mutex_unlock(&mutex_);
79-
#if defined(__APPLE__)
80-
// Lock / unlock will fail in a static initializer on OSX and iOS.
81-
FIREBASE_ASSERT(ret == 0 || ret == EINVAL);
82-
#else
83-
FIREBASE_ASSERT(ret == 0);
84-
#endif // defined(__APPLE__)
85-
(void)ret;
86-
#else
87-
if (mode_ & kModeRecursive) {
88-
ReleaseMutex(synchronization_object_);
89-
} else {
90-
ReleaseSemaphore(synchronization_object_, 1, 0);
91-
}
92-
#endif // !FIREBASE_PLATFORM_WINDOWS
93-
}
43+
~Mutex();
44+
45+
// Acquires the lock for this mutex, blocking until it is available.
46+
void Acquire();
47+
48+
// Releases the lock for this mutex acquired by a previous `Acquire()` call.
49+
void Release();
9450

9551
// Returns the implementation-defined native mutex handle.
9652
// Used by firebase::Thread implementation.
97-
#if !FIREBASE_PLATFORM_WINDOWS
98-
pthread_mutex_t* native_handle() { return &mutex_; }
99-
#else
53+
#if FIREBASE_PLATFORM_WINDOWS
10054
HANDLE* native_handle() { return &synchronization_object_; }
101-
#endif
55+
#else
56+
pthread_mutex_t* native_handle() { return &mutex_; }
57+
#endif // FIREBASE_PLATFORM_WINDOWS
10258

10359
private:
10460
Mutex(const Mutex&) = delete;
10561
Mutex& operator=(const Mutex&) = delete;
10662

107-
void Initialize(Mode mode) {
108-
#if !FIREBASE_PLATFORM_WINDOWS
109-
pthread_mutexattr_t attr;
110-
int ret = pthread_mutexattr_init(&attr);
111-
FIREBASE_ASSERT(ret == 0);
112-
if (mode & kModeRecursive) {
113-
ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
114-
FIREBASE_ASSERT(ret == 0);
115-
}
116-
ret = pthread_mutex_init(&mutex_, &attr);
117-
FIREBASE_ASSERT(ret == 0);
118-
ret = pthread_mutexattr_destroy(&attr);
119-
FIREBASE_ASSERT(ret == 0);
120-
#else
121-
mode_ = mode;
122-
if (mode & kModeRecursive) {
123-
synchronization_object_ = CreateMutex(nullptr, FALSE, nullptr);
124-
} else {
125-
synchronization_object_ = CreateSemaphore(nullptr, 1, 1, nullptr);
126-
}
127-
#endif // !FIREBASE_PLATFORM_WINDOWS
128-
}
129-
130-
#if !FIREBASE_PLATFORM_WINDOWS
131-
pthread_mutex_t mutex_;
132-
#else
63+
#if FIREBASE_PLATFORM_WINDOWS
13364
HANDLE synchronization_object_;
13465
Mode mode_;
135-
#endif // !FIREBASE_PLATFORM_WINDOWS
66+
#else
67+
pthread_mutex_t mutex_;
68+
#endif // FIREBASE_PLATFORM_WINDOWS
13669
};
13770

13871
/// @brief Acquire and hold a /ref Mutex, while in scope.
@@ -158,7 +91,6 @@ class MutexLock {
15891
Mutex* mutex_;
15992
};
16093

161-
// NOLINTNEXTLINE - allow namespace overridden
16294
} // namespace firebase
16395

16496
#endif // FIREBASE_APP_SRC_MUTEX_H_

app/src/mutex_pthread.cc

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include <errno.h>
17+
#include <pthread.h>
18+
19+
#include "app/src/assert.h"
20+
#include "app/src/mutex.h"
21+
22+
namespace firebase {
23+
24+
Mutex::Mutex(Mode mode) {
25+
pthread_mutexattr_t attr;
26+
int ret = pthread_mutexattr_init(&attr);
27+
FIREBASE_ASSERT(ret == 0);
28+
if (mode & kModeRecursive) {
29+
ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
30+
FIREBASE_ASSERT(ret == 0);
31+
}
32+
ret = pthread_mutex_init(&mutex_, &attr);
33+
FIREBASE_ASSERT(ret == 0);
34+
ret = pthread_mutexattr_destroy(&attr);
35+
FIREBASE_ASSERT(ret == 0);
36+
}
37+
38+
Mutex::~Mutex() {
39+
int ret = pthread_mutex_destroy(&mutex_);
40+
FIREBASE_ASSERT(ret == 0);
41+
(void)ret;
42+
}
43+
44+
void Mutex::Acquire() {
45+
int ret = pthread_mutex_lock(&mutex_);
46+
if (ret == EINVAL) {
47+
return;
48+
}
49+
#if defined(__APPLE__)
50+
// Lock / unlock will fail in a static initializer on OSX and iOS.
51+
FIREBASE_ASSERT(ret == 0 || ret == EINVAL);
52+
#else
53+
FIREBASE_ASSERT(ret == 0);
54+
#endif // defined(__APPLE__)
55+
(void)ret;
56+
}
57+
58+
void Mutex::Release() {
59+
int ret = pthread_mutex_unlock(&mutex_);
60+
#if defined(__APPLE__)
61+
// Lock / unlock will fail in a static initializer on OSX and iOS.
62+
FIREBASE_ASSERT(ret == 0 || ret == EINVAL);
63+
#else
64+
FIREBASE_ASSERT(ret == 0);
65+
#endif // defined(__APPLE__)
66+
(void)ret;
67+
}
68+
69+
} // namespace firebase

app/src/mutex_windows.cc

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include <windows.h>
17+
18+
#include "app/src/assert.h"
19+
#include "app/src/include/firebase/internal/platform.h"
20+
#include "app/src/mutex.h"
21+
22+
namespace firebase {
23+
24+
Mutex::Mutex(Mode mode) : mode_(mode) {
25+
if (mode & kModeRecursive) {
26+
synchronization_object_ = CreateMutex(nullptr, FALSE, nullptr);
27+
} else {
28+
synchronization_object_ = CreateSemaphore(nullptr, 1, 1, nullptr);
29+
}
30+
}
31+
32+
Mutex::~Mutex() { CloseHandle(synchronization_object_); }
33+
34+
void Mutex::Acquire() {
35+
DWORD ret = WaitForSingleObject(synchronization_object_, INFINITE);
36+
FIREBASE_ASSERT(ret == WAIT_OBJECT_0);
37+
(void)ret;
38+
}
39+
40+
void Mutex::Release() {
41+
if (mode_ & kModeRecursive) {
42+
ReleaseMutex(synchronization_object_);
43+
} else {
44+
ReleaseSemaphore(synchronization_object_, 1, 0);
45+
}
46+
}
47+
48+
} // namespace firebase

app/src/reference_counted_future_impl.h

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <map>
2222
#include <vector>
2323

24+
#include "app/src/assert.h"
2425
#include "app/src/cleanup_notifier.h"
2526
#include "app/src/include/firebase/future.h"
2627
#include "app/src/include/firebase/internal/common.h"

app/src/secure/user_secure_fake_internal.cc

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "app/src/secure/user_secure_fake_internal.h"
1616

1717
#include "app/src/include/firebase/internal/platform.h"
18+
#include "app/src/log.h"
1819

1920
#if FIREBASE_PLATFORM_WINDOWS
2021
#include <direct.h>

app/src/secure/user_secure_windows_internal.cc

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#define NOMINMAX
1818
#include <wincred.h>
1919

20+
#include "app/src/log.h"
21+
2022
namespace firebase {
2123
namespace app {
2224
namespace secure {

database/src/desktop/persistence/noop_persistence_manager.cc

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

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

17+
#include "app/src/assert.h"
18+
1719
#define VERIFY_INSIDE_TRANSACTION() \
1820
FIREBASE_DEV_ASSERT_MESSAGE( \
1921
this->inside_transaction_, \

0 commit comments

Comments
 (0)