| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#ifndef RTC_BASE_SYNCHRONIZATION_MUTEX_H_ |
| 12 |
#define RTC_BASE_SYNCHRONIZATION_MUTEX_H_ |
| 13 |
|
| 14 |
#include <atomic> |
| 15 |
|
| 16 |
#include "absl/base/attributes.h" |
| 17 |
#include "absl/base/const_init.h" |
| 18 |
#include "rtc_base/checks.h" |
| 19 |
#include "rtc_base/thread_annotations.h" |
| 20 |
|
| 21 |
#if defined(WEBRTC_RACE_CHECK_MUTEX) |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include "rtc_base/synchronization/mutex_race_check.h" |
| 26 |
#elif defined(WEBRTC_ABSL_MUTEX) |
| 27 |
#include "rtc_base/synchronization/mutex_abseil.h" |
| 28 |
#elif defined(WEBRTC_WIN) |
| 29 |
#include "rtc_base/synchronization/mutex_critical_section.h" |
| 30 |
#elif defined(WEBRTC_POSIX) |
| 31 |
#include "rtc_base/synchronization/mutex_pthread.h" |
| 32 |
#else |
| 33 |
#error Unsupported platform. |
| 34 |
#endif |
| 35 |
|
| 36 |
namespace webrtc { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class RTC_LOCKABLE Mutex final { |
| 41 |
public: |
| 42 |
Mutex() = default; |
| 43 |
Mutex(const Mutex&) = delete; |
| 44 |
Mutex& operator=(const Mutex&) = delete; |
| 45 |
|
| 46 |
void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION() { |
| 47 |
impl_.Lock(); |
| 48 |
} |
| 49 |
ABSL_MUST_USE_RESULT bool TryLock() RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true) { |
| 50 |
return impl_.TryLock(); |
| 51 |
} |
| 52 |
void Unlock() RTC_UNLOCK_FUNCTION() { |
| 53 |
impl_.Unlock(); |
| 54 |
} |
| 55 |
|
| 56 |
private: |
| 57 |
MutexImpl impl_; |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
class RTC_SCOPED_LOCKABLE MutexLock final { |
| 62 |
public: |
| 63 |
MutexLock(const MutexLock&) = delete; |
| 64 |
MutexLock& operator=(const MutexLock&) = delete; |
| 65 |
|
| 66 |
explicit MutexLock(Mutex* mutex) RTC_EXCLUSIVE_LOCK_FUNCTION(mutex) |
| 67 |
: mutex_(mutex) { |
| 68 |
mutex->Lock(); |
| 69 |
} |
| 70 |
~MutexLock() RTC_UNLOCK_FUNCTION() { mutex_->Unlock(); } |
| 71 |
|
| 72 |
private: |
| 73 |
Mutex* mutex_; |
| 74 |
}; |
| 75 |
|
| 76 |
|
| 77 |
#if defined(WEBRTC_ABSL_MUTEX) |
| 78 |
using GlobalMutex = absl::Mutex; |
| 79 |
using GlobalMutexLock = absl::MutexLock; |
| 80 |
#else |
| 81 |
class RTC_LOCKABLE GlobalMutex final { |
| 82 |
public: |
| 83 |
GlobalMutex(const GlobalMutex&) = delete; |
| 84 |
GlobalMutex& operator=(const GlobalMutex&) = delete; |
| 85 |
|
| 86 |
constexpr explicit GlobalMutex(absl::ConstInitType ) |
| 87 |
: mutex_locked_(0) {} |
| 88 |
|
| 89 |
void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION(); |
| 90 |
void Unlock() RTC_UNLOCK_FUNCTION(); |
| 91 |
|
| 92 |
private: |
| 93 |
std::atomic<int> mutex_locked_; |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
class RTC_SCOPED_LOCKABLE GlobalMutexLock final { |
| 98 |
public: |
| 99 |
GlobalMutexLock(const GlobalMutexLock&) = delete; |
| 100 |
GlobalMutexLock& operator=(const GlobalMutexLock&) = delete; |
| 101 |
|
| 102 |
explicit GlobalMutexLock(GlobalMutex* mutex) |
| 103 |
RTC_EXCLUSIVE_LOCK_FUNCTION(mutex_); |
| 104 |
~GlobalMutexLock() RTC_UNLOCK_FUNCTION(); |
| 105 |
|
| 106 |
private: |
| 107 |
GlobalMutex* mutex_; |
| 108 |
}; |
| 109 |
#endif |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
#endif |