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
3128namespace 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_
0 commit comments