blob: 2cf0e67c3df423f5dccb48a4293647df84471f3f [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
Mirko Bonadeif325ea22021-07-23 16:15:16 +020021#if defined(WEBRTC_ABSL_MUTEX)
Markus Handell8e75bd42020-06-05 11:47:40 +020022#include "rtc_base/synchronization/mutex_abseil.h" // nogncheck
Markus Handellf70fbc82020-06-04 00:41:20 +020023#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
31namespace webrtc {
32
33// The Mutex guarantees exclusive access and aims to follow Abseil semantics
34// (i.e. non-reentrant etc).
35class RTC_LOCKABLE Mutex final {
36 public:
37 Mutex() = default;
38 Mutex(const Mutex&) = delete;
39 Mutex& operator=(const Mutex&) = delete;
40
Markus Handell2d27b1a2020-06-09 17:34:41 +020041 void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION() {
Markus Handell2d27b1a2020-06-09 17:34:41 +020042 impl_.Lock();
Markus Handellf70fbc82020-06-04 00:41:20 +020043 }
Danil Chapovalov098da172021-01-14 16:15:31 +010044 ABSL_MUST_USE_RESULT bool TryLock() RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
Markus Handell2a23bd22020-09-30 12:59:10 +020045 return impl_.TryLock();
Markus Handell2d27b1a2020-06-09 17:34:41 +020046 }
Niels Möller5b747232021-07-26 17:16:25 +020047 // Return immediately if this thread holds the mutex, or RTC_DCHECK_IS_ON==0.
48 // Otherwise, may report an error (typically by crashing with a diagnostic),
49 // or may return immediately.
50 void AssertHeld() const RTC_ASSERT_EXCLUSIVE_LOCK() { impl_.AssertHeld(); }
Markus Handell2d27b1a2020-06-09 17:34:41 +020051 void Unlock() RTC_UNLOCK_FUNCTION() {
Markus Handell2d27b1a2020-06-09 17:34:41 +020052 impl_.Unlock();
53 }
Markus Handellf70fbc82020-06-04 00:41:20 +020054
55 private:
56 MutexImpl impl_;
Markus Handellf70fbc82020-06-04 00:41:20 +020057};
58
59// MutexLock, for serializing execution through a scope.
60class RTC_SCOPED_LOCKABLE MutexLock final {
61 public:
62 MutexLock(const MutexLock&) = delete;
63 MutexLock& operator=(const MutexLock&) = delete;
64
65 explicit MutexLock(Mutex* mutex) RTC_EXCLUSIVE_LOCK_FUNCTION(mutex)
66 : mutex_(mutex) {
67 mutex->Lock();
68 }
69 ~MutexLock() RTC_UNLOCK_FUNCTION() { mutex_->Unlock(); }
70
71 private:
72 Mutex* mutex_;
73};
74
Markus Handellf70fbc82020-06-04 00:41:20 +020075} // namespace webrtc
76
77#endif // RTC_BASE_SYNCHRONIZATION_MUTEX_H_