Debian Codegraph @

Xpjproject/third_party/webrtc_aec3/src/rtc_base/synchronization/mutex.h

raw symbols SCIP
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
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 // To use the race check mutex, define WEBRTC_RACE_CHECK_MUTEX globally. This
23 // also adds a dependency to absl::Mutex from logging.cc due to concurrent
24 // invocation of the static logging system.
25 #include "rtc_base/synchronization/mutex_race_check.h"
26 #elif defined(WEBRTC_ABSL_MUTEX)
27 #include "rtc_base/synchronization/mutex_abseil.h" // nogncheck
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 // The Mutex guarantees exclusive access and aims to follow Abseil semantics
39 // (i.e. non-reentrant etc).
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 // MutexLock, for serializing execution through a scope.
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 // A mutex used to protect global variables. Do NOT use for other purposes.
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 /*unused*/)
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_; // 0 means lock not taken, 1 means taken.
94 };
95
96 // GlobalMutexLock, for serializing execution through a scope.
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 // if defined(WEBRTC_ABSL_MUTEX)
110
111 } // namespace webrtc
112
113 #endif // RTC_BASE_SYNCHRONIZATION_MUTEX_H_