Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 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 | |
Danil Chapovalov | 098da17 | 2021-01-14 16:15:31 +0100 | [diff] [blame] | 26 | #include "absl/base/attributes.h" |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 27 | #include "rtc_base/thread_annotations.h" |
| 28 | |
| 29 | namespace webrtc { |
| 30 | |
| 31 | class 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 Chapovalov | 098da17 | 2021-01-14 16:15:31 +0100 | [diff] [blame] | 41 | ABSL_MUST_USE_RESULT bool TryLock() RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true) { |
Markus Handell | f70fbc8 | 2020-06-04 00:41:20 +0200 | [diff] [blame] | 42 | 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_ |