recursive_critical_section.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2004 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_DEPRECATED_RECURSIVE_CRITICAL_SECTION_H_
  11. #define RTC_BASE_DEPRECATED_RECURSIVE_CRITICAL_SECTION_H_
  12. #include "rtc_base/constructor_magic.h"
  13. #include "rtc_base/platform_thread_types.h"
  14. #include "rtc_base/thread_annotations.h"
  15. #if defined(WEBRTC_WIN)
  16. // clang-format off
  17. // clang formating would change include order.
  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. #endif // defined(WEBRTC_WIN)
  26. #if defined(WEBRTC_POSIX)
  27. #include <pthread.h>
  28. #endif
  29. // See notes in the 'Performance' unit test for the effects of this flag.
  30. #define RTC_USE_NATIVE_MUTEX_ON_MAC 1
  31. #if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC
  32. #include <dispatch/dispatch.h>
  33. #endif
  34. namespace rtc {
  35. // NOTE: This class is deprecated. Please use webrtc::Mutex instead!
  36. // Search using https://www.google.com/?q=recursive+lock+considered+harmful
  37. // to find the reasons.
  38. //
  39. // Locking methods (Enter, TryEnter, Leave)are const to permit protecting
  40. // members inside a const context without requiring mutable
  41. // RecursiveCriticalSections everywhere. RecursiveCriticalSection is
  42. // reentrant lock.
  43. class RTC_LOCKABLE RecursiveCriticalSection {
  44. public:
  45. RecursiveCriticalSection();
  46. ~RecursiveCriticalSection();
  47. void Enter() const RTC_EXCLUSIVE_LOCK_FUNCTION();
  48. bool TryEnter() const RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true);
  49. void Leave() const RTC_UNLOCK_FUNCTION();
  50. private:
  51. // Use only for RTC_DCHECKing.
  52. bool CurrentThreadIsOwner() const;
  53. #if defined(WEBRTC_WIN)
  54. mutable CRITICAL_SECTION crit_;
  55. #elif defined(WEBRTC_POSIX)
  56. #if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC
  57. // Number of times the lock has been locked + number of threads waiting.
  58. // TODO(tommi): We could use this number and subtract the recursion count
  59. // to find places where we have multiple threads contending on the same lock.
  60. mutable volatile int lock_queue_;
  61. // |recursion_| represents the recursion count + 1 for the thread that owns
  62. // the lock. Only modified by the thread that owns the lock.
  63. mutable int recursion_;
  64. // Used to signal a single waiting thread when the lock becomes available.
  65. mutable dispatch_semaphore_t semaphore_;
  66. // The thread that currently holds the lock. Required to handle recursion.
  67. mutable PlatformThreadRef owning_thread_;
  68. #else
  69. mutable pthread_mutex_t mutex_;
  70. #endif
  71. mutable PlatformThreadRef thread_; // Only used by RTC_DCHECKs.
  72. mutable int recursion_count_; // Only used by RTC_DCHECKs.
  73. #else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX)
  74. #error Unsupported platform.
  75. #endif
  76. };
  77. // CritScope, for serializing execution through a scope.
  78. class RTC_SCOPED_LOCKABLE CritScope {
  79. public:
  80. explicit CritScope(const RecursiveCriticalSection* cs)
  81. RTC_EXCLUSIVE_LOCK_FUNCTION(cs);
  82. ~CritScope() RTC_UNLOCK_FUNCTION();
  83. private:
  84. const RecursiveCriticalSection* const cs_;
  85. RTC_DISALLOW_COPY_AND_ASSIGN(CritScope);
  86. };
  87. } // namespace rtc
  88. #endif // RTC_BASE_DEPRECATED_RECURSIVE_CRITICAL_SECTION_H_