mutex_critical_section.h 1.7 KB

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