Skip to content

Commit 89ecf20

Browse files
authored
Move mutex.h header to public internal (#792)
1 parent 30bf73e commit 89ecf20

File tree

5 files changed

+100
-76
lines changed

5 files changed

+100
-76
lines changed

app/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ set(internal_HDRS
227227
src/include/firebase/future.h
228228
src/include/firebase/internal/common.h
229229
src/include/firebase/internal/future_impl.h
230+
src/include/firebase/internal/mutex.h
230231
src/include/firebase/internal/type_traits.h
231232
src/include/firebase/log.h
232233
src/include/firebase/util.h
+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2016 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+
17+
#ifndef FIREBASE_APP_SRC_INCLUDE_FIREBASE_INTERNAL_MUTEX_H_
18+
#define FIREBASE_APP_SRC_INCLUDE_FIREBASE_INTERNAL_MUTEX_H_
19+
20+
#include "firebase/internal/platform.h"
21+
22+
#if FIREBASE_PLATFORM_WINDOWS
23+
#include <windows.h>
24+
#else
25+
#include <pthread.h>
26+
#endif // FIREBASE_PLATFORM_WINDOWS
27+
28+
namespace firebase {
29+
30+
/// @brief A simple synchronization lock. Only one thread at a time can Acquire.
31+
class Mutex {
32+
public:
33+
// Bitfield that describes the mutex configuration.
34+
enum Mode {
35+
kModeNonRecursive = (0 << 0),
36+
kModeRecursive = (1 << 0),
37+
};
38+
39+
Mutex() : Mutex(kModeRecursive) {}
40+
41+
explicit Mutex(Mode mode);
42+
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();
50+
51+
// Returns the implementation-defined native mutex handle.
52+
// Used by firebase::Thread implementation.
53+
#if FIREBASE_PLATFORM_WINDOWS
54+
HANDLE* native_handle() { return &synchronization_object_; }
55+
#else
56+
pthread_mutex_t* native_handle() { return &mutex_; }
57+
#endif // FIREBASE_PLATFORM_WINDOWS
58+
59+
private:
60+
Mutex(const Mutex&) = delete;
61+
Mutex& operator=(const Mutex&) = delete;
62+
63+
#if FIREBASE_PLATFORM_WINDOWS
64+
HANDLE synchronization_object_;
65+
Mode mode_;
66+
#else
67+
pthread_mutex_t mutex_;
68+
#endif // FIREBASE_PLATFORM_WINDOWS
69+
};
70+
71+
/// @brief Acquire and hold a /ref Mutex, while in scope.
72+
///
73+
/// Example usage:
74+
/// \code{.cpp}
75+
/// Mutex syncronization_mutex;
76+
/// void MyFunctionThatRequiresSynchronization() {
77+
/// MutexLock lock(syncronization_mutex);
78+
/// // ... logic ...
79+
/// }
80+
/// \endcode
81+
class MutexLock {
82+
public:
83+
explicit MutexLock(Mutex& mutex) : mutex_(&mutex) { mutex_->Acquire(); }
84+
~MutexLock() { mutex_->Release(); }
85+
86+
private:
87+
// Copy is disallowed.
88+
MutexLock(const MutexLock& rhs); // NOLINT
89+
MutexLock& operator=(const MutexLock& rhs);
90+
91+
Mutex* mutex_;
92+
};
93+
94+
} // namespace firebase
95+
96+
#endif // FIREBASE_APP_SRC_INCLUDE_FIREBASE_INTERNAL_MUTEX_H_

app/src/mutex.h

+1-75
Original file line numberDiff line numberDiff line change
@@ -17,80 +17,6 @@
1717
#ifndef FIREBASE_APP_SRC_MUTEX_H_
1818
#define FIREBASE_APP_SRC_MUTEX_H_
1919

20-
#include "app/src/include/firebase/internal/platform.h"
21-
22-
#if FIREBASE_PLATFORM_WINDOWS
23-
#include <windows.h>
24-
#else
25-
#include <pthread.h>
26-
#endif // FIREBASE_PLATFORM_WINDOWS
27-
28-
namespace firebase {
29-
30-
/// @brief A simple synchronization lock. Only one thread at a time can Acquire.
31-
class Mutex {
32-
public:
33-
// Bitfield that describes the mutex configuration.
34-
enum Mode {
35-
kModeNonRecursive = (0 << 0),
36-
kModeRecursive = (1 << 0),
37-
};
38-
39-
Mutex() : Mutex(kModeRecursive) {}
40-
41-
explicit Mutex(Mode mode);
42-
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();
50-
51-
// Returns the implementation-defined native mutex handle.
52-
// Used by firebase::Thread implementation.
53-
#if FIREBASE_PLATFORM_WINDOWS
54-
HANDLE* native_handle() { return &synchronization_object_; }
55-
#else
56-
pthread_mutex_t* native_handle() { return &mutex_; }
57-
#endif // FIREBASE_PLATFORM_WINDOWS
58-
59-
private:
60-
Mutex(const Mutex&) = delete;
61-
Mutex& operator=(const Mutex&) = delete;
62-
63-
#if FIREBASE_PLATFORM_WINDOWS
64-
HANDLE synchronization_object_;
65-
Mode mode_;
66-
#else
67-
pthread_mutex_t mutex_;
68-
#endif // FIREBASE_PLATFORM_WINDOWS
69-
};
70-
71-
/// @brief Acquire and hold a /ref Mutex, while in scope.
72-
///
73-
/// Example usage:
74-
/// \code{.cpp}
75-
/// Mutex syncronization_mutex;
76-
/// void MyFunctionThatRequiresSynchronization() {
77-
/// MutexLock lock(syncronization_mutex);
78-
/// // ... logic ...
79-
/// }
80-
/// \endcode
81-
class MutexLock {
82-
public:
83-
explicit MutexLock(Mutex& mutex) : mutex_(&mutex) { mutex_->Acquire(); }
84-
~MutexLock() { mutex_->Release(); }
85-
86-
private:
87-
// Copy is disallowed.
88-
MutexLock(const MutexLock& rhs); // NOLINT
89-
MutexLock& operator=(const MutexLock& rhs);
90-
91-
Mutex* mutex_;
92-
};
93-
94-
} // namespace firebase
20+
#include "app/src/include/firebase/internal/mutex.h"
9521

9622
#endif // FIREBASE_APP_SRC_MUTEX_H_

docs/Doxyfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ REFERENCES_LINK_SOURCE = NO
2222
EXTRACT_STATIC = YES # Required for constant headers in firebase/analytics.
2323

2424
ENABLED_SECTIONS=cpp_examples
25-
EXCLUDE_SYMBOLS = internal
25+
EXCLUDE = app/src/include/firebase/internal
2626

2727
#WARN_IF_UNDOCUMENTED = YES
2828
WARN_IF_DOC_ERROR = YES

testing/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ target_include_directories(firebase_testing
148148
${FLATBUFFERS_SOURCE_DIR}/include
149149
PRIVATE
150150
${FIREBASE_CPP_SDK_ROOT_DIR}
151+
${FIREBASE_CPP_SDK_ROOT_DIR}/app/src/include
151152
${FIREBASE_GEN_FILE_DIR}
152153
)
153154

0 commit comments

Comments
 (0)