| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef WEBRTC_BASE_CRITICALSECTION_H_ | 11 #ifndef WEBRTC_BASE_CRITICALSECTION_H_ |
| 12 #define WEBRTC_BASE_CRITICALSECTION_H_ | 12 #define WEBRTC_BASE_CRITICALSECTION_H_ |
| 13 | 13 |
| 14 #include "webrtc/base/atomicops.h" | 14 #include "webrtc/base/atomicops.h" |
| 15 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
| 16 #include "webrtc/base/constructormagic.h" | 16 #include "webrtc/base/constructormagic.h" |
| 17 #include "webrtc/base/platform_thread_types.h" |
| 17 #include "webrtc/base/thread_annotations.h" | 18 #include "webrtc/base/thread_annotations.h" |
| 18 #include "webrtc/base/platform_thread_types.h" | 19 #include "webrtc/typedefs.h" |
| 19 | 20 |
| 20 #if defined(WEBRTC_WIN) | 21 #if defined(WEBRTC_WIN) |
| 21 // Include winsock2.h before including <windows.h> to maintain consistency with | 22 // Include winsock2.h before including <windows.h> to maintain consistency with |
| 22 // win32.h. We can't include win32.h directly here since it pulls in | 23 // win32.h. We can't include win32.h directly here since it pulls in |
| 23 // headers such as basictypes.h which causes problems in Chromium where webrtc | 24 // headers such as basictypes.h which causes problems in Chromium where webrtc |
| 24 // exists as two separate projects, webrtc and libjingle. | 25 // exists as two separate projects, webrtc and libjingle. |
| 25 #include <winsock2.h> | 26 #include <winsock2.h> |
| 26 #include <windows.h> | 27 #include <windows.h> |
| 27 #include <sal.h> // must come after windows headers. | 28 #include <sal.h> // must come after windows headers. |
| 28 #endif // defined(WEBRTC_WIN) | 29 #endif // defined(WEBRTC_WIN) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 bool TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true); | 61 bool TryEnter() const EXCLUSIVE_TRYLOCK_FUNCTION(true); |
| 61 void Leave() const UNLOCK_FUNCTION(); | 62 void Leave() const UNLOCK_FUNCTION(); |
| 62 | 63 |
| 63 private: | 64 private: |
| 64 // Use only for RTC_DCHECKing. | 65 // Use only for RTC_DCHECKing. |
| 65 bool CurrentThreadIsOwner() const; | 66 bool CurrentThreadIsOwner() const; |
| 66 | 67 |
| 67 #if defined(WEBRTC_WIN) | 68 #if defined(WEBRTC_WIN) |
| 68 mutable CRITICAL_SECTION crit_; | 69 mutable CRITICAL_SECTION crit_; |
| 69 #elif defined(WEBRTC_POSIX) | 70 #elif defined(WEBRTC_POSIX) |
| 70 #if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC | 71 # if defined(WEBRTC_MAC) && !USE_NATIVE_MUTEX_ON_MAC |
| 71 // Number of times the lock has been locked + number of threads waiting. | 72 // Number of times the lock has been locked + number of threads waiting. |
| 72 // TODO(tommi): We could use this number and subtract the recursion count | 73 // TODO(tommi): We could use this number and subtract the recursion count |
| 73 // to find places where we have multiple threads contending on the same lock. | 74 // to find places where we have multiple threads contending on the same lock. |
| 74 mutable volatile int lock_queue_; | 75 mutable volatile int lock_queue_; |
| 75 // |recursion_| represents the recursion count + 1 for the thread that owns | 76 // |recursion_| represents the recursion count + 1 for the thread that owns |
| 76 // the lock. Only modified by the thread that owns the lock. | 77 // the lock. Only modified by the thread that owns the lock. |
| 77 mutable int recursion_; | 78 mutable int recursion_; |
| 78 // Used to signal a single waiting thread when the lock becomes available. | 79 // Used to signal a single waiting thread when the lock becomes available. |
| 79 mutable dispatch_semaphore_t semaphore_; | 80 mutable dispatch_semaphore_t semaphore_; |
| 80 // The thread that currently holds the lock. Required to handle recursion. | 81 // The thread that currently holds the lock. Required to handle recursion. |
| 81 mutable PlatformThreadRef owning_thread_; | 82 mutable PlatformThreadRef owning_thread_; |
| 82 #else | 83 # else |
| 83 mutable pthread_mutex_t mutex_; | 84 mutable pthread_mutex_t mutex_; |
| 84 #endif | 85 # endif |
| 85 CS_DEBUG_CODE(mutable PlatformThreadRef thread_); | 86 mutable PlatformThreadRef thread_; // Only used by RTC_DCHECKs. |
| 86 CS_DEBUG_CODE(mutable int recursion_count_); | 87 mutable int recursion_count_; // Only used by RTC_DCHECKs. |
| 88 #else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) |
| 89 # error Unsupported platform. |
| 87 #endif | 90 #endif |
| 88 }; | 91 }; |
| 89 | 92 |
| 90 // CritScope, for serializing execution through a scope. | 93 // CritScope, for serializing execution through a scope. |
| 91 class SCOPED_LOCKABLE CritScope { | 94 class SCOPED_LOCKABLE CritScope { |
| 92 public: | 95 public: |
| 93 explicit CritScope(const CriticalSection* cs) EXCLUSIVE_LOCK_FUNCTION(cs); | 96 explicit CritScope(const CriticalSection* cs) EXCLUSIVE_LOCK_FUNCTION(cs); |
| 94 ~CritScope() UNLOCK_FUNCTION(); | 97 ~CritScope() UNLOCK_FUNCTION(); |
| 95 private: | 98 private: |
| 96 const CriticalSection* const cs_; | 99 const CriticalSection* const cs_; |
| 97 RTC_DISALLOW_COPY_AND_ASSIGN(CritScope); | 100 RTC_DISALLOW_COPY_AND_ASSIGN(CritScope); |
| 98 }; | 101 }; |
| 99 | 102 |
| 100 // Tries to lock a critical section on construction via | 103 // Tries to lock a critical section on construction via |
| 101 // CriticalSection::TryEnter, and unlocks on destruction if the | 104 // CriticalSection::TryEnter, and unlocks on destruction if the |
| 102 // lock was taken. Never blocks. | 105 // lock was taken. Never blocks. |
| 103 // | 106 // |
| 104 // IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in | 107 // IMPORTANT: Unlike CritScope, the lock may not be owned by this thread in |
| 105 // subsequent code. Users *must* check locked() to determine if the | 108 // subsequent code. Users *must* check locked() to determine if the |
| 106 // lock was taken. If you're not calling locked(), you're doing it wrong! | 109 // lock was taken. If you're not calling locked(), you're doing it wrong! |
| 107 class TryCritScope { | 110 class TryCritScope { |
| 108 public: | 111 public: |
| 109 explicit TryCritScope(const CriticalSection* cs); | 112 explicit TryCritScope(const CriticalSection* cs); |
| 110 ~TryCritScope(); | 113 ~TryCritScope(); |
| 111 #if defined(WEBRTC_WIN) | 114 #if defined(WEBRTC_WIN) |
| 112 _Check_return_ bool locked() const; | 115 _Check_return_ bool locked() const; |
| 113 #else | 116 #elif defined(WEBRTC_POSIX) |
| 114 bool locked() const __attribute__ ((__warn_unused_result__)); | 117 bool locked() const __attribute__ ((__warn_unused_result__)); |
| 118 #else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX) |
| 119 # error Unsupported platform. |
| 115 #endif | 120 #endif |
| 116 private: | 121 private: |
| 117 const CriticalSection* const cs_; | 122 const CriticalSection* const cs_; |
| 118 const bool locked_; | 123 const bool locked_; |
| 119 CS_DEBUG_CODE(mutable bool lock_was_called_); | 124 mutable bool lock_was_called_; // Only used by RTC_DCHECKs. |
| 120 RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope); | 125 RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope); |
| 121 }; | 126 }; |
| 122 | 127 |
| 123 // A POD lock used to protect global variables. Do NOT use for other purposes. | 128 // A POD lock used to protect global variables. Do NOT use for other purposes. |
| 124 // No custom constructor or private data member should be added. | 129 // No custom constructor or private data member should be added. |
| 125 class LOCKABLE GlobalLockPod { | 130 class LOCKABLE GlobalLockPod { |
| 126 public: | 131 public: |
| 127 void Lock() EXCLUSIVE_LOCK_FUNCTION(); | 132 void Lock() EXCLUSIVE_LOCK_FUNCTION(); |
| 128 | 133 |
| 129 void Unlock() UNLOCK_FUNCTION(); | 134 void Unlock() UNLOCK_FUNCTION(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 142 explicit GlobalLockScope(GlobalLockPod* lock) EXCLUSIVE_LOCK_FUNCTION(lock); | 147 explicit GlobalLockScope(GlobalLockPod* lock) EXCLUSIVE_LOCK_FUNCTION(lock); |
| 143 ~GlobalLockScope() UNLOCK_FUNCTION(); | 148 ~GlobalLockScope() UNLOCK_FUNCTION(); |
| 144 private: | 149 private: |
| 145 GlobalLockPod* const lock_; | 150 GlobalLockPod* const lock_; |
| 146 RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope); | 151 RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope); |
| 147 }; | 152 }; |
| 148 | 153 |
| 149 } // namespace rtc | 154 } // namespace rtc |
| 150 | 155 |
| 151 #endif // WEBRTC_BASE_CRITICALSECTION_H_ | 156 #endif // WEBRTC_BASE_CRITICALSECTION_H_ |
| OLD | NEW |