xref: /aosp_15_r20/external/webrtc/rtc_base/synchronization/mutex_critical_section.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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_CRITICAL_SECTION_H_
12 #define RTC_BASE_SYNCHRONIZATION_MUTEX_CRITICAL_SECTION_H_
13 
14 #if defined(WEBRTC_WIN)
15 // clang-format off
16 // clang formating would change include order.
17 
18 // Include winsock2.h before including <windows.h> to maintain consistency with
19 // win32.h. To include win32.h directly, it must be broken out into its own
20 // build target.
21 #include <winsock2.h>
22 #include <windows.h>
23 #include <sal.h>  // must come after windows headers.
24 // clang-format on
25 
26 #include "absl/base/attributes.h"
27 #include "rtc_base/thread_annotations.h"
28 
29 namespace webrtc {
30 
31 class RTC_LOCKABLE MutexImpl final {
32  public:
MutexImpl()33   MutexImpl() { InitializeCriticalSection(&critical_section_); }
34   MutexImpl(const MutexImpl&) = delete;
35   MutexImpl& operator=(const MutexImpl&) = delete;
~MutexImpl()36   ~MutexImpl() { DeleteCriticalSection(&critical_section_); }
37 
Lock()38   void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION() {
39     EnterCriticalSection(&critical_section_);
40   }
TryLock()41   ABSL_MUST_USE_RESULT bool TryLock() RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
42     return TryEnterCriticalSection(&critical_section_) != FALSE;
43   }
AssertHeld()44   void AssertHeld() const RTC_ASSERT_EXCLUSIVE_LOCK() {}
Unlock()45   void Unlock() RTC_UNLOCK_FUNCTION() {
46     LeaveCriticalSection(&critical_section_);
47   }
48 
49  private:
50   CRITICAL_SECTION critical_section_;
51 };
52 
53 }  // namespace webrtc
54 
55 #endif  // #if defined(WEBRTC_WIN)
56 #endif  // RTC_BASE_SYNCHRONIZATION_MUTEX_CRITICAL_SECTION_H_
57