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 "rtc_base/thread_annotations.h" 27 28 namespace webrtc { 29 30 class RTC_LOCKABLE MutexImpl final { 31 public: MutexImpl()32 MutexImpl() { InitializeCriticalSection(&critical_section_); } 33 MutexImpl(const MutexImpl&) = delete; 34 MutexImpl& operator=(const MutexImpl&) = delete; ~MutexImpl()35 ~MutexImpl() { DeleteCriticalSection(&critical_section_); } 36 Lock()37 void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION() { 38 EnterCriticalSection(&critical_section_); 39 } TryLock()40 RTC_WARN_UNUSED_RESULT bool TryLock() RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true) { 41 return TryEnterCriticalSection(&critical_section_) != FALSE; 42 } Unlock()43 void Unlock() RTC_UNLOCK_FUNCTION() { 44 LeaveCriticalSection(&critical_section_); 45 } 46 47 private: 48 CRITICAL_SECTION critical_section_; 49 }; 50 51 } // namespace webrtc 52 53 #endif // #if defined(WEBRTC_WIN) 54 #endif // RTC_BASE_SYNCHRONIZATION_MUTEX_CRITICAL_SECTION_H_ 55