Chromium Code Reviews| 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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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_; |
|
nisse-webrtc
2017/06/26 12:53:09
As discussed offline, it would make sense to me to
eladalon
2017/06/26 13:38:12
I'll leave this until we take care of the TODO at
| |
| 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_; |
| 86 CS_DEBUG_CODE(mutable int recursion_count_); | 87 mutable int recursion_count_; |
| 87 #endif | 88 #endif |
| 88 }; | 89 }; |
| 89 | 90 |
| 90 // CritScope, for serializing execution through a scope. | 91 // CritScope, for serializing execution through a scope. |
| 91 class SCOPED_LOCKABLE CritScope { | 92 class SCOPED_LOCKABLE CritScope { |
| 92 public: | 93 public: |
| 93 explicit CritScope(const CriticalSection* cs) EXCLUSIVE_LOCK_FUNCTION(cs); | 94 explicit CritScope(const CriticalSection* cs) EXCLUSIVE_LOCK_FUNCTION(cs); |
| 94 ~CritScope() UNLOCK_FUNCTION(); | 95 ~CritScope() UNLOCK_FUNCTION(); |
| 95 private: | 96 private: |
| 96 const CriticalSection* const cs_; | 97 const CriticalSection* const cs_; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 109 explicit TryCritScope(const CriticalSection* cs); | 110 explicit TryCritScope(const CriticalSection* cs); |
| 110 ~TryCritScope(); | 111 ~TryCritScope(); |
| 111 #if defined(WEBRTC_WIN) | 112 #if defined(WEBRTC_WIN) |
| 112 _Check_return_ bool locked() const; | 113 _Check_return_ bool locked() const; |
| 113 #else | 114 #else |
| 114 bool locked() const __attribute__ ((__warn_unused_result__)); | 115 bool locked() const __attribute__ ((__warn_unused_result__)); |
| 115 #endif | 116 #endif |
| 116 private: | 117 private: |
| 117 const CriticalSection* const cs_; | 118 const CriticalSection* const cs_; |
| 118 const bool locked_; | 119 const bool locked_; |
| 119 CS_DEBUG_CODE(mutable bool lock_was_called_); | 120 mutable bool lock_was_called_; |
| 120 RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope); | 121 RTC_DISALLOW_COPY_AND_ASSIGN(TryCritScope); |
| 121 }; | 122 }; |
| 122 | 123 |
| 123 // A POD lock used to protect global variables. Do NOT use for other purposes. | 124 // 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. | 125 // No custom constructor or private data member should be added. |
| 125 class LOCKABLE GlobalLockPod { | 126 class LOCKABLE GlobalLockPod { |
| 126 public: | 127 public: |
| 127 void Lock() EXCLUSIVE_LOCK_FUNCTION(); | 128 void Lock() EXCLUSIVE_LOCK_FUNCTION(); |
| 128 | 129 |
| 129 void Unlock() UNLOCK_FUNCTION(); | 130 void Unlock() UNLOCK_FUNCTION(); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 142 explicit GlobalLockScope(GlobalLockPod* lock) EXCLUSIVE_LOCK_FUNCTION(lock); | 143 explicit GlobalLockScope(GlobalLockPod* lock) EXCLUSIVE_LOCK_FUNCTION(lock); |
| 143 ~GlobalLockScope() UNLOCK_FUNCTION(); | 144 ~GlobalLockScope() UNLOCK_FUNCTION(); |
| 144 private: | 145 private: |
| 145 GlobalLockPod* const lock_; | 146 GlobalLockPod* const lock_; |
| 146 RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope); | 147 RTC_DISALLOW_COPY_AND_ASSIGN(GlobalLockScope); |
| 147 }; | 148 }; |
| 148 | 149 |
| 149 } // namespace rtc | 150 } // namespace rtc |
| 150 | 151 |
| 151 #endif // WEBRTC_BASE_CRITICALSECTION_H_ | 152 #endif // WEBRTC_BASE_CRITICALSECTION_H_ |
| OLD | NEW |