blob: e1512e96cccc7c4e941b8145567e3a618ed0fba4 [file] [log] [blame]
Markus Handellf70fbc82020-06-04 00:41:20 +02001/*
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 Chapovalov098da172021-01-14 16:15:31 +010016#include "absl/base/attributes.h"
Markus Handellf70fbc82020-06-04 00:41:20 +020017#include "absl/base/const_init.h"
18#include "rtc_base/checks.h"
Markus Handellf70fbc82020-06-04 00:41:20 +020019#include "rtc_base/thread_annotations.h"
20
Markus Handell0d3c09a2021-04-19 09:12:15 +020021#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)
Markus Handell8e75bd42020-06-05 11:47:40 +020027#include "rtc_base/synchronization/mutex_abseil.h" // nogncheck
Markus Handellf70fbc82020-06-04 00:41:20 +020028#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
36namespace webrtc {
37
38// The Mutex guarantees exclusive access and aims to follow Abseil semantics
39// (i.e. non-reentrant etc).
40class RTC_LOCKABLE Mutex final {
41 public:
42 Mutex() = default;
43 Mutex(const Mutex&) = delete;
44 Mutex& operator=(const Mutex&) = delete;
45
Markus Handell2d27b1a2020-06-09 17:34:41 +020046 void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION() {
Markus Handell2d27b1a2020-06-09 17:34:41 +020047 impl_.Lock();
Markus Handellf70fbc82020-06-04 00:41:20 +020048 }
Danil Chapovalov098da172021-01-14 16:15:31 +010049 ABSL_MUST_USE_RESULT bool TryLock() RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
Markus Handell2a23bd22020-09-30 12:59:10 +020050 return impl_.TryLock();
Markus Handell2d27b1a2020-06-09 17:34:41 +020051 }
52 void Unlock() RTC_UNLOCK_FUNCTION() {
Markus Handell2d27b1a2020-06-09 17:34:41 +020053 impl_.Unlock();
54 }
Markus Handellf70fbc82020-06-04 00:41:20 +020055
56 private:
57 MutexImpl impl_;
Markus Handellf70fbc82020-06-04 00:41:20 +020058};
59
60// MutexLock, for serializing execution through a scope.
61class 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)
78using GlobalMutex = absl::Mutex;
79using GlobalMutexLock = absl::MutexLock;
80#else
81class 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.
97class RTC_SCOPED_LOCKABLE GlobalMutexLock final {
98 public:
99 GlobalMutexLock(const GlobalMutexLock&) = delete;
100 GlobalMutexLock& operator=(const GlobalMutexLock&) = delete;
101
Markus Handell0dd35d32020-07-15 11:53:44 +0200102 explicit GlobalMutexLock(GlobalMutex* mutex)
103 RTC_EXCLUSIVE_LOCK_FUNCTION(mutex_);
Markus Handellf70fbc82020-06-04 00:41:20 +0200104 ~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_