Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef RTC_BASE_SYNCHRONIZATION_MUTEX_H_ |
| 12 | #define RTC_BASE_SYNCHRONIZATION_MUTEX_H_ |
| 13 | |
| 14 | #include <atomic> |
| 15 | |
Danil Chapovalov | 098da17 | 2021-01-14 16:15:31 +0100 | [diff] [blame^] | 16 | #include "absl/base/attributes.h" |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 17 | #include "absl/base/const_init.h" |
| 18 | #include "rtc_base/checks.h" |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 19 | #include "rtc_base/thread_annotations.h" |
| 20 | |
| 21 | #if defined(WEBRTC_ABSL_MUTEX) |
Markus Handell | 8e75bd4 | 2020-06-05 11:47:40 +0200 | [diff] [blame] | 22 | #include "rtc_base/synchronization/mutex_abseil.h" // nogncheck |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 23 | #elif defined(WEBRTC_WIN) |
| 24 | #include "rtc_base/synchronization/mutex_critical_section.h" |
| 25 | #elif defined(WEBRTC_POSIX) |
| 26 | #include "rtc_base/synchronization/mutex_pthread.h" |
| 27 | #else |
| 28 | #error Unsupported platform. |
| 29 | #endif |
| 30 | |
| 31 | namespace webrtc { |
| 32 | |
| 33 | // The Mutex guarantees exclusive access and aims to follow Abseil semantics |
| 34 | // (i.e. non-reentrant etc). |
| 35 | class RTC_LOCKABLE Mutex final { |
| 36 | public: |
| 37 | Mutex() = default; |
| 38 | Mutex(const Mutex&) = delete; |
| 39 | Mutex& operator=(const Mutex&) = delete; |
| 40 | |
Markus Handell | 2d27b1a | 2020-06-09 17:34:41 +0200 | [diff] [blame] | 41 | void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION() { |
Markus Handell | 2d27b1a | 2020-06-09 17:34:41 +0200 | [diff] [blame] | 42 | impl_.Lock(); |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 43 | } |
Danil Chapovalov | 098da17 | 2021-01-14 16:15:31 +0100 | [diff] [blame^] | 44 | ABSL_MUST_USE_RESULT bool TryLock() RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true) { |
Markus Handell | 2a23bd2 | 2020-09-30 12:59:10 +0200 | [diff] [blame] | 45 | return impl_.TryLock(); |
Markus Handell | 2d27b1a | 2020-06-09 17:34:41 +0200 | [diff] [blame] | 46 | } |
| 47 | void Unlock() RTC_UNLOCK_FUNCTION() { |
Markus Handell | 2d27b1a | 2020-06-09 17:34:41 +0200 | [diff] [blame] | 48 | impl_.Unlock(); |
| 49 | } |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 50 | |
| 51 | private: |
| 52 | MutexImpl impl_; |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | // MutexLock, for serializing execution through a scope. |
| 56 | class RTC_SCOPED_LOCKABLE MutexLock final { |
| 57 | public: |
| 58 | MutexLock(const MutexLock&) = delete; |
| 59 | MutexLock& operator=(const MutexLock&) = delete; |
| 60 | |
| 61 | explicit MutexLock(Mutex* mutex) RTC_EXCLUSIVE_LOCK_FUNCTION(mutex) |
| 62 | : mutex_(mutex) { |
| 63 | mutex->Lock(); |
| 64 | } |
| 65 | ~MutexLock() RTC_UNLOCK_FUNCTION() { mutex_->Unlock(); } |
| 66 | |
| 67 | private: |
| 68 | Mutex* mutex_; |
| 69 | }; |
| 70 | |
| 71 | // A mutex used to protect global variables. Do NOT use for other purposes. |
| 72 | #if defined(WEBRTC_ABSL_MUTEX) |
| 73 | using GlobalMutex = absl::Mutex; |
| 74 | using GlobalMutexLock = absl::MutexLock; |
| 75 | #else |
| 76 | class RTC_LOCKABLE GlobalMutex final { |
| 77 | public: |
| 78 | GlobalMutex(const GlobalMutex&) = delete; |
| 79 | GlobalMutex& operator=(const GlobalMutex&) = delete; |
| 80 | |
| 81 | constexpr explicit GlobalMutex(absl::ConstInitType /*unused*/) |
| 82 | : mutex_locked_(0) {} |
| 83 | |
| 84 | void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION(); |
| 85 | void Unlock() RTC_UNLOCK_FUNCTION(); |
| 86 | |
| 87 | private: |
| 88 | std::atomic<int> mutex_locked_; // 0 means lock not taken, 1 means taken. |
| 89 | }; |
| 90 | |
| 91 | // GlobalMutexLock, for serializing execution through a scope. |
| 92 | class RTC_SCOPED_LOCKABLE GlobalMutexLock final { |
| 93 | public: |
| 94 | GlobalMutexLock(const GlobalMutexLock&) = delete; |
| 95 | GlobalMutexLock& operator=(const GlobalMutexLock&) = delete; |
| 96 | |
Markus Handell | 0dd35d3 | 2020-07-15 11:53:44 +0200 | [diff] [blame] | 97 | explicit GlobalMutexLock(GlobalMutex* mutex) |
| 98 | RTC_EXCLUSIVE_LOCK_FUNCTION(mutex_); |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 99 | ~GlobalMutexLock() RTC_UNLOCK_FUNCTION(); |
| 100 | |
| 101 | private: |
| 102 | GlobalMutex* mutex_; |
| 103 | }; |
| 104 | #endif // if defined(WEBRTC_ABSL_MUTEX) |
| 105 | |
| 106 | } // namespace webrtc |
| 107 | |
| 108 | #endif // RTC_BASE_SYNCHRONIZATION_MUTEX_H_ |