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