16
16
17
17
#ifndef FIREBASE_APP_SRC_MUTEX_H_
18
18
#define FIREBASE_APP_SRC_MUTEX_H_
19
- #include < errno.h>
20
19
21
20
#include " app/src/include/firebase/internal/platform.h"
22
21
23
- #if !FIREBASE_PLATFORM_WINDOWS
24
- #include < pthread.h>
25
- #else
22
+ #if FIREBASE_PLATFORM_WINDOWS
26
23
#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
30
27
31
28
namespace firebase {
32
29
@@ -39,100 +36,36 @@ class Mutex {
39
36
kModeRecursive = (1 << 0 ),
40
37
};
41
38
42
- Mutex () { Initialize (kModeRecursive ); }
39
+ Mutex () : Mutex (kModeRecursive ) { }
43
40
44
- explicit Mutex (Mode mode) { Initialize (mode); }
41
+ explicit Mutex (Mode mode);
45
42
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 ();
94
50
95
51
// Returns the implementation-defined native mutex handle.
96
52
// 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
100
54
HANDLE* native_handle () { return &synchronization_object_; }
101
- #endif
55
+ #else
56
+ pthread_mutex_t * native_handle () { return &mutex_; }
57
+ #endif // FIREBASE_PLATFORM_WINDOWS
102
58
103
59
private:
104
60
Mutex (const Mutex&) = delete ;
105
61
Mutex& operator =(const Mutex&) = delete ;
106
62
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
133
64
HANDLE synchronization_object_;
134
65
Mode mode_;
135
- #endif // !FIREBASE_PLATFORM_WINDOWS
66
+ #else
67
+ pthread_mutex_t mutex_;
68
+ #endif // FIREBASE_PLATFORM_WINDOWS
136
69
};
137
70
138
71
// / @brief Acquire and hold a /ref Mutex, while in scope.
@@ -158,7 +91,6 @@ class MutexLock {
158
91
Mutex* mutex_;
159
92
};
160
93
161
- // NOLINTNEXTLINE - allow namespace overridden
162
94
} // namespace firebase
163
95
164
96
#endif // FIREBASE_APP_SRC_MUTEX_H_
0 commit comments