blob: cb3d6a095ce1c12c2a69e23f62e07bd17c329463 [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_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
Danil Chapovalov098da172021-01-14 16:15:31 +010026#include "absl/base/attributes.h"
Markus Handellf70fbc82020-06-04 00:41:20 +020027#include "rtc_base/thread_annotations.h"
28
29namespace webrtc {
30
31class RTC_LOCKABLE MutexImpl final {
32 public:
33 MutexImpl() { InitializeCriticalSection(&critical_section_); }
34 MutexImpl(const MutexImpl&) = delete;
35 MutexImpl& operator=(const MutexImpl&) = delete;
36 ~MutexImpl() { DeleteCriticalSection(&critical_section_); }
37
38 void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION() {
39 EnterCriticalSection(&critical_section_);
40 }
Danil Chapovalov098da172021-01-14 16:15:31 +010041 ABSL_MUST_USE_RESULT bool TryLock() RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
Markus Handellf70fbc82020-06-04 00:41:20 +020042 return TryEnterCriticalSection(&critical_section_) != FALSE;
43 }
44 void Unlock() RTC_UNLOCK_FUNCTION() {
45 LeaveCriticalSection(&critical_section_);
46 }
47
48 private:
49 CRITICAL_SECTION critical_section_;
50};
51
52} // namespace webrtc
53
54#endif // #if defined(WEBRTC_WIN)
55#endif // RTC_BASE_SYNCHRONIZATION_MUTEX_CRITICAL_SECTION_H_